rake-db 2.16.0 → 2.17.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/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,12 +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
- beforeChange?(db: Db, up: boolean, redo: boolean): void | Promise<void>;
1009
- afterChange?(db: Db, up: boolean, redo: boolean): void | Promise<void>;
1010
- beforeMigrate?(db: Db): void | Promise<void>;
1011
- afterMigrate?(db: Db): void | Promise<void>;
1012
- beforeRollback?(db: Db): void | Promise<void>;
1013
- afterRollback?(db: Db): void | Promise<void>;
1027
+ beforeChange?: ChangeCallback$1;
1028
+ afterChange?: ChangeCallback$1;
1029
+ afterChangeCommit?: ChangeCommitCallback;
1030
+ beforeMigrate?: MigrationCallback;
1031
+ afterMigrate?: MigrationCallback;
1032
+ beforeRollback?: MigrationCallback;
1033
+ afterRollback?: MigrationCallback;
1014
1034
  }
1015
1035
  interface InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> extends QueryLogOptions {
1016
1036
  columnTypes?: CT | ((t: DefaultColumnTypes<DefaultSchemaConfig>) => CT);
@@ -1032,46 +1052,78 @@ interface InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> extends
1032
1052
  forceDefaultExports?: boolean;
1033
1053
  import?(path: string): Promise<unknown>;
1034
1054
  /**
1035
- * Is called once before migrating (up) or rolling back one or more migrations.
1055
+ * Is called once per db before migrating or rolling back a set of migrations.
1036
1056
  *
1037
- * @param db - query builder
1038
- * @param up - whether it's migrating up or down
1039
- * @param redo - whether it's migrating down and then up for `redo` command
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
1040
1072
  */
1041
- beforeChange?(db: Db, up: boolean, redo: boolean): void | Promise<void>;
1073
+ afterChange?: ChangeCallback$1;
1042
1074
  /**
1043
- * Is called once after migrating (up) or rolling back one or more migrations.
1075
+ * Is called once per db after migrating or rolling back a set of migrations.
1076
+ * Runs **after** committing migrations transaction.
1044
1077
  *
1045
- * @param db - query builder
1046
- * @param up - whether it's migrating up or down
1047
- * @param redo - whether it's migrating down and then up for `redo` command
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
1048
1081
  */
1049
- afterChange?(db: Db, up: boolean, redo: boolean): void | Promise<void>;
1082
+ afterChangeCommit?: ChangeCommitCallback;
1050
1083
  /**
1051
- * Is called once before migrating (up) one or more migrations.
1084
+ * Is called once per db before migrating (up) a set of migrations.
1052
1085
  *
1053
- * @param db - query builder
1086
+ * @param arg.db - query builder
1087
+ * @param arg.migrations - applied migrations
1054
1088
  */
1055
- beforeMigrate?(db: Db): void | Promise<void>;
1089
+ beforeMigrate?: MigrationCallback;
1056
1090
  /**
1057
- * Is called once after migrating (up) one or more migrations.
1091
+ * Is called once per db after migrating (up) a set of migrations.
1058
1092
  *
1059
- * @param db - query builder
1093
+ * @param arg.db - query builder
1094
+ * @param arg.migrations - applied migrations
1060
1095
  */
1061
- afterMigrate?(db: Db): void | Promise<void>;
1096
+ afterMigrate?: MigrationCallback;
1062
1097
  /**
1063
- * Is called once before rolling back one or more migrations.
1098
+ * Is called once per db before rolling back a set of migrations.
1064
1099
  *
1065
- * @param db - query builder
1100
+ * @param arg.db - query builder
1101
+ * @param arg.migrations - rolled back migrations
1066
1102
  */
1067
- beforeRollback?(db: Db): void | Promise<void>;
1103
+ beforeRollback?: MigrationCallback;
1068
1104
  /**
1069
- * Is called once before rolling back one or more migrations.
1105
+ * Is called once per db before rolling back a set of migrations.
1070
1106
  *
1071
- * @param db - query builder
1107
+ * @param arg.db - query builder
1108
+ * @param arg.migrations - rolled back migrations
1072
1109
  */
1073
- afterRollback?(db: Db): void | Promise<void>;
1110
+ afterRollback?: MigrationCallback;
1074
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>;
1075
1127
  type AnyRakeDbConfig = RakeDbConfig<any, any>;
1076
1128
  type Db = DbResult<RakeDbColumnTypes>;
1077
1129
  interface RakeDbBaseTable<CT> {
@@ -1127,25 +1179,6 @@ declare const createDb: <SchemaConfig extends ColumnSchemaConfig<orchid_core.Col
1127
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>;
1128
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>;
1129
1181
 
1130
- interface MigrationItem {
1131
- path: string;
1132
- version: string;
1133
- /**
1134
- * Function that loads the migration content,
1135
- * can store lazy import of a migration file.
1136
- * Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
1137
- */
1138
- load(): Promise<unknown>;
1139
- }
1140
- interface MigrationsSet {
1141
- renameTo?: RakeDbMigrationId;
1142
- migrations: MigrationItem[];
1143
- }
1144
-
1145
- interface RakeDbCtx {
1146
- migrationsPromise?: Promise<MigrationsSet>;
1147
- }
1148
-
1149
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>;
1150
1183
  declare const generate: (config: AnyRakeDbConfig, [name]: string[]) => Promise<void>;
1151
1184
  declare const makeFileVersion: (ctx: RakeDbCtx, config: AnyRakeDbConfig) => Promise<string>;
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";
@@ -2477,6 +2478,11 @@ function makeMigrateFn(defaultCount, up, fn) {
2477
2478
  await adapter.close();
2478
2479
  }
2479
2480
  localAsts = [];
2481
+ (_a = config.afterChangeCommit) == null ? void 0 : _a.call(config, {
2482
+ options: opts,
2483
+ up,
2484
+ migrations: set.migrations
2485
+ });
2480
2486
  }
2481
2487
  await runCodeUpdaterAfterAll(
2482
2488
  options[0],
@@ -2560,7 +2566,7 @@ function prepareConfig(config, args, hasArg) {
2560
2566
  return config;
2561
2567
  }
2562
2568
  const migrateOrRollback = async (trx, config, set, versions, count, asts, up, redo2, force, skipLock) => {
2563
- var _a, _b, _c, _d, _e;
2569
+ var _a, _b, _c;
2564
2570
  const { sequence, map: versionsMap } = versions;
2565
2571
  if (up) {
2566
2572
  const rollbackTo = checkMigrationOrder(config, set, versions, force);
@@ -2591,8 +2597,13 @@ const migrateOrRollback = async (trx, config, set, versions, count, asts, up, re
2591
2597
  if (!skipLock)
2592
2598
  await queryLock(trx);
2593
2599
  let db;
2594
- await ((_a = config[up ? "beforeMigrate" : "beforeRollback"]) == null ? void 0 : _a.call(config, db != null ? db : db = getDb(trx)));
2595
- await ((_b = config.beforeChange) == null ? void 0 : _b.call(config, db != null ? db : db = getDb(trx), up, redo2));
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
+ }
2596
2607
  for (const file of set.migrations) {
2597
2608
  if (up && versionsMap[file.version] || !up && !versionsMap[file.version]) {
2598
2609
  continue;
@@ -2608,12 +2619,17 @@ const migrateOrRollback = async (trx, config, set, versions, count, asts, up, re
2608
2619
  versionsMap[file.version] = void 0;
2609
2620
  sequence.pop();
2610
2621
  }
2611
- (_c = config.logger) == null ? void 0 : _c.log(
2622
+ (_b = config.logger) == null ? void 0 : _b.log(
2612
2623
  `${up ? "Migrated" : "Rolled back"} ${orchidCore.pathToLog(file.path)}`
2613
2624
  );
2614
2625
  }
2615
- await ((_d = config.afterChange) == null ? void 0 : _d.call(config, db != null ? db : db = getDb(trx), up, redo2));
2616
- await ((_e = config[up ? "afterMigrate" : "afterRollback"]) == null ? void 0 : _e.call(config, db != null ? db : db = getDb(trx)));
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
+ }
2617
2633
  };
2618
2634
  const checkMigrationOrder = (config, set, { sequence, map }, force) => {
2619
2635
  const last = sequence[sequence.length - 1];