|
|
@ -27,8 +27,12 @@ export default class NcUpgrader { |
|
|
|
public static async upgrade(ctx: NcUpgraderCtx): Promise<any> { |
|
|
|
public static async upgrade(ctx: NcUpgraderCtx): Promise<any> { |
|
|
|
this.log(`upgrade : Started`); |
|
|
|
this.log(`upgrade : Started`); |
|
|
|
|
|
|
|
|
|
|
|
// app version when upgrade started
|
|
|
|
// upgrader version when upgrade started
|
|
|
|
let oldVersion; |
|
|
|
let prevVersion; |
|
|
|
|
|
|
|
// last successfully applied upgrader version
|
|
|
|
|
|
|
|
let currentVersion; |
|
|
|
|
|
|
|
// latest available upgrader version
|
|
|
|
|
|
|
|
const latestVersion = process.env.NC_VERSION; |
|
|
|
|
|
|
|
|
|
|
|
const NC_VERSIONS: any[] = [ |
|
|
|
const NC_VERSIONS: any[] = [ |
|
|
|
{ name: '0009000', handler: null }, |
|
|
|
{ name: '0009000', handler: null }, |
|
|
@ -66,10 +70,9 @@ export default class NcUpgrader { |
|
|
|
throw e; |
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (configObj.version !== process.env.NC_VERSION) { |
|
|
|
if (configObj.version !== latestVersion) { |
|
|
|
oldVersion = configObj.version; |
|
|
|
prevVersion = configObj.version; |
|
|
|
// last successfully upgraded version
|
|
|
|
currentVersion = configObj.version; |
|
|
|
let fromVersion = configObj.version; |
|
|
|
|
|
|
|
for (const version of NC_VERSIONS) { |
|
|
|
for (const version of NC_VERSIONS) { |
|
|
|
// compare current version and old version
|
|
|
|
// compare current version and old version
|
|
|
|
if (version.name <= configObj.version) { |
|
|
|
if (version.name <= configObj.version) { |
|
|
@ -105,44 +108,42 @@ export default class NcUpgrader { |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
await upgrderCtx.ncMeta.commit(); |
|
|
|
await upgrderCtx.ncMeta.commit(); |
|
|
|
fromVersion = version.name; |
|
|
|
currentVersion = version.name; |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
await upgrderCtx.ncMeta.rollback(e); |
|
|
|
await upgrderCtx.ncMeta.rollback(e); |
|
|
|
Tele.emit('evt', { |
|
|
|
Tele.emit('evt', { |
|
|
|
evt_type: 'appMigration:failed', |
|
|
|
evt_type: 'appMigration:failed', |
|
|
|
// app version when upgrade started
|
|
|
|
prev: prevVersion, |
|
|
|
current: oldVersion, |
|
|
|
from: currentVersion, |
|
|
|
// last successfully upgraded version
|
|
|
|
to: latestVersion, |
|
|
|
from: fromVersion, |
|
|
|
|
|
|
|
// latest upgrade version available
|
|
|
|
|
|
|
|
to: process.env.NC_VERSION, |
|
|
|
|
|
|
|
msg: e.message, |
|
|
|
msg: e.message, |
|
|
|
err: e?.stack?.split?.('\n').slice(0, 2).join('\n'), |
|
|
|
err: e?.stack?.split?.('\n').slice(0, 2).join('\n'), |
|
|
|
}); |
|
|
|
}); |
|
|
|
console.log( |
|
|
|
console.log( |
|
|
|
getUpgradeErrorLog(e, fromVersion, process.env.NC_VERSION) |
|
|
|
getUpgradeErrorLog(e, currentVersion, latestVersion, prevVersion) |
|
|
|
); |
|
|
|
); |
|
|
|
throw e; |
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
// todo: backup data
|
|
|
|
// todo: backup data
|
|
|
|
|
|
|
|
|
|
|
|
if (version.name === process.env.NC_VERSION) { |
|
|
|
if (version.name === latestVersion) { |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
config.version = process.env.NC_VERSION; |
|
|
|
config.version = latestVersion; |
|
|
|
|
|
|
|
|
|
|
|
Tele.emit('evt', { |
|
|
|
Tele.emit('evt', { |
|
|
|
evt_type: 'appMigration:upgraded', |
|
|
|
evt_type: 'appMigration:upgraded', |
|
|
|
from: oldVersion, |
|
|
|
prev: prevVersion, |
|
|
|
to: process.env.NC_VERSION, |
|
|
|
from: prevVersion, |
|
|
|
|
|
|
|
to: latestVersion, |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this.log(`upgrade : Inserting config to meta database`); |
|
|
|
this.log(`upgrade : Inserting config to meta database`); |
|
|
|
const configObj: any = {}; |
|
|
|
const configObj: any = {}; |
|
|
|
const isOld = (await ctx.ncMeta.projectList())?.length; |
|
|
|
const isOld = (await ctx.ncMeta.projectList())?.length; |
|
|
|
configObj.version = isOld ? '0009000' : process.env.NC_VERSION; |
|
|
|
configObj.version = isOld ? '0009000' : latestVersion; |
|
|
|
await ctx.ncMeta.metaInsert('', '', MetaTable.STORE, { |
|
|
|
await ctx.ncMeta.metaInsert('', '', MetaTable.STORE, { |
|
|
|
key: NcUpgrader.STORE_KEY, |
|
|
|
key: NcUpgrader.STORE_KEY, |
|
|
|
value: JSON.stringify(configObj), |
|
|
|
value: JSON.stringify(configObj), |
|
|
@ -160,11 +161,11 @@ export default class NcUpgrader { |
|
|
|
|
|
|
|
|
|
|
|
function getUpgradeErrorLog( |
|
|
|
function getUpgradeErrorLog( |
|
|
|
e: Error, |
|
|
|
e: Error, |
|
|
|
fromVersion: string, |
|
|
|
currentVersion: string, |
|
|
|
newVersion: string, |
|
|
|
latestVersion: string, |
|
|
|
oldVersion: string |
|
|
|
prevVersion: string |
|
|
|
) { |
|
|
|
) { |
|
|
|
const errorTitle = `Migration from ${fromVersion} (old version: ${oldVersion}) to ${newVersion} failed`; |
|
|
|
const errorTitle = `Migration from ${currentVersion} (old version: ${prevVersion}) to ${latestVersion} failed`; |
|
|
|
|
|
|
|
|
|
|
|
return boxen( |
|
|
|
return boxen( |
|
|
|
`Error
|
|
|
|
`Error
|
|
|
|