Make migration script generic

This commit is contained in:
Shaunak Kishore
2015-09-28 00:53:59 -04:00
parent c3ff6b1858
commit 4fd741f204

View File

@ -1,48 +1,36 @@
// Given an old-form glyph entry, returns the new glyph entry.
migrate_glyph = (glyph) => {
const codepoint = parseInt(glyph.name.substr(3), 16);
const character = String.fromCodePoint(codepoint);
const data = cjklib.getCharacterData(character);
assert(glyph.manual.verified !== undefined);
const result = {
character: character,
codepoint: codepoint,
metadata: {
definition: undefined,
kangxi_index: data.kangxi_index,
pinyin: undefined,
strokes: undefined,
},
stages: {
path: Glyphs.get_svg_path(glyph),
bridges: glyph.manual.bridges,
strokes: glyph.derived.strokes,
analysis: undefined,
order: undefined,
settled: undefined,
},
};
assert(result.stages.path !== undefined);
assert(result.stages.bridges !== undefined);
assert(result.stages.strokes !== undefined);
return result;
}
"use strict";
migrate_glyphs = () => {
const names = Glyphs.find({}, {fields: {name: 1}, sort: {name: 1}}).fetch();
names.reverse();
for (name of names) {
const glyph = Glyphs.findOne({name: name.name});
const migrated_glyph = migrate_glyph(glyph);
Glyphs.insert(migrated_glyph);
const completionCallback = undefined;
const perGlyphCallback = undefined;
// Runs the given per-glyph callback for each glyph in the database.
// When all the glyphs are migrated, runs the completion callback.
const runMigration = () => {
console.log('Running migration...');
if (perGlyphCallback) {
const codepoints =
Glyphs.find({}, {fields: {codepoint: 1}, sort: {codepoint: 1}}).fetch();
for (let i = 0; i < codepoints.length; i++) {
const glyph = Glyphs.findOne({codepoint: codepoints[i].codepoint});
assert(glyph, 'Glyphs changed during migration!');
perGlyphCallback(glyph);
if ((i + 1) % 1000 === 0) {
console.log(`Migrated ${i + 1} glyphs.`);
}
}
}
if (completionCallback) {
completionCallback();
}
console.log('Migration complete.');
}
Meteor.startup(() => {
//console.log('Running migration...');
//cjklib.promise.then(Meteor.bindEnvironment(migrate_glyphs))
// .catch(console.error.bind(console));
Glyphs._ensureIndex({character: 1}, {unique: true});
Glyphs._ensureIndex({codepoint: 1}, {unique: true});
if (!perGlyphCallback && !completionCallback) {
return;
}
console.log('Preparing for migration...');
cjklib.promise.then(Meteor.bindEnvironment(runMigration))
.catch(console.error.bind(console));
});