rake-db 2.10.72 → 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +21 -8
- package/dist/index.js +134 -106
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -107
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1394,7 +1394,7 @@ var __spreadValues$3 = (a, b) => {
|
|
|
1394
1394
|
return a;
|
|
1395
1395
|
};
|
|
1396
1396
|
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
1397
|
-
const createMigrationInterface = (tx, up, config) => {
|
|
1397
|
+
const createMigrationInterface = (tx, up, config, asts) => {
|
|
1398
1398
|
const adapter = new TransactionAdapter(tx, tx.client, tx.types);
|
|
1399
1399
|
const { query, arrays } = adapter;
|
|
1400
1400
|
const log = logParamToLogObject(config.logger || console, config.log);
|
|
@@ -1413,7 +1413,7 @@ const createMigrationInterface = (tx, up, config) => {
|
|
|
1413
1413
|
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
1414
1414
|
db[key] = proto[key];
|
|
1415
1415
|
}
|
|
1416
|
-
db.migratedAsts =
|
|
1416
|
+
db.migratedAsts = asts;
|
|
1417
1417
|
return Object.assign(db, {
|
|
1418
1418
|
adapter,
|
|
1419
1419
|
log,
|
|
@@ -2198,8 +2198,7 @@ const removeMigratedVersion = async (db, version, config) => {
|
|
|
2198
2198
|
const getMigratedVersionsMap = async (db, config) => {
|
|
2199
2199
|
try {
|
|
2200
2200
|
const result = await db.arrays(
|
|
2201
|
-
`SELECT *
|
|
2202
|
-
FROM ${quoteWithSchema({ name: config.migrationsTable })}`
|
|
2201
|
+
`SELECT * FROM ${quoteWithSchema({ name: config.migrationsTable })}`
|
|
2203
2202
|
);
|
|
2204
2203
|
return Object.fromEntries(result.rows.map((row) => [row[0], true]));
|
|
2205
2204
|
} catch (err) {
|
|
@@ -2227,128 +2226,156 @@ var __spreadValues$2 = (a, b) => {
|
|
|
2227
2226
|
}
|
|
2228
2227
|
return a;
|
|
2229
2228
|
};
|
|
2229
|
+
const RAKE_DB_LOCK_KEY = "8582141715823621641";
|
|
2230
|
+
function makeMigrateFn(defaultCount, up, fn) {
|
|
2231
|
+
return async (options, config, args = []) => {
|
|
2232
|
+
const files = await getMigrations(config, up);
|
|
2233
|
+
const count = getCount(args);
|
|
2234
|
+
const conf = prepareConfig(config, args, count);
|
|
2235
|
+
const asts = [];
|
|
2236
|
+
const appCodeUpdaterCache = {};
|
|
2237
|
+
const { appCodeUpdater } = conf;
|
|
2238
|
+
const arrOptions = toArray(options);
|
|
2239
|
+
let localAsts = asts;
|
|
2240
|
+
for (const opts of arrOptions) {
|
|
2241
|
+
const adapter = new Adapter(opts);
|
|
2242
|
+
try {
|
|
2243
|
+
await adapter.transaction(begin, async (trx) => {
|
|
2244
|
+
await trx.query(
|
|
2245
|
+
`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`
|
|
2246
|
+
);
|
|
2247
|
+
await fn(
|
|
2248
|
+
trx,
|
|
2249
|
+
conf,
|
|
2250
|
+
files,
|
|
2251
|
+
count != null ? count : defaultCount,
|
|
2252
|
+
localAsts
|
|
2253
|
+
);
|
|
2254
|
+
});
|
|
2255
|
+
} finally {
|
|
2256
|
+
await adapter.close();
|
|
2257
|
+
}
|
|
2258
|
+
localAsts = [];
|
|
2259
|
+
}
|
|
2260
|
+
await runCodeUpdaterAfterAll(
|
|
2261
|
+
arrOptions[0],
|
|
2262
|
+
config,
|
|
2263
|
+
appCodeUpdater,
|
|
2264
|
+
asts,
|
|
2265
|
+
appCodeUpdaterCache
|
|
2266
|
+
);
|
|
2267
|
+
};
|
|
2268
|
+
}
|
|
2269
|
+
const migrate = makeMigrateFn(
|
|
2270
|
+
Infinity,
|
|
2271
|
+
true,
|
|
2272
|
+
(trx, configs, files, count, asts) => migrateOrRollback(trx, configs, files, count, asts, true)
|
|
2273
|
+
);
|
|
2274
|
+
const rollback = makeMigrateFn(
|
|
2275
|
+
1,
|
|
2276
|
+
false,
|
|
2277
|
+
(trx, config, files, count, asts) => migrateOrRollback(trx, config, files, count, asts, false)
|
|
2278
|
+
);
|
|
2279
|
+
const redo = makeMigrateFn(
|
|
2280
|
+
1,
|
|
2281
|
+
false,
|
|
2282
|
+
async (trx, config, files, count, asts) => {
|
|
2283
|
+
await migrateOrRollback(trx, config, files, count, asts, false);
|
|
2284
|
+
files.reverse();
|
|
2285
|
+
await migrateOrRollback(trx, config, files, count, asts, true);
|
|
2286
|
+
files.reverse();
|
|
2287
|
+
}
|
|
2288
|
+
);
|
|
2230
2289
|
const getDb = (adapter) => createDb$1({ adapter });
|
|
2231
|
-
const
|
|
2232
|
-
var _a, _b, _c, _d, _e, _f;
|
|
2233
|
-
config = __spreadValues$2({}, config);
|
|
2234
|
-
const files = await getMigrations(config, up);
|
|
2235
|
-
let count = up ? Infinity : 1;
|
|
2236
|
-
let argI = 0;
|
|
2290
|
+
const getCount = (args) => {
|
|
2237
2291
|
const num = args[0] === "all" ? Infinity : parseInt(args[0]);
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
}
|
|
2242
|
-
const
|
|
2292
|
+
return isNaN(num) ? void 0 : num;
|
|
2293
|
+
};
|
|
2294
|
+
function prepareConfig(config, args, count) {
|
|
2295
|
+
config = __spreadValues$2({}, config);
|
|
2296
|
+
const i = count === void 0 ? 0 : 1;
|
|
2297
|
+
const arg = args[i];
|
|
2243
2298
|
if (arg === "--code") {
|
|
2244
|
-
config.useCodeUpdater = args[
|
|
2299
|
+
config.useCodeUpdater = args[i + 1] !== "false";
|
|
2245
2300
|
}
|
|
2246
2301
|
if (!config.useCodeUpdater)
|
|
2247
2302
|
delete config.appCodeUpdater;
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
try {
|
|
2259
|
-
for (const file of files) {
|
|
2260
|
-
if (up && migratedVersions[file.version] || !up && !migratedVersions[file.version]) {
|
|
2261
|
-
continue;
|
|
2262
|
-
}
|
|
2263
|
-
if (count-- <= 0)
|
|
2264
|
-
break;
|
|
2265
|
-
await processMigration(
|
|
2266
|
-
adapter,
|
|
2267
|
-
up,
|
|
2268
|
-
file,
|
|
2269
|
-
config,
|
|
2270
|
-
opts,
|
|
2271
|
-
appCodeUpdaterCache
|
|
2272
|
-
);
|
|
2273
|
-
(_c = config.logger) == null ? void 0 : _c.log(
|
|
2274
|
-
`${up ? "Migrated" : "Rolled back"} ${pathToLog(file.path)}`
|
|
2275
|
-
);
|
|
2276
|
-
}
|
|
2277
|
-
if (up) {
|
|
2278
|
-
await ((_d = config.afterMigrate) == null ? void 0 : _d.call(config, db != null ? db : db = getDb(adapter)));
|
|
2279
|
-
} else {
|
|
2280
|
-
await ((_e = config.afterRollback) == null ? void 0 : _e.call(config, db != null ? db : db = getDb(adapter)));
|
|
2281
|
-
}
|
|
2282
|
-
} finally {
|
|
2283
|
-
await ((_f = config.appCodeUpdater) == null ? void 0 : _f.afterAll({
|
|
2284
|
-
options: opts,
|
|
2285
|
-
basePath: config.basePath,
|
|
2286
|
-
cache: appCodeUpdaterCache,
|
|
2287
|
-
logger: config.logger,
|
|
2288
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2289
|
-
baseTable: config.baseTable,
|
|
2290
|
-
import: config.import
|
|
2291
|
-
}));
|
|
2292
|
-
await adapter.close();
|
|
2303
|
+
return config;
|
|
2304
|
+
}
|
|
2305
|
+
const migrateOrRollback = async (trx, config, files, count, asts, up) => {
|
|
2306
|
+
var _a, _b, _c;
|
|
2307
|
+
let db;
|
|
2308
|
+
await ((_a = config[up ? "beforeMigrate" : "beforeRollback"]) == null ? void 0 : _a.call(config, db != null ? db : db = getDb(trx)));
|
|
2309
|
+
const migratedVersions = await getMigratedVersionsMap(trx, config);
|
|
2310
|
+
for (const file of files) {
|
|
2311
|
+
if (up && migratedVersions[file.version] || !up && !migratedVersions[file.version]) {
|
|
2312
|
+
continue;
|
|
2293
2313
|
}
|
|
2294
|
-
|
|
2314
|
+
if (count-- <= 0)
|
|
2315
|
+
break;
|
|
2316
|
+
await runMigration(trx, up, file, config, asts);
|
|
2317
|
+
(_b = config.logger) == null ? void 0 : _b.log(
|
|
2318
|
+
`${up ? "Migrated" : "Rolled back"} ${pathToLog(file.path)}`
|
|
2319
|
+
);
|
|
2295
2320
|
}
|
|
2321
|
+
await ((_c = config[up ? "afterMigrate" : "afterRollback"]) == null ? void 0 : _c.call(config, db != null ? db : db = getDb(trx)));
|
|
2296
2322
|
};
|
|
2297
|
-
|
|
2298
|
-
const begin = {
|
|
2299
|
-
text: "BEGIN",
|
|
2300
|
-
values: emptyArray
|
|
2301
|
-
};
|
|
2302
|
-
const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
|
|
2303
|
-
var _a;
|
|
2304
|
-
const asts = await db.transaction(begin, async (tx) => {
|
|
2305
|
-
clearChanges();
|
|
2306
|
-
let changes = changeCache[file.path];
|
|
2307
|
-
if (!changes) {
|
|
2308
|
-
const module = await file.load();
|
|
2309
|
-
const exported = (module == null ? void 0 : module.default) && toArray(module.default);
|
|
2310
|
-
if (config.forceDefaultExports && !exported) {
|
|
2311
|
-
throw new RakeDbError(
|
|
2312
|
-
`Missing a default export in ${file.path} migration`
|
|
2313
|
-
);
|
|
2314
|
-
}
|
|
2315
|
-
changes = exported || getCurrentChanges();
|
|
2316
|
-
changeCache[file.path] = changes;
|
|
2317
|
-
}
|
|
2318
|
-
const db2 = createMigrationInterface(tx, up, config);
|
|
2319
|
-
if (changes.length) {
|
|
2320
|
-
const from = up ? 0 : changes.length - 1;
|
|
2321
|
-
const to = up ? changes.length : -1;
|
|
2322
|
-
const step = up ? 1 : -1;
|
|
2323
|
-
for (let i = from; i !== to; i += step) {
|
|
2324
|
-
await changes[i](db2, up);
|
|
2325
|
-
}
|
|
2326
|
-
}
|
|
2327
|
-
await (up ? saveMigratedVersion : removeMigratedVersion)(
|
|
2328
|
-
db2.adapter,
|
|
2329
|
-
file.version,
|
|
2330
|
-
config
|
|
2331
|
-
);
|
|
2332
|
-
return db2.migratedAsts;
|
|
2333
|
-
});
|
|
2323
|
+
async function runCodeUpdaterAfterAll(options, config, appCodeUpdater, asts, cache) {
|
|
2334
2324
|
for (const ast of asts) {
|
|
2335
|
-
await (
|
|
2325
|
+
await (appCodeUpdater == null ? void 0 : appCodeUpdater.process({
|
|
2336
2326
|
ast,
|
|
2337
2327
|
options,
|
|
2338
2328
|
basePath: config.basePath,
|
|
2339
|
-
cache
|
|
2329
|
+
cache,
|
|
2340
2330
|
logger: config.logger,
|
|
2341
2331
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2342
2332
|
baseTable: config.baseTable,
|
|
2343
2333
|
import: config.import
|
|
2344
2334
|
}));
|
|
2345
2335
|
}
|
|
2336
|
+
await (appCodeUpdater == null ? void 0 : appCodeUpdater.afterAll({
|
|
2337
|
+
options,
|
|
2338
|
+
basePath: config.basePath,
|
|
2339
|
+
cache,
|
|
2340
|
+
logger: config.logger,
|
|
2341
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2342
|
+
baseTable: config.baseTable,
|
|
2343
|
+
import: config.import
|
|
2344
|
+
}));
|
|
2345
|
+
}
|
|
2346
|
+
const changeCache = {};
|
|
2347
|
+
const begin = {
|
|
2348
|
+
text: "BEGIN",
|
|
2349
|
+
values: emptyArray
|
|
2346
2350
|
};
|
|
2347
|
-
const
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2351
|
+
const runMigration = async (trx, up, file, config, asts) => {
|
|
2352
|
+
clearChanges();
|
|
2353
|
+
let changes = changeCache[file.path];
|
|
2354
|
+
if (!changes) {
|
|
2355
|
+
const module = await file.load();
|
|
2356
|
+
const exported = (module == null ? void 0 : module.default) && toArray(module.default);
|
|
2357
|
+
if (config.forceDefaultExports && !exported) {
|
|
2358
|
+
throw new RakeDbError(
|
|
2359
|
+
`Missing a default export in ${file.path} migration`
|
|
2360
|
+
);
|
|
2361
|
+
}
|
|
2362
|
+
changes = exported || getCurrentChanges();
|
|
2363
|
+
changeCache[file.path] = changes;
|
|
2364
|
+
}
|
|
2365
|
+
const db = createMigrationInterface(trx, up, config, asts);
|
|
2366
|
+
if (changes.length) {
|
|
2367
|
+
const from = up ? 0 : changes.length - 1;
|
|
2368
|
+
const to = up ? changes.length : -1;
|
|
2369
|
+
const step = up ? 1 : -1;
|
|
2370
|
+
for (let i = from; i !== to; i += step) {
|
|
2371
|
+
await changes[i](db, up);
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
await (up ? saveMigratedVersion : removeMigratedVersion)(
|
|
2375
|
+
db.adapter,
|
|
2376
|
+
file.version,
|
|
2377
|
+
config
|
|
2378
|
+
);
|
|
2352
2379
|
};
|
|
2353
2380
|
|
|
2354
2381
|
const execute = async (options, sql) => {
|
|
@@ -3857,5 +3884,5 @@ Migrate and rollback common arguments:
|
|
|
3857
3884
|
--code false do not run code updater
|
|
3858
3885
|
`;
|
|
3859
3886
|
|
|
3860
|
-
export { Migration, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
|
3887
|
+
export { Migration, RAKE_DB_LOCK_KEY, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
|
3861
3888
|
//# sourceMappingURL=index.mjs.map
|