Files
makemeahanzi/server/migration.js
2015-11-07 16:21:58 -05:00

83 lines
2.5 KiB
JavaScript

"use strict";
const checkStrokeExtractorStability = (glyph) => {
const strokes = stroke_extractor.getStrokes(
glyph.stages.path, glyph.stages.bridges);
if (!_.isEqual(strokes.strokes.sort(), glyph.stages.strokes.sort())) {
console.log(`Different strokes for ${glyph.character}`);
}
}
const migrateOldGlyphSchemaToNew = (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,
verified: undefined,
},
};
assert(result.stages.path !== undefined);
assert(result.stages.bridges !== undefined);
assert(result.stages.strokes !== undefined);
return result;
}
const setAnalysisStageToReady = (glyph) => {
const data = cjklib.getCharacterData(glyph.character);
if (data && data.strokes === glyph.stages.strokes.length) {
glyph.stages.analysis = {sentinel: true};
} else {
console.log(`Different stroke count for ${glyph.character}`);
}
}
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(() => {
if (!perGlyphCallback && !completionCallback) {
return;
}
console.log('Preparing for migration...');
cjklib.promise.then(Meteor.bindEnvironment(runMigration))
.catch(console.error.bind(console));
});