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 :`);
let oldVersion;
try {
ctx.ncMeta = await ctx.ncMeta.startTransaction();
const NC_VERSIONS: any[] = [
{ name: '0009000', handler: null },
{ name: '0009044', handler: null },
{ name: '0011043', handler: ncProjectEnvUpgrader },
{ name: '0011045', handler: ncProjectEnvUpgrader0011045 },
{ name: '0090000', handler: ncProjectUpgraderV2_0090000 },
{ name: '0098004', handler: ncDataTypesUpgrader },
{ name: '0098005', handler: ncProjectRolesUpgrader },
{ name: '0100002', handler: ncFilterUpgrader },
{ name: '0101002', handler: ncAttachmentUpgrader },
{ name: '0104002', handler: ncAttachmentUpgrader_0104002 },
];
if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) {
return;
}
this.log(`upgrade : Getting configuration from meta database`);
const NC_VERSIONS: any[] = [
{ name: '0009000', handler: null },
{ name: '0009044', handler: null },
{ name: '0011043', handler: ncProjectEnvUpgrader },
{ name: '0011045', handler: ncProjectEnvUpgrader0011045 },
{ name: '0090000', handler: ncProjectUpgraderV2_0090000 },
{ name: '0098004', handler: ncDataTypesUpgrader },
{ name: '0098005', handler: ncProjectRolesUpgrader },
{ name: '0100002', handler: ncFilterUpgrader },
{ name: '0101002', handler: ncAttachmentUpgrader },
{ name: '0104002', handler: ncAttachmentUpgrader_0104002 },
];
if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) {
return;
}
this.log(`upgrade : Getting configuration from meta database`);
const config = await ctx.ncMeta.metaGet('', '', 'nc_store', {
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', {
key: this.STORE_KEY,
});
let upgrderCtx: NcUpgraderCtx;
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) {
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) {
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;
}
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();
Tele.emit('evt', {
evt_type: 'appMigration:upgraded',
from: oldVersion,
to: 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),
});
} catch (e) {
await ctx.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;
if (isOld) {
await this.upgrade(ctx);
}
}
}

Loading…
Cancel
Save