@contember/database-migrations 2.1.0-alpha.3 → 2.1.0-alpha.35
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 +11 -6
- package/src/Migration.ts +0 -1
- package/src/SnapshotMigrationResolver.ts +0 -1
- package/src/runner.ts +6 -13
- package/tests/cases/unit/groupMigrationResolver.test.ts +0 -1
- package/tests/cases/unit/snapshotMigrationResolver.test.ts +14 -10
- package/tsconfig.settings.json +0 -1
- package/dist/development/index.cjs +0 -17
- package/dist/development/index.cjs.map +0 -1
- package/dist/development/index.js +0 -17
- package/dist/development/index.js.map +0 -1
- package/dist/development/src/GroupMigrationsResolver.cjs +0 -38
- package/dist/development/src/GroupMigrationsResolver.cjs.map +0 -1
- package/dist/development/src/GroupMigrationsResolver.js +0 -38
- package/dist/development/src/GroupMigrationsResolver.js.map +0 -1
- package/dist/development/src/Migration.cjs +0 -18
- package/dist/development/src/Migration.cjs.map +0 -1
- package/dist/development/src/Migration.js +0 -18
- package/dist/development/src/Migration.js.map +0 -1
- package/dist/development/src/MigrationsRunner.cjs +0 -21
- package/dist/development/src/MigrationsRunner.cjs.map +0 -1
- package/dist/development/src/MigrationsRunner.js +0 -20
- package/dist/development/src/MigrationsRunner.js.map +0 -1
- package/dist/development/src/SnapshotMigrationResolver.cjs +0 -49
- package/dist/development/src/SnapshotMigrationResolver.cjs.map +0 -1
- package/dist/development/src/SnapshotMigrationResolver.js +0 -49
- package/dist/development/src/SnapshotMigrationResolver.js.map +0 -1
- package/dist/development/src/helpers.cjs +0 -38
- package/dist/development/src/helpers.cjs.map +0 -1
- package/dist/development/src/helpers.js +0 -38
- package/dist/development/src/helpers.js.map +0 -1
- package/dist/development/src/runner.cjs +0 -94
- package/dist/development/src/runner.cjs.map +0 -1
- package/dist/development/src/runner.js +0 -95
- package/dist/development/src/runner.js.map +0 -1
- package/dist/production/index.cjs +0 -17
- package/dist/production/index.cjs.map +0 -1
- package/dist/production/index.js +0 -17
- package/dist/production/index.js.map +0 -1
- package/dist/production/src/GroupMigrationsResolver.cjs +0 -38
- package/dist/production/src/GroupMigrationsResolver.cjs.map +0 -1
- package/dist/production/src/GroupMigrationsResolver.js +0 -38
- package/dist/production/src/GroupMigrationsResolver.js.map +0 -1
- package/dist/production/src/Migration.cjs +0 -18
- package/dist/production/src/Migration.cjs.map +0 -1
- package/dist/production/src/Migration.js +0 -18
- package/dist/production/src/Migration.js.map +0 -1
- package/dist/production/src/MigrationsRunner.cjs +0 -21
- package/dist/production/src/MigrationsRunner.cjs.map +0 -1
- package/dist/production/src/MigrationsRunner.js +0 -20
- package/dist/production/src/MigrationsRunner.js.map +0 -1
- package/dist/production/src/SnapshotMigrationResolver.cjs +0 -49
- package/dist/production/src/SnapshotMigrationResolver.cjs.map +0 -1
- package/dist/production/src/SnapshotMigrationResolver.js +0 -49
- package/dist/production/src/SnapshotMigrationResolver.js.map +0 -1
- package/dist/production/src/helpers.cjs +0 -38
- package/dist/production/src/helpers.cjs.map +0 -1
- package/dist/production/src/helpers.js +0 -38
- package/dist/production/src/helpers.js.map +0 -1
- package/dist/production/src/runner.cjs +0 -94
- package/dist/production/src/runner.cjs.map +0 -1
- package/dist/production/src/runner.js +0 -95
- package/dist/production/src/runner.js.map +0 -1
- package/dist/types/GroupMigrationsResolver.d.ts +0 -9
- package/dist/types/GroupMigrationsResolver.d.ts.map +0 -1
- package/dist/types/Migration.d.ts +0 -22
- package/dist/types/Migration.d.ts.map +0 -1
- package/dist/types/MigrationsResolver.d.ts +0 -8
- package/dist/types/MigrationsResolver.d.ts.map +0 -1
- package/dist/types/MigrationsRunner.d.ts +0 -12
- package/dist/types/MigrationsRunner.d.ts.map +0 -1
- package/dist/types/SnapshotMigrationResolver.d.ts +0 -14
- package/dist/types/SnapshotMigrationResolver.d.ts.map +0 -1
- package/dist/types/helpers.d.ts +0 -10
- package/dist/types/helpers.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -9
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/runner.d.ts +0 -13
- package/dist/types/runner.d.ts.map +0 -1
- package/dist/types/tsconfig.tsbuildinfo +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/database-migrations",
|
|
3
|
-
"version": "2.1.0-alpha.
|
|
3
|
+
"version": "2.1.0-alpha.35",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "./dist/production/index.js",
|
|
6
6
|
"typings": "./dist/types/index.d.ts",
|
|
@@ -23,14 +23,19 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@contember/database": "
|
|
27
|
-
"node-pg-migrate": "
|
|
26
|
+
"@contember/database": "workspace:*",
|
|
27
|
+
"node-pg-migrate": "catalog:"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"pg": "
|
|
30
|
+
"pg": "catalog:"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"pg": "
|
|
33
|
+
"pg": "catalog:"
|
|
34
34
|
},
|
|
35
|
-
"type": "module"
|
|
35
|
+
"type": "module",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/contember/contember.git",
|
|
39
|
+
"directory": "packages/database-migrations"
|
|
40
|
+
}
|
|
36
41
|
}
|
package/src/Migration.ts
CHANGED
|
@@ -14,7 +14,6 @@ export class SnapshotMigrationResolver<Args> implements MigrationsResolver<Args>
|
|
|
14
14
|
resolveMigrations(
|
|
15
15
|
{ runMigrations }: MigrationsResolverContext,
|
|
16
16
|
): Migration<Args>[] {
|
|
17
|
-
|
|
18
17
|
if (this.canUseSnapshot(runMigrations)) {
|
|
19
18
|
return this.getSnapshotMigration()
|
|
20
19
|
}
|
package/src/runner.ts
CHANGED
|
@@ -26,9 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
26
26
|
SOFTWARE.
|
|
27
27
|
*/
|
|
28
28
|
|
|
29
|
-
import {
|
|
30
|
-
createMigrationBuilder,
|
|
31
|
-
} from './helpers'
|
|
29
|
+
import { createMigrationBuilder } from './helpers'
|
|
32
30
|
import { Connection, withDatabaseAdvisoryLock, wrapIdentifier } from '@contember/database'
|
|
33
31
|
import { Migration, RunMigration } from './Migration'
|
|
34
32
|
import { MigrationsResolver } from './MigrationsResolver'
|
|
@@ -40,7 +38,6 @@ export type RunnerOption<Args = unknown> = {
|
|
|
40
38
|
migrationArgs: Args
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
|
|
44
41
|
// Random but well-known identifier shared by all instances of node-pg-migrate
|
|
45
42
|
const PG_MIGRATE_LOCK_ID = 7241865325823964
|
|
46
43
|
|
|
@@ -55,12 +52,11 @@ const ensureMigrationsTable = async (db: Connection.ConnectionLike, options: Run
|
|
|
55
52
|
id INT PRIMARY KEY,
|
|
56
53
|
name varchar(255) NOT NULL,
|
|
57
54
|
run_on timestamp NOT NULL
|
|
58
|
-
)
|
|
59
|
-
)
|
|
55
|
+
)`)
|
|
60
56
|
await db.query(`ALTER TABLE ${fullTableName} ADD COLUMN IF NOT EXISTS "group" TEXT DEFAULT NULL`)
|
|
61
57
|
|
|
62
58
|
await db.query(`ALTER TABLE ${fullTableName} ALTER id DROP DEFAULT`)
|
|
63
|
-
const seqName = (await db.query<{seq_name: string}>(`
|
|
59
|
+
const seqName = (await db.query<{ seq_name: string }>(`
|
|
64
60
|
SELECT pg_get_serial_sequence('${fullTableName}', 'id') as seq_name
|
|
65
61
|
`)).rows[0].seq_name
|
|
66
62
|
if (seqName) {
|
|
@@ -76,7 +72,7 @@ const ensureMigrationsTable = async (db: Connection.ConnectionLike, options: Run
|
|
|
76
72
|
|
|
77
73
|
const getRunMigrations = async (db: Connection.ConnectionLike, options: RunnerOption<unknown>) => {
|
|
78
74
|
const fullTableName = getMigrationsTableName(options)
|
|
79
|
-
return (await db.query<{name: string; group: string | null}>(`SELECT name, "group" FROM ${fullTableName} ORDER BY run_on, id`)).rows
|
|
75
|
+
return (await db.query<{ name: string; group: string | null }>(`SELECT name, "group" FROM ${fullTableName} ORDER BY run_on, id`)).rows
|
|
80
76
|
}
|
|
81
77
|
|
|
82
78
|
const getMigrationsToRun = <Args>(options: RunnerOption, runNames: string[], migrations: Migration<Args>[]): Migration<Args>[] => {
|
|
@@ -95,7 +91,6 @@ export default async <Args>(
|
|
|
95
91
|
await db.query(`SET search_path TO ${wrapIdentifier(options.schema)}`)
|
|
96
92
|
await ensureMigrationsTable(db, options)
|
|
97
93
|
|
|
98
|
-
|
|
99
94
|
const runMigrations = await getRunMigrations(db, options)
|
|
100
95
|
const migrations = await migrationsResolver.resolveMigrations({
|
|
101
96
|
runMigrations,
|
|
@@ -120,7 +115,7 @@ export default async <Args>(
|
|
|
120
115
|
for (const sql of steps) {
|
|
121
116
|
await trx.query(sql)
|
|
122
117
|
}
|
|
123
|
-
const maxId = await trx.query<{id: number}>(`SELECT MAX(id) as id FROM ${getMigrationsTableName(options)}`)
|
|
118
|
+
const maxId = await trx.query<{ id: number }>(`SELECT MAX(id) as id FROM ${getMigrationsTableName(options)}`)
|
|
124
119
|
const nextId = (maxId.rows[0]?.id ?? 0) + 1
|
|
125
120
|
await trx.query(
|
|
126
121
|
`INSERT INTO ${getMigrationsTableName(options)} (id, name, run_on, "group") VALUES (?, ?, NOW(), ?);`,
|
|
@@ -140,9 +135,7 @@ export default async <Args>(
|
|
|
140
135
|
return toRun.map(m => ({
|
|
141
136
|
name: m.name,
|
|
142
137
|
}))
|
|
143
|
-
|
|
144
138
|
})
|
|
145
|
-
})
|
|
139
|
+
})
|
|
146
140
|
)
|
|
147
|
-
|
|
148
141
|
}
|
|
@@ -2,7 +2,6 @@ import { describe, expect, test } from 'bun:test'
|
|
|
2
2
|
import { GroupMigrationsResolver, Migration } from '../../../src'
|
|
3
3
|
|
|
4
4
|
describe('group migration resolver', () => {
|
|
5
|
-
|
|
6
5
|
test('merges migrations', () => {
|
|
7
6
|
const runner = () => null
|
|
8
7
|
const resolver = new GroupMigrationsResolver({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { Migration, SnapshotMigrationResolver } from '../../../src'
|
|
3
3
|
|
|
4
4
|
describe('snapshot migration resolver', () => {
|
|
5
5
|
test('use snapshot with latest migration timestamp', () => {
|
|
@@ -16,13 +16,18 @@ describe('snapshot migration resolver', () => {
|
|
|
16
16
|
|
|
17
17
|
test('use snapshot with latest base migration timestamp', () => {
|
|
18
18
|
const snapshotRunner = () => null
|
|
19
|
-
const resolver = new SnapshotMigrationResolver(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
const resolver = new SnapshotMigrationResolver(
|
|
20
|
+
snapshotRunner,
|
|
21
|
+
{
|
|
22
|
+
'2023-07-26-105000-xx': () => null,
|
|
23
|
+
'2023-07-26-105500-yy': () => null,
|
|
24
|
+
},
|
|
25
|
+
'snapshot',
|
|
26
|
+
{
|
|
27
|
+
'2023-07-26-105000-xx': () => null,
|
|
28
|
+
'2023-07-26-105700-yy': () => null,
|
|
29
|
+
},
|
|
30
|
+
)
|
|
26
31
|
|
|
27
32
|
expect(resolver.resolveMigrations({ runMigrations: [] })).toStrictEqual([
|
|
28
33
|
new Migration('2023-07-26-105700-snapshot', snapshotRunner),
|
|
@@ -44,7 +49,6 @@ describe('snapshot migration resolver', () => {
|
|
|
44
49
|
])
|
|
45
50
|
})
|
|
46
51
|
|
|
47
|
-
|
|
48
52
|
test('merge snapshot with new migrations', () => {
|
|
49
53
|
const snapshotRunner = () => null
|
|
50
54
|
const yyRunner = () => null
|
package/tsconfig.settings.json
CHANGED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const MigrationsRunner = require("./src/MigrationsRunner.cjs");
|
|
4
|
-
const GroupMigrationsResolver = require("./src/GroupMigrationsResolver.cjs");
|
|
5
|
-
const SnapshotMigrationResolver = require("./src/SnapshotMigrationResolver.cjs");
|
|
6
|
-
const helpers = require("./src/helpers.cjs");
|
|
7
|
-
const Migration = require("./src/Migration.cjs");
|
|
8
|
-
exports.MigrationsRunner = MigrationsRunner.MigrationsRunner;
|
|
9
|
-
exports.GroupMigrationsResolver = GroupMigrationsResolver.GroupMigrationsResolver;
|
|
10
|
-
exports.SnapshotMigrationResolver = SnapshotMigrationResolver.SnapshotMigrationResolver;
|
|
11
|
-
exports.checkOrder = helpers.checkOrder;
|
|
12
|
-
exports.createMigrationBuilder = helpers.createMigrationBuilder;
|
|
13
|
-
exports.escapeValue = helpers.escapeValue;
|
|
14
|
-
exports.timePrefixLength = helpers.timePrefixLength;
|
|
15
|
-
exports.Migration = Migration.Migration;
|
|
16
|
-
exports.MigrationGroup = Migration.MigrationGroup;
|
|
17
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { MigrationsRunner } from "./src/MigrationsRunner.js";
|
|
2
|
-
import { GroupMigrationsResolver } from "./src/GroupMigrationsResolver.js";
|
|
3
|
-
import { SnapshotMigrationResolver } from "./src/SnapshotMigrationResolver.js";
|
|
4
|
-
import { checkOrder, createMigrationBuilder, escapeValue, timePrefixLength } from "./src/helpers.js";
|
|
5
|
-
import { Migration, MigrationGroup } from "./src/Migration.js";
|
|
6
|
-
export {
|
|
7
|
-
GroupMigrationsResolver,
|
|
8
|
-
Migration,
|
|
9
|
-
MigrationGroup,
|
|
10
|
-
MigrationsRunner,
|
|
11
|
-
SnapshotMigrationResolver,
|
|
12
|
-
checkOrder,
|
|
13
|
-
createMigrationBuilder,
|
|
14
|
-
escapeValue,
|
|
15
|
-
timePrefixLength
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const Migration = require("./Migration.cjs");
|
|
4
|
-
const helpers = require("./helpers.cjs");
|
|
5
|
-
class GroupMigrationsResolver {
|
|
6
|
-
constructor(primaryGroup, groups) {
|
|
7
|
-
this.primaryGroup = primaryGroup;
|
|
8
|
-
this.groups = groups;
|
|
9
|
-
}
|
|
10
|
-
resolveMigrations({ runMigrations }) {
|
|
11
|
-
const allMigrations = [];
|
|
12
|
-
const primaryMigrations = this.primaryGroup.resolveMigrations({
|
|
13
|
-
runMigrations: runMigrations.filter((it) => it.group === null)
|
|
14
|
-
});
|
|
15
|
-
allMigrations.push(...primaryMigrations);
|
|
16
|
-
for (const groupName in this.groups) {
|
|
17
|
-
const groupMigrations = this.groups[groupName].resolveMigrations({
|
|
18
|
-
runMigrations: runMigrations.filter((it) => it.group === groupName)
|
|
19
|
-
}).map((it) => new Migration.Migration(it.name, it.migration, groupName));
|
|
20
|
-
allMigrations.push(...groupMigrations);
|
|
21
|
-
}
|
|
22
|
-
return allMigrations.sort((a, b) => {
|
|
23
|
-
const compareResult = a.name.slice(0, helpers.timePrefixLength).localeCompare(b.name.slice(0, helpers.timePrefixLength));
|
|
24
|
-
if (compareResult !== 0) {
|
|
25
|
-
return compareResult;
|
|
26
|
-
}
|
|
27
|
-
if (a.group === null) {
|
|
28
|
-
return -1;
|
|
29
|
-
}
|
|
30
|
-
if (b.group === null) {
|
|
31
|
-
return 1;
|
|
32
|
-
}
|
|
33
|
-
return a.group.localeCompare(b.group) || a.name.localeCompare(b.name);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
exports.GroupMigrationsResolver = GroupMigrationsResolver;
|
|
38
|
-
//# sourceMappingURL=GroupMigrationsResolver.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"GroupMigrationsResolver.cjs","sources":["../../../../packages/database-migrations/src/GroupMigrationsResolver.ts"],"sourcesContent":["import { MigrationsResolver, MigrationsResolverContext } from './MigrationsResolver'\nimport { Migration } from './Migration'\nimport { timePrefixLength } from './helpers'\n\nexport class GroupMigrationsResolver<Args> implements MigrationsResolver<Args> {\n\tconstructor(\n\t\tprivate readonly primaryGroup: MigrationsResolver<Args>,\n\t\tprivate readonly groups: Record<string, MigrationsResolver<Args>>,\n\t) {\n\t}\n\n\tresolveMigrations({ runMigrations }: MigrationsResolverContext): Migration<Args>[] {\n\t\tconst allMigrations: Migration<Args>[] = []\n\t\tconst primaryMigrations = this.primaryGroup.resolveMigrations({\n\t\t\trunMigrations: runMigrations.filter(it => it.group === null),\n\t\t})\n\t\tallMigrations.push(...primaryMigrations)\n\t\tfor (const groupName in this.groups) {\n\t\t\tconst groupMigrations = this.groups[groupName].resolveMigrations({\n\t\t\t\trunMigrations: runMigrations.filter(it => it.group === groupName),\n\t\t\t}).map(it => new Migration(it.name, it.migration, groupName))\n\t\t\tallMigrations.push(...groupMigrations)\n\t\t}\n\t\treturn allMigrations.sort((a, b) => {\n\t\t\tconst compareResult = a.name.slice(0, timePrefixLength).localeCompare(b.name.slice(0, timePrefixLength))\n\t\t\tif (compareResult !== 0) {\n\t\t\t\treturn compareResult\n\t\t\t}\n\t\t\t// if the time is same, prefer primary group\n\t\t\tif (a.group === null) {\n\t\t\t\treturn -1\n\t\t\t}\n\t\t\tif (b.group === null) {\n\t\t\t\treturn 1\n\t\t\t}\n\t\t\treturn a.group.localeCompare(b.group) || a.name.localeCompare(b.name)\n\t\t})\n\t}\n}\n"],"names":["Migration","timePrefixLength"],"mappings":";;;;AAIO,MAAM,wBAAkE;AAAA,EAC9E,YACkB,cACA,QAChB;AAFgB,SAAA,eAAA;AACA,SAAA,SAAA;AAAA,EAAA;AAAA,EAIlB,kBAAkB,EAAE,iBAA+D;AAClF,UAAM,gBAAmC,CAAC;AACpC,UAAA,oBAAoB,KAAK,aAAa,kBAAkB;AAAA,MAC7D,eAAe,cAAc,OAAO,CAAM,OAAA,GAAG,UAAU,IAAI;AAAA,IAAA,CAC3D;AACa,kBAAA,KAAK,GAAG,iBAAiB;AAC5B,eAAA,aAAa,KAAK,QAAQ;AACpC,YAAM,kBAAkB,KAAK,OAAO,SAAS,EAAE,kBAAkB;AAAA,QAChE,eAAe,cAAc,OAAO,CAAM,OAAA,GAAG,UAAU,SAAS;AAAA,MAChE,CAAA,EAAE,IAAI,CAAM,OAAA,IAAIA,UAAU,UAAA,GAAG,MAAM,GAAG,WAAW,SAAS,CAAC;AAC9C,oBAAA,KAAK,GAAG,eAAe;AAAA,IAAA;AAEtC,WAAO,cAAc,KAAK,CAAC,GAAG,MAAM;AACnC,YAAM,gBAAgB,EAAE,KAAK,MAAM,GAAGC,QAAgB,gBAAA,EAAE,cAAc,EAAE,KAAK,MAAM,GAAGA,QAAgB,gBAAA,CAAC;AACvG,UAAI,kBAAkB,GAAG;AACjB,eAAA;AAAA,MAAA;AAGJ,UAAA,EAAE,UAAU,MAAM;AACd,eAAA;AAAA,MAAA;AAEJ,UAAA,EAAE,UAAU,MAAM;AACd,eAAA;AAAA,MAAA;AAED,aAAA,EAAE,MAAM,cAAc,EAAE,KAAK,KAAK,EAAE,KAAK,cAAc,EAAE,IAAI;AAAA,IAAA,CACpE;AAAA,EAAA;AAEH;;"}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Migration } from "./Migration.js";
|
|
2
|
-
import { timePrefixLength } from "./helpers.js";
|
|
3
|
-
class GroupMigrationsResolver {
|
|
4
|
-
constructor(primaryGroup, groups) {
|
|
5
|
-
this.primaryGroup = primaryGroup;
|
|
6
|
-
this.groups = groups;
|
|
7
|
-
}
|
|
8
|
-
resolveMigrations({ runMigrations }) {
|
|
9
|
-
const allMigrations = [];
|
|
10
|
-
const primaryMigrations = this.primaryGroup.resolveMigrations({
|
|
11
|
-
runMigrations: runMigrations.filter((it) => it.group === null)
|
|
12
|
-
});
|
|
13
|
-
allMigrations.push(...primaryMigrations);
|
|
14
|
-
for (const groupName in this.groups) {
|
|
15
|
-
const groupMigrations = this.groups[groupName].resolveMigrations({
|
|
16
|
-
runMigrations: runMigrations.filter((it) => it.group === groupName)
|
|
17
|
-
}).map((it) => new Migration(it.name, it.migration, groupName));
|
|
18
|
-
allMigrations.push(...groupMigrations);
|
|
19
|
-
}
|
|
20
|
-
return allMigrations.sort((a, b) => {
|
|
21
|
-
const compareResult = a.name.slice(0, timePrefixLength).localeCompare(b.name.slice(0, timePrefixLength));
|
|
22
|
-
if (compareResult !== 0) {
|
|
23
|
-
return compareResult;
|
|
24
|
-
}
|
|
25
|
-
if (a.group === null) {
|
|
26
|
-
return -1;
|
|
27
|
-
}
|
|
28
|
-
if (b.group === null) {
|
|
29
|
-
return 1;
|
|
30
|
-
}
|
|
31
|
-
return a.group.localeCompare(b.group) || a.name.localeCompare(b.name);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
export {
|
|
36
|
-
GroupMigrationsResolver
|
|
37
|
-
};
|
|
38
|
-
//# sourceMappingURL=GroupMigrationsResolver.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"GroupMigrationsResolver.js","sources":["../../../../packages/database-migrations/src/GroupMigrationsResolver.ts"],"sourcesContent":["import { MigrationsResolver, MigrationsResolverContext } from './MigrationsResolver'\nimport { Migration } from './Migration'\nimport { timePrefixLength } from './helpers'\n\nexport class GroupMigrationsResolver<Args> implements MigrationsResolver<Args> {\n\tconstructor(\n\t\tprivate readonly primaryGroup: MigrationsResolver<Args>,\n\t\tprivate readonly groups: Record<string, MigrationsResolver<Args>>,\n\t) {\n\t}\n\n\tresolveMigrations({ runMigrations }: MigrationsResolverContext): Migration<Args>[] {\n\t\tconst allMigrations: Migration<Args>[] = []\n\t\tconst primaryMigrations = this.primaryGroup.resolveMigrations({\n\t\t\trunMigrations: runMigrations.filter(it => it.group === null),\n\t\t})\n\t\tallMigrations.push(...primaryMigrations)\n\t\tfor (const groupName in this.groups) {\n\t\t\tconst groupMigrations = this.groups[groupName].resolveMigrations({\n\t\t\t\trunMigrations: runMigrations.filter(it => it.group === groupName),\n\t\t\t}).map(it => new Migration(it.name, it.migration, groupName))\n\t\t\tallMigrations.push(...groupMigrations)\n\t\t}\n\t\treturn allMigrations.sort((a, b) => {\n\t\t\tconst compareResult = a.name.slice(0, timePrefixLength).localeCompare(b.name.slice(0, timePrefixLength))\n\t\t\tif (compareResult !== 0) {\n\t\t\t\treturn compareResult\n\t\t\t}\n\t\t\t// if the time is same, prefer primary group\n\t\t\tif (a.group === null) {\n\t\t\t\treturn -1\n\t\t\t}\n\t\t\tif (b.group === null) {\n\t\t\t\treturn 1\n\t\t\t}\n\t\t\treturn a.group.localeCompare(b.group) || a.name.localeCompare(b.name)\n\t\t})\n\t}\n}\n"],"names":[],"mappings":";;AAIO,MAAM,wBAAkE;AAAA,EAC9E,YACkB,cACA,QAChB;AAFgB,SAAA,eAAA;AACA,SAAA,SAAA;AAAA,EAAA;AAAA,EAIlB,kBAAkB,EAAE,iBAA+D;AAClF,UAAM,gBAAmC,CAAC;AACpC,UAAA,oBAAoB,KAAK,aAAa,kBAAkB;AAAA,MAC7D,eAAe,cAAc,OAAO,CAAM,OAAA,GAAG,UAAU,IAAI;AAAA,IAAA,CAC3D;AACa,kBAAA,KAAK,GAAG,iBAAiB;AAC5B,eAAA,aAAa,KAAK,QAAQ;AACpC,YAAM,kBAAkB,KAAK,OAAO,SAAS,EAAE,kBAAkB;AAAA,QAChE,eAAe,cAAc,OAAO,CAAM,OAAA,GAAG,UAAU,SAAS;AAAA,MAChE,CAAA,EAAE,IAAI,CAAM,OAAA,IAAI,UAAU,GAAG,MAAM,GAAG,WAAW,SAAS,CAAC;AAC9C,oBAAA,KAAK,GAAG,eAAe;AAAA,IAAA;AAEtC,WAAO,cAAc,KAAK,CAAC,GAAG,MAAM;AACnC,YAAM,gBAAgB,EAAE,KAAK,MAAM,GAAG,gBAAgB,EAAE,cAAc,EAAE,KAAK,MAAM,GAAG,gBAAgB,CAAC;AACvG,UAAI,kBAAkB,GAAG;AACjB,eAAA;AAAA,MAAA;AAGJ,UAAA,EAAE,UAAU,MAAM;AACd,eAAA;AAAA,MAAA;AAEJ,UAAA,EAAE,UAAU,MAAM;AACd,eAAA;AAAA,MAAA;AAED,aAAA,EAAE,MAAM,cAAc,EAAE,KAAK,KAAK,EAAE,KAAK,cAAc,EAAE,IAAI;AAAA,IAAA,CACpE;AAAA,EAAA;AAEH;"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
class Migration {
|
|
4
|
-
constructor(name, migration, group = null) {
|
|
5
|
-
this.name = name;
|
|
6
|
-
this.migration = migration;
|
|
7
|
-
this.group = group;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
class MigrationGroup {
|
|
11
|
-
constructor(snapshot, migrations) {
|
|
12
|
-
this.snapshot = snapshot;
|
|
13
|
-
this.migrations = migrations;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.Migration = Migration;
|
|
17
|
-
exports.MigrationGroup = MigrationGroup;
|
|
18
|
-
//# sourceMappingURL=Migration.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Migration.cjs","sources":["../../../../packages/database-migrations/src/Migration.ts"],"sourcesContent":["import { MigrationBuilder } from 'node-pg-migrate'\nimport { Connection } from '@contember/database'\n\nexport type MigrationExecutor<Args = unknown> = (builder: MigrationBuilder, args: MigrationArgs<Args>) => Promise<void> | void\n\nexport class Migration<Args = unknown> {\n\tconstructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly migration: MigrationExecutor<Args>,\n\t\tpublic readonly group: string | null = null,\n\t) {\n\t}\n}\n\nexport type MigrationArgs<Args> =\n\t& Args\n\t& {\n\t\tconnection: Connection.ConnectionLike\n\t}\n\n\nexport interface RunMigration {\n\tname: string\n\tgroup: string | null\n}\n\nexport class MigrationGroup<Args = unknown> {\n\tconstructor(\n\t\tpublic readonly snapshot: MigrationExecutor<Args>,\n\t\tpublic readonly migrations: Record<string, MigrationExecutor<Args>>,\n\t) {\n\t}\n}\n"],"names":[],"mappings":";;AAKO,MAAM,UAA0B;AAAA,EACtC,YACiB,MACA,WACA,QAAuB,MACtC;AAHe,SAAA,OAAA;AACA,SAAA,YAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAGlB;AAcO,MAAM,eAA+B;AAAA,EAC3C,YACiB,UACA,YACf;AAFe,SAAA,WAAA;AACA,SAAA,aAAA;AAAA,EAAA;AAGlB;;;"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
class Migration {
|
|
2
|
-
constructor(name, migration, group = null) {
|
|
3
|
-
this.name = name;
|
|
4
|
-
this.migration = migration;
|
|
5
|
-
this.group = group;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
class MigrationGroup {
|
|
9
|
-
constructor(snapshot, migrations) {
|
|
10
|
-
this.snapshot = snapshot;
|
|
11
|
-
this.migrations = migrations;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export {
|
|
15
|
-
Migration,
|
|
16
|
-
MigrationGroup
|
|
17
|
-
};
|
|
18
|
-
//# sourceMappingURL=Migration.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Migration.js","sources":["../../../../packages/database-migrations/src/Migration.ts"],"sourcesContent":["import { MigrationBuilder } from 'node-pg-migrate'\nimport { Connection } from '@contember/database'\n\nexport type MigrationExecutor<Args = unknown> = (builder: MigrationBuilder, args: MigrationArgs<Args>) => Promise<void> | void\n\nexport class Migration<Args = unknown> {\n\tconstructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly migration: MigrationExecutor<Args>,\n\t\tpublic readonly group: string | null = null,\n\t) {\n\t}\n}\n\nexport type MigrationArgs<Args> =\n\t& Args\n\t& {\n\t\tconnection: Connection.ConnectionLike\n\t}\n\n\nexport interface RunMigration {\n\tname: string\n\tgroup: string | null\n}\n\nexport class MigrationGroup<Args = unknown> {\n\tconstructor(\n\t\tpublic readonly snapshot: MigrationExecutor<Args>,\n\t\tpublic readonly migrations: Record<string, MigrationExecutor<Args>>,\n\t) {\n\t}\n}\n"],"names":[],"mappings":"AAKO,MAAM,UAA0B;AAAA,EACtC,YACiB,MACA,WACA,QAAuB,MACtC;AAHe,SAAA,OAAA;AACA,SAAA,YAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAGlB;AAcO,MAAM,eAA+B;AAAA,EAC3C,YACiB,UACA,YACf;AAFe,SAAA,WAAA;AACA,SAAA,aAAA;AAAA,EAAA;AAGlB;"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const _interopNamespaceDefaultOnly = (e) => Object.freeze(Object.defineProperty({ __proto__: null, default: e }, Symbol.toStringTag, { value: "Module" }));
|
|
4
|
-
class MigrationsRunner {
|
|
5
|
-
constructor(connection, schema, migrationsResolver) {
|
|
6
|
-
this.connection = connection;
|
|
7
|
-
this.schema = schema;
|
|
8
|
-
this.migrationsResolver = migrationsResolver;
|
|
9
|
-
}
|
|
10
|
-
async migrate(log, migrationArgs) {
|
|
11
|
-
const migrate = (await Promise.resolve().then(() => /* @__PURE__ */ _interopNamespaceDefaultOnly(require("./runner.cjs")))).default;
|
|
12
|
-
return await migrate(this.migrationsResolver, this.connection, {
|
|
13
|
-
schema: this.schema,
|
|
14
|
-
migrationsTable: "migrations",
|
|
15
|
-
migrationArgs,
|
|
16
|
-
log
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.MigrationsRunner = MigrationsRunner;
|
|
21
|
-
//# sourceMappingURL=MigrationsRunner.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MigrationsRunner.cjs","sources":["../../../../packages/database-migrations/src/MigrationsRunner.ts"],"sourcesContent":["import { Connection } from '@contember/database'\nimport { MigrationsResolver } from './MigrationsResolver'\n\nexport class MigrationsRunner<MigrationArgs> {\n\tconstructor(\n\t\tprivate readonly connection: Connection.ConnectionLike,\n\t\tprivate readonly schema: string,\n\t\tprivate readonly migrationsResolver: MigrationsResolver<MigrationArgs>,\n\t) {}\n\n\tpublic async migrate(\n\t\tlog: (msg: string) => void,\n\t\tmigrationArgs: MigrationArgs,\n\t): Promise<{ name: string }[]> {\n\t\tconst migrate = (await import('./runner')).default\n\t\treturn await migrate(this.migrationsResolver, this.connection, {\n\t\t\tschema: this.schema,\n\t\t\tmigrationsTable: 'migrations',\n\t\t\tmigrationArgs,\n\t\t\tlog,\n\t\t})\n\t}\n}\n"],"names":[],"mappings":";;;AAGO,MAAM,iBAAgC;AAAA,EAC5C,YACkB,YACA,QACA,oBAChB;AAHgB,SAAA,aAAA;AACA,SAAA,SAAA;AACA,SAAA,qBAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,QACZ,KACA,eAC8B;AAC9B,UAAM,WAAW,MAAM,QAAO,QAAA,EAAA,KAAA,MAAA,6CAAA,QAAA,cAAU,CAAG,CAAA,GAAA;AAC3C,WAAO,MAAM,QAAQ,KAAK,oBAAoB,KAAK,YAAY;AAAA,MAC9D,QAAQ,KAAK;AAAA,MACb,iBAAiB;AAAA,MACjB;AAAA,MACA;AAAA,IAAA,CACA;AAAA,EAAA;AAEH;;"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
class MigrationsRunner {
|
|
2
|
-
constructor(connection, schema, migrationsResolver) {
|
|
3
|
-
this.connection = connection;
|
|
4
|
-
this.schema = schema;
|
|
5
|
-
this.migrationsResolver = migrationsResolver;
|
|
6
|
-
}
|
|
7
|
-
async migrate(log, migrationArgs) {
|
|
8
|
-
const migrate = (await import("./runner.js")).default;
|
|
9
|
-
return await migrate(this.migrationsResolver, this.connection, {
|
|
10
|
-
schema: this.schema,
|
|
11
|
-
migrationsTable: "migrations",
|
|
12
|
-
migrationArgs,
|
|
13
|
-
log
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
MigrationsRunner
|
|
19
|
-
};
|
|
20
|
-
//# sourceMappingURL=MigrationsRunner.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MigrationsRunner.js","sources":["../../../../packages/database-migrations/src/MigrationsRunner.ts"],"sourcesContent":["import { Connection } from '@contember/database'\nimport { MigrationsResolver } from './MigrationsResolver'\n\nexport class MigrationsRunner<MigrationArgs> {\n\tconstructor(\n\t\tprivate readonly connection: Connection.ConnectionLike,\n\t\tprivate readonly schema: string,\n\t\tprivate readonly migrationsResolver: MigrationsResolver<MigrationArgs>,\n\t) {}\n\n\tpublic async migrate(\n\t\tlog: (msg: string) => void,\n\t\tmigrationArgs: MigrationArgs,\n\t): Promise<{ name: string }[]> {\n\t\tconst migrate = (await import('./runner')).default\n\t\treturn await migrate(this.migrationsResolver, this.connection, {\n\t\t\tschema: this.schema,\n\t\t\tmigrationsTable: 'migrations',\n\t\t\tmigrationArgs,\n\t\t\tlog,\n\t\t})\n\t}\n}\n"],"names":[],"mappings":"AAGO,MAAM,iBAAgC;AAAA,EAC5C,YACkB,YACA,QACA,oBAChB;AAHgB,SAAA,aAAA;AACA,SAAA,SAAA;AACA,SAAA,qBAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,QACZ,KACA,eAC8B;AAC9B,UAAM,WAAW,MAAM,OAAO,aAAU,GAAG;AAC3C,WAAO,MAAM,QAAQ,KAAK,oBAAoB,KAAK,YAAY;AAAA,MAC9D,QAAQ,KAAK;AAAA,MACb,iBAAiB;AAAA,MACjB;AAAA,MACA;AAAA,IAAA,CACA;AAAA,EAAA;AAEH;"}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const Migration = require("./Migration.cjs");
|
|
4
|
-
const helpers = require("./helpers.cjs");
|
|
5
|
-
class SnapshotMigrationResolver {
|
|
6
|
-
constructor(snapshot, migrations, suffix = "snapshot", baseMigrations) {
|
|
7
|
-
this.snapshot = snapshot;
|
|
8
|
-
this.migrations = migrations;
|
|
9
|
-
this.suffix = suffix;
|
|
10
|
-
this.baseMigrations = baseMigrations;
|
|
11
|
-
}
|
|
12
|
-
resolveMigrations({ runMigrations }) {
|
|
13
|
-
if (this.canUseSnapshot(runMigrations)) {
|
|
14
|
-
return this.getSnapshotMigration();
|
|
15
|
-
}
|
|
16
|
-
const resolvedMigrations = this.getEffectiveMigrations(runMigrations);
|
|
17
|
-
helpers.checkOrder(runMigrations, resolvedMigrations);
|
|
18
|
-
return resolvedMigrations;
|
|
19
|
-
}
|
|
20
|
-
canUseSnapshot(runMigrations) {
|
|
21
|
-
return runMigrations.length === 0 && !process.env.CONTEMBER_MIGRATIONS_NO_SNAPSHOT;
|
|
22
|
-
}
|
|
23
|
-
getSnapshotMigration() {
|
|
24
|
-
const migrations = Object.keys(this.migrations);
|
|
25
|
-
const baseMigrations = Object.keys(this.baseMigrations ?? {});
|
|
26
|
-
const lastMigration = migrations[migrations.length - 1]?.slice(0, helpers.timePrefixLength);
|
|
27
|
-
const lastBaseMigration = baseMigrations[baseMigrations.length - 1]?.slice(0, helpers.timePrefixLength);
|
|
28
|
-
const fallback = "0000-00-00-000000";
|
|
29
|
-
const timePrefix = lastMigration && lastBaseMigration ? lastMigration > lastBaseMigration ? lastMigration : lastBaseMigration : lastMigration ?? lastBaseMigration ?? fallback;
|
|
30
|
-
return [
|
|
31
|
-
new Migration.Migration(`${timePrefix}-${this.suffix}`, this.snapshot)
|
|
32
|
-
];
|
|
33
|
-
}
|
|
34
|
-
getEffectiveMigrations(runMigrations) {
|
|
35
|
-
const migrations = Object.entries(this.migrations).map(([version, executor]) => new Migration.Migration(version, executor));
|
|
36
|
-
const wasSnapshotExecuted = runMigrations.length > 0 && runMigrations[0]?.name.slice(helpers.timePrefixLength) === `-${this.suffix}`;
|
|
37
|
-
if (!wasSnapshotExecuted) {
|
|
38
|
-
return migrations;
|
|
39
|
-
}
|
|
40
|
-
const executedSnapshotMigration = new Migration.Migration(runMigrations[0].name, this.snapshot);
|
|
41
|
-
const minVersion = runMigrations[0].name.slice(0, helpers.timePrefixLength);
|
|
42
|
-
return [
|
|
43
|
-
executedSnapshotMigration,
|
|
44
|
-
...migrations.filter((it) => it.name.slice(0, helpers.timePrefixLength) > minVersion)
|
|
45
|
-
];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.SnapshotMigrationResolver = SnapshotMigrationResolver;
|
|
49
|
-
//# sourceMappingURL=SnapshotMigrationResolver.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SnapshotMigrationResolver.cjs","sources":["../../../../packages/database-migrations/src/SnapshotMigrationResolver.ts"],"sourcesContent":["import { Migration, MigrationExecutor, RunMigration } from './Migration'\nimport { MigrationsResolver, MigrationsResolverContext } from './MigrationsResolver'\nimport { checkOrder, timePrefixLength } from './helpers'\n\nexport class SnapshotMigrationResolver<Args> implements MigrationsResolver<Args> {\n\tconstructor(\n\t\tprivate snapshot: MigrationExecutor<Args>,\n\t\tprivate migrations: Record<string, MigrationExecutor<Args>>,\n\t\tprivate suffix: string = 'snapshot',\n\t\tprivate baseMigrations?: Record<string, MigrationExecutor<Args>>,\n\t) {\n\t}\n\n\tresolveMigrations(\n\t\t{ runMigrations }: MigrationsResolverContext,\n\t): Migration<Args>[] {\n\n\t\tif (this.canUseSnapshot(runMigrations)) {\n\t\t\treturn this.getSnapshotMigration()\n\t\t}\n\n\t\tconst resolvedMigrations = this.getEffectiveMigrations(runMigrations)\n\n\t\tcheckOrder(runMigrations, resolvedMigrations)\n\n\t\treturn resolvedMigrations\n\t}\n\n\tprivate canUseSnapshot(runMigrations: RunMigration[]): boolean {\n\t\treturn runMigrations.length === 0 && !process.env.CONTEMBER_MIGRATIONS_NO_SNAPSHOT\n\t}\n\n\tprivate getSnapshotMigration(): Migration<Args>[] {\n\t\tconst migrations = Object.keys(this.migrations)\n\t\tconst baseMigrations = Object.keys(this.baseMigrations ?? {})\n\n\t\tconst lastMigration = migrations[migrations.length - 1]?.slice(0, timePrefixLength)\n\t\tconst lastBaseMigration = baseMigrations[baseMigrations.length - 1]?.slice(0, timePrefixLength)\n\n\t\tconst fallback = '0000-00-00-000000'\n\t\t// time prefix matches latest base or current group migration\n\t\tconst timePrefix = lastMigration && lastBaseMigration\n\t\t\t? (lastMigration > lastBaseMigration ? lastMigration : lastBaseMigration)\n\t\t\t: (lastMigration ?? lastBaseMigration ?? fallback)\n\n\t\treturn [\n\t\t\tnew Migration<Args>(`${timePrefix}-${this.suffix}`, this.snapshot),\n\t\t]\n\t}\n\n\tprivate getEffectiveMigrations(runMigrations: RunMigration[]): Migration<Args>[] {\n\t\tconst migrations = Object.entries(this.migrations).map(([version, executor]) => new Migration<Args>(version, executor))\n\n\t\tconst wasSnapshotExecuted = runMigrations.length > 0\n\t\t\t&& runMigrations[0]?.name.slice(timePrefixLength) === `-${this.suffix}`\n\n\t\tif (!wasSnapshotExecuted) {\n\t\t\treturn migrations\n\t\t}\n\n\t\tconst executedSnapshotMigration = new Migration<Args>(runMigrations[0].name, this.snapshot)\n\t\tconst minVersion = runMigrations[0].name.slice(0, timePrefixLength)\n\n\t\treturn [\n\t\t\texecutedSnapshotMigration,\n\t\t\t...migrations.filter(it => it.name.slice(0, timePrefixLength) > minVersion),\n\t\t]\n\t}\n}\n"],"names":["checkOrder","timePrefixLength","Migration"],"mappings":";;;;AAIO,MAAM,0BAAoE;AAAA,EAChF,YACS,UACA,YACA,SAAiB,YACjB,gBACP;AAJO,SAAA,WAAA;AACA,SAAA,aAAA;AACA,SAAA,SAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAIT,kBACC,EAAE,iBACkB;AAEhB,QAAA,KAAK,eAAe,aAAa,GAAG;AACvC,aAAO,KAAK,qBAAqB;AAAA,IAAA;AAG5B,UAAA,qBAAqB,KAAK,uBAAuB,aAAa;AAEpEA,YAAA,WAAW,eAAe,kBAAkB;AAErC,WAAA;AAAA,EAAA;AAAA,EAGA,eAAe,eAAwC;AAC9D,WAAO,cAAc,WAAW,KAAK,CAAC,QAAQ,IAAI;AAAA,EAAA;AAAA,EAG3C,uBAA0C;AACjD,UAAM,aAAa,OAAO,KAAK,KAAK,UAAU;AAC9C,UAAM,iBAAiB,OAAO,KAAK,KAAK,kBAAkB,CAAA,CAAE;AAEtD,UAAA,gBAAgB,WAAW,WAAW,SAAS,CAAC,GAAG,MAAM,GAAGC,wBAAgB;AAC5E,UAAA,oBAAoB,eAAe,eAAe,SAAS,CAAC,GAAG,MAAM,GAAGA,wBAAgB;AAE9F,UAAM,WAAW;AAEX,UAAA,aAAa,iBAAiB,oBAChC,gBAAgB,oBAAoB,gBAAgB,oBACpD,iBAAiB,qBAAqB;AAEnC,WAAA;AAAA,MACN,IAAIC,oBAAgB,GAAG,UAAU,IAAI,KAAK,MAAM,IAAI,KAAK,QAAQ;AAAA,IAClE;AAAA,EAAA;AAAA,EAGO,uBAAuB,eAAkD;AAChF,UAAM,aAAa,OAAO,QAAQ,KAAK,UAAU,EAAE,IAAI,CAAC,CAAC,SAAS,QAAQ,MAAM,IAAIA,UAAgB,UAAA,SAAS,QAAQ,CAAC;AAEtH,UAAM,sBAAsB,cAAc,SAAS,KAC/C,cAAc,CAAC,GAAG,KAAK,MAAMD,QAAgB,gBAAA,MAAM,IAAI,KAAK,MAAM;AAEtE,QAAI,CAAC,qBAAqB;AAClB,aAAA;AAAA,IAAA;AAGF,UAAA,4BAA4B,IAAIC,UAAAA,UAAgB,cAAc,CAAC,EAAE,MAAM,KAAK,QAAQ;AAC1F,UAAM,aAAa,cAAc,CAAC,EAAE,KAAK,MAAM,GAAGD,wBAAgB;AAE3D,WAAA;AAAA,MACN;AAAA,MACA,GAAG,WAAW,OAAO,CAAM,OAAA,GAAG,KAAK,MAAM,GAAGA,QAAgB,gBAAA,IAAI,UAAU;AAAA,IAC3E;AAAA,EAAA;AAEF;;"}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Migration } from "./Migration.js";
|
|
2
|
-
import { checkOrder, timePrefixLength } from "./helpers.js";
|
|
3
|
-
class SnapshotMigrationResolver {
|
|
4
|
-
constructor(snapshot, migrations, suffix = "snapshot", baseMigrations) {
|
|
5
|
-
this.snapshot = snapshot;
|
|
6
|
-
this.migrations = migrations;
|
|
7
|
-
this.suffix = suffix;
|
|
8
|
-
this.baseMigrations = baseMigrations;
|
|
9
|
-
}
|
|
10
|
-
resolveMigrations({ runMigrations }) {
|
|
11
|
-
if (this.canUseSnapshot(runMigrations)) {
|
|
12
|
-
return this.getSnapshotMigration();
|
|
13
|
-
}
|
|
14
|
-
const resolvedMigrations = this.getEffectiveMigrations(runMigrations);
|
|
15
|
-
checkOrder(runMigrations, resolvedMigrations);
|
|
16
|
-
return resolvedMigrations;
|
|
17
|
-
}
|
|
18
|
-
canUseSnapshot(runMigrations) {
|
|
19
|
-
return runMigrations.length === 0 && !process.env.CONTEMBER_MIGRATIONS_NO_SNAPSHOT;
|
|
20
|
-
}
|
|
21
|
-
getSnapshotMigration() {
|
|
22
|
-
const migrations = Object.keys(this.migrations);
|
|
23
|
-
const baseMigrations = Object.keys(this.baseMigrations ?? {});
|
|
24
|
-
const lastMigration = migrations[migrations.length - 1]?.slice(0, timePrefixLength);
|
|
25
|
-
const lastBaseMigration = baseMigrations[baseMigrations.length - 1]?.slice(0, timePrefixLength);
|
|
26
|
-
const fallback = "0000-00-00-000000";
|
|
27
|
-
const timePrefix = lastMigration && lastBaseMigration ? lastMigration > lastBaseMigration ? lastMigration : lastBaseMigration : lastMigration ?? lastBaseMigration ?? fallback;
|
|
28
|
-
return [
|
|
29
|
-
new Migration(`${timePrefix}-${this.suffix}`, this.snapshot)
|
|
30
|
-
];
|
|
31
|
-
}
|
|
32
|
-
getEffectiveMigrations(runMigrations) {
|
|
33
|
-
const migrations = Object.entries(this.migrations).map(([version, executor]) => new Migration(version, executor));
|
|
34
|
-
const wasSnapshotExecuted = runMigrations.length > 0 && runMigrations[0]?.name.slice(timePrefixLength) === `-${this.suffix}`;
|
|
35
|
-
if (!wasSnapshotExecuted) {
|
|
36
|
-
return migrations;
|
|
37
|
-
}
|
|
38
|
-
const executedSnapshotMigration = new Migration(runMigrations[0].name, this.snapshot);
|
|
39
|
-
const minVersion = runMigrations[0].name.slice(0, timePrefixLength);
|
|
40
|
-
return [
|
|
41
|
-
executedSnapshotMigration,
|
|
42
|
-
...migrations.filter((it) => it.name.slice(0, timePrefixLength) > minVersion)
|
|
43
|
-
];
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
export {
|
|
47
|
-
SnapshotMigrationResolver
|
|
48
|
-
};
|
|
49
|
-
//# sourceMappingURL=SnapshotMigrationResolver.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SnapshotMigrationResolver.js","sources":["../../../../packages/database-migrations/src/SnapshotMigrationResolver.ts"],"sourcesContent":["import { Migration, MigrationExecutor, RunMigration } from './Migration'\nimport { MigrationsResolver, MigrationsResolverContext } from './MigrationsResolver'\nimport { checkOrder, timePrefixLength } from './helpers'\n\nexport class SnapshotMigrationResolver<Args> implements MigrationsResolver<Args> {\n\tconstructor(\n\t\tprivate snapshot: MigrationExecutor<Args>,\n\t\tprivate migrations: Record<string, MigrationExecutor<Args>>,\n\t\tprivate suffix: string = 'snapshot',\n\t\tprivate baseMigrations?: Record<string, MigrationExecutor<Args>>,\n\t) {\n\t}\n\n\tresolveMigrations(\n\t\t{ runMigrations }: MigrationsResolverContext,\n\t): Migration<Args>[] {\n\n\t\tif (this.canUseSnapshot(runMigrations)) {\n\t\t\treturn this.getSnapshotMigration()\n\t\t}\n\n\t\tconst resolvedMigrations = this.getEffectiveMigrations(runMigrations)\n\n\t\tcheckOrder(runMigrations, resolvedMigrations)\n\n\t\treturn resolvedMigrations\n\t}\n\n\tprivate canUseSnapshot(runMigrations: RunMigration[]): boolean {\n\t\treturn runMigrations.length === 0 && !process.env.CONTEMBER_MIGRATIONS_NO_SNAPSHOT\n\t}\n\n\tprivate getSnapshotMigration(): Migration<Args>[] {\n\t\tconst migrations = Object.keys(this.migrations)\n\t\tconst baseMigrations = Object.keys(this.baseMigrations ?? {})\n\n\t\tconst lastMigration = migrations[migrations.length - 1]?.slice(0, timePrefixLength)\n\t\tconst lastBaseMigration = baseMigrations[baseMigrations.length - 1]?.slice(0, timePrefixLength)\n\n\t\tconst fallback = '0000-00-00-000000'\n\t\t// time prefix matches latest base or current group migration\n\t\tconst timePrefix = lastMigration && lastBaseMigration\n\t\t\t? (lastMigration > lastBaseMigration ? lastMigration : lastBaseMigration)\n\t\t\t: (lastMigration ?? lastBaseMigration ?? fallback)\n\n\t\treturn [\n\t\t\tnew Migration<Args>(`${timePrefix}-${this.suffix}`, this.snapshot),\n\t\t]\n\t}\n\n\tprivate getEffectiveMigrations(runMigrations: RunMigration[]): Migration<Args>[] {\n\t\tconst migrations = Object.entries(this.migrations).map(([version, executor]) => new Migration<Args>(version, executor))\n\n\t\tconst wasSnapshotExecuted = runMigrations.length > 0\n\t\t\t&& runMigrations[0]?.name.slice(timePrefixLength) === `-${this.suffix}`\n\n\t\tif (!wasSnapshotExecuted) {\n\t\t\treturn migrations\n\t\t}\n\n\t\tconst executedSnapshotMigration = new Migration<Args>(runMigrations[0].name, this.snapshot)\n\t\tconst minVersion = runMigrations[0].name.slice(0, timePrefixLength)\n\n\t\treturn [\n\t\t\texecutedSnapshotMigration,\n\t\t\t...migrations.filter(it => it.name.slice(0, timePrefixLength) > minVersion),\n\t\t]\n\t}\n}\n"],"names":[],"mappings":";;AAIO,MAAM,0BAAoE;AAAA,EAChF,YACS,UACA,YACA,SAAiB,YACjB,gBACP;AAJO,SAAA,WAAA;AACA,SAAA,aAAA;AACA,SAAA,SAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAIT,kBACC,EAAE,iBACkB;AAEhB,QAAA,KAAK,eAAe,aAAa,GAAG;AACvC,aAAO,KAAK,qBAAqB;AAAA,IAAA;AAG5B,UAAA,qBAAqB,KAAK,uBAAuB,aAAa;AAEpE,eAAW,eAAe,kBAAkB;AAErC,WAAA;AAAA,EAAA;AAAA,EAGA,eAAe,eAAwC;AAC9D,WAAO,cAAc,WAAW,KAAK,CAAC,QAAQ,IAAI;AAAA,EAAA;AAAA,EAG3C,uBAA0C;AACjD,UAAM,aAAa,OAAO,KAAK,KAAK,UAAU;AAC9C,UAAM,iBAAiB,OAAO,KAAK,KAAK,kBAAkB,CAAA,CAAE;AAEtD,UAAA,gBAAgB,WAAW,WAAW,SAAS,CAAC,GAAG,MAAM,GAAG,gBAAgB;AAC5E,UAAA,oBAAoB,eAAe,eAAe,SAAS,CAAC,GAAG,MAAM,GAAG,gBAAgB;AAE9F,UAAM,WAAW;AAEX,UAAA,aAAa,iBAAiB,oBAChC,gBAAgB,oBAAoB,gBAAgB,oBACpD,iBAAiB,qBAAqB;AAEnC,WAAA;AAAA,MACN,IAAI,UAAgB,GAAG,UAAU,IAAI,KAAK,MAAM,IAAI,KAAK,QAAQ;AAAA,IAClE;AAAA,EAAA;AAAA,EAGO,uBAAuB,eAAkD;AAChF,UAAM,aAAa,OAAO,QAAQ,KAAK,UAAU,EAAE,IAAI,CAAC,CAAC,SAAS,QAAQ,MAAM,IAAI,UAAgB,SAAS,QAAQ,CAAC;AAEtH,UAAM,sBAAsB,cAAc,SAAS,KAC/C,cAAc,CAAC,GAAG,KAAK,MAAM,gBAAgB,MAAM,IAAI,KAAK,MAAM;AAEtE,QAAI,CAAC,qBAAqB;AAClB,aAAA;AAAA,IAAA;AAGF,UAAA,4BAA4B,IAAI,UAAgB,cAAc,CAAC,EAAE,MAAM,KAAK,QAAQ;AAC1F,UAAM,aAAa,cAAc,CAAC,EAAE,KAAK,MAAM,GAAG,gBAAgB;AAE3D,WAAA;AAAA,MACN;AAAA,MACA,GAAG,WAAW,OAAO,CAAM,OAAA,GAAG,KAAK,MAAM,GAAG,gBAAgB,IAAI,UAAU;AAAA,IAC3E;AAAA,EAAA;AAEF;"}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const MigrationBuilderImpl = require("node-pg-migrate/dist/migration-builder.js");
|
|
4
|
-
const utils_js = require("node-pg-migrate/dist/utils.js");
|
|
5
|
-
function createMigrationBuilder() {
|
|
6
|
-
const impl = "default" in MigrationBuilderImpl ? MigrationBuilderImpl.default : MigrationBuilderImpl;
|
|
7
|
-
return new impl(
|
|
8
|
-
{
|
|
9
|
-
query: null,
|
|
10
|
-
select: null
|
|
11
|
-
},
|
|
12
|
-
{},
|
|
13
|
-
false,
|
|
14
|
-
{}
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
function escapeValue(value) {
|
|
18
|
-
return utils_js.escapeValue(value);
|
|
19
|
-
}
|
|
20
|
-
const timePrefixLength = "YYYY-MM-DD-XXXXXX".length;
|
|
21
|
-
const checkOrder = (runMigrations, migrations) => {
|
|
22
|
-
const len = Math.min(runMigrations.length, migrations.length);
|
|
23
|
-
for (let i = 0; i < len; i++) {
|
|
24
|
-
const run = runMigrations[i];
|
|
25
|
-
const migrationName = migrations[i].name;
|
|
26
|
-
if (migrationName > run.name) {
|
|
27
|
-
throw new Error(`Previously executed migration ${run} is missing`);
|
|
28
|
-
}
|
|
29
|
-
if (run.name > migrationName) {
|
|
30
|
-
throw new Error(`Not executed migration ${migrationName} is preceding already run migration ${run.name}`);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
exports.checkOrder = checkOrder;
|
|
35
|
-
exports.createMigrationBuilder = createMigrationBuilder;
|
|
36
|
-
exports.escapeValue = escapeValue;
|
|
37
|
-
exports.timePrefixLength = timePrefixLength;
|
|
38
|
-
//# sourceMappingURL=helpers.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.cjs","sources":["../../../../packages/database-migrations/src/helpers.ts"],"sourcesContent":["import { MigrationBuilder } from 'node-pg-migrate'\nimport { Migration, RunMigration } from './Migration'\nimport MigrationBuilderImpl from 'node-pg-migrate/dist/migration-builder.js'\nimport { escapeValue as pgEscape } from 'node-pg-migrate/dist/utils.js'\n\nexport function createMigrationBuilder(): MigrationBuilder & { getSql: () => string; getSqlSteps: () => string[] } {\n\tconst impl = 'default' in MigrationBuilderImpl ? (MigrationBuilderImpl as any).default : MigrationBuilderImpl\n\treturn new impl(\n\t\t{\n\t\t\tquery: null,\n\t\t\tselect: null,\n\t\t} as any,\n\t\t{},\n\t\tfalse,\n\t\t{} as any,\n\t)\n}\n\nexport function escapeValue(value: any): string | number {\n\treturn pgEscape(value)\n}\n\nexport const timePrefixLength = 'YYYY-MM-DD-XXXXXX'.length\n\nexport const checkOrder = (runMigrations: RunMigration[], migrations: Migration<any>[]) => {\n\tconst len = Math.min(runMigrations.length, migrations.length)\n\tfor (let i = 0; i < len; i++) {\n\t\tconst run = runMigrations[i]\n\t\tconst migrationName = migrations[i].name\n\t\tif (migrationName > run.name) {\n\t\t\tthrow new Error(`Previously executed migration ${run} is missing`)\n\t\t}\n\t\tif (run.name > migrationName) {\n\t\t\tthrow new Error(`Not executed migration ${migrationName} is preceding already run migration ${run.name}`)\n\t\t}\n\t}\n}\n"],"names":["pgEscape"],"mappings":";;;;AAKO,SAAS,yBAAmG;AAClH,QAAM,OAAO,aAAa,uBAAwB,qBAA6B,UAAU;AACzF,SAAO,IAAI;AAAA,IACV;AAAA,MACC,OAAO;AAAA,MACP,QAAQ;AAAA,IACT;AAAA,IACA,CAAC;AAAA,IACD;AAAA,IACA,CAAA;AAAA,EACD;AACD;AAEO,SAAS,YAAY,OAA6B;AACxD,SAAOA,SAAAA,YAAS,KAAK;AACtB;AAEO,MAAM,mBAAmB,oBAAoB;AAEvC,MAAA,aAAa,CAAC,eAA+B,eAAiC;AAC1F,QAAM,MAAM,KAAK,IAAI,cAAc,QAAQ,WAAW,MAAM;AAC5D,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AACvB,UAAA,MAAM,cAAc,CAAC;AACrB,UAAA,gBAAgB,WAAW,CAAC,EAAE;AAChC,QAAA,gBAAgB,IAAI,MAAM;AAC7B,YAAM,IAAI,MAAM,iCAAiC,GAAG,aAAa;AAAA,IAAA;AAE9D,QAAA,IAAI,OAAO,eAAe;AAC7B,YAAM,IAAI,MAAM,0BAA0B,aAAa,uCAAuC,IAAI,IAAI,EAAE;AAAA,IAAA;AAAA,EACzG;AAEF;;;;;"}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import MigrationBuilderImpl from "node-pg-migrate/dist/migration-builder.js";
|
|
2
|
-
import { escapeValue as escapeValue$1 } from "node-pg-migrate/dist/utils.js";
|
|
3
|
-
function createMigrationBuilder() {
|
|
4
|
-
const impl = "default" in MigrationBuilderImpl ? MigrationBuilderImpl.default : MigrationBuilderImpl;
|
|
5
|
-
return new impl(
|
|
6
|
-
{
|
|
7
|
-
query: null,
|
|
8
|
-
select: null
|
|
9
|
-
},
|
|
10
|
-
{},
|
|
11
|
-
false,
|
|
12
|
-
{}
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
function escapeValue(value) {
|
|
16
|
-
return escapeValue$1(value);
|
|
17
|
-
}
|
|
18
|
-
const timePrefixLength = "YYYY-MM-DD-XXXXXX".length;
|
|
19
|
-
const checkOrder = (runMigrations, migrations) => {
|
|
20
|
-
const len = Math.min(runMigrations.length, migrations.length);
|
|
21
|
-
for (let i = 0; i < len; i++) {
|
|
22
|
-
const run = runMigrations[i];
|
|
23
|
-
const migrationName = migrations[i].name;
|
|
24
|
-
if (migrationName > run.name) {
|
|
25
|
-
throw new Error(`Previously executed migration ${run} is missing`);
|
|
26
|
-
}
|
|
27
|
-
if (run.name > migrationName) {
|
|
28
|
-
throw new Error(`Not executed migration ${migrationName} is preceding already run migration ${run.name}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
export {
|
|
33
|
-
checkOrder,
|
|
34
|
-
createMigrationBuilder,
|
|
35
|
-
escapeValue,
|
|
36
|
-
timePrefixLength
|
|
37
|
-
};
|
|
38
|
-
//# sourceMappingURL=helpers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sources":["../../../../packages/database-migrations/src/helpers.ts"],"sourcesContent":["import { MigrationBuilder } from 'node-pg-migrate'\nimport { Migration, RunMigration } from './Migration'\nimport MigrationBuilderImpl from 'node-pg-migrate/dist/migration-builder.js'\nimport { escapeValue as pgEscape } from 'node-pg-migrate/dist/utils.js'\n\nexport function createMigrationBuilder(): MigrationBuilder & { getSql: () => string; getSqlSteps: () => string[] } {\n\tconst impl = 'default' in MigrationBuilderImpl ? (MigrationBuilderImpl as any).default : MigrationBuilderImpl\n\treturn new impl(\n\t\t{\n\t\t\tquery: null,\n\t\t\tselect: null,\n\t\t} as any,\n\t\t{},\n\t\tfalse,\n\t\t{} as any,\n\t)\n}\n\nexport function escapeValue(value: any): string | number {\n\treturn pgEscape(value)\n}\n\nexport const timePrefixLength = 'YYYY-MM-DD-XXXXXX'.length\n\nexport const checkOrder = (runMigrations: RunMigration[], migrations: Migration<any>[]) => {\n\tconst len = Math.min(runMigrations.length, migrations.length)\n\tfor (let i = 0; i < len; i++) {\n\t\tconst run = runMigrations[i]\n\t\tconst migrationName = migrations[i].name\n\t\tif (migrationName > run.name) {\n\t\t\tthrow new Error(`Previously executed migration ${run} is missing`)\n\t\t}\n\t\tif (run.name > migrationName) {\n\t\t\tthrow new Error(`Not executed migration ${migrationName} is preceding already run migration ${run.name}`)\n\t\t}\n\t}\n}\n"],"names":["pgEscape"],"mappings":";;AAKO,SAAS,yBAAmG;AAClH,QAAM,OAAO,aAAa,uBAAwB,qBAA6B,UAAU;AACzF,SAAO,IAAI;AAAA,IACV;AAAA,MACC,OAAO;AAAA,MACP,QAAQ;AAAA,IACT;AAAA,IACA,CAAC;AAAA,IACD;AAAA,IACA,CAAA;AAAA,EACD;AACD;AAEO,SAAS,YAAY,OAA6B;AACxD,SAAOA,cAAS,KAAK;AACtB;AAEO,MAAM,mBAAmB,oBAAoB;AAEvC,MAAA,aAAa,CAAC,eAA+B,eAAiC;AAC1F,QAAM,MAAM,KAAK,IAAI,cAAc,QAAQ,WAAW,MAAM;AAC5D,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AACvB,UAAA,MAAM,cAAc,CAAC;AACrB,UAAA,gBAAgB,WAAW,CAAC,EAAE;AAChC,QAAA,gBAAgB,IAAI,MAAM;AAC7B,YAAM,IAAI,MAAM,iCAAiC,GAAG,aAAa;AAAA,IAAA;AAE9D,QAAA,IAAI,OAAO,eAAe;AAC7B,YAAM,IAAI,MAAM,0BAA0B,aAAa,uCAAuC,IAAI,IAAI,EAAE;AAAA,IAAA;AAAA,EACzG;AAEF;"}
|