rake-db 2.15.11 → 2.17.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 +112 -35
- package/dist/index.js +98 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -986,6 +986,25 @@ declare namespace RakeDbAst {
|
|
|
986
986
|
}
|
|
987
987
|
}
|
|
988
988
|
|
|
989
|
+
interface RakeDbCtx {
|
|
990
|
+
migrationsPromise?: Promise<MigrationsSet>;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
interface MigrationItem {
|
|
994
|
+
path: string;
|
|
995
|
+
version: string;
|
|
996
|
+
/**
|
|
997
|
+
* Function that loads the migration content,
|
|
998
|
+
* can store lazy import of a migration file.
|
|
999
|
+
* Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
|
|
1000
|
+
*/
|
|
1001
|
+
load(): Promise<unknown>;
|
|
1002
|
+
}
|
|
1003
|
+
interface MigrationsSet {
|
|
1004
|
+
renameTo?: RakeDbMigrationId;
|
|
1005
|
+
migrations: MigrationItem[];
|
|
1006
|
+
}
|
|
1007
|
+
|
|
989
1008
|
interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends QueryLogOptions {
|
|
990
1009
|
schemaConfig: SchemaConfig;
|
|
991
1010
|
columnTypes: CT;
|
|
@@ -1005,10 +1024,13 @@ interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColu
|
|
|
1005
1024
|
useCodeUpdater?: boolean;
|
|
1006
1025
|
forceDefaultExports?: boolean;
|
|
1007
1026
|
import(path: string): Promise<unknown>;
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1027
|
+
beforeChange?: ChangeCallback$1;
|
|
1028
|
+
afterChange?: ChangeCallback$1;
|
|
1029
|
+
afterChangeCommit?: ChangeCommitCallback;
|
|
1030
|
+
beforeMigrate?: MigrationCallback;
|
|
1031
|
+
afterMigrate?: MigrationCallback;
|
|
1032
|
+
beforeRollback?: MigrationCallback;
|
|
1033
|
+
afterRollback?: MigrationCallback;
|
|
1012
1034
|
}
|
|
1013
1035
|
interface InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> extends QueryLogOptions {
|
|
1014
1036
|
columnTypes?: CT | ((t: DefaultColumnTypes<DefaultSchemaConfig>) => CT);
|
|
@@ -1029,11 +1051,79 @@ interface InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> extends
|
|
|
1029
1051
|
useCodeUpdater?: boolean;
|
|
1030
1052
|
forceDefaultExports?: boolean;
|
|
1031
1053
|
import?(path: string): Promise<unknown>;
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1054
|
+
/**
|
|
1055
|
+
* Is called once per db before migrating or rolling back a set of migrations.
|
|
1056
|
+
*
|
|
1057
|
+
* @param arg.db - query builder
|
|
1058
|
+
* @param arg.up - whether it's migrating up or down
|
|
1059
|
+
* @param arg.redo - whether it's migrating down and then up for `redo` command
|
|
1060
|
+
* @param arg.migrations - array of executed (up or down) migrations
|
|
1061
|
+
*/
|
|
1062
|
+
beforeChange?: ChangeCallback$1;
|
|
1063
|
+
/**
|
|
1064
|
+
* Is called once per db after migrating or rolling back a set of migrations.
|
|
1065
|
+
* Runs inside the same transaction as migrations,
|
|
1066
|
+
* for running after commit use {@link afterChangeCommit}.
|
|
1067
|
+
*
|
|
1068
|
+
* @param arg.db - query builder
|
|
1069
|
+
* @param arg.up - whether it's migrating up or down
|
|
1070
|
+
* @param arg.redo - whether it's migrating down and then up for `redo` command
|
|
1071
|
+
* @param arg.migrations - array of executed (up or down) migrations
|
|
1072
|
+
*/
|
|
1073
|
+
afterChange?: ChangeCallback$1;
|
|
1074
|
+
/**
|
|
1075
|
+
* Is called once per db after migrating or rolling back a set of migrations.
|
|
1076
|
+
* Runs **after** committing migrations transaction.
|
|
1077
|
+
*
|
|
1078
|
+
* @param arg.options - database connection options
|
|
1079
|
+
* @param arg.up - whether it's migrating up or down
|
|
1080
|
+
* @param arg.migrations - array of executed (up or down) migrations
|
|
1081
|
+
*/
|
|
1082
|
+
afterChangeCommit?: ChangeCommitCallback;
|
|
1083
|
+
/**
|
|
1084
|
+
* Is called once per db before migrating (up) a set of migrations.
|
|
1085
|
+
*
|
|
1086
|
+
* @param arg.db - query builder
|
|
1087
|
+
* @param arg.migrations - applied migrations
|
|
1088
|
+
*/
|
|
1089
|
+
beforeMigrate?: MigrationCallback;
|
|
1090
|
+
/**
|
|
1091
|
+
* Is called once per db after migrating (up) a set of migrations.
|
|
1092
|
+
*
|
|
1093
|
+
* @param arg.db - query builder
|
|
1094
|
+
* @param arg.migrations - applied migrations
|
|
1095
|
+
*/
|
|
1096
|
+
afterMigrate?: MigrationCallback;
|
|
1097
|
+
/**
|
|
1098
|
+
* Is called once per db before rolling back a set of migrations.
|
|
1099
|
+
*
|
|
1100
|
+
* @param arg.db - query builder
|
|
1101
|
+
* @param arg.migrations - rolled back migrations
|
|
1102
|
+
*/
|
|
1103
|
+
beforeRollback?: MigrationCallback;
|
|
1104
|
+
/**
|
|
1105
|
+
* Is called once per db before rolling back a set of migrations.
|
|
1106
|
+
*
|
|
1107
|
+
* @param arg.db - query builder
|
|
1108
|
+
* @param arg.migrations - rolled back migrations
|
|
1109
|
+
*/
|
|
1110
|
+
afterRollback?: MigrationCallback;
|
|
1036
1111
|
}
|
|
1112
|
+
type ChangeCallback$1 = (arg: {
|
|
1113
|
+
db: Db;
|
|
1114
|
+
up: boolean;
|
|
1115
|
+
redo: boolean;
|
|
1116
|
+
migrations: MigrationItem[];
|
|
1117
|
+
}) => void | Promise<void>;
|
|
1118
|
+
type ChangeCommitCallback = (arg: {
|
|
1119
|
+
options: AdapterOptions;
|
|
1120
|
+
up: boolean;
|
|
1121
|
+
migrations: MigrationItem[];
|
|
1122
|
+
}) => void | Promise<void>;
|
|
1123
|
+
type MigrationCallback = (arg: {
|
|
1124
|
+
db: Db;
|
|
1125
|
+
migrations: MigrationItem[];
|
|
1126
|
+
}) => void | Promise<void>;
|
|
1037
1127
|
type AnyRakeDbConfig = RakeDbConfig<any, any>;
|
|
1038
1128
|
type Db = DbResult<RakeDbColumnTypes>;
|
|
1039
1129
|
interface RakeDbBaseTable<CT> {
|
|
@@ -1085,28 +1175,9 @@ declare const getDatabaseAndUserFromOptions: (options: AdapterOptions) => {
|
|
|
1085
1175
|
user: string;
|
|
1086
1176
|
};
|
|
1087
1177
|
|
|
1088
|
-
declare const createDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(
|
|
1089
|
-
declare const dropDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(
|
|
1090
|
-
declare const resetDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT extends RakeDbColumnTypes>(
|
|
1091
|
-
|
|
1092
|
-
interface MigrationItem {
|
|
1093
|
-
path: string;
|
|
1094
|
-
version: string;
|
|
1095
|
-
/**
|
|
1096
|
-
* Function that loads the migration content,
|
|
1097
|
-
* can store lazy import of a migration file.
|
|
1098
|
-
* Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
|
|
1099
|
-
*/
|
|
1100
|
-
load(): Promise<unknown>;
|
|
1101
|
-
}
|
|
1102
|
-
interface MigrationsSet {
|
|
1103
|
-
renameTo?: RakeDbMigrationId;
|
|
1104
|
-
migrations: MigrationItem[];
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
interface RakeDbCtx {
|
|
1108
|
-
migrationsPromise?: Promise<MigrationsSet>;
|
|
1109
|
-
}
|
|
1178
|
+
declare const createDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
1179
|
+
declare const dropDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
1180
|
+
declare const resetDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT extends RakeDbColumnTypes>(options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
1110
1181
|
|
|
1111
1182
|
declare const writeMigrationFile: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(config: RakeDbConfig<SchemaConfig, CT>, version: string, name: string, content: (importPath: string, name: string) => string) => Promise<void>;
|
|
1112
1183
|
declare const generate: (config: AnyRakeDbConfig, [name]: string[]) => Promise<void>;
|
|
@@ -1126,7 +1197,7 @@ type RakeDbAppliedVersions = {
|
|
|
1126
1197
|
declare const getMigratedVersionsMap: <SchemaConfig extends ColumnSchemaConfig<orchid_core.ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, orchid_core.CoreBaseOperators, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>>, CT>(ctx: RakeDbCtx, adapter: Adapter | TransactionAdapter, config: RakeDbConfig<SchemaConfig, CT>, renameTo?: RakeDbMigrationId) => Promise<RakeDbAppliedVersions>;
|
|
1127
1198
|
|
|
1128
1199
|
declare const RAKE_DB_LOCK_KEY = "8582141715823621641";
|
|
1129
|
-
type MigrateFn = <SchemaConfig extends ColumnSchemaConfig, CT extends RakeDbColumnTypes>(ctx: RakeDbCtx, options:
|
|
1200
|
+
type MigrateFn = <SchemaConfig extends ColumnSchemaConfig, CT extends RakeDbColumnTypes>(ctx: RakeDbCtx, options: AdapterOptions[], config: RakeDbConfig<SchemaConfig, CT>, args?: string[]) => Promise<void>;
|
|
1130
1201
|
/**
|
|
1131
1202
|
* Will run all pending yet migrations, sequentially in order,
|
|
1132
1203
|
* will apply `change` functions top-to-bottom.
|
|
@@ -1149,7 +1220,7 @@ declare const rollback: MigrateFn;
|
|
|
1149
1220
|
* Takes the same options as {@link migrate}.
|
|
1150
1221
|
*/
|
|
1151
1222
|
declare const redo: MigrateFn;
|
|
1152
|
-
declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, RakeDbColumnTypes>, set: MigrationsSet, versions: RakeDbAppliedVersions, count: number, asts: RakeDbAst[], up: boolean, force?: boolean, skipLock?: boolean) => Promise<void>;
|
|
1223
|
+
declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, RakeDbColumnTypes>, set: MigrationsSet, versions: RakeDbAppliedVersions, count: number, asts: RakeDbAst[], up: boolean, redo: boolean, force?: boolean, skipLock?: boolean) => Promise<void>;
|
|
1153
1224
|
declare const changeCache: Record<string, ChangeCallback<RakeDbColumnTypes>[] | undefined>;
|
|
1154
1225
|
|
|
1155
1226
|
/**
|
|
@@ -1167,14 +1238,19 @@ type RakeDbFn = (<SchemaConfig extends ColumnSchemaConfig, CT extends RakeDbColu
|
|
|
1167
1238
|
lazy: RakeDbLazyFn;
|
|
1168
1239
|
};
|
|
1169
1240
|
type RakeDbFnReturns<CT extends RakeDbColumnTypes | undefined> = RakeDbChangeFn<CT extends undefined ? DefaultColumnTypes<DefaultSchemaConfig> : CT> & {
|
|
1170
|
-
promise: Promise<
|
|
1241
|
+
promise: Promise<RakeDbResult>;
|
|
1171
1242
|
};
|
|
1243
|
+
interface RakeDbResult {
|
|
1244
|
+
options: AdapterOptions[];
|
|
1245
|
+
config: AnyRakeDbConfig;
|
|
1246
|
+
args: string[];
|
|
1247
|
+
}
|
|
1172
1248
|
/**
|
|
1173
1249
|
* Type of {@link rakeDb.lazy} function
|
|
1174
1250
|
*/
|
|
1175
1251
|
type RakeDbLazyFn = <SchemaConfig extends ColumnSchemaConfig, CT extends RakeDbColumnTypes>(options: MaybeArray<AdapterOptions>, partialConfig?: InputRakeDbConfig<SchemaConfig, CT>) => {
|
|
1176
1252
|
change: RakeDbChangeFn<CT>;
|
|
1177
|
-
run(args: string[], config?: Partial<RakeDbConfig<SchemaConfig, CT>>): Promise<
|
|
1253
|
+
run(args: string[], config?: Partial<RakeDbConfig<SchemaConfig, CT>>): Promise<RakeDbResult>;
|
|
1178
1254
|
};
|
|
1179
1255
|
/**
|
|
1180
1256
|
* Function to use in migrations to wrap database changes
|
|
@@ -1190,5 +1266,6 @@ type RakeDbChangeFn<CT extends RakeDbColumnTypes> = (fn: ChangeCallback<CT>) =>
|
|
|
1190
1266
|
* @param args - optionally provide an array of cli args. Default is `process.argv.slice(2)`.
|
|
1191
1267
|
*/
|
|
1192
1268
|
declare const rakeDb: RakeDbFn;
|
|
1269
|
+
declare const rakeDbAliases: RecordOptionalString;
|
|
1193
1270
|
|
|
1194
|
-
export { AnyRakeDbConfig, AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, ConstraintArg, DbMigration, DropMode, InputRakeDbConfig, Migration, MigrationAdapter, MigrationColumnTypes, ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAppliedVersions, RakeDbAst, RakeDbBaseTable, RakeDbChangeFn, RakeDbColumnTypes, RakeDbConfig, RakeDbFn, RakeDbFnReturns, RakeDbLazyFn, RakeDbMigrationId, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, deleteMigratedVersion, dropDb, generate, generateTimeStamp, getDatabaseAndUserFromOptions, getMigratedVersionsMap, makeFileVersion, migrate, migrateOrRollback, migrationConfigDefaults, processRakeDbConfig, rakeDb, redo, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
|
1271
|
+
export { AnyRakeDbConfig, AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, ConstraintArg, DbMigration, DropMode, InputRakeDbConfig, Migration, MigrationAdapter, MigrationColumnTypes, ModuleExportsRecord, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAppliedVersions, RakeDbAst, RakeDbBaseTable, RakeDbChangeFn, RakeDbColumnTypes, RakeDbConfig, RakeDbFn, RakeDbFnReturns, RakeDbLazyFn, RakeDbMigrationId, RakeDbResult, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, deleteMigratedVersion, dropDb, generate, generateTimeStamp, getDatabaseAndUserFromOptions, getMigratedVersionsMap, makeFileVersion, migrate, migrateOrRollback, migrationConfigDefaults, processRakeDbConfig, rakeDb, rakeDbAliases, redo, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -2413,6 +2413,7 @@ var __spreadValues$5 = (a, b) => {
|
|
|
2413
2413
|
const RAKE_DB_LOCK_KEY = "8582141715823621641";
|
|
2414
2414
|
function makeMigrateFn(defaultCount, up, fn) {
|
|
2415
2415
|
return async (ctx, options, config, args = []) => {
|
|
2416
|
+
var _a;
|
|
2416
2417
|
const set = await getMigrations(ctx, config, up);
|
|
2417
2418
|
const arg = args[0];
|
|
2418
2419
|
let force = arg === "force";
|
|
@@ -2428,9 +2429,8 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2428
2429
|
const asts = [];
|
|
2429
2430
|
const appCodeUpdaterCache = {};
|
|
2430
2431
|
const { appCodeUpdater } = conf;
|
|
2431
|
-
const arrOptions = orchidCore.toArray(options);
|
|
2432
2432
|
let localAsts = asts;
|
|
2433
|
-
for (const opts of
|
|
2433
|
+
for (const opts of options) {
|
|
2434
2434
|
const adapter = new pqb.Adapter(opts);
|
|
2435
2435
|
try {
|
|
2436
2436
|
await transaction(adapter, async (trx) => {
|
|
@@ -2478,9 +2478,14 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2478
2478
|
await adapter.close();
|
|
2479
2479
|
}
|
|
2480
2480
|
localAsts = [];
|
|
2481
|
+
(_a = config.afterChangeCommit) == null ? void 0 : _a.call(config, {
|
|
2482
|
+
options: opts,
|
|
2483
|
+
up,
|
|
2484
|
+
migrations: set.migrations
|
|
2485
|
+
});
|
|
2481
2486
|
}
|
|
2482
2487
|
await runCodeUpdaterAfterAll(
|
|
2483
|
-
|
|
2488
|
+
options[0],
|
|
2484
2489
|
config,
|
|
2485
2490
|
appCodeUpdater,
|
|
2486
2491
|
asts,
|
|
@@ -2491,19 +2496,48 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2491
2496
|
const migrate = makeMigrateFn(
|
|
2492
2497
|
Infinity,
|
|
2493
2498
|
true,
|
|
2494
|
-
(trx, config, set, versions, count, asts, force) => migrateOrRollback(
|
|
2499
|
+
(trx, config, set, versions, count, asts, force) => migrateOrRollback(
|
|
2500
|
+
trx,
|
|
2501
|
+
config,
|
|
2502
|
+
set,
|
|
2503
|
+
versions,
|
|
2504
|
+
count,
|
|
2505
|
+
asts,
|
|
2506
|
+
true,
|
|
2507
|
+
false,
|
|
2508
|
+
force
|
|
2509
|
+
)
|
|
2495
2510
|
);
|
|
2496
2511
|
const rollback = makeMigrateFn(
|
|
2497
2512
|
1,
|
|
2498
2513
|
false,
|
|
2499
|
-
(trx, config, set, versions, count, asts, force) => migrateOrRollback(
|
|
2514
|
+
(trx, config, set, versions, count, asts, force) => migrateOrRollback(
|
|
2515
|
+
trx,
|
|
2516
|
+
config,
|
|
2517
|
+
set,
|
|
2518
|
+
versions,
|
|
2519
|
+
count,
|
|
2520
|
+
asts,
|
|
2521
|
+
false,
|
|
2522
|
+
false,
|
|
2523
|
+
force
|
|
2524
|
+
)
|
|
2500
2525
|
);
|
|
2501
2526
|
const redo = makeMigrateFn(
|
|
2502
2527
|
1,
|
|
2503
2528
|
true,
|
|
2504
2529
|
async (trx, config, set, versions, count, asts, force) => {
|
|
2505
2530
|
set.migrations.reverse();
|
|
2506
|
-
await migrateOrRollback(
|
|
2531
|
+
await migrateOrRollback(
|
|
2532
|
+
trx,
|
|
2533
|
+
config,
|
|
2534
|
+
set,
|
|
2535
|
+
versions,
|
|
2536
|
+
count,
|
|
2537
|
+
asts,
|
|
2538
|
+
false,
|
|
2539
|
+
true
|
|
2540
|
+
);
|
|
2507
2541
|
set.migrations.reverse();
|
|
2508
2542
|
await migrateOrRollback(
|
|
2509
2543
|
trx,
|
|
@@ -2513,6 +2547,7 @@ const redo = makeMigrateFn(
|
|
|
2513
2547
|
count,
|
|
2514
2548
|
asts,
|
|
2515
2549
|
true,
|
|
2550
|
+
true,
|
|
2516
2551
|
force,
|
|
2517
2552
|
true
|
|
2518
2553
|
);
|
|
@@ -2530,7 +2565,7 @@ function prepareConfig(config, args, hasArg) {
|
|
|
2530
2565
|
delete config.appCodeUpdater;
|
|
2531
2566
|
return config;
|
|
2532
2567
|
}
|
|
2533
|
-
const migrateOrRollback = async (trx, config, set, versions, count, asts, up, force, skipLock) => {
|
|
2568
|
+
const migrateOrRollback = async (trx, config, set, versions, count, asts, up, redo2, force, skipLock) => {
|
|
2534
2569
|
var _a, _b, _c;
|
|
2535
2570
|
const { sequence, map: versionsMap } = versions;
|
|
2536
2571
|
if (up) {
|
|
@@ -2553,7 +2588,8 @@ const migrateOrRollback = async (trx, config, set, versions, count, asts, up, fo
|
|
|
2553
2588
|
versions,
|
|
2554
2589
|
sequence.length - i,
|
|
2555
2590
|
asts,
|
|
2556
|
-
false
|
|
2591
|
+
false,
|
|
2592
|
+
redo2
|
|
2557
2593
|
);
|
|
2558
2594
|
set.migrations.reverse();
|
|
2559
2595
|
}
|
|
@@ -2561,7 +2597,13 @@ const migrateOrRollback = async (trx, config, set, versions, count, asts, up, fo
|
|
|
2561
2597
|
if (!skipLock)
|
|
2562
2598
|
await queryLock(trx);
|
|
2563
2599
|
let db;
|
|
2564
|
-
|
|
2600
|
+
const beforeMigrate = config[up ? "beforeMigrate" : "beforeRollback"];
|
|
2601
|
+
if (beforeMigrate || config.beforeChange) {
|
|
2602
|
+
db != null ? db : db = getDb(trx);
|
|
2603
|
+
const { migrations } = set;
|
|
2604
|
+
await (beforeMigrate == null ? void 0 : beforeMigrate({ db, migrations }));
|
|
2605
|
+
await ((_a = config.beforeChange) == null ? void 0 : _a.call(config, { db, migrations, up, redo: redo2 }));
|
|
2606
|
+
}
|
|
2565
2607
|
for (const file of set.migrations) {
|
|
2566
2608
|
if (up && versionsMap[file.version] || !up && !versionsMap[file.version]) {
|
|
2567
2609
|
continue;
|
|
@@ -2581,7 +2623,13 @@ const migrateOrRollback = async (trx, config, set, versions, count, asts, up, fo
|
|
|
2581
2623
|
`${up ? "Migrated" : "Rolled back"} ${orchidCore.pathToLog(file.path)}`
|
|
2582
2624
|
);
|
|
2583
2625
|
}
|
|
2584
|
-
|
|
2626
|
+
const afterMigrate = config[up ? "afterMigrate" : "afterRollback"];
|
|
2627
|
+
if (config.afterChange || afterMigrate) {
|
|
2628
|
+
db != null ? db : db = getDb(trx);
|
|
2629
|
+
const { migrations } = set;
|
|
2630
|
+
await ((_c = config.afterChange) == null ? void 0 : _c.call(config, { db, up, redo: redo2, migrations }));
|
|
2631
|
+
await (afterMigrate == null ? void 0 : afterMigrate({ db, migrations }));
|
|
2632
|
+
}
|
|
2585
2633
|
};
|
|
2586
2634
|
const checkMigrationOrder = (config, set, { sequence, map }, force) => {
|
|
2587
2635
|
const last = sequence[sequence.length - 1];
|
|
@@ -2896,9 +2944,9 @@ Don't use this command for database service providers, only for a local db.`;
|
|
|
2896
2944
|
await createMigrationsTable(db, config);
|
|
2897
2945
|
await db.close();
|
|
2898
2946
|
};
|
|
2899
|
-
const createDb = async (
|
|
2900
|
-
for (const
|
|
2901
|
-
await createOrDrop(
|
|
2947
|
+
const createDb = async (options, config) => {
|
|
2948
|
+
for (const opts of options) {
|
|
2949
|
+
await createOrDrop(opts, opts, config, {
|
|
2902
2950
|
sql({ database, user }) {
|
|
2903
2951
|
return `CREATE DATABASE "${database}"${user ? ` OWNER "${user}"` : ""}`;
|
|
2904
2952
|
},
|
|
@@ -2912,9 +2960,9 @@ const createDb = async (arg, config) => {
|
|
|
2912
2960
|
});
|
|
2913
2961
|
}
|
|
2914
2962
|
};
|
|
2915
|
-
const dropDb = async (
|
|
2916
|
-
for (const
|
|
2917
|
-
await createOrDrop(
|
|
2963
|
+
const dropDb = async (options, config) => {
|
|
2964
|
+
for (const opts of options) {
|
|
2965
|
+
await createOrDrop(opts, opts, config, {
|
|
2918
2966
|
sql({ database }) {
|
|
2919
2967
|
return `DROP DATABASE "${database}"`;
|
|
2920
2968
|
},
|
|
@@ -2927,10 +2975,10 @@ const dropDb = async (arg, config) => {
|
|
|
2927
2975
|
});
|
|
2928
2976
|
}
|
|
2929
2977
|
};
|
|
2930
|
-
const resetDb = async (
|
|
2931
|
-
await dropDb(
|
|
2932
|
-
await createDb(
|
|
2933
|
-
await migrate({},
|
|
2978
|
+
const resetDb = async (options, config) => {
|
|
2979
|
+
await dropDb(options, config);
|
|
2980
|
+
await createDb(options, config);
|
|
2981
|
+
await migrate({}, options, config);
|
|
2934
2982
|
};
|
|
2935
2983
|
|
|
2936
2984
|
const filterSchema = (table) => `${table} !~ '^pg_' AND ${table} != 'information_schema'`;
|
|
@@ -4089,9 +4137,7 @@ const runRecurrentMigrations = async (options, config) => {
|
|
|
4089
4137
|
let files = 0;
|
|
4090
4138
|
await readdirRecursive(config.recurrentPath, async (path) => {
|
|
4091
4139
|
files++;
|
|
4092
|
-
dbs != null ? dbs : dbs =
|
|
4093
|
-
(opts) => pqb.createDb({ adapter: new pqb.Adapter(opts) })
|
|
4094
|
-
);
|
|
4140
|
+
dbs != null ? dbs : dbs = options.map((opts) => pqb.createDb({ adapter: new pqb.Adapter(opts) }));
|
|
4095
4141
|
const sql = await fs.readFile(path, "utf-8");
|
|
4096
4142
|
await Promise.all(
|
|
4097
4143
|
dbs.map(async (db) => {
|
|
@@ -4460,39 +4506,55 @@ function change(fn) {
|
|
|
4460
4506
|
pushChange(fn);
|
|
4461
4507
|
return fn;
|
|
4462
4508
|
}
|
|
4463
|
-
const
|
|
4509
|
+
const rakeDbAliases = {
|
|
4510
|
+
migrate: "up",
|
|
4511
|
+
rollback: "down",
|
|
4512
|
+
s: "status",
|
|
4513
|
+
rec: "recurrent"
|
|
4514
|
+
};
|
|
4515
|
+
const runCommand = async (opts, config, args = process.argv.slice(2)) => {
|
|
4464
4516
|
var _a, _b;
|
|
4465
|
-
|
|
4517
|
+
let arg = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
|
|
4518
|
+
if (rakeDbAliases[arg]) {
|
|
4519
|
+
args = [...args];
|
|
4520
|
+
arg = args[0] = rakeDbAliases[arg];
|
|
4521
|
+
}
|
|
4522
|
+
const options = orchidCore.toArray(opts);
|
|
4466
4523
|
if (arg === "create") {
|
|
4467
4524
|
await createDb(options, config);
|
|
4468
4525
|
} else if (arg === "drop") {
|
|
4469
4526
|
await dropDb(options, config);
|
|
4470
4527
|
} else if (arg === "reset") {
|
|
4471
4528
|
await resetDb(options, config);
|
|
4472
|
-
} else if (arg === "up"
|
|
4529
|
+
} else if (arg === "up") {
|
|
4473
4530
|
await migrate({}, options, config, args.slice(1));
|
|
4474
|
-
} else if (arg === "down"
|
|
4531
|
+
} else if (arg === "down") {
|
|
4475
4532
|
await rollback({}, options, config, args.slice(1));
|
|
4476
4533
|
} else if (arg === "redo") {
|
|
4477
4534
|
await redo({}, options, config, args.slice(1));
|
|
4478
4535
|
} else if (arg === "new") {
|
|
4479
4536
|
await generate(config, args.slice(1));
|
|
4480
4537
|
} else if (arg === "pull") {
|
|
4481
|
-
await pullDbStructure(
|
|
4482
|
-
} else if (arg === "status"
|
|
4483
|
-
await listMigrationsStatuses(
|
|
4538
|
+
await pullDbStructure(options[0], config);
|
|
4539
|
+
} else if (arg === "status") {
|
|
4540
|
+
await listMigrationsStatuses(options, config, args.slice(1));
|
|
4484
4541
|
} else if (arg === "rebase") {
|
|
4485
|
-
await rebase(
|
|
4542
|
+
await rebase(options, config);
|
|
4486
4543
|
} else if (arg === "change-ids") {
|
|
4487
|
-
await changeIds(
|
|
4544
|
+
await changeIds(options, config, args.slice(1));
|
|
4488
4545
|
} else if (config.commands[arg]) {
|
|
4489
|
-
await config.commands[arg](
|
|
4490
|
-
} else if (arg !== "
|
|
4546
|
+
await config.commands[arg](options, config, args.slice(1));
|
|
4547
|
+
} else if (arg !== "recurrent") {
|
|
4491
4548
|
(_b = config.logger) == null ? void 0 : _b.log(help);
|
|
4492
4549
|
}
|
|
4493
|
-
if (arg === "
|
|
4550
|
+
if (arg === "up" || arg === "recurrent" || arg === "redo") {
|
|
4494
4551
|
await runRecurrentMigrations(options, config);
|
|
4495
4552
|
}
|
|
4553
|
+
return {
|
|
4554
|
+
options,
|
|
4555
|
+
config,
|
|
4556
|
+
args
|
|
4557
|
+
};
|
|
4496
4558
|
};
|
|
4497
4559
|
const help = `Usage: rake-db [command] [arguments]
|
|
4498
4560
|
|
|
@@ -4551,6 +4613,7 @@ exports.migrateOrRollback = migrateOrRollback;
|
|
|
4551
4613
|
exports.migrationConfigDefaults = migrationConfigDefaults;
|
|
4552
4614
|
exports.processRakeDbConfig = processRakeDbConfig;
|
|
4553
4615
|
exports.rakeDb = rakeDb;
|
|
4616
|
+
exports.rakeDbAliases = rakeDbAliases;
|
|
4554
4617
|
exports.redo = redo;
|
|
4555
4618
|
exports.resetDb = resetDb;
|
|
4556
4619
|
exports.rollback = rollback;
|