rake-db 2.3.8 → 2.3.10

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.3.10
4
+
5
+ ### Patch Changes
6
+
7
+ - Add custom commands to rake-db
8
+
9
+ ## 2.3.9
10
+
11
+ ### Patch Changes
12
+
13
+ - Add migrate and rollback callbacks
14
+
3
15
  ## 2.3.8
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -389,13 +389,19 @@ declare namespace RakeDbAst {
389
389
  };
390
390
  }
391
391
 
392
+ declare type Db = DbResult<typeof columnTypes>;
392
393
  declare type RakeDbConfig = {
393
394
  migrationsPath: string;
394
395
  migrationsTable: string;
396
+ commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig, args: string[]) => Promise<void>>;
395
397
  requireTs(path: string): Promise<void>;
396
398
  noPrimaryKey?: NoPrimaryKeyOption;
397
399
  appCodeUpdater?: AppCodeUpdater;
398
400
  useCodeUpdater?: boolean;
401
+ beforeMigrate?(db: Db): Promise<void>;
402
+ afterMigrate?(db: Db): Promise<void>;
403
+ beforeRollback?(db: Db): Promise<void>;
404
+ afterRollback?(db: Db): Promise<void>;
399
405
  } & QueryLogOptions;
400
406
  declare type AppCodeUpdater = (params: {
401
407
  ast: RakeDbAst;
package/dist/index.js CHANGED
@@ -50,8 +50,9 @@ var __spreadValues$6 = (a, b) => {
50
50
  };
51
51
  var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
52
52
  const migrationConfigDefaults = {
53
- migrationsPath: path__default["default"].resolve("src", "migrations"),
53
+ migrationsPath: path__default["default"].resolve("src", "db", "migrations"),
54
54
  migrationsTable: "schemaMigrations",
55
+ commands: {},
55
56
  requireTs: (path2) => (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path2),
56
57
  log: true,
57
58
  logger: console,
@@ -1137,8 +1138,9 @@ var __spreadValues$1 = (a, b) => {
1137
1138
  }
1138
1139
  return a;
1139
1140
  };
1141
+ const getDb = (adapter) => pqb.createDb({ adapter, columnTypes: pqb.columnTypes });
1140
1142
  const migrateOrRollback = async (options, config, args, up) => {
1141
- var _a;
1143
+ var _a, _b, _c, _d, _e;
1142
1144
  config = __spreadValues$1({}, config);
1143
1145
  const files = await getMigrationFiles(config, up);
1144
1146
  let count = up ? Infinity : 1;
@@ -1156,8 +1158,14 @@ const migrateOrRollback = async (options, config, args, up) => {
1156
1158
  delete config.appCodeUpdater;
1157
1159
  const appCodeUpdaterCache = {};
1158
1160
  for (const opts of pqb.toArray(options)) {
1159
- const db = new pqb.Adapter(opts);
1160
- const migratedVersions = await getMigratedVersionsMap(db, config);
1161
+ const adapter = new pqb.Adapter(opts);
1162
+ let db;
1163
+ if (up) {
1164
+ await ((_a = config.beforeMigrate) == null ? void 0 : _a.call(config, db != null ? db : db = getDb(adapter)));
1165
+ } else {
1166
+ await ((_b = config.beforeRollback) == null ? void 0 : _b.call(config, db != null ? db : db = getDb(adapter)));
1167
+ }
1168
+ const migratedVersions = await getMigratedVersionsMap(adapter, config);
1161
1169
  try {
1162
1170
  for (const file of files) {
1163
1171
  if (up && migratedVersions[file.version] || !up && !migratedVersions[file.version]) {
@@ -1165,11 +1173,23 @@ const migrateOrRollback = async (options, config, args, up) => {
1165
1173
  }
1166
1174
  if (count-- <= 0)
1167
1175
  break;
1168
- await processMigration(db, up, file, config, opts, appCodeUpdaterCache);
1169
- (_a = config.logger) == null ? void 0 : _a.log(`${file.path} ${up ? "migrated" : "rolled back"}`);
1176
+ await processMigration(
1177
+ adapter,
1178
+ up,
1179
+ file,
1180
+ config,
1181
+ opts,
1182
+ appCodeUpdaterCache
1183
+ );
1184
+ (_c = config.logger) == null ? void 0 : _c.log(`${file.path} ${up ? "migrated" : "rolled back"}`);
1185
+ }
1186
+ if (up) {
1187
+ await ((_d = config.afterMigrate) == null ? void 0 : _d.call(config, db != null ? db : db = getDb(adapter)));
1188
+ } else {
1189
+ await ((_e = config.afterRollback) == null ? void 0 : _e.call(config, db != null ? db : db = getDb(adapter)));
1170
1190
  }
1171
1191
  } finally {
1172
- await db.close();
1192
+ await adapter.close();
1173
1193
  }
1174
1194
  delete config.appCodeUpdater;
1175
1195
  }
@@ -1942,6 +1962,8 @@ const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2))
1942
1962
  await generate(config, args.slice(1));
1943
1963
  } else if (command === "pull") {
1944
1964
  await pullDbStructure(pqb.toArray(options)[0], config);
1965
+ } else if (config.commands[command]) {
1966
+ await config.commands[command](pqb.toArray(options), config, args.slice(1));
1945
1967
  } else {
1946
1968
  printHelp();
1947
1969
  }