@prairielearn/migrations 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +2 -0
- package/CHANGELOG.md +19 -0
- package/README.md +145 -0
- package/dist/batched-migrations/batched-migration-job.d.ts +42 -0
- package/dist/batched-migrations/batched-migration-job.js +25 -0
- package/dist/batched-migrations/batched-migration-job.js.map +1 -0
- package/dist/batched-migrations/batched-migration-job.sql +12 -0
- package/dist/batched-migrations/batched-migration-runner.d.ts +29 -0
- package/dist/batched-migrations/batched-migration-runner.js +136 -0
- package/dist/batched-migrations/batched-migration-runner.js.map +1 -0
- package/dist/batched-migrations/batched-migration-runner.sql +93 -0
- package/dist/batched-migrations/batched-migration-runner.test.js +185 -0
- package/dist/batched-migrations/batched-migration-runner.test.js.map +1 -0
- package/dist/batched-migrations/batched-migration.d.ts +79 -0
- package/dist/batched-migrations/batched-migration.js +73 -0
- package/dist/batched-migrations/batched-migration.js.map +1 -0
- package/dist/batched-migrations/batched-migration.sql +95 -0
- package/dist/batched-migrations/batched-migrations-runner.d.ts +63 -0
- package/dist/batched-migrations/batched-migrations-runner.js +272 -0
- package/dist/batched-migrations/batched-migrations-runner.js.map +1 -0
- package/dist/batched-migrations/batched-migrations-runner.sql +35 -0
- package/dist/batched-migrations/batched-migrations-runner.test.js +116 -0
- package/dist/batched-migrations/batched-migrations-runner.test.js.map +1 -0
- package/dist/batched-migrations/fixtures/20230406184103_successful_migration.d.ts +9 -0
- package/dist/batched-migrations/fixtures/20230406184103_successful_migration.js +14 -0
- package/dist/batched-migrations/fixtures/20230406184103_successful_migration.js.map +1 -0
- package/dist/batched-migrations/fixtures/20230407230446_no_rows_migration.d.ts +8 -0
- package/dist/batched-migrations/fixtures/20230407230446_no_rows_migration.js +16 -0
- package/dist/batched-migrations/fixtures/20230407230446_no_rows_migration.js.map +1 -0
- package/dist/batched-migrations/index.d.ts +3 -0
- package/dist/batched-migrations/index.js +18 -0
- package/dist/batched-migrations/index.js.map +1 -0
- package/dist/index.d.ts +3 -12
- package/dist/index.js +15 -192
- package/dist/index.js.map +1 -1
- package/dist/load-migrations.d.ts +8 -0
- package/dist/load-migrations.js +60 -0
- package/dist/load-migrations.js.map +1 -0
- package/dist/load-migrations.test.d.ts +1 -0
- package/dist/{index.test.js → load-migrations.test.js} +12 -65
- package/dist/load-migrations.test.js.map +1 -0
- package/dist/migrations/fixtures/20230407210430_insert_user.d.ts +1 -0
- package/dist/migrations/fixtures/20230407210430_insert_user.js.map +1 -0
- package/dist/migrations/index.d.ts +1 -0
- package/dist/migrations/index.js +6 -0
- package/dist/migrations/index.js.map +1 -0
- package/dist/migrations/migrations.d.ts +6 -0
- package/dist/migrations/migrations.js +158 -0
- package/dist/migrations/migrations.js.map +1 -0
- package/dist/migrations/migrations.test.d.ts +1 -0
- package/dist/migrations/migrations.test.js +78 -0
- package/dist/migrations/migrations.test.js.map +1 -0
- package/package.json +15 -12
- package/schema-migrations/20230303193423_batched_migrations__create.sql +49 -0
- package/src/batched-migrations/batched-migration-job.sql +12 -0
- package/src/batched-migrations/batched-migration-job.ts +34 -0
- package/src/batched-migrations/batched-migration-runner.sql +93 -0
- package/src/batched-migrations/batched-migration-runner.test.ts +208 -0
- package/src/batched-migrations/batched-migration-runner.ts +215 -0
- package/src/batched-migrations/batched-migration.sql +95 -0
- package/src/batched-migrations/batched-migration.ts +129 -0
- package/src/batched-migrations/batched-migrations-runner.sql +35 -0
- package/src/batched-migrations/batched-migrations-runner.test.ts +111 -0
- package/src/batched-migrations/batched-migrations-runner.ts +327 -0
- package/src/batched-migrations/fixtures/20230406184103_successful_migration.ts +13 -0
- package/src/batched-migrations/fixtures/20230406184107_failing_migration.js +16 -0
- package/src/batched-migrations/fixtures/20230407230446_no_rows_migration.ts +15 -0
- package/src/batched-migrations/index.ts +21 -0
- package/src/index.ts +20 -201
- package/src/{index.test.ts → load-migrations.test.ts} +8 -73
- package/src/load-migrations.ts +76 -0
- package/src/migrations/index.ts +1 -0
- package/src/migrations/migrations.test.ts +80 -0
- package/src/migrations/migrations.ts +149 -0
- package/tsconfig.json +1 -1
- package/dist/fixtures/20230407210430_insert_user.js.map +0 -1
- package/dist/index.test.js.map +0 -1
- /package/dist/{fixtures/20230407210430_insert_user.d.ts → batched-migrations/batched-migration-runner.test.d.ts} +0 -0
- /package/dist/{index.test.d.ts → batched-migrations/batched-migrations-runner.test.d.ts} +0 -0
- /package/dist/{fixtures → migrations/fixtures}/20230407210409_create_users.sql +0 -0
- /package/dist/{fixtures → migrations/fixtures}/20230407210430_insert_user.js +0 -0
- /package/dist/{index.sql → migrations/migrations.sql} +0 -0
- /package/src/{fixtures → migrations/fixtures}/20230407210409_create_users.sql +0 -0
- /package/src/{fixtures → migrations/fixtures}/20230407210430_insert_user.ts +0 -0
- /package/src/{index.sql → migrations/migrations.sql} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,199 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
|
-
var _a;
|
|
29
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
31
|
-
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
6
|
+
exports.SCHEMA_MIGRATIONS_PATH = exports.selectRecentJobsWithStatus = exports.selectBatchedMigrationForTimestamp = exports.selectBatchedMigration = exports.selectAllBatchedMigrations = exports.finalizeBatchedMigration = exports.enqueueBatchedMigration = exports.stopBatchedMigrations = exports.startBatchedMigrations = exports.initBatchedMigrations = exports.makeBatchedMigration = exports.init = void 0;
|
|
32
7
|
const path_1 = __importDefault(require("path"));
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// quickly as possible. A long-running migration likely means that
|
|
48
|
-
// Postgres is locking a whole table, which is unacceptable in production.
|
|
49
|
-
autoRenew: true,
|
|
50
|
-
}, async () => {
|
|
51
|
-
logger_1.logger.verbose(`Acquired lock ${lockName}`);
|
|
52
|
-
await initWithLock(migrationDir, project);
|
|
53
|
-
});
|
|
54
|
-
logger_1.logger.verbose(`Released lock ${lockName}`);
|
|
55
|
-
}
|
|
56
|
-
exports.init = init;
|
|
57
|
-
/**
|
|
58
|
-
* Timestamp prefixes will be of the form `YYYYMMDDHHMMSS`, which will have 14 digits.
|
|
59
|
-
* If this code is still around in the year 10000... good luck.
|
|
60
|
-
*/
|
|
61
|
-
const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+\.[a-z]+$/;
|
|
62
|
-
async function readAndValidateMigrationsFromDirectory(dir, extensions) {
|
|
63
|
-
const migrationFiles = (await fs_extra_1.default.readdir(dir)).filter((m) => extensions.some((e) => m.endsWith(e)));
|
|
64
|
-
const migrations = migrationFiles.map((mf) => {
|
|
65
|
-
const match = mf.match(MIGRATION_FILENAME_REGEX);
|
|
66
|
-
if (!match) {
|
|
67
|
-
throw new Error(`Invalid migration filename: ${mf}`);
|
|
68
|
-
}
|
|
69
|
-
const timestamp = match[1] ?? null;
|
|
70
|
-
if (timestamp === null) {
|
|
71
|
-
throw new Error(`Migration ${mf} does not have a timestamp`);
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
filename: mf,
|
|
75
|
-
timestamp,
|
|
76
|
-
};
|
|
77
|
-
});
|
|
78
|
-
// First pass: validate that all migrations have a unique timestamp prefix.
|
|
79
|
-
// This will avoid data loss and conflicts in unexpected scenarios.
|
|
80
|
-
const seenTimestamps = new Set();
|
|
81
|
-
for (const migration of migrations) {
|
|
82
|
-
const { filename, timestamp } = migration;
|
|
83
|
-
if (timestamp !== null) {
|
|
84
|
-
if (seenTimestamps.has(timestamp)) {
|
|
85
|
-
throw new Error(`Duplicate migration timestamp: ${timestamp} (${filename})`);
|
|
86
|
-
}
|
|
87
|
-
seenTimestamps.add(timestamp);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return migrations;
|
|
91
|
-
}
|
|
92
|
-
exports.readAndValidateMigrationsFromDirectory = readAndValidateMigrationsFromDirectory;
|
|
93
|
-
function sortMigrationFiles(migrationFiles) {
|
|
94
|
-
return migrationFiles.sort((a, b) => {
|
|
95
|
-
return a.timestamp.localeCompare(b.timestamp);
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
exports.sortMigrationFiles = sortMigrationFiles;
|
|
99
|
-
function getMigrationsToExecute(migrationFiles, executedMigrations) {
|
|
100
|
-
// If no migrations have ever been run, run them all.
|
|
101
|
-
if (executedMigrations.length === 0) {
|
|
102
|
-
return migrationFiles;
|
|
103
|
-
}
|
|
104
|
-
const executedMigrationTimestamps = new Set(executedMigrations.map((m) => m.timestamp));
|
|
105
|
-
return migrationFiles.filter((m) => !executedMigrationTimestamps.has(m.timestamp));
|
|
106
|
-
}
|
|
107
|
-
exports.getMigrationsToExecute = getMigrationsToExecute;
|
|
108
|
-
async function initWithLock(migrationDir, project) {
|
|
109
|
-
logger_1.logger.verbose('Starting DB schema migration');
|
|
110
|
-
// Create the migrations table if needed
|
|
111
|
-
await sqldb.queryAsync(sql.create_migrations_table, {});
|
|
112
|
-
// Apply necessary changes to the migrations table as needed.
|
|
113
|
-
try {
|
|
114
|
-
await sqldb.queryAsync('SELECT project FROM migrations;', {});
|
|
115
|
-
}
|
|
116
|
-
catch (err) {
|
|
117
|
-
if (err.routine === 'errorMissingColumn') {
|
|
118
|
-
logger_1.logger.info('Altering migrations table');
|
|
119
|
-
await sqldb.queryAsync(sql.add_projects_column, {});
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
throw err;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
try {
|
|
126
|
-
await sqldb.queryAsync('SELECT timestamp FROM migrations;', {});
|
|
127
|
-
}
|
|
128
|
-
catch (err) {
|
|
129
|
-
if (err.routine === 'errorMissingColumn') {
|
|
130
|
-
logger_1.logger.info('Altering migrations table again');
|
|
131
|
-
await sqldb.queryAsync(sql.add_timestamp_column, {});
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
throw err;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
let allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });
|
|
138
|
-
const migrationFiles = await readAndValidateMigrationsFromDirectory(migrationDir, [
|
|
139
|
-
'.sql',
|
|
140
|
-
'.js',
|
|
141
|
-
'.ts',
|
|
142
|
-
'.mjs',
|
|
143
|
-
]);
|
|
144
|
-
// Validation: if we not all previously-executed migrations have timestamps,
|
|
145
|
-
// prompt the user to deploy an earlier version that includes both indexes
|
|
146
|
-
// and timestamps.
|
|
147
|
-
const migrationsMissingTimestamps = allMigrations.rows.filter((m) => !m.timestamp);
|
|
148
|
-
if (migrationsMissingTimestamps.length > 0) {
|
|
149
|
-
throw new Error([
|
|
150
|
-
'The following migrations are missing timestamps:',
|
|
151
|
-
migrationsMissingTimestamps.map((m) => ` ${m.filename}`),
|
|
152
|
-
// This revision was the most recent commit to `master` before the
|
|
153
|
-
// code handling indexes was removed.
|
|
154
|
-
'You must deploy revision 1aa43c7348fa24cf636413d720d06a2fa9e38ef2 first.',
|
|
155
|
-
].join('\n'));
|
|
156
|
-
}
|
|
157
|
-
// Refetch the list of migrations from the database.
|
|
158
|
-
allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });
|
|
159
|
-
// Sort the migration files into execution order.
|
|
160
|
-
const sortedMigrationFiles = sortMigrationFiles(migrationFiles);
|
|
161
|
-
// Figure out which migrations have to be applied.
|
|
162
|
-
const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, allMigrations.rows);
|
|
163
|
-
for (const { filename, timestamp } of migrationsToExecute) {
|
|
164
|
-
if (allMigrations.rows.length === 0) {
|
|
165
|
-
// if we are running all the migrations then log at a lower level
|
|
166
|
-
logger_1.logger.verbose(`Running migration ${filename}`);
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
logger_1.logger.info(`Running migration ${filename}`);
|
|
170
|
-
}
|
|
171
|
-
const migrationPath = path_1.default.join(migrationDir, filename);
|
|
172
|
-
if (filename.endsWith('.sql')) {
|
|
173
|
-
const migrationSql = await fs_extra_1.default.readFile(migrationPath, 'utf8');
|
|
174
|
-
try {
|
|
175
|
-
await sqldb.queryAsync(migrationSql, {});
|
|
176
|
-
}
|
|
177
|
-
catch (err) {
|
|
178
|
-
error.addData(err, { sqlFile: filename });
|
|
179
|
-
throw err;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
const migrationModule = await (_a = migrationPath, Promise.resolve().then(() => __importStar(require(_a))));
|
|
184
|
-
const implementation = migrationModule.default;
|
|
185
|
-
if (typeof implementation !== 'function') {
|
|
186
|
-
throw new Error(`Migration ${filename} does not export a default function`);
|
|
187
|
-
}
|
|
188
|
-
await implementation();
|
|
189
|
-
}
|
|
190
|
-
// Record the migration.
|
|
191
|
-
await sqldb.queryAsync(sql.insert_migration, {
|
|
192
|
-
filename: filename,
|
|
193
|
-
timestamp,
|
|
194
|
-
project,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
exports.initWithLock = initWithLock;
|
|
8
|
+
var migrations_1 = require("./migrations");
|
|
9
|
+
Object.defineProperty(exports, "init", { enumerable: true, get: function () { return migrations_1.init; } });
|
|
10
|
+
var batched_migrations_1 = require("./batched-migrations");
|
|
11
|
+
Object.defineProperty(exports, "makeBatchedMigration", { enumerable: true, get: function () { return batched_migrations_1.makeBatchedMigration; } });
|
|
12
|
+
Object.defineProperty(exports, "initBatchedMigrations", { enumerable: true, get: function () { return batched_migrations_1.initBatchedMigrations; } });
|
|
13
|
+
Object.defineProperty(exports, "startBatchedMigrations", { enumerable: true, get: function () { return batched_migrations_1.startBatchedMigrations; } });
|
|
14
|
+
Object.defineProperty(exports, "stopBatchedMigrations", { enumerable: true, get: function () { return batched_migrations_1.stopBatchedMigrations; } });
|
|
15
|
+
Object.defineProperty(exports, "enqueueBatchedMigration", { enumerable: true, get: function () { return batched_migrations_1.enqueueBatchedMigration; } });
|
|
16
|
+
Object.defineProperty(exports, "finalizeBatchedMigration", { enumerable: true, get: function () { return batched_migrations_1.finalizeBatchedMigration; } });
|
|
17
|
+
Object.defineProperty(exports, "selectAllBatchedMigrations", { enumerable: true, get: function () { return batched_migrations_1.selectAllBatchedMigrations; } });
|
|
18
|
+
Object.defineProperty(exports, "selectBatchedMigration", { enumerable: true, get: function () { return batched_migrations_1.selectBatchedMigration; } });
|
|
19
|
+
Object.defineProperty(exports, "selectBatchedMigrationForTimestamp", { enumerable: true, get: function () { return batched_migrations_1.selectBatchedMigrationForTimestamp; } });
|
|
20
|
+
Object.defineProperty(exports, "selectRecentJobsWithStatus", { enumerable: true, get: function () { return batched_migrations_1.selectRecentJobsWithStatus; } });
|
|
21
|
+
exports.SCHEMA_MIGRATIONS_PATH = path_1.default.resolve(__dirname, '..', 'schema-migrations');
|
|
199
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AAExB,2CAAoC;AAA3B,kGAAA,IAAI,OAAA;AAEb,2DAe8B;AAV5B,0HAAA,oBAAoB,OAAA;AACpB,2HAAA,qBAAqB,OAAA;AACrB,4HAAA,sBAAsB,OAAA;AACtB,2HAAA,qBAAqB,OAAA;AACrB,6HAAA,uBAAuB,OAAA;AACvB,8HAAA,wBAAwB,OAAA;AACxB,gIAAA,0BAA0B,OAAA;AAC1B,4HAAA,sBAAsB,OAAA;AACtB,wIAAA,kCAAkC,OAAA;AAClC,gIAAA,0BAA0B,OAAA;AAGf,QAAA,sBAAsB,GAAG,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,EAAE,mBAAmB,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface MigrationFile {
|
|
2
|
+
directory: string;
|
|
3
|
+
filename: string;
|
|
4
|
+
timestamp: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function readAndValidateMigrationsFromDirectory(dir: string, extensions: string[]): Promise<MigrationFile[]>;
|
|
7
|
+
export declare function readAndValidateMigrationsFromDirectories(directories: string[], extensions: string[]): Promise<MigrationFile[]>;
|
|
8
|
+
export declare function sortMigrationFiles(migrationFiles: MigrationFile[]): MigrationFile[];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.sortMigrationFiles = exports.readAndValidateMigrationsFromDirectories = exports.readAndValidateMigrationsFromDirectory = void 0;
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
/**
|
|
9
|
+
* Timestamp prefixes will be of the form `YYYYMMDDHHMMSS`, which will have 14 digits.
|
|
10
|
+
* If this code is still around in the year 10000... good luck.
|
|
11
|
+
*/
|
|
12
|
+
const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+\.[a-z]+/;
|
|
13
|
+
async function readAndValidateMigrationsFromDirectory(dir, extensions) {
|
|
14
|
+
const migrationFiles = (await fs_extra_1.default.readdir(dir)).filter((m) => extensions.some((e) => m.endsWith(e)));
|
|
15
|
+
const migrations = migrationFiles.map((mf) => {
|
|
16
|
+
const match = mf.match(MIGRATION_FILENAME_REGEX);
|
|
17
|
+
if (!match) {
|
|
18
|
+
throw new Error(`Invalid migration filename: ${mf}`);
|
|
19
|
+
}
|
|
20
|
+
const timestamp = match[1] ?? null;
|
|
21
|
+
if (timestamp === null) {
|
|
22
|
+
throw new Error(`Migration ${mf} does not have a timestamp`);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
directory: dir,
|
|
26
|
+
filename: mf,
|
|
27
|
+
timestamp,
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
// First pass: validate that all migrations have a unique timestamp prefix.
|
|
31
|
+
// This will avoid data loss and conflicts in unexpected scenarios.
|
|
32
|
+
const seenTimestamps = new Set();
|
|
33
|
+
for (const migration of migrations) {
|
|
34
|
+
const { filename, timestamp } = migration;
|
|
35
|
+
if (timestamp !== null) {
|
|
36
|
+
if (seenTimestamps.has(timestamp)) {
|
|
37
|
+
throw new Error(`Duplicate migration timestamp: ${timestamp} (${filename})`);
|
|
38
|
+
}
|
|
39
|
+
seenTimestamps.add(timestamp);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return migrations;
|
|
43
|
+
}
|
|
44
|
+
exports.readAndValidateMigrationsFromDirectory = readAndValidateMigrationsFromDirectory;
|
|
45
|
+
async function readAndValidateMigrationsFromDirectories(directories, extensions) {
|
|
46
|
+
const allMigrations = [];
|
|
47
|
+
for (const directory of directories) {
|
|
48
|
+
const migrations = await readAndValidateMigrationsFromDirectory(directory, extensions);
|
|
49
|
+
allMigrations.push(...migrations);
|
|
50
|
+
}
|
|
51
|
+
return allMigrations;
|
|
52
|
+
}
|
|
53
|
+
exports.readAndValidateMigrationsFromDirectories = readAndValidateMigrationsFromDirectories;
|
|
54
|
+
function sortMigrationFiles(migrationFiles) {
|
|
55
|
+
return migrationFiles.sort((a, b) => {
|
|
56
|
+
return a.timestamp.localeCompare(b.timestamp);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
exports.sortMigrationFiles = sortMigrationFiles;
|
|
60
|
+
//# sourceMappingURL=load-migrations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-migrations.js","sourceRoot":"","sources":["../src/load-migrations.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA0B;AAE1B;;;GAGG;AACH,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAQpD,KAAK,UAAU,sCAAsC,CAC1D,GAAW,EACX,UAAoB;IAEpB,MAAM,cAAc,GAAG,CAAC,MAAM,kBAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAC1D,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAA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,IAAI,CAAC,GAAG,UAAU,CAAC,CAAC;KACnC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAVD,4FAUC;AAED,SAAgB,kBAAkB,CAAC,cAA+B;IAChE,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QAClC,OAAO,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;AACL,CAAC;AAJD,gDAIC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -31,8 +31,7 @@ const chai_as_promised_1 = __importDefault(require("chai-as-promised"));
|
|
|
31
31
|
const path_1 = __importDefault(require("path"));
|
|
32
32
|
const tmp_promise_1 = __importDefault(require("tmp-promise"));
|
|
33
33
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
34
|
-
const
|
|
35
|
-
const postgres_1 = require("@prairielearn/postgres");
|
|
34
|
+
const load_migrations_1 = require("./load-migrations");
|
|
36
35
|
chai_1.default.use(chai_as_promised_1.default);
|
|
37
36
|
async function withMigrationFiles(files, fn) {
|
|
38
37
|
await tmp_promise_1.default.withDir(async function (tmpDir) {
|
|
@@ -42,107 +41,55 @@ async function withMigrationFiles(files, fn) {
|
|
|
42
41
|
await fn(tmpDir.path);
|
|
43
42
|
}, { unsafeCleanup: true });
|
|
44
43
|
}
|
|
45
|
-
describe('migrations', () => {
|
|
44
|
+
describe('load-migrations', () => {
|
|
46
45
|
describe('readAndValidateMigrationsFromDirectory', () => {
|
|
47
46
|
it('handles migrations without a timestamp', async () => {
|
|
48
47
|
await withMigrationFiles(['001_testing.sql'], async (tmpDir) => {
|
|
49
|
-
await chai_1.assert.isRejected((0,
|
|
48
|
+
await chai_1.assert.isRejected((0, load_migrations_1.readAndValidateMigrationsFromDirectory)(tmpDir, ['.sql']), 'Invalid migration filename: 001_testing.sql');
|
|
50
49
|
});
|
|
51
50
|
});
|
|
52
51
|
it('handles duplicate timestamps', async () => {
|
|
53
52
|
await withMigrationFiles(['20220101010101_testing.sql', '20220101010101_testing_again.sql'], async (tmpDir) => {
|
|
54
|
-
await chai_1.assert.isRejected((0,
|
|
53
|
+
await chai_1.assert.isRejected((0, load_migrations_1.readAndValidateMigrationsFromDirectory)(tmpDir, ['.sql']), 'Duplicate migration timestamp');
|
|
55
54
|
});
|
|
56
55
|
});
|
|
57
56
|
});
|
|
58
57
|
describe('sortMigrationFiles', () => {
|
|
59
58
|
it('sorts by timestamp', () => {
|
|
60
|
-
chai_1.assert.deepEqual((0,
|
|
59
|
+
chai_1.assert.deepEqual((0, load_migrations_1.sortMigrationFiles)([
|
|
61
60
|
{
|
|
61
|
+
directory: 'migrations',
|
|
62
62
|
filename: '20220101010103_testing_3.sql',
|
|
63
63
|
timestamp: '20220101010103',
|
|
64
64
|
},
|
|
65
65
|
{
|
|
66
|
+
directory: 'migrations',
|
|
66
67
|
filename: '20220101010101_testing_1.sql',
|
|
67
68
|
timestamp: '20220101010101',
|
|
68
69
|
},
|
|
69
70
|
{
|
|
71
|
+
directory: 'migrations',
|
|
70
72
|
filename: '20220101010102_testing_2.sql',
|
|
71
73
|
timestamp: '20220101010102',
|
|
72
74
|
},
|
|
73
75
|
]), [
|
|
74
76
|
{
|
|
77
|
+
directory: 'migrations',
|
|
75
78
|
filename: '20220101010101_testing_1.sql',
|
|
76
79
|
timestamp: '20220101010101',
|
|
77
80
|
},
|
|
78
81
|
{
|
|
82
|
+
directory: 'migrations',
|
|
79
83
|
filename: '20220101010102_testing_2.sql',
|
|
80
84
|
timestamp: '20220101010102',
|
|
81
85
|
},
|
|
82
86
|
{
|
|
87
|
+
directory: 'migrations',
|
|
83
88
|
filename: '20220101010103_testing_3.sql',
|
|
84
89
|
timestamp: '20220101010103',
|
|
85
90
|
},
|
|
86
91
|
]);
|
|
87
92
|
});
|
|
88
93
|
});
|
|
89
|
-
describe('getMigrationsToExecute', () => {
|
|
90
|
-
it('handles the case of no executed migrations', () => {
|
|
91
|
-
const migrationFiles = [
|
|
92
|
-
{
|
|
93
|
-
filename: '001_testing.sql',
|
|
94
|
-
timestamp: '20220101010101',
|
|
95
|
-
},
|
|
96
|
-
];
|
|
97
|
-
chai_1.assert.deepEqual((0, index_1.getMigrationsToExecute)(migrationFiles, []), migrationFiles);
|
|
98
|
-
});
|
|
99
|
-
it('handles case where subset of migrations have been executed', () => {
|
|
100
|
-
const migrationFiles = [
|
|
101
|
-
{
|
|
102
|
-
filename: '20220101010101_testing_1.sql',
|
|
103
|
-
timestamp: '20220101010101',
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
filename: '20220101010102_testing_2.sql',
|
|
107
|
-
timestamp: '20220101010102',
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
filename: '20220101010103_testing_3.sql',
|
|
111
|
-
timestamp: '20220101010103',
|
|
112
|
-
},
|
|
113
|
-
];
|
|
114
|
-
const executedMigrations = [
|
|
115
|
-
{
|
|
116
|
-
timestamp: '20220101010101',
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
timestamp: '20220101010102',
|
|
120
|
-
},
|
|
121
|
-
];
|
|
122
|
-
chai_1.assert.deepEqual((0, index_1.getMigrationsToExecute)(migrationFiles, executedMigrations), [
|
|
123
|
-
{ timestamp: '20220101010103', filename: '20220101010103_testing_3.sql' },
|
|
124
|
-
]);
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
describe('initWithLock', () => {
|
|
128
|
-
const postgresTestUtils = (0, postgres_1.makePostgresTestUtils)({
|
|
129
|
-
database: 'prairielearn_migrations',
|
|
130
|
-
});
|
|
131
|
-
before(async () => {
|
|
132
|
-
await postgresTestUtils.createDatabase();
|
|
133
|
-
});
|
|
134
|
-
after(async () => {
|
|
135
|
-
await postgresTestUtils.dropDatabase();
|
|
136
|
-
});
|
|
137
|
-
it('runs both SQL and JavaScript migrations', async () => {
|
|
138
|
-
const migrationDir = path_1.default.join(__dirname, 'fixtures');
|
|
139
|
-
await (0, index_1.initWithLock)(migrationDir, 'prairielearn_migrations');
|
|
140
|
-
// If both migrations ran successfully, there should be a single user
|
|
141
|
-
// in the database.
|
|
142
|
-
const users = await (0, postgres_1.queryAsync)('SELECT * FROM users', {});
|
|
143
|
-
chai_1.assert.lengthOf(users.rows, 1);
|
|
144
|
-
chai_1.assert.equal(users.rows[0].name, 'Test User');
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
94
|
});
|
|
148
|
-
//# sourceMappingURL=
|
|
95
|
+
//# sourceMappingURL=load-migrations.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-migrations.test.js","sourceRoot":"","sources":["../src/load-migrations.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAoC;AACpC,wEAA8C;AAC9C,gDAAwB;AACxB,8DAA8B;AAC9B,wDAA0B;AAE1B,uDAA+F;AAE/F,cAAI,CAAC,GAAG,CAAC,0BAAc,CAAC,CAAC;AAEzB,KAAK,UAAU,kBAAkB,CAAC,KAAe,EAAE,EAAqC;IACtF,MAAM,qBAAG,CAAC,OAAO,CACf,KAAK,WAAW,MAAM;QACpB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;YACxB,MAAM,kBAAE,CAAC,SAAS,CAAC,cAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;SACtD;QACD,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC,EACD,EAAE,aAAa,EAAE,IAAI,EAAE,CACxB,CAAC;AACJ,CAAC;AAED,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,QAAQ,CAAC,wCAAwC,EAAE,GAAG,EAAE;QACtD,EAAE,CAAC,wCAAwC,EAAE,KAAK,IAAI,EAAE;YACtD,MAAM,kBAAkB,CAAC,CAAC,iBAAiB,CAAC,EAAE,KAA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,SAAS,EAAE,gBAAgB;iBAC5B;aACF,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"20230407210430_insert_user.js","sourceRoot":"","sources":["../../../src/migrations/fixtures/20230407210430_insert_user.ts"],"names":[],"mappings":";;AAAA,qDAAoD;AAEpD,MAAM,CAAC,OAAO,GAAG,KAAK,UAAU,OAAO;IACrC,MAAM,IAAA,qBAAU,EAAC,+CAA+C,EAAE,EAAE,CAAC,CAAC;AACxE,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { init } from './migrations';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.init = void 0;
|
|
4
|
+
var migrations_1 = require("./migrations");
|
|
5
|
+
Object.defineProperty(exports, "init", { enumerable: true, get: function () { return migrations_1.init; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":";;;AAAA,2CAAoC;AAA3B,kGAAA,IAAI,OAAA"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { MigrationFile } from '../load-migrations';
|
|
2
|
+
export declare function init(directories: string | string[], project: string): Promise<void>;
|
|
3
|
+
export declare function getMigrationsToExecute(migrationFiles: MigrationFile[], executedMigrations: {
|
|
4
|
+
timestamp: string | null;
|
|
5
|
+
}[]): MigrationFile[];
|
|
6
|
+
export declare function initWithLock(directories: string[], project: string): Promise<void>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.initWithLock = exports.getMigrationsToExecute = exports.init = void 0;
|
|
30
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
31
|
+
const path_1 = __importDefault(require("path"));
|
|
32
|
+
const namedLocks = __importStar(require("@prairielearn/named-locks"));
|
|
33
|
+
const logger_1 = require("@prairielearn/logger");
|
|
34
|
+
const sqldb = __importStar(require("@prairielearn/postgres"));
|
|
35
|
+
const error = __importStar(require("@prairielearn/error"));
|
|
36
|
+
const load_migrations_1 = require("../load-migrations");
|
|
37
|
+
const sql = sqldb.loadSqlEquiv(__filename);
|
|
38
|
+
async function init(directories, project) {
|
|
39
|
+
const migrationDirectories = Array.isArray(directories) ? directories : [directories];
|
|
40
|
+
const lockName = 'migrations';
|
|
41
|
+
logger_1.logger.verbose(`Waiting for lock ${lockName}`);
|
|
42
|
+
await namedLocks.doWithLock(lockName, {
|
|
43
|
+
// Migrations *might* take a long time to run, so we'll enable automatic
|
|
44
|
+
// lock renewal so that our lock doesn't get killed by the Postgres
|
|
45
|
+
// idle session timeout.
|
|
46
|
+
//
|
|
47
|
+
// That said, we should generally try to keep migrations executing as
|
|
48
|
+
// quickly as possible. A long-running migration likely means that
|
|
49
|
+
// Postgres is locking a whole table, which is unacceptable in production.
|
|
50
|
+
autoRenew: true,
|
|
51
|
+
}, async () => {
|
|
52
|
+
logger_1.logger.verbose(`Acquired lock ${lockName}`);
|
|
53
|
+
await initWithLock(migrationDirectories, project);
|
|
54
|
+
});
|
|
55
|
+
logger_1.logger.verbose(`Released lock ${lockName}`);
|
|
56
|
+
}
|
|
57
|
+
exports.init = init;
|
|
58
|
+
function getMigrationsToExecute(migrationFiles, executedMigrations) {
|
|
59
|
+
// If no migrations have ever been run, run them all.
|
|
60
|
+
if (executedMigrations.length === 0) {
|
|
61
|
+
return migrationFiles;
|
|
62
|
+
}
|
|
63
|
+
const executedMigrationTimestamps = new Set(executedMigrations.map((m) => m.timestamp));
|
|
64
|
+
return migrationFiles.filter((m) => !executedMigrationTimestamps.has(m.timestamp));
|
|
65
|
+
}
|
|
66
|
+
exports.getMigrationsToExecute = getMigrationsToExecute;
|
|
67
|
+
async function initWithLock(directories, project) {
|
|
68
|
+
logger_1.logger.verbose('Starting DB schema migration');
|
|
69
|
+
// Create the migrations table if needed
|
|
70
|
+
await sqldb.queryAsync(sql.create_migrations_table, {});
|
|
71
|
+
// Apply necessary changes to the migrations table as needed.
|
|
72
|
+
try {
|
|
73
|
+
await sqldb.queryAsync('SELECT project FROM migrations;', {});
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
if (err.routine === 'errorMissingColumn') {
|
|
77
|
+
logger_1.logger.info('Altering migrations table');
|
|
78
|
+
await sqldb.queryAsync(sql.add_projects_column, {});
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
await sqldb.queryAsync('SELECT timestamp FROM migrations;', {});
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
if (err.routine === 'errorMissingColumn') {
|
|
89
|
+
logger_1.logger.info('Altering migrations table again');
|
|
90
|
+
await sqldb.queryAsync(sql.add_timestamp_column, {});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });
|
|
97
|
+
const migrationFiles = await (0, load_migrations_1.readAndValidateMigrationsFromDirectories)(directories, [
|
|
98
|
+
'.sql',
|
|
99
|
+
'.js',
|
|
100
|
+
'.ts',
|
|
101
|
+
'.mjs',
|
|
102
|
+
]);
|
|
103
|
+
// Validation: if we not all previously-executed migrations have timestamps,
|
|
104
|
+
// prompt the user to deploy an earlier version that includes both indexes
|
|
105
|
+
// and timestamps.
|
|
106
|
+
const migrationsMissingTimestamps = allMigrations.rows.filter((m) => !m.timestamp);
|
|
107
|
+
if (migrationsMissingTimestamps.length > 0) {
|
|
108
|
+
throw new Error([
|
|
109
|
+
'The following migrations are missing timestamps:',
|
|
110
|
+
migrationsMissingTimestamps.map((m) => ` ${m.filename}`),
|
|
111
|
+
// This revision was the most recent commit to `master` before the
|
|
112
|
+
// code handling indexes was removed.
|
|
113
|
+
'You must deploy revision 1aa43c7348fa24cf636413d720d06a2fa9e38ef2 first.',
|
|
114
|
+
].join('\n'));
|
|
115
|
+
}
|
|
116
|
+
// Refetch the list of migrations from the database.
|
|
117
|
+
allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });
|
|
118
|
+
// Sort the migration files into execution order.
|
|
119
|
+
const sortedMigrationFiles = (0, load_migrations_1.sortMigrationFiles)(migrationFiles);
|
|
120
|
+
// Figure out which migrations have to be applied.
|
|
121
|
+
const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, allMigrations.rows);
|
|
122
|
+
for (const { directory, filename, timestamp } of migrationsToExecute) {
|
|
123
|
+
if (allMigrations.rows.length === 0) {
|
|
124
|
+
// if we are running all the migrations then log at a lower level
|
|
125
|
+
logger_1.logger.verbose(`Running migration ${filename}`);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
logger_1.logger.info(`Running migration ${filename}`);
|
|
129
|
+
}
|
|
130
|
+
const migrationPath = path_1.default.join(directory, filename);
|
|
131
|
+
if (filename.endsWith('.sql')) {
|
|
132
|
+
const migrationSql = await fs_extra_1.default.readFile(migrationPath, 'utf8');
|
|
133
|
+
try {
|
|
134
|
+
await sqldb.queryAsync(migrationSql, {});
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
error.addData(err, { sqlFile: filename });
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
const migrationModule = await Promise.resolve(`${migrationPath}`).then(s => __importStar(require(s)));
|
|
143
|
+
const implementation = migrationModule.default;
|
|
144
|
+
if (typeof implementation !== 'function') {
|
|
145
|
+
throw new Error(`Migration ${filename} does not export a default function`);
|
|
146
|
+
}
|
|
147
|
+
await implementation();
|
|
148
|
+
}
|
|
149
|
+
// Record the migration.
|
|
150
|
+
await sqldb.queryAsync(sql.insert_migration, {
|
|
151
|
+
filename: filename,
|
|
152
|
+
timestamp,
|
|
153
|
+
project,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
exports.initWithLock = initWithLock;
|
|
158
|
+
//# sourceMappingURL=migrations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.js","sourceR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