@data-fair/lib-node 2.2.3 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/upgrade-scripts.d.ts +1 -1
- package/upgrade-scripts.js +26 -15
package/package.json
CHANGED
package/upgrade-scripts.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export interface UpgradeScript {
|
|
|
5
5
|
description: string;
|
|
6
6
|
exec: (db: Db, debug: Debugger) => Promise<void>;
|
|
7
7
|
}
|
|
8
|
-
export default function (db: Db, locks: Locks, basePath?: string): Promise<void>;
|
|
8
|
+
export default function (db: Db, locks: Locks, basePath?: string, bootstrap?: () => Promise<undefined | string>): Promise<void>;
|
package/upgrade-scripts.js
CHANGED
|
@@ -4,7 +4,7 @@ import { readdirSync, readFileSync } from 'fs'
|
|
|
4
4
|
import Debug from 'debug'
|
|
5
5
|
const debug = Debug('upgrade')
|
|
6
6
|
// chose the proper scripts to execute, then run them
|
|
7
|
-
export default async function (db, locks, basePath = './') {
|
|
7
|
+
export default async function (db, locks, basePath = './', bootstrap) {
|
|
8
8
|
const ack = await locks.acquire('upgrade')
|
|
9
9
|
if (!ack) {
|
|
10
10
|
console.warn('upgrade scripts lock is already acquired, skip them')
|
|
@@ -15,13 +15,13 @@ export default async function (db, locks, basePath = './') {
|
|
|
15
15
|
// are not considered "up" and the previous versions keep running in the mean time
|
|
16
16
|
} else {
|
|
17
17
|
try {
|
|
18
|
-
await runScripts(db, basePath)
|
|
18
|
+
await runScripts(db, basePath, bootstrap)
|
|
19
19
|
} finally {
|
|
20
20
|
await locks.release('upgrade')
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
async function runScripts (db, basePath) {
|
|
24
|
+
async function runScripts (db, basePath, bootstrap) {
|
|
25
25
|
const dir = path.resolve(basePath)
|
|
26
26
|
const pjsonPath = path.join(dir, 'package.json')
|
|
27
27
|
debug('read service info from ' + pjsonPath)
|
|
@@ -30,22 +30,33 @@ async function runScripts (db, basePath) {
|
|
|
30
30
|
debug(`service=${pjson.name} version=${pjson.version}`)
|
|
31
31
|
const services = db.collection('services')
|
|
32
32
|
const service = await services.findOne({ id: pjson.name })
|
|
33
|
-
|
|
33
|
+
let previousPersion = service?.version
|
|
34
34
|
debug('list scripts from ' + scriptsRoot)
|
|
35
35
|
let scripts = await listScripts(scriptsRoot)
|
|
36
|
-
if (
|
|
37
|
-
debug(
|
|
38
|
-
scripts = scripts.filter(scriptDef => scriptDef.version === 'init')
|
|
36
|
+
if (previousPersion) {
|
|
37
|
+
debug(`Current service version from database : ${previousPersion}`)
|
|
39
38
|
} else {
|
|
40
|
-
debug(
|
|
41
|
-
scripts = scripts.filter(scriptDef => scriptDef.version
|
|
39
|
+
debug('No service version found in database, this is the first time the upgrade system is run')
|
|
40
|
+
scripts = scripts.filter(scriptDef => scriptDef.version === 'init')
|
|
41
|
+
if (bootstrap) {
|
|
42
|
+
previousPersion = await bootstrap()
|
|
43
|
+
if (previousPersion) {
|
|
44
|
+
debug('Bootstrap function returned version %s', previousPersion)
|
|
45
|
+
} else {
|
|
46
|
+
debug('Bootstrap function returned no version, this is fresh install, do not run any script')
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
debug('No bootstrap function, it could be a fresh install, do not run any script')
|
|
50
|
+
}
|
|
42
51
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
if (previousPersion) {
|
|
53
|
+
for (const scriptDef of scripts) {
|
|
54
|
+
if (semver.gte(scriptDef.version, previousPersion)) {
|
|
55
|
+
for (const scriptName of scriptDef.names) {
|
|
56
|
+
const script = (await import(path.join(scriptsRoot, scriptDef.version, scriptName))).default
|
|
57
|
+
debug('Apply script %s/%s : %s', scriptDef.version, scriptName, script.description)
|
|
58
|
+
await script.exec(db, Debug(`upgrade:${scriptDef.version}:${scriptName}`))
|
|
59
|
+
}
|
|
49
60
|
}
|
|
50
61
|
}
|
|
51
62
|
}
|