alp-migrations 7.3.0 → 8.1.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/CHANGELOG.md +24 -1
- package/dist/definitions/readRecursiveDirectory.d.ts +2 -2
- package/dist/definitions/readRecursiveDirectory.d.ts.map +1 -1
- package/dist/{index-node16.mjs → index-node18.mjs} +8 -7
- package/dist/index-node18.mjs.map +1 -0
- package/package.json +21 -17
- package/src/index.ts +5 -5
- package/src/readRecursiveDirectory.ts +3 -3
- package/.editorconfig +0 -13
- package/.eslintrc.json +0 -5
- package/AUTHORS +0 -1
- package/dist/index-node16.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [8.1.0](https://github.com/christophehurpeau/alp/compare/alp-migrations@8.0.0...alp-migrations@8.1.0) (2023-12-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* update dependencies ([ddc8f92](https://github.com/christophehurpeau/alp/commit/ddc8f92cccacf6ed2baabf8555f0b37fe281ce9d))
|
|
12
|
+
|
|
13
|
+
Version bump for dependency: alp-types
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## [8.0.0](https://github.com/christophehurpeau/alp/compare/alp-migrations@7.3.0...alp-migrations@8.0.0) (2023-07-29)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### ⚠ BREAKING CHANGES
|
|
20
|
+
|
|
21
|
+
* drop node 16
|
|
22
|
+
|
|
23
|
+
### Code Refactoring
|
|
24
|
+
|
|
25
|
+
* update to node 18 ([26280d6](https://github.com/christophehurpeau/alp/commit/26280d638aba1bd46fa42ad5a571b9626f1fff6d))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
6
29
|
## [7.3.0](https://github.com/christophehurpeau/alp/compare/alp-migrations@7.2.3...alp-migrations@7.3.0) (2023-03-19)
|
|
7
30
|
|
|
8
31
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type { Stats } from 'fs';
|
|
2
|
+
import type { Stats } from 'node:fs';
|
|
3
3
|
export interface CallbackParam {
|
|
4
4
|
filename: string;
|
|
5
5
|
basedir: string;
|
|
6
6
|
path: string;
|
|
7
7
|
stat: Stats;
|
|
8
8
|
}
|
|
9
|
-
export default function readRecursiveDirectory(directory: string, callback: (param: CallbackParam) => void |
|
|
9
|
+
export default function readRecursiveDirectory(directory: string, callback: (param: CallbackParam) => Promise<void> | void): Promise<void>;
|
|
10
10
|
//# sourceMappingURL=readRecursiveDirectory.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readRecursiveDirectory.d.ts","sourceRoot":"","sources":["../../src/readRecursiveDirectory.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"readRecursiveDirectory.d.ts","sourceRoot":"","sources":["../../src/readRecursiveDirectory.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAGrC,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,KAAK,CAAC;CACb;AAED,wBAA8B,sBAAsB,CAClD,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GACvD,OAAO,CAAC,IAAI,CAAC,CAoBf"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger } from 'nightingale-logger';
|
|
2
2
|
import semver from 'semver';
|
|
3
|
-
import { readdir, stat } from 'fs/promises';
|
|
3
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
4
4
|
|
|
5
5
|
async function readRecursiveDirectory(directory, callback) {
|
|
6
6
|
const files = await readdir(directory);
|
|
@@ -35,6 +35,7 @@ class MigrationsManager {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/* eslint-disable unicorn/no-process-exit */
|
|
38
|
+
|
|
38
39
|
const logger = new Logger('alp:migrations');
|
|
39
40
|
async function migrate({
|
|
40
41
|
app,
|
|
@@ -62,7 +63,7 @@ async function migrate({
|
|
|
62
63
|
return;
|
|
63
64
|
}
|
|
64
65
|
const versionExecResult = /([\d.]+)(_.*|\.js)$/.exec(fileName);
|
|
65
|
-
if (!versionExecResult
|
|
66
|
+
if (!versionExecResult?.[1]) {
|
|
66
67
|
return;
|
|
67
68
|
}
|
|
68
69
|
const version = versionExecResult[1];
|
|
@@ -79,9 +80,9 @@ async function migrate({
|
|
|
79
80
|
try {
|
|
80
81
|
const migrateFn = await import(`${dirname}/${migration.fileName}`);
|
|
81
82
|
await migrateFn();
|
|
82
|
-
} catch (
|
|
83
|
+
} catch (error) {
|
|
83
84
|
logger.error(`Migration to ${migration.version} Failed !`);
|
|
84
|
-
throw
|
|
85
|
+
throw error;
|
|
85
86
|
}
|
|
86
87
|
logger.success(`Migration to ${migration.fileName} done !`);
|
|
87
88
|
|
|
@@ -90,12 +91,12 @@ async function migrate({
|
|
|
90
91
|
await migrationsManager.addMigrationDone(migration);
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
|
-
} catch (
|
|
94
|
-
logger.error(
|
|
94
|
+
} catch (error) {
|
|
95
|
+
logger.error(error);
|
|
95
96
|
process.exit(1);
|
|
96
97
|
}
|
|
97
98
|
process.removeListener('unhandledRejection', unhandledRejectionHandler);
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
export { MigrationsManager, migrate as default };
|
|
101
|
-
//# sourceMappingURL=index-
|
|
102
|
+
//# sourceMappingURL=index-node18.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-node18.mjs","sources":["../src/readRecursiveDirectory.ts","../src/Manager.ts","../src/index.ts"],"sourcesContent":["import type { Stats } from 'node:fs';\nimport { readdir, stat as fsStat } from 'node:fs/promises';\n\nexport interface CallbackParam {\n filename: string;\n basedir: string;\n path: string;\n stat: Stats;\n}\n\nexport default async function readRecursiveDirectory(\n directory: string,\n callback: (param: CallbackParam) => Promise<void> | void,\n): Promise<void> {\n const files = await readdir(directory);\n\n await Promise.all(\n files.map(async (file): Promise<void> => {\n const path = `${directory}/${file}`;\n const stat = await fsStat(path);\n\n if (stat?.isDirectory()) {\n await readRecursiveDirectory(path, callback);\n return;\n }\n await callback({\n filename: file,\n basedir: directory,\n path,\n stat,\n });\n }),\n );\n}\n","import type { MongoStore, MongoInsertType, MongoBaseModel } from 'liwi-mongo';\n\nexport interface Migration extends MongoBaseModel {\n version: string;\n fileName: string;\n}\n\nexport default class MigrationsManager {\n store: MongoStore<Migration>;\n\n constructor(store: MongoStore<Migration>) {\n this.store = store;\n }\n\n findLastVersion(): Promise<string | undefined> {\n return this.store.findOne({}, { created: -1 }).then((row) => row?.version);\n }\n\n addMigrationDone(migration: MongoInsertType<Migration>): Promise<Migration> {\n return this.store.insertOne(migration);\n }\n}\n","/* eslint-disable unicorn/no-process-exit */\nimport type { NodeApplication, NodeConfig } from 'alp-types';\nimport { Logger } from 'nightingale-logger';\nimport semver from 'semver';\nimport type MigrationsManager from './Manager';\nimport type { CallbackParam } from './readRecursiveDirectory';\nimport readRecursiveDirectory from './readRecursiveDirectory';\n\nconst logger = new Logger('alp:migrations');\n\nexport { default as MigrationsManager } from './Manager';\n\nexport interface Options {\n app: NodeApplication;\n migrationsManager: MigrationsManager;\n config?: NodeConfig;\n dirname?: string;\n}\n\nexport default async function migrate({\n app,\n migrationsManager,\n config = app.config,\n dirname = `${app.dirname}/migrations`,\n}: Options): Promise<void> {\n const unhandledRejectionHandler = (reason: unknown): void => {\n logger.error('unhandledRejection', { err: reason });\n process.exit(1);\n };\n process.on('unhandledRejection', unhandledRejectionHandler);\n\n const packageVersion = config.packageConfig.version as string;\n const currentVersion = await migrationsManager.findLastVersion();\n\n let migrations: { version: string; fileName: string }[] = [];\n\n logger.info('migrate', { packageVersion, currentVersion });\n\n await readRecursiveDirectory(dirname, (res: CallbackParam) => {\n const fileName = res.path.slice(dirname.length + 1);\n\n if (!fileName.endsWith('.js')) {\n return;\n }\n\n const versionExecResult = /([\\d.]+)(_.*|\\.js)$/.exec(fileName);\n\n if (!versionExecResult?.[1]) {\n return;\n }\n\n const version: string = versionExecResult[1];\n\n if (currentVersion && semver.lte(version, currentVersion)) return;\n\n migrations.push({ version, fileName });\n });\n\n migrations = migrations.sort((a, b) =>\n semver.gt(a.version, b.version) ? 1 : -1,\n );\n\n try {\n for (const migration of migrations) {\n logger.info(`Migration to ${migration.fileName}`);\n try {\n const migrateFn: unknown = await import(\n `${dirname}/${migration.fileName}`\n );\n await (migrateFn as () => Promise<void>)();\n } catch (error) {\n logger.error(`Migration to ${migration.version} Failed !`);\n throw error;\n }\n\n logger.success(`Migration to ${migration.fileName} done !`);\n\n // only add to db if migration version <= package version\n if (semver.lte(migration.version, packageVersion)) {\n await migrationsManager.addMigrationDone(migration);\n }\n }\n } catch (error: any) {\n logger.error(error as Error);\n process.exit(1);\n }\n\n process.removeListener('unhandledRejection', unhandledRejectionHandler);\n}\n"],"names":["readRecursiveDirectory","directory","callback","files","readdir","Promise","all","map","file","path","stat","fsStat","isDirectory","filename","basedir","MigrationsManager","constructor","store","findLastVersion","findOne","created","then","row","version","addMigrationDone","migration","insertOne","logger","Logger","migrate","app","migrationsManager","config","dirname","unhandledRejectionHandler","reason","error","err","process","exit","on","packageVersion","packageConfig","currentVersion","migrations","info","res","fileName","slice","length","endsWith","versionExecResult","exec","semver","lte","push","sort","a","b","gt","migrateFn","success","removeListener"],"mappings":";;;;AAUe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ACF,KAAA;GACD,CAAC,OAAOW,KAAU,EAAE;AACnBT,IAAAA,MAAM,CAACS,KAAK,CAACA,KAAc,CAAC,CAAA;AAC5BE,IAAAA,OAAO,CAACC,IAAI,CAAC,CAAC,CAAC,CAAA;AACjB,GAAA;AAEAD,EAAAA,OAAO,CAACwB,cAAc,CAAC,oBAAoB,EAAE5B,yBAAyB,CAAC,CAAA;AACzE;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alp-migrations",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"description": "migrations scripts in alp",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Christophe Hurpeau <christophe@hurpeau.com> (https://christophe.hurpeau.com)",
|
|
@@ -13,19 +13,24 @@
|
|
|
13
13
|
"homepage": "https://github.com/christophehurpeau/alp",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
16
|
+
"node": ">=18.12.0"
|
|
17
17
|
},
|
|
18
|
-
"main": "./dist/index-
|
|
18
|
+
"main": "./dist/index-node18.mjs",
|
|
19
19
|
"types": "./dist/definitions/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
"./package.json": "./package.json",
|
|
22
22
|
".": {
|
|
23
|
+
"types": "./dist/definitions/index.d.ts",
|
|
23
24
|
"node": {
|
|
24
|
-
"import": "./dist/index-
|
|
25
|
+
"import": "./dist/index-node18.mjs"
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
},
|
|
28
29
|
"sideEffects": false,
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
29
34
|
"scripts": {
|
|
30
35
|
"build": "yarn clean:build && rollup --config rollup.config.mjs && yarn run build:definitions",
|
|
31
36
|
"build:definitions": "tsc -p tsconfig.json",
|
|
@@ -40,7 +45,7 @@
|
|
|
40
45
|
"babelEnvs": [
|
|
41
46
|
{
|
|
42
47
|
"target": "node",
|
|
43
|
-
"version": "
|
|
48
|
+
"version": "18"
|
|
44
49
|
}
|
|
45
50
|
],
|
|
46
51
|
"entries": [
|
|
@@ -48,20 +53,19 @@
|
|
|
48
53
|
]
|
|
49
54
|
},
|
|
50
55
|
"peerDependencies": {
|
|
51
|
-
"liwi-mongo": "^10.
|
|
56
|
+
"liwi-mongo": "^10.5.0"
|
|
52
57
|
},
|
|
53
58
|
"dependencies": {
|
|
54
|
-
"alp-types": "3.1.
|
|
55
|
-
"nightingale-logger": "^
|
|
59
|
+
"alp-types": "3.1.1",
|
|
60
|
+
"nightingale-logger": "^15.0.0",
|
|
56
61
|
"semver": "^7.3.5"
|
|
57
62
|
},
|
|
58
63
|
"devDependencies": {
|
|
59
|
-
"@babel/core": "7.
|
|
60
|
-
"@types/node": "18.
|
|
61
|
-
"@types/semver": "7.
|
|
62
|
-
"liwi-mongo": "10.
|
|
63
|
-
"pob-babel": "
|
|
64
|
-
"typescript": "
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
}
|
|
64
|
+
"@babel/core": "7.23.6",
|
|
65
|
+
"@types/node": "18.18.8",
|
|
66
|
+
"@types/semver": "7.5.4",
|
|
67
|
+
"liwi-mongo": "10.5.0",
|
|
68
|
+
"pob-babel": "38.0.0",
|
|
69
|
+
"typescript": "5.3.3"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -45,7 +45,7 @@ export default async function migrate({
|
|
|
45
45
|
|
|
46
46
|
const versionExecResult = /([\d.]+)(_.*|\.js)$/.exec(fileName);
|
|
47
47
|
|
|
48
|
-
if (!versionExecResult
|
|
48
|
+
if (!versionExecResult?.[1]) {
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -68,9 +68,9 @@ export default async function migrate({
|
|
|
68
68
|
`${dirname}/${migration.fileName}`
|
|
69
69
|
);
|
|
70
70
|
await (migrateFn as () => Promise<void>)();
|
|
71
|
-
} catch (
|
|
71
|
+
} catch (error) {
|
|
72
72
|
logger.error(`Migration to ${migration.version} Failed !`);
|
|
73
|
-
throw
|
|
73
|
+
throw error;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
logger.success(`Migration to ${migration.fileName} done !`);
|
|
@@ -80,8 +80,8 @@ export default async function migrate({
|
|
|
80
80
|
await migrationsManager.addMigrationDone(migration);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
} catch (
|
|
84
|
-
logger.error(
|
|
83
|
+
} catch (error: any) {
|
|
84
|
+
logger.error(error as Error);
|
|
85
85
|
process.exit(1);
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Stats } from 'fs';
|
|
2
|
-
import { readdir, stat as fsStat } from 'fs/promises';
|
|
1
|
+
import type { Stats } from 'node:fs';
|
|
2
|
+
import { readdir, stat as fsStat } from 'node:fs/promises';
|
|
3
3
|
|
|
4
4
|
export interface CallbackParam {
|
|
5
5
|
filename: string;
|
|
@@ -10,7 +10,7 @@ export interface CallbackParam {
|
|
|
10
10
|
|
|
11
11
|
export default async function readRecursiveDirectory(
|
|
12
12
|
directory: string,
|
|
13
|
-
callback: (param: CallbackParam) => void |
|
|
13
|
+
callback: (param: CallbackParam) => Promise<void> | void,
|
|
14
14
|
): Promise<void> {
|
|
15
15
|
const files = await readdir(directory);
|
|
16
16
|
|
package/.editorconfig
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# EditorConfig helps developers define and maintain consistent
|
|
2
|
-
# coding styles between different editors and IDEs
|
|
3
|
-
# http://editorconfig.org
|
|
4
|
-
|
|
5
|
-
root = true
|
|
6
|
-
|
|
7
|
-
[*]
|
|
8
|
-
indent_style = space
|
|
9
|
-
indent_size = 2
|
|
10
|
-
end_of_line = lf
|
|
11
|
-
charset = utf-8
|
|
12
|
-
trim_trailing_whitespace = true
|
|
13
|
-
insert_final_newline = true
|
package/.eslintrc.json
DELETED
package/AUTHORS
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Christophe Hurpeau <christophe@hurpeau.com>
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index-node16.mjs","sources":["../src/readRecursiveDirectory.ts","../src/Manager.ts","../src/index.ts"],"sourcesContent":["import type { Stats } from 'fs';\nimport { readdir, stat as fsStat } from 'fs/promises';\n\nexport interface CallbackParam {\n filename: string;\n basedir: string;\n path: string;\n stat: Stats;\n}\n\nexport default async function readRecursiveDirectory(\n directory: string,\n callback: (param: CallbackParam) => void | Promise<void>,\n): Promise<void> {\n const files = await readdir(directory);\n\n await Promise.all(\n files.map(async (file): Promise<void> => {\n const path = `${directory}/${file}`;\n const stat = await fsStat(path);\n\n if (stat?.isDirectory()) {\n await readRecursiveDirectory(path, callback);\n return;\n }\n await callback({\n filename: file,\n basedir: directory,\n path,\n stat,\n });\n }),\n );\n}\n","import type { MongoStore, MongoInsertType, MongoBaseModel } from 'liwi-mongo';\n\nexport interface Migration extends MongoBaseModel {\n version: string;\n fileName: string;\n}\n\nexport default class MigrationsManager {\n store: MongoStore<Migration>;\n\n constructor(store: MongoStore<Migration>) {\n this.store = store;\n }\n\n findLastVersion(): Promise<string | undefined> {\n return this.store.findOne({}, { created: -1 }).then((row) => row?.version);\n }\n\n addMigrationDone(migration: MongoInsertType<Migration>): Promise<Migration> {\n return this.store.insertOne(migration);\n }\n}\n","/* eslint-disable unicorn/no-process-exit */\nimport type { NodeApplication, NodeConfig } from 'alp-types';\nimport { Logger } from 'nightingale-logger';\nimport semver from 'semver';\nimport type MigrationsManager from './Manager';\nimport type { CallbackParam } from './readRecursiveDirectory';\nimport readRecursiveDirectory from './readRecursiveDirectory';\n\nconst logger = new Logger('alp:migrations');\n\nexport { default as MigrationsManager } from './Manager';\n\nexport interface Options {\n app: NodeApplication;\n migrationsManager: MigrationsManager;\n config?: NodeConfig;\n dirname?: string;\n}\n\nexport default async function migrate({\n app,\n migrationsManager,\n config = app.config,\n dirname = `${app.dirname}/migrations`,\n}: Options): Promise<void> {\n const unhandledRejectionHandler = (reason: unknown): void => {\n logger.error('unhandledRejection', { err: reason });\n process.exit(1);\n };\n process.on('unhandledRejection', unhandledRejectionHandler);\n\n const packageVersion = config.packageConfig.version as string;\n const currentVersion = await migrationsManager.findLastVersion();\n\n let migrations: { version: string; fileName: string }[] = [];\n\n logger.info('migrate', { packageVersion, currentVersion });\n\n await readRecursiveDirectory(dirname, (res: CallbackParam) => {\n const fileName = res.path.slice(dirname.length + 1);\n\n if (!fileName.endsWith('.js')) {\n return;\n }\n\n const versionExecResult = /([\\d.]+)(_.*|\\.js)$/.exec(fileName);\n\n if (!versionExecResult || !versionExecResult[1]) {\n return;\n }\n\n const version: string = versionExecResult[1];\n\n if (currentVersion && semver.lte(version, currentVersion)) return;\n\n migrations.push({ version, fileName });\n });\n\n migrations = migrations.sort((a, b) =>\n semver.gt(a.version, b.version) ? 1 : -1,\n );\n\n try {\n for (const migration of migrations) {\n logger.info(`Migration to ${migration.fileName}`);\n try {\n const migrateFn: unknown = await import(\n `${dirname}/${migration.fileName}`\n );\n await (migrateFn as () => Promise<void>)();\n } catch (err) {\n logger.error(`Migration to ${migration.version} Failed !`);\n throw err;\n }\n\n logger.success(`Migration to ${migration.fileName} done !`);\n\n // only add to db if migration version <= package version\n if (semver.lte(migration.version, packageVersion)) {\n await migrationsManager.addMigrationDone(migration);\n }\n }\n } catch (err: any) {\n logger.error(err as Error);\n process.exit(1);\n }\n\n process.removeListener('unhandledRejection', unhandledRejectionHandler);\n}\n"],"names":["readRecursiveDirectory","directory","callback","files","readdir","Promise","all","map","file","path","stat","fsStat","isDirectory","filename","basedir","MigrationsManager","constructor","store","findLastVersion","findOne","created","then","row","version","addMigrationDone","migration","insertOne","logger","Logger","migrate","app","migrationsManager","config","dirname","unhandledRejectionHandler","reason","error","err","process","exit","on","packageVersion","packageConfig","currentVersion","migrations","info","res","fileName","slice","length","endsWith","versionExecResult","exec","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