core: add migration service

This commit is contained in:
undefined committed 2024-07-03 18:09:48 +08:00
1 parent 9e4643c1bf
commit 70872fe931
6 files changed
+99 -50

No files matched your search

+12 -21
View File
@@ -65,10 +65,10 @@ export async function apply(ctx: Context) {
for (const h of handlers.filter((i) => i.endsWith('.ts'))) {
ctx.loader.reloadPlugin(ctx, path.resolve(handlerDir, h), {}, `hydrooj/handler/${h.split('.')[0]}`);
}
ctx.plugin(require('../service/migration').default);
await handler(pending, fail, ctx);
await addon(pending, fail, ctx);
await ctx.lifecycle.flush();
for (const i in global.Hydro.handler) await global.Hydro.handler[i]();
const scriptDir = path.resolve(__dirname, '..', 'script');
for (const h of await fs.readdir(scriptDir)) {
ctx.loader.reloadPlugin(ctx, path.resolve(scriptDir, h), {}, `hydrooj/script/${h.split('.')[0]}`);
@@ -78,27 +78,18 @@ export async function apply(ctx: Context) {
await ctx.lifecycle.flush();
await ctx.parallel('app/started');
if (process.env.NODE_APP_INSTANCE === '0') {
const { default: scripts, init } = require('../upgrade');
let dbVer = (await modelSystem.get('db.ver')) ?? 0;
const isFresh = !dbVer;
const expected = scripts.length;
if (isFresh) {
await init();
await modelSystem.set('db.ver', expected);
} else {
while (dbVer < expected) {
const func = scripts[dbVer];
if (typeof func !== 'function') {
dbVer++;
continue;
await new Promise((resolve, reject) => {
ctx.inject(['migration'], async (c) => {
c.migration.registerChannel('hydrooj', require('../upgrade').coreScripts);
try {
await c.migration.doUpgrade();
resolve(null);
} catch (e) {
logger.error('Upgrade failed: %O', e);
reject(e);
}
logger.info('Upgrading database: from %d to %d', dbVer, expected);
const result = await func();
if (!result) break;
dbVer++;
await modelSystem.set('db.ver', dbVer);
}
}
});
});
}
for (const f of global.addons) {
const dir = path.join(f, 'public');