@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.
Files changed (80) hide show
  1. package/package.json +11 -6
  2. package/src/Migration.ts +0 -1
  3. package/src/SnapshotMigrationResolver.ts +0 -1
  4. package/src/runner.ts +6 -13
  5. package/tests/cases/unit/groupMigrationResolver.test.ts +0 -1
  6. package/tests/cases/unit/snapshotMigrationResolver.test.ts +14 -10
  7. package/tsconfig.settings.json +0 -1
  8. package/dist/development/index.cjs +0 -17
  9. package/dist/development/index.cjs.map +0 -1
  10. package/dist/development/index.js +0 -17
  11. package/dist/development/index.js.map +0 -1
  12. package/dist/development/src/GroupMigrationsResolver.cjs +0 -38
  13. package/dist/development/src/GroupMigrationsResolver.cjs.map +0 -1
  14. package/dist/development/src/GroupMigrationsResolver.js +0 -38
  15. package/dist/development/src/GroupMigrationsResolver.js.map +0 -1
  16. package/dist/development/src/Migration.cjs +0 -18
  17. package/dist/development/src/Migration.cjs.map +0 -1
  18. package/dist/development/src/Migration.js +0 -18
  19. package/dist/development/src/Migration.js.map +0 -1
  20. package/dist/development/src/MigrationsRunner.cjs +0 -21
  21. package/dist/development/src/MigrationsRunner.cjs.map +0 -1
  22. package/dist/development/src/MigrationsRunner.js +0 -20
  23. package/dist/development/src/MigrationsRunner.js.map +0 -1
  24. package/dist/development/src/SnapshotMigrationResolver.cjs +0 -49
  25. package/dist/development/src/SnapshotMigrationResolver.cjs.map +0 -1
  26. package/dist/development/src/SnapshotMigrationResolver.js +0 -49
  27. package/dist/development/src/SnapshotMigrationResolver.js.map +0 -1
  28. package/dist/development/src/helpers.cjs +0 -38
  29. package/dist/development/src/helpers.cjs.map +0 -1
  30. package/dist/development/src/helpers.js +0 -38
  31. package/dist/development/src/helpers.js.map +0 -1
  32. package/dist/development/src/runner.cjs +0 -94
  33. package/dist/development/src/runner.cjs.map +0 -1
  34. package/dist/development/src/runner.js +0 -95
  35. package/dist/development/src/runner.js.map +0 -1
  36. package/dist/production/index.cjs +0 -17
  37. package/dist/production/index.cjs.map +0 -1
  38. package/dist/production/index.js +0 -17
  39. package/dist/production/index.js.map +0 -1
  40. package/dist/production/src/GroupMigrationsResolver.cjs +0 -38
  41. package/dist/production/src/GroupMigrationsResolver.cjs.map +0 -1
  42. package/dist/production/src/GroupMigrationsResolver.js +0 -38
  43. package/dist/production/src/GroupMigrationsResolver.js.map +0 -1
  44. package/dist/production/src/Migration.cjs +0 -18
  45. package/dist/production/src/Migration.cjs.map +0 -1
  46. package/dist/production/src/Migration.js +0 -18
  47. package/dist/production/src/Migration.js.map +0 -1
  48. package/dist/production/src/MigrationsRunner.cjs +0 -21
  49. package/dist/production/src/MigrationsRunner.cjs.map +0 -1
  50. package/dist/production/src/MigrationsRunner.js +0 -20
  51. package/dist/production/src/MigrationsRunner.js.map +0 -1
  52. package/dist/production/src/SnapshotMigrationResolver.cjs +0 -49
  53. package/dist/production/src/SnapshotMigrationResolver.cjs.map +0 -1
  54. package/dist/production/src/SnapshotMigrationResolver.js +0 -49
  55. package/dist/production/src/SnapshotMigrationResolver.js.map +0 -1
  56. package/dist/production/src/helpers.cjs +0 -38
  57. package/dist/production/src/helpers.cjs.map +0 -1
  58. package/dist/production/src/helpers.js +0 -38
  59. package/dist/production/src/helpers.js.map +0 -1
  60. package/dist/production/src/runner.cjs +0 -94
  61. package/dist/production/src/runner.cjs.map +0 -1
  62. package/dist/production/src/runner.js +0 -95
  63. package/dist/production/src/runner.js.map +0 -1
  64. package/dist/types/GroupMigrationsResolver.d.ts +0 -9
  65. package/dist/types/GroupMigrationsResolver.d.ts.map +0 -1
  66. package/dist/types/Migration.d.ts +0 -22
  67. package/dist/types/Migration.d.ts.map +0 -1
  68. package/dist/types/MigrationsResolver.d.ts +0 -8
  69. package/dist/types/MigrationsResolver.d.ts.map +0 -1
  70. package/dist/types/MigrationsRunner.d.ts +0 -12
  71. package/dist/types/MigrationsRunner.d.ts.map +0 -1
  72. package/dist/types/SnapshotMigrationResolver.d.ts +0 -14
  73. package/dist/types/SnapshotMigrationResolver.d.ts.map +0 -1
  74. package/dist/types/helpers.d.ts +0 -10
  75. package/dist/types/helpers.d.ts.map +0 -1
  76. package/dist/types/index.d.ts +0 -9
  77. package/dist/types/index.d.ts.map +0 -1
  78. package/dist/types/runner.d.ts +0 -13
  79. package/dist/types/runner.d.ts.map +0 -1
  80. package/dist/types/tsconfig.tsbuildinfo +0 -1
@@ -1,94 +0,0 @@
1
- "use strict";
2
- const helpers = require("./helpers.cjs");
3
- const Database = require("@contember/database");
4
- const PG_MIGRATE_LOCK_ID = 7241865325823964;
5
- const getMigrationsTableName = (options) => {
6
- return `${Database.wrapIdentifier(options.schema)}.${Database.wrapIdentifier(options.migrationsTable)}`;
7
- };
8
- const ensureMigrationsTable = async (db, options) => {
9
- try {
10
- const fullTableName = getMigrationsTableName(options);
11
- await db.query(
12
- `
13
- CREATE TABLE IF NOT EXISTS ${fullTableName} (
14
- id INT PRIMARY KEY,
15
- name varchar(255) NOT NULL,
16
- run_on timestamp NOT NULL
17
- )`
18
- );
19
- await db.query(`ALTER TABLE ${fullTableName} ADD COLUMN IF NOT EXISTS "group" TEXT DEFAULT NULL`);
20
- await db.query(`ALTER TABLE ${fullTableName} ALTER id DROP DEFAULT`);
21
- const seqName = (await db.query(`
22
- SELECT pg_get_serial_sequence('${fullTableName}', 'id') as seq_name
23
- `)).rows[0].seq_name;
24
- if (seqName) {
25
- await db.query(`DROP SEQUENCE ${seqName}`);
26
- }
27
- } catch (err) {
28
- if (!(err instanceof Error)) {
29
- throw err;
30
- }
31
- throw new Error(`Unable to ensure migrations table: ${err.stack}`);
32
- }
33
- };
34
- const getRunMigrations = async (db, options) => {
35
- const fullTableName = getMigrationsTableName(options);
36
- return (await db.query(`SELECT name, "group" FROM ${fullTableName} ORDER BY run_on, id`)).rows;
37
- };
38
- const getMigrationsToRun = (options, runNames, migrations) => {
39
- return migrations.filter(({ name }) => runNames.indexOf(name) < 0);
40
- };
41
- const runner = async (migrationsResolver, connection, options) => {
42
- const logger = options.log;
43
- return await connection.scope(
44
- (db) => Database.withDatabaseAdvisoryLock(db, PG_MIGRATE_LOCK_ID, async () => {
45
- await db.query(`CREATE SCHEMA IF NOT EXISTS ${Database.wrapIdentifier(options.schema)}`);
46
- await db.query(`SET search_path TO ${Database.wrapIdentifier(options.schema)}`);
47
- await ensureMigrationsTable(db, options);
48
- const runMigrations = await getRunMigrations(db, options);
49
- const migrations = await migrationsResolver.resolveMigrations({
50
- runMigrations
51
- });
52
- const runNames = runMigrations.map((it) => it.name);
53
- const toRun = getMigrationsToRun(options, runNames, migrations);
54
- if (!toRun.length) {
55
- return [];
56
- }
57
- logger(`Migrating ${toRun.length} file(s):`);
58
- return await db.transaction(async (trx) => {
59
- try {
60
- for (const migration of toRun) {
61
- try {
62
- logger(` Executing migration ${migration.name}...`);
63
- const migrationsBuilder = helpers.createMigrationBuilder();
64
- await migration.migration(migrationsBuilder, { ...options.migrationArgs, connection: trx });
65
- const steps = migrationsBuilder.getSqlSteps();
66
- for (const sql of steps) {
67
- await trx.query(sql);
68
- }
69
- const maxId = await trx.query(`SELECT MAX(id) as id FROM ${getMigrationsTableName(options)}`);
70
- const nextId = (maxId.rows[0]?.id ?? 0) + 1;
71
- await trx.query(
72
- `INSERT INTO ${getMigrationsTableName(options)} (id, name, run_on, "group") VALUES (?, ?, NOW(), ?);`,
73
- [nextId, migration.name, migration.group]
74
- );
75
- logger(` Done`);
76
- } catch (e) {
77
- logger(` FAILED`);
78
- throw e;
79
- }
80
- }
81
- } catch (err) {
82
- logger("Rolling back attempted migration ...");
83
- await trx.rollback();
84
- throw err;
85
- }
86
- return toRun.map((m) => ({
87
- name: m.name
88
- }));
89
- });
90
- })
91
- );
92
- };
93
- module.exports = runner;
94
- //# sourceMappingURL=runner.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runner.cjs","sources":["../../../../packages/database-migrations/src/runner.ts"],"sourcesContent":["/*\nThis file is based on migration runner from node-pg-migrate. (https://github.com/salsita/node-pg-migrate)\n\nThe MIT License (MIT)\n\nCopyright (c) 2016-2020 Salsita Software &lt;jando@salsitasoft.com&gt;\n\nCopyright (c) 2014-2016 Theo Ephraim\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n */\n\nimport {\n\tcreateMigrationBuilder,\n} from './helpers'\nimport { Connection, withDatabaseAdvisoryLock, wrapIdentifier } from '@contember/database'\nimport { Migration, RunMigration } from './Migration'\nimport { MigrationsResolver } from './MigrationsResolver'\n\nexport type RunnerOption<Args = unknown> = {\n\tmigrationsTable: string\n\tschema: string\n\tlog: (msg: string) => void\n\tmigrationArgs: Args\n}\n\n\n// Random but well-known identifier shared by all instances of node-pg-migrate\nconst PG_MIGRATE_LOCK_ID = 7241865325823964\n\nconst getMigrationsTableName = (options: RunnerOption): string => {\n\treturn `${wrapIdentifier(options.schema)}.${wrapIdentifier(options.migrationsTable)}`\n}\nconst ensureMigrationsTable = async (db: Connection.ConnectionLike, options: RunnerOption): Promise<void> => {\n\ttry {\n\t\tconst fullTableName = getMigrationsTableName(options)\n\t\tawait db.query(`\n\t\t\tCREATE TABLE IF NOT EXISTS ${fullTableName} (\n\t\t\t\tid INT PRIMARY KEY,\n\t\t\t\tname varchar(255) NOT NULL,\n\t\t\t\trun_on timestamp NOT NULL\n\t\t\t)`,\n\t\t)\n\t\tawait db.query(`ALTER TABLE ${fullTableName} ADD COLUMN IF NOT EXISTS \"group\" TEXT DEFAULT NULL`)\n\n\t\tawait db.query(`ALTER TABLE ${fullTableName} ALTER id DROP DEFAULT`)\n\t\tconst seqName = (await db.query<{seq_name: string}>(`\n\t\t\tSELECT pg_get_serial_sequence('${fullTableName}', 'id') as seq_name\n\t\t`)).rows[0].seq_name\n\t\tif (seqName) {\n\t\t\tawait db.query(`DROP SEQUENCE ${seqName}`)\n\t\t}\n\t} catch (err) {\n\t\tif (!(err instanceof Error)) {\n\t\t\tthrow err\n\t\t}\n\t\tthrow new Error(`Unable to ensure migrations table: ${err.stack}`)\n\t}\n}\n\nconst getRunMigrations = async (db: Connection.ConnectionLike, options: RunnerOption<unknown>) => {\n\tconst fullTableName = getMigrationsTableName(options)\n\treturn (await db.query<{name: string; group: string | null}>(`SELECT name, \"group\" FROM ${fullTableName} ORDER BY run_on, id`)).rows\n}\n\nconst getMigrationsToRun = <Args>(options: RunnerOption, runNames: string[], migrations: Migration<Args>[]): Migration<Args>[] => {\n\treturn migrations.filter(({ name }) => runNames.indexOf(name) < 0)\n}\n\nexport default async <Args>(\n\tmigrationsResolver: MigrationsResolver<Args>,\n\tconnection: Connection.ConnectionLike,\n\toptions: RunnerOption<Args>,\n): Promise<{ name: string }[]> => {\n\tconst logger = options.log\n\treturn await connection.scope(db =>\n\t\twithDatabaseAdvisoryLock(db, PG_MIGRATE_LOCK_ID, async () => {\n\t\t\tawait db.query(`CREATE SCHEMA IF NOT EXISTS ${wrapIdentifier(options.schema)}`)\n\t\t\tawait db.query(`SET search_path TO ${wrapIdentifier(options.schema)}`)\n\t\t\tawait ensureMigrationsTable(db, options)\n\n\n\t\t\tconst runMigrations = await getRunMigrations(db, options)\n\t\t\tconst migrations = await migrationsResolver.resolveMigrations({\n\t\t\t\trunMigrations,\n\t\t\t})\n\n\t\t\tconst runNames = runMigrations.map(it => it.name)\n\t\t\tconst toRun: Migration<Args>[] = getMigrationsToRun(options, runNames, migrations)\n\n\t\t\tif (!toRun.length) {\n\t\t\t\treturn []\n\t\t\t}\n\t\t\tlogger(`Migrating ${toRun.length} file(s):`)\n\t\t\treturn await db.transaction(async trx => {\n\t\t\t\ttry {\n\t\t\t\t\tfor (const migration of toRun) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tlogger(` Executing migration ${migration.name}...`)\n\t\t\t\t\t\t\tconst migrationsBuilder = createMigrationBuilder()\n\t\t\t\t\t\t\tawait migration.migration(migrationsBuilder, { ...options.migrationArgs, connection: trx })\n\t\t\t\t\t\t\tconst steps = migrationsBuilder.getSqlSteps()\n\n\t\t\t\t\t\t\tfor (const sql of steps) {\n\t\t\t\t\t\t\t\tawait trx.query(sql)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst maxId = await trx.query<{id: number}>(`SELECT MAX(id) as id FROM ${getMigrationsTableName(options)}`)\n\t\t\t\t\t\t\tconst nextId = (maxId.rows[0]?.id ?? 0) + 1\n\t\t\t\t\t\t\tawait trx.query(\n\t\t\t\t\t\t\t\t`INSERT INTO ${getMigrationsTableName(options)} (id, name, run_on, \"group\") VALUES (?, ?, NOW(), ?);`,\n\t\t\t\t\t\t\t\t[nextId, migration.name, migration.group],\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tlogger(` Done`)\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\tlogger(` FAILED`)\n\t\t\t\t\t\t\tthrow e\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} catch (err) {\n\t\t\t\t\tlogger('Rolling back attempted migration ...')\n\t\t\t\t\tawait trx.rollback()\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qBAAO,UAAU;AACX,oBAAA;AAAA,YAAA;AAAA,UACP;AAAA,iBAEO,KAAK;AACb,iBAAO,sCAAsC;AAC7C,gBAAM,IAAI,SAAS;AACb,gBAAA;AAAA,QAAA;AAEA,eAAA,MAAM,IAAI,CAAM,OAAA;AAAA,UACtB,MAAM,EAAE;AAAA,QAAA,EACP;AAAA,MAAA,CAEF;AAAA,IACD,CAAA;AAAA,EACF;AAED;;"}
@@ -1,95 +0,0 @@
1
- import { createMigrationBuilder } from "./helpers.js";
2
- import { withDatabaseAdvisoryLock, wrapIdentifier } from "@contember/database";
3
- const PG_MIGRATE_LOCK_ID = 7241865325823964;
4
- const getMigrationsTableName = (options) => {
5
- return `${wrapIdentifier(options.schema)}.${wrapIdentifier(options.migrationsTable)}`;
6
- };
7
- const ensureMigrationsTable = async (db, options) => {
8
- try {
9
- const fullTableName = getMigrationsTableName(options);
10
- await db.query(
11
- `
12
- CREATE TABLE IF NOT EXISTS ${fullTableName} (
13
- id INT PRIMARY KEY,
14
- name varchar(255) NOT NULL,
15
- run_on timestamp NOT NULL
16
- )`
17
- );
18
- await db.query(`ALTER TABLE ${fullTableName} ADD COLUMN IF NOT EXISTS "group" TEXT DEFAULT NULL`);
19
- await db.query(`ALTER TABLE ${fullTableName} ALTER id DROP DEFAULT`);
20
- const seqName = (await db.query(`
21
- SELECT pg_get_serial_sequence('${fullTableName}', 'id') as seq_name
22
- `)).rows[0].seq_name;
23
- if (seqName) {
24
- await db.query(`DROP SEQUENCE ${seqName}`);
25
- }
26
- } catch (err) {
27
- if (!(err instanceof Error)) {
28
- throw err;
29
- }
30
- throw new Error(`Unable to ensure migrations table: ${err.stack}`);
31
- }
32
- };
33
- const getRunMigrations = async (db, options) => {
34
- const fullTableName = getMigrationsTableName(options);
35
- return (await db.query(`SELECT name, "group" FROM ${fullTableName} ORDER BY run_on, id`)).rows;
36
- };
37
- const getMigrationsToRun = (options, runNames, migrations) => {
38
- return migrations.filter(({ name }) => runNames.indexOf(name) < 0);
39
- };
40
- const runner = async (migrationsResolver, connection, options) => {
41
- const logger = options.log;
42
- return await connection.scope(
43
- (db) => withDatabaseAdvisoryLock(db, PG_MIGRATE_LOCK_ID, async () => {
44
- await db.query(`CREATE SCHEMA IF NOT EXISTS ${wrapIdentifier(options.schema)}`);
45
- await db.query(`SET search_path TO ${wrapIdentifier(options.schema)}`);
46
- await ensureMigrationsTable(db, options);
47
- const runMigrations = await getRunMigrations(db, options);
48
- const migrations = await migrationsResolver.resolveMigrations({
49
- runMigrations
50
- });
51
- const runNames = runMigrations.map((it) => it.name);
52
- const toRun = getMigrationsToRun(options, runNames, migrations);
53
- if (!toRun.length) {
54
- return [];
55
- }
56
- logger(`Migrating ${toRun.length} file(s):`);
57
- return await db.transaction(async (trx) => {
58
- try {
59
- for (const migration of toRun) {
60
- try {
61
- logger(` Executing migration ${migration.name}...`);
62
- const migrationsBuilder = createMigrationBuilder();
63
- await migration.migration(migrationsBuilder, { ...options.migrationArgs, connection: trx });
64
- const steps = migrationsBuilder.getSqlSteps();
65
- for (const sql of steps) {
66
- await trx.query(sql);
67
- }
68
- const maxId = await trx.query(`SELECT MAX(id) as id FROM ${getMigrationsTableName(options)}`);
69
- const nextId = (maxId.rows[0]?.id ?? 0) + 1;
70
- await trx.query(
71
- `INSERT INTO ${getMigrationsTableName(options)} (id, name, run_on, "group") VALUES (?, ?, NOW(), ?);`,
72
- [nextId, migration.name, migration.group]
73
- );
74
- logger(` Done`);
75
- } catch (e) {
76
- logger(` FAILED`);
77
- throw e;
78
- }
79
- }
80
- } catch (err) {
81
- logger("Rolling back attempted migration ...");
82
- await trx.rollback();
83
- throw err;
84
- }
85
- return toRun.map((m) => ({
86
- name: m.name
87
- }));
88
- });
89
- })
90
- );
91
- };
92
- export {
93
- runner as default
94
- };
95
- //# sourceMappingURL=runner.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runner.js","sources":["../../../../packages/database-migrations/src/runner.ts"],"sourcesContent":["/*\nThis file is based on migration runner from node-pg-migrate. (https://github.com/salsita/node-pg-migrate)\n\nThe MIT License (MIT)\n\nCopyright (c) 2016-2020 Salsita Software &lt;jando@salsitasoft.com&gt;\n\nCopyright (c) 2014-2016 Theo Ephraim\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n */\n\nimport {\n\tcreateMigrationBuilder,\n} from './helpers'\nimport { Connection, withDatabaseAdvisoryLock, wrapIdentifier } from '@contember/database'\nimport { Migration, RunMigration } from './Migration'\nimport { MigrationsResolver } from './MigrationsResolver'\n\nexport type RunnerOption<Args = unknown> = {\n\tmigrationsTable: string\n\tschema: string\n\tlog: (msg: string) => void\n\tmigrationArgs: Args\n}\n\n\n// Random but well-known identifier shared by all instances of node-pg-migrate\nconst PG_MIGRATE_LOCK_ID = 7241865325823964\n\nconst getMigrationsTableName = (options: RunnerOption): string => {\n\treturn `${wrapIdentifier(options.schema)}.${wrapIdentifier(options.migrationsTable)}`\n}\nconst ensureMigrationsTable = async (db: Connection.ConnectionLike, options: RunnerOption): Promise<void> => {\n\ttry {\n\t\tconst fullTableName = getMigrationsTableName(options)\n\t\tawait db.query(`\n\t\t\tCREATE TABLE IF NOT EXISTS ${fullTableName} (\n\t\t\t\tid INT PRIMARY KEY,\n\t\t\t\tname varchar(255) NOT NULL,\n\t\t\t\trun_on timestamp NOT NULL\n\t\t\t)`,\n\t\t)\n\t\tawait db.query(`ALTER TABLE ${fullTableName} ADD COLUMN IF NOT EXISTS \"group\" TEXT DEFAULT NULL`)\n\n\t\tawait db.query(`ALTER TABLE ${fullTableName} ALTER id DROP DEFAULT`)\n\t\tconst seqName = (await db.query<{seq_name: string}>(`\n\t\t\tSELECT pg_get_serial_sequence('${fullTableName}', 'id') as seq_name\n\t\t`)).rows[0].seq_name\n\t\tif (seqName) {\n\t\t\tawait db.query(`DROP SEQUENCE ${seqName}`)\n\t\t}\n\t} catch (err) {\n\t\tif (!(err instanceof Error)) {\n\t\t\tthrow err\n\t\t}\n\t\tthrow new Error(`Unable to ensure migrations table: ${err.stack}`)\n\t}\n}\n\nconst getRunMigrations = async (db: Connection.ConnectionLike, options: RunnerOption<unknown>) => {\n\tconst fullTableName = getMigrationsTableName(options)\n\treturn (await db.query<{name: string; group: string | null}>(`SELECT name, \"group\" FROM ${fullTableName} ORDER BY run_on, id`)).rows\n}\n\nconst getMigrationsToRun = <Args>(options: RunnerOption, runNames: string[], migrations: Migration<Args>[]): Migration<Args>[] => {\n\treturn migrations.filter(({ name }) => runNames.indexOf(name) < 0)\n}\n\nexport default async <Args>(\n\tmigrationsResolver: MigrationsResolver<Args>,\n\tconnection: Connection.ConnectionLike,\n\toptions: RunnerOption<Args>,\n): Promise<{ name: string }[]> => {\n\tconst logger = options.log\n\treturn await connection.scope(db =>\n\t\twithDatabaseAdvisoryLock(db, PG_MIGRATE_LOCK_ID, async () => {\n\t\t\tawait db.query(`CREATE SCHEMA IF NOT EXISTS ${wrapIdentifier(options.schema)}`)\n\t\t\tawait db.query(`SET search_path TO ${wrapIdentifier(options.schema)}`)\n\t\t\tawait ensureMigrationsTable(db, options)\n\n\n\t\t\tconst runMigrations = await getRunMigrations(db, options)\n\t\t\tconst migrations = await migrationsResolver.resolveMigrations({\n\t\t\t\trunMigrations,\n\t\t\t})\n\n\t\t\tconst runNames = runMigrations.map(it => it.name)\n\t\t\tconst toRun: Migration<Args>[] = getMigrationsToRun(options, runNames, migrations)\n\n\t\t\tif (!toRun.length) {\n\t\t\t\treturn []\n\t\t\t}\n\t\t\tlogger(`Migrating ${toRun.length} file(s):`)\n\t\t\treturn await db.transaction(async trx => {\n\t\t\t\ttry {\n\t\t\t\t\tfor (const migration of toRun) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tlogger(` Executing migration ${migration.name}...`)\n\t\t\t\t\t\t\tconst migrationsBuilder = createMigrationBuilder()\n\t\t\t\t\t\t\tawait migration.migration(migrationsBuilder, { ...options.migrationArgs, connection: trx })\n\t\t\t\t\t\t\tconst steps = migrationsBuilder.getSqlSteps()\n\n\t\t\t\t\t\t\tfor (const sql of steps) {\n\t\t\t\t\t\t\t\tawait trx.query(sql)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst maxId = await trx.query<{id: number}>(`SELECT MAX(id) as id FROM ${getMigrationsTableName(options)}`)\n\t\t\t\t\t\t\tconst nextId = (maxId.rows[0]?.id ?? 0) + 1\n\t\t\t\t\t\t\tawait trx.query(\n\t\t\t\t\t\t\t\t`INSERT INTO ${getMigrationsTableName(options)} (id, name, run_on, \"group\") VALUES (?, ?, NOW(), ?);`,\n\t\t\t\t\t\t\t\t[nextId, migration.name, migration.group],\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tlogger(` Done`)\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\tlogger(` FAILED`)\n\t\t\t\t\t\t\tthrow e\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} catch (err) {\n\t\t\t\t\tlogger('Rolling back attempted migration ...')\n\t\t\t\t\tawait trx.rollback()\n\t\t\t\t\tthrow err\n\t\t\t\t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AM,IAAI,SAAS;AACb,gBAAA;AAAA,QAAA;AAEA,eAAA,MAAM,IAAI,CAAM,OAAA;AAAA,UACtB,MAAM,EAAE;AAAA,QAAA,EACP;AAAA,MAAA,CAEF;AAAA,IACD,CAAA;AAAA,EACF;AAED;"}
@@ -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