Browse Source

refactor: run each upgrader in an individual transaction

Signed-off-by: Pranav C <pranavxc@gmail.com>
refactor/upgrader
Pranav C 2 years ago
parent
commit
fcb899feb9
  1. 177
      packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts

177
packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts

@ -27,96 +27,105 @@ export default class NcUpgrader {
this.log(`upgrade :`); this.log(`upgrade :`);
let oldVersion; let oldVersion;
try { const NC_VERSIONS: any[] = [
ctx.ncMeta = await ctx.ncMeta.startTransaction(); { name: '0009000', handler: null },
{ name: '0009044', handler: null },
const NC_VERSIONS: any[] = [ { name: '0011043', handler: ncProjectEnvUpgrader },
{ name: '0009000', handler: null }, { name: '0011045', handler: ncProjectEnvUpgrader0011045 },
{ name: '0009044', handler: null }, { name: '0090000', handler: ncProjectUpgraderV2_0090000 },
{ name: '0011043', handler: ncProjectEnvUpgrader }, { name: '0098004', handler: ncDataTypesUpgrader },
{ name: '0011045', handler: ncProjectEnvUpgrader0011045 }, { name: '0098005', handler: ncProjectRolesUpgrader },
{ name: '0090000', handler: ncProjectUpgraderV2_0090000 }, { name: '0100002', handler: ncFilterUpgrader },
{ name: '0098004', handler: ncDataTypesUpgrader }, { name: '0101002', handler: ncAttachmentUpgrader },
{ name: '0098005', handler: ncProjectRolesUpgrader }, { name: '0104002', handler: ncAttachmentUpgrader_0104002 },
{ name: '0100002', handler: ncFilterUpgrader }, ];
{ name: '0101002', handler: ncAttachmentUpgrader }, if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) {
{ name: '0104002', handler: ncAttachmentUpgrader_0104002 }, return;
]; }
if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) { this.log(`upgrade : Getting configuration from meta database`);
return;
} const config = await ctx.ncMeta.metaGet('', '', 'nc_store', {
this.log(`upgrade : Getting configuration from meta database`); key: this.STORE_KEY,
});
if (config) {
const configObj: NcConfig = JSON.parse(config.value);
if (configObj.version !== process.env.NC_VERSION) {
oldVersion = configObj.version;
for (const version of NC_VERSIONS) {
// compare current version and old version
if (version.name <= configObj.version) {
continue;
}
const config = await ctx.ncMeta.metaGet('', '', 'nc_store', { let upgrderCtx: NcUpgraderCtx;
key: this.STORE_KEY, try {
}); upgrderCtx = {
...ctx,
ncMeta: await ctx.ncMeta.startTransaction(),
};
this.log(
`upgrade : Upgrading '%s' => '%s'`,
configObj.version,
version.name
);
await version?.handler?.(upgrderCtx);
// update version in meta after each upgrade
config.version = version.name;
await upgrderCtx.ncMeta.metaUpdate(
'',
'',
'nc_store',
{
value: JSON.stringify(config),
},
{
key: NcUpgrader.STORE_KEY,
}
);
await upgrderCtx.ncMeta.commit();
Tele.emit('evt', {
evt_type: 'appMigration:upgraded',
from: oldVersion,
to: process.env.NC_VERSION,
});
} catch (e) {
await upgrderCtx.ncMeta.rollback(e);
Tele.emit('evt', {
evt_type: 'appMigration:failed',
from: oldVersion,
to: process.env.NC_VERSION,
msg: e.message,
err: e?.stack?.split?.('\n').slice(0, 2).join('\n'),
});
console.log(
getUpgradeErrorLog(e, oldVersion, process.env.NC_VERSION)
);
throw e;
}
// todo: backup data
if (config) { if (version.name === process.env.NC_VERSION) {
const configObj: NcConfig = JSON.parse(config.value); break;
if (configObj.version !== process.env.NC_VERSION) {
oldVersion = configObj.version;
for (const version of NC_VERSIONS) {
// compare current version and old version
if (version.name > configObj.version) {
this.log(
`upgrade : Upgrading '%s' => '%s'`,
configObj.version,
version.name
);
await version?.handler?.(ctx);
// update version in meta after each upgrade
config.version = version.name;
await ctx.ncMeta.metaUpdate(
'',
'',
'nc_store',
{
value: JSON.stringify(config),
},
{
key: NcUpgrader.STORE_KEY,
}
);
// todo: backup data
}
if (version.name === process.env.NC_VERSION) {
break;
}
} }
config.version = process.env.NC_VERSION;
}
} else {
this.log(`upgrade : Inserting config to meta database`);
const configObj: any = {};
const isOld = (await ctx.ncMeta.projectList())?.length;
configObj.version = isOld ? '0009000' : process.env.NC_VERSION;
await ctx.ncMeta.metaInsert('', '', 'nc_store', {
key: NcUpgrader.STORE_KEY,
value: JSON.stringify(configObj),
});
if (isOld) {
await this.upgrade(ctx);
} }
config.version = process.env.NC_VERSION;
} }
await ctx.ncMeta.commit(); } else {
Tele.emit('evt', { this.log(`upgrade : Inserting config to meta database`);
evt_type: 'appMigration:upgraded', const configObj: any = {};
from: oldVersion, const isOld = (await ctx.ncMeta.projectList())?.length;
to: process.env.NC_VERSION, configObj.version = isOld ? '0009000' : process.env.NC_VERSION;
await ctx.ncMeta.metaInsert('', '', 'nc_store', {
key: NcUpgrader.STORE_KEY,
value: JSON.stringify(configObj),
}); });
} catch (e) { if (isOld) {
await ctx.ncMeta.rollback(e); await this.upgrade(ctx);
Tele.emit('evt', { }
evt_type: 'appMigration:failed',
from: oldVersion,
to: process.env.NC_VERSION,
msg: e.message,
err: e?.stack?.split?.('\n').slice(0, 2).join('\n'),
});
console.log(getUpgradeErrorLog(e, oldVersion, process.env.NC_VERSION));
throw e;
} }
} }

Loading…
Cancel
Save