@data-fair/lib-node 2.2.3 → 2.3.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.2.3",
3
+ "version": "2.3.1",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -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, isFresh?: () => Promise<boolean>): Promise<void>;
@@ -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 = './', isFresh) {
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, isFresh)
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, isFresh) {
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
- const version = service?.version
33
+ let previousPersion = service?.version
34
34
  debug('list scripts from ' + scriptsRoot)
35
35
  let scripts = await listScripts(scriptsRoot)
36
- if (!version) {
37
- debug('No service version found in database, this is probably a fresh install')
38
- scripts = scripts.filter(scriptDef => scriptDef.version === 'init')
36
+ if (previousPersion) {
37
+ debug(`Current service version from database : ${previousPersion}`)
39
38
  } else {
40
- debug(`Current service version from database : ${version}`)
41
- scripts = scripts.filter(scriptDef => scriptDef.version !== 'init')
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 (isFresh) {
42
+ if (await isFresh()) {
43
+ debug('isFresh function returned true, this is fresh install, do not run any script')
44
+ } else {
45
+ debug('isFresh function returned false, this is not fresh install, run all init scripts')
46
+ previousPersion = '0.0.0'
47
+ }
48
+ } else {
49
+ debug('No isFresh function, it could be a fresh install, do not run any script')
50
+ }
42
51
  }
43
- for (const scriptDef of scripts) {
44
- if (semver.gte(scriptDef.version, version)) {
45
- for (const scriptName of scriptDef.names) {
46
- const script = (await import(path.join(scriptsRoot, scriptDef.version, scriptName))).default
47
- debug('Apply script %s/%s : %s', scriptDef.version, scriptName, script.description)
48
- await script.exec(db, Debug(`upgrade:${scriptDef.version}:${scriptName}`))
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
  }