mirror of
https://github.com/skishore/makemeahanzi.git
synced 2025-11-03 05:48:23 +08:00
Make migration script generic
This commit is contained in:
@ -1,48 +1,36 @@
|
|||||||
// Given an old-form glyph entry, returns the new glyph entry.
|
"use strict";
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
migrate_glyphs = () => {
|
const completionCallback = undefined;
|
||||||
const names = Glyphs.find({}, {fields: {name: 1}, sort: {name: 1}}).fetch();
|
|
||||||
names.reverse();
|
const perGlyphCallback = undefined;
|
||||||
for (name of names) {
|
|
||||||
const glyph = Glyphs.findOne({name: name.name});
|
// Runs the given per-glyph callback for each glyph in the database.
|
||||||
const migrated_glyph = migrate_glyph(glyph);
|
// When all the glyphs are migrated, runs the completion callback.
|
||||||
Glyphs.insert(migrated_glyph);
|
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.');
|
console.log('Migration complete.');
|
||||||
}
|
}
|
||||||
|
|
||||||
Meteor.startup(() => {
|
Meteor.startup(() => {
|
||||||
//console.log('Running migration...');
|
if (!perGlyphCallback && !completionCallback) {
|
||||||
//cjklib.promise.then(Meteor.bindEnvironment(migrate_glyphs))
|
return;
|
||||||
// .catch(console.error.bind(console));
|
}
|
||||||
Glyphs._ensureIndex({character: 1}, {unique: true});
|
console.log('Preparing for migration...');
|
||||||
Glyphs._ensureIndex({codepoint: 1}, {unique: true});
|
cjklib.promise.then(Meteor.bindEnvironment(runMigration))
|
||||||
|
.catch(console.error.bind(console));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user