rake-db 2.2.1 → 2.2.4

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.esm.js CHANGED
@@ -4,21 +4,21 @@ import path from 'path';
4
4
  import { readdir, mkdir, writeFile } from 'fs/promises';
5
5
  import { singular } from 'pluralize';
6
6
 
7
- var __defProp$5 = Object.defineProperty;
7
+ var __defProp$6 = Object.defineProperty;
8
8
  var __defProps$5 = Object.defineProperties;
9
9
  var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
10
- var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
11
- var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
12
- var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
13
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
- var __spreadValues$5 = (a, b) => {
10
+ var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
11
+ var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
12
+ var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
13
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
+ var __spreadValues$6 = (a, b) => {
15
15
  for (var prop in b || (b = {}))
16
- if (__hasOwnProp$5.call(b, prop))
17
- __defNormalProp$5(a, prop, b[prop]);
18
- if (__getOwnPropSymbols$5)
19
- for (var prop of __getOwnPropSymbols$5(b)) {
20
- if (__propIsEnum$5.call(b, prop))
21
- __defNormalProp$5(a, prop, b[prop]);
16
+ if (__hasOwnProp$6.call(b, prop))
17
+ __defNormalProp$6(a, prop, b[prop]);
18
+ if (__getOwnPropSymbols$6)
19
+ for (var prop of __getOwnPropSymbols$6(b)) {
20
+ if (__propIsEnum$6.call(b, prop))
21
+ __defNormalProp$6(a, prop, b[prop]);
22
22
  }
23
23
  return a;
24
24
  };
@@ -28,10 +28,11 @@ const migrationConfigDefaults = {
28
28
  migrationsTable: "schemaMigrations",
29
29
  requireTs: require,
30
30
  log: true,
31
- logger: console
31
+ logger: console,
32
+ useCodeUpdaterByDefault: true
32
33
  };
33
34
  const getMigrationConfigWithDefaults = (config) => {
34
- return __spreadValues$5(__spreadValues$5({}, migrationConfigDefaults), config);
35
+ return __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
35
36
  };
36
37
  const getDatabaseAndUserFromOptions = (options) => {
37
38
  if (options.databaseURL) {
@@ -59,9 +60,9 @@ const setAdapterOptions = (options, set) => {
59
60
  if (set.password !== void 0) {
60
61
  url.password = set.password;
61
62
  }
62
- return __spreadProps$5(__spreadValues$5({}, options), { databaseURL: url.toString() });
63
+ return __spreadProps$5(__spreadValues$6({}, options), { databaseURL: url.toString() });
63
64
  } else {
64
- return __spreadValues$5(__spreadValues$5({}, options), set);
65
+ return __spreadValues$6(__spreadValues$6({}, options), set);
65
66
  }
66
67
  };
67
68
  const askAdminCredentials = async () => {
@@ -200,21 +201,21 @@ const setCurrentMigrationUp = (up) => {
200
201
  const getCurrentPromise = () => currentPromise;
201
202
  const getCurrentChangeCallback = () => currentChangeCallback;
202
203
 
203
- var __defProp$4 = Object.defineProperty;
204
+ var __defProp$5 = Object.defineProperty;
204
205
  var __defProps$4 = Object.defineProperties;
205
206
  var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
206
- var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
207
- var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
208
- var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
209
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
210
- var __spreadValues$4 = (a, b) => {
207
+ var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
208
+ var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
209
+ var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
210
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
211
+ var __spreadValues$5 = (a, b) => {
211
212
  for (var prop in b || (b = {}))
212
- if (__hasOwnProp$4.call(b, prop))
213
- __defNormalProp$4(a, prop, b[prop]);
214
- if (__getOwnPropSymbols$4)
215
- for (var prop of __getOwnPropSymbols$4(b)) {
216
- if (__propIsEnum$4.call(b, prop))
217
- __defNormalProp$4(a, prop, b[prop]);
213
+ if (__hasOwnProp$5.call(b, prop))
214
+ __defNormalProp$5(a, prop, b[prop]);
215
+ if (__getOwnPropSymbols$5)
216
+ for (var prop of __getOwnPropSymbols$5(b)) {
217
+ if (__propIsEnum$5.call(b, prop))
218
+ __defNormalProp$5(a, prop, b[prop]);
218
219
  }
219
220
  return a;
220
221
  };
@@ -255,7 +256,7 @@ const addColumnIndex = (indexes, key, item) => {
255
256
  if (item.data) {
256
257
  if (item.data.index) {
257
258
  indexes.push({
258
- columns: [__spreadProps$4(__spreadValues$4({}, item.data.index), { column: key })],
259
+ columns: [__spreadProps$4(__spreadValues$5({}, item.data.index), { column: key })],
259
260
  options: item.data.index
260
261
  });
261
262
  }
@@ -391,21 +392,21 @@ WHERE
391
392
  return rows;
392
393
  };
393
394
 
394
- var __defProp$3 = Object.defineProperty;
395
+ var __defProp$4 = Object.defineProperty;
395
396
  var __defProps$3 = Object.defineProperties;
396
397
  var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
397
- var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
398
- var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
399
- var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
400
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
401
- var __spreadValues$3 = (a, b) => {
398
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
399
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
400
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
401
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
402
+ var __spreadValues$4 = (a, b) => {
402
403
  for (var prop in b || (b = {}))
403
- if (__hasOwnProp$3.call(b, prop))
404
- __defNormalProp$3(a, prop, b[prop]);
405
- if (__getOwnPropSymbols$3)
406
- for (var prop of __getOwnPropSymbols$3(b)) {
407
- if (__propIsEnum$3.call(b, prop))
408
- __defNormalProp$3(a, prop, b[prop]);
404
+ if (__hasOwnProp$4.call(b, prop))
405
+ __defNormalProp$4(a, prop, b[prop]);
406
+ if (__getOwnPropSymbols$4)
407
+ for (var prop of __getOwnPropSymbols$4(b)) {
408
+ if (__propIsEnum$4.call(b, prop))
409
+ __defNormalProp$4(a, prop, b[prop]);
409
410
  }
410
411
  return a;
411
412
  };
@@ -414,7 +415,6 @@ const types = Object.assign(Object.create(columnTypes), {
414
415
  raw
415
416
  });
416
417
  const createTable = async (migration, up, tableName, options, fn) => {
417
- var _a, _b;
418
418
  const shape = getColumnTypes(types, fn);
419
419
  const tableData = getTableData();
420
420
  const ast = makeAst$1(
@@ -430,7 +430,7 @@ const createTable = async (migration, up, tableName, options, fn) => {
430
430
  for (const query of queries) {
431
431
  await migration.query(query);
432
432
  }
433
- await ((_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
433
+ await runCodeUpdater(migration, ast);
434
434
  };
435
435
  const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
436
436
  const shapePKeys = [];
@@ -440,13 +440,13 @@ const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
440
440
  }
441
441
  }
442
442
  const primaryKey = tableData.primaryKey;
443
- return __spreadProps$3(__spreadValues$3(__spreadProps$3(__spreadValues$3({
443
+ return __spreadProps$3(__spreadValues$4(__spreadProps$3(__spreadValues$4({
444
444
  type: "table",
445
445
  action: up ? "create" : "drop",
446
446
  name: tableName,
447
447
  shape
448
448
  }, tableData), {
449
- primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$3(__spreadValues$3({}, primaryKey), { columns: [...shapePKeys, ...primaryKey.columns] }) : { columns: shapePKeys }
449
+ primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$3(__spreadValues$4({}, primaryKey), { columns: [...shapePKeys, ...primaryKey.columns] }) : { columns: shapePKeys }
450
450
  }), options), {
451
451
  noPrimaryKey: options.noPrimaryKey ? "ignore" : noPrimaryKey || "error"
452
452
  });
@@ -520,21 +520,21 @@ const astToQueries$1 = (ast) => {
520
520
  return result;
521
521
  };
522
522
 
523
- var __defProp$2 = Object.defineProperty;
523
+ var __defProp$3 = Object.defineProperty;
524
524
  var __defProps$2 = Object.defineProperties;
525
525
  var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
526
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
527
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
528
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
529
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
530
- var __spreadValues$2 = (a, b) => {
526
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
527
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
528
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
529
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
530
+ var __spreadValues$3 = (a, b) => {
531
531
  for (var prop in b || (b = {}))
532
- if (__hasOwnProp$2.call(b, prop))
533
- __defNormalProp$2(a, prop, b[prop]);
534
- if (__getOwnPropSymbols$2)
535
- for (var prop of __getOwnPropSymbols$2(b)) {
536
- if (__propIsEnum$2.call(b, prop))
537
- __defNormalProp$2(a, prop, b[prop]);
532
+ if (__hasOwnProp$3.call(b, prop))
533
+ __defNormalProp$3(a, prop, b[prop]);
534
+ if (__getOwnPropSymbols$3)
535
+ for (var prop of __getOwnPropSymbols$3(b)) {
536
+ if (__propIsEnum$3.call(b, prop))
537
+ __defNormalProp$3(a, prop, b[prop]);
538
538
  }
539
539
  return a;
540
540
  };
@@ -554,7 +554,7 @@ const mergeTableData = (a, b) => {
554
554
  } else {
555
555
  a.primaryKey = {
556
556
  columns: [...a.primaryKey.columns, ...b.primaryKey.columns],
557
- options: __spreadValues$2(__spreadValues$2({}, a.primaryKey.options), b.primaryKey.options)
557
+ options: __spreadValues$3(__spreadValues$3({}, a.primaryKey.options), b.primaryKey.options)
558
558
  };
559
559
  }
560
560
  }
@@ -605,7 +605,7 @@ const columnTypeToColumnChange = (item) => {
605
605
  if (foreignKey && "fn" in foreignKey) {
606
606
  throw new Error("Callback in foreignKey is not allowed in migration");
607
607
  }
608
- return __spreadProps$2(__spreadValues$2({
608
+ return __spreadProps$2(__spreadValues$3({
609
609
  column: item,
610
610
  type: item.toSQL(),
611
611
  nullable: item.isNullable,
@@ -621,7 +621,7 @@ const tableChangeMethods = {
621
621
  add,
622
622
  drop,
623
623
  change(from, to, options) {
624
- return __spreadValues$2({
624
+ return __spreadValues$3({
625
625
  type: "change",
626
626
  from: columnTypeToColumnChange(from),
627
627
  to: columnTypeToColumnChange(to)
@@ -652,7 +652,6 @@ const tableChangeMethods = {
652
652
  }
653
653
  };
654
654
  const changeTable = async (migration, up, tableName, options, fn) => {
655
- var _a, _b;
656
655
  resetTableData();
657
656
  resetChangeTableData();
658
657
  const tableChanger = Object.create(columnTypes);
@@ -663,7 +662,7 @@ const changeTable = async (migration, up, tableName, options, fn) => {
663
662
  for (const query of queries) {
664
663
  await migration.query(query);
665
664
  }
666
- await ((_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
665
+ await runCodeUpdater(migration, ast);
667
666
  };
668
667
  const makeAst = (up, name, changeData, changeTableData2, options) => {
669
668
  const { comment } = options;
@@ -672,17 +671,17 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
672
671
  const item = changeData[key];
673
672
  if ("type" in item) {
674
673
  if (up) {
675
- shape[key] = item.type === "change" && item.usingUp ? __spreadProps$2(__spreadValues$2({}, item), { using: item.usingUp }) : item;
674
+ shape[key] = item.type === "change" && item.usingUp ? __spreadProps$2(__spreadValues$3({}, item), { using: item.usingUp }) : item;
676
675
  } else {
677
676
  if (item.type === "rename") {
678
- shape[item.name] = __spreadProps$2(__spreadValues$2({}, item), { name: key });
677
+ shape[item.name] = __spreadProps$2(__spreadValues$3({}, item), { name: key });
679
678
  } else {
680
- shape[key] = item.type === "add" ? __spreadProps$2(__spreadValues$2({}, item), { type: "drop" }) : item.type === "drop" ? __spreadProps$2(__spreadValues$2({}, item), { type: "add" }) : item.type === "change" ? __spreadProps$2(__spreadValues$2({}, item), { from: item.to, to: item.from, using: item.usingDown }) : item;
679
+ shape[key] = item.type === "add" ? __spreadProps$2(__spreadValues$3({}, item), { type: "drop" }) : item.type === "drop" ? __spreadProps$2(__spreadValues$3({}, item), { type: "add" }) : item.type === "change" ? __spreadProps$2(__spreadValues$3({}, item), { from: item.to, to: item.from, using: item.usingDown }) : item;
681
680
  }
682
681
  }
683
682
  }
684
683
  }
685
- return __spreadValues$2({
684
+ return __spreadValues$3({
686
685
  type: "changeTable",
687
686
  name,
688
687
  comment: comment ? up ? Array.isArray(comment) ? comment[1] : comment : Array.isArray(comment) ? comment[0] : null : void 0,
@@ -698,10 +697,10 @@ const astToQueries = (ast) => {
698
697
  values: []
699
698
  });
700
699
  }
701
- const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$2({}, ast.add.primaryKey) : {
700
+ const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$3({}, ast.add.primaryKey) : {
702
701
  columns: []
703
702
  };
704
- const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$2({}, ast.drop.primaryKey) : {
703
+ const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$3({}, ast.drop.primaryKey) : {
705
704
  columns: []
706
705
  };
707
706
  for (const key in ast.shape) {
@@ -798,7 +797,7 @@ const astToQueries = (ast) => {
798
797
  if (fromIndex) {
799
798
  dropIndexes.push({
800
799
  columns: [
801
- __spreadValues$2({
800
+ __spreadValues$3({
802
801
  column: key
803
802
  }, fromIndex)
804
803
  ],
@@ -808,7 +807,7 @@ const astToQueries = (ast) => {
808
807
  if (toIndex) {
809
808
  addIndexes.push({
810
809
  columns: [
811
- __spreadValues$2({
810
+ __spreadValues$3({
812
811
  column: key
813
812
  }, toIndex)
814
813
  ],
@@ -857,21 +856,21 @@ const astToQueries = (ast) => {
857
856
  return result;
858
857
  };
859
858
 
860
- var __defProp$1 = Object.defineProperty;
859
+ var __defProp$2 = Object.defineProperty;
861
860
  var __defProps$1 = Object.defineProperties;
862
861
  var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
863
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
864
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
865
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
866
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
867
- var __spreadValues$1 = (a, b) => {
862
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
863
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
864
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
865
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
866
+ var __spreadValues$2 = (a, b) => {
868
867
  for (var prop in b || (b = {}))
869
- if (__hasOwnProp$1.call(b, prop))
870
- __defNormalProp$1(a, prop, b[prop]);
871
- if (__getOwnPropSymbols$1)
872
- for (var prop of __getOwnPropSymbols$1(b)) {
873
- if (__propIsEnum$1.call(b, prop))
874
- __defNormalProp$1(a, prop, b[prop]);
868
+ if (__hasOwnProp$2.call(b, prop))
869
+ __defNormalProp$2(a, prop, b[prop]);
870
+ if (__getOwnPropSymbols$2)
871
+ for (var prop of __getOwnPropSymbols$2(b)) {
872
+ if (__propIsEnum$2.call(b, prop))
873
+ __defNormalProp$2(a, prop, b[prop]);
875
874
  }
876
875
  return a;
877
876
  };
@@ -882,6 +881,9 @@ class UnknownColumn extends ColumnType {
882
881
  this.dataType = dataType;
883
882
  this.operators = Operators.any;
884
883
  }
884
+ toCode() {
885
+ return "unknown";
886
+ }
885
887
  }
886
888
  const createJoinTable = async (migration, up, tables, options, fn) => {
887
889
  const tableName = options.tableName || joinWords(...tables);
@@ -890,14 +892,14 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
890
892
  migration,
891
893
  up,
892
894
  tableName,
893
- __spreadProps$1(__spreadValues$1({}, options), { noPrimaryKey: true }),
895
+ __spreadProps$1(__spreadValues$2({}, options), { noPrimaryKey: true }),
894
896
  () => ({})
895
897
  );
896
898
  }
897
899
  const tablesWithPrimaryKeys = await Promise.all(
898
900
  tables.map(async (table) => {
899
901
  const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
900
- (items) => items.map((item) => __spreadProps$1(__spreadValues$1({}, item), {
902
+ (items) => items.map((item) => __spreadProps$1(__spreadValues$2({}, item), {
901
903
  joinedName: joinWords(singular(table), item.name)
902
904
  }))
903
905
  );
@@ -939,30 +941,32 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
939
941
  });
940
942
  };
941
943
 
942
- var __defProp = Object.defineProperty;
944
+ var __defProp$1 = Object.defineProperty;
943
945
  var __defProps = Object.defineProperties;
944
946
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
945
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
946
- var __hasOwnProp = Object.prototype.hasOwnProperty;
947
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
948
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
949
- var __spreadValues = (a, b) => {
947
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
948
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
949
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
950
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
951
+ var __spreadValues$1 = (a, b) => {
950
952
  for (var prop in b || (b = {}))
951
- if (__hasOwnProp.call(b, prop))
952
- __defNormalProp(a, prop, b[prop]);
953
- if (__getOwnPropSymbols)
954
- for (var prop of __getOwnPropSymbols(b)) {
955
- if (__propIsEnum.call(b, prop))
956
- __defNormalProp(a, prop, b[prop]);
953
+ if (__hasOwnProp$1.call(b, prop))
954
+ __defNormalProp$1(a, prop, b[prop]);
955
+ if (__getOwnPropSymbols$1)
956
+ for (var prop of __getOwnPropSymbols$1(b)) {
957
+ if (__propIsEnum$1.call(b, prop))
958
+ __defNormalProp$1(a, prop, b[prop]);
957
959
  }
958
960
  return a;
959
961
  };
960
962
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
961
963
  class Migration extends TransactionAdapter {
962
- constructor(tx, up, options) {
964
+ constructor(tx, up, options, adapterOptions, appCodeUpdaterCache) {
963
965
  super(tx, tx.client, tx.types);
964
966
  this.up = up;
965
967
  this.options = options;
968
+ this.adapterOptions = adapterOptions;
969
+ this.appCodeUpdaterCache = appCodeUpdaterCache;
966
970
  this.log = logParamToLogObject(options.logger || console, options.log);
967
971
  }
968
972
  async query(query, types = this.types, log = this.log) {
@@ -996,7 +1000,6 @@ class Migration extends TransactionAdapter {
996
1000
  return changeTable(this, this.up, tableName, options, fn);
997
1001
  }
998
1002
  async renameTable(from, to) {
999
- var _a, _b;
1000
1003
  const ast = {
1001
1004
  type: "renameTable",
1002
1005
  from: this.up ? from : to,
@@ -1005,7 +1008,7 @@ class Migration extends TransactionAdapter {
1005
1008
  await this.query(
1006
1009
  `ALTER TABLE ${quoteTable(ast.from)} RENAME TO "${ast.to}"`
1007
1010
  );
1008
- await ((_b = (_a = this.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
1011
+ await runCodeUpdater(this, ast);
1009
1012
  }
1010
1013
  addColumn(tableName, columnName, fn) {
1011
1014
  return addColumn(this, this.up, tableName, columnName, fn);
@@ -1087,7 +1090,7 @@ const wrapWithLog = async (log, query, fn) => {
1087
1090
  if (!log) {
1088
1091
  return fn();
1089
1092
  } else {
1090
- const sql = typeof query === "string" ? { text: query, values: [] } : query.values ? query : __spreadProps(__spreadValues({}, query), { values: [] });
1093
+ const sql = typeof query === "string" ? { text: query, values: [] } : query.values ? query : __spreadProps(__spreadValues$1({}, query), { values: [] });
1091
1094
  const logData = log.beforeQuery(sql);
1092
1095
  try {
1093
1096
  const result = await fn();
@@ -1105,16 +1108,15 @@ const addColumn = (migration, up, tableName, columnName, fn) => {
1105
1108
  }));
1106
1109
  };
1107
1110
  const addIndex = (migration, up, tableName, columns, options) => {
1108
- return changeTable(migration, up, tableName, {}, (t) => __spreadValues({}, t.add(t.index(columns, options))));
1111
+ return changeTable(migration, up, tableName, {}, (t) => __spreadValues$1({}, t.add(t.index(columns, options))));
1109
1112
  };
1110
1113
  const addForeignKey = (migration, up, tableName, columns, foreignTable, foreignColumns, options) => {
1111
- return changeTable(migration, up, tableName, {}, (t) => __spreadValues({}, t.add(t.foreignKey(columns, foreignTable, foreignColumns, options))));
1114
+ return changeTable(migration, up, tableName, {}, (t) => __spreadValues$1({}, t.add(t.foreignKey(columns, foreignTable, foreignColumns, options))));
1112
1115
  };
1113
1116
  const addPrimaryKey = (migration, up, tableName, columns, options) => {
1114
- return changeTable(migration, up, tableName, {}, (t) => __spreadValues({}, t.add(t.primaryKey(columns, options))));
1117
+ return changeTable(migration, up, tableName, {}, (t) => __spreadValues$1({}, t.add(t.primaryKey(columns, options))));
1115
1118
  };
1116
1119
  const createSchema = async (migration, up, name) => {
1117
- var _a, _b;
1118
1120
  const ast = {
1119
1121
  type: "schema",
1120
1122
  action: up ? "create" : "drop",
@@ -1123,11 +1125,10 @@ const createSchema = async (migration, up, name) => {
1123
1125
  await migration.query(
1124
1126
  `${ast.action === "create" ? "CREATE" : "DROP"} SCHEMA "${name}"`
1125
1127
  );
1126
- await ((_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
1128
+ await runCodeUpdater(migration, ast);
1127
1129
  };
1128
1130
  const createExtension = async (migration, up, name, options) => {
1129
- var _a, _b;
1130
- const ast = __spreadValues({
1131
+ const ast = __spreadValues$1({
1131
1132
  type: "extension",
1132
1133
  action: up ? "create" : "drop",
1133
1134
  name
@@ -1139,17 +1140,54 @@ const createExtension = async (migration, up, name, options) => {
1139
1140
  query = `CREATE EXTENSION${ast.ifExists ? " IF NOT EXISTS" : ""} "${ast.name}"${ast.schema ? ` SCHEMA "${ast.schema}"` : ""}${ast.version ? ` VERSION '${ast.version}'` : ""}${ast.cascade ? " CASCADE" : ""}`;
1140
1141
  }
1141
1142
  await migration.query(query);
1142
- await ((_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
1143
+ await runCodeUpdater(migration, ast);
1143
1144
  };
1144
1145
  const queryExists = (db, sql) => {
1145
1146
  return db.query(sql).then(({ rowCount }) => rowCount > 0);
1146
1147
  };
1148
+ const runCodeUpdater = (migration, ast) => {
1149
+ var _a, _b;
1150
+ return (_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, {
1151
+ ast,
1152
+ options: migration.adapterOptions,
1153
+ cache: migration.appCodeUpdaterCache
1154
+ });
1155
+ };
1147
1156
 
1157
+ var __defProp = Object.defineProperty;
1158
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
1159
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
1160
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
1161
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1162
+ var __spreadValues = (a, b) => {
1163
+ for (var prop in b || (b = {}))
1164
+ if (__hasOwnProp.call(b, prop))
1165
+ __defNormalProp(a, prop, b[prop]);
1166
+ if (__getOwnPropSymbols)
1167
+ for (var prop of __getOwnPropSymbols(b)) {
1168
+ if (__propIsEnum.call(b, prop))
1169
+ __defNormalProp(a, prop, b[prop]);
1170
+ }
1171
+ return a;
1172
+ };
1148
1173
  const migrateOrRollback = async (options, config, args, up) => {
1149
1174
  var _a;
1175
+ config = __spreadValues({}, config);
1150
1176
  const files = await getMigrationFiles(config, up);
1151
- const argCount = args[0] === "all" ? Infinity : parseInt(args[0]);
1152
- let count = isNaN(argCount) ? up ? Infinity : 1 : argCount;
1177
+ let count = up ? Infinity : 1;
1178
+ let argI = 0;
1179
+ const num = args[0] === "all" ? Infinity : parseInt(args[0]);
1180
+ if (!isNaN(num)) {
1181
+ argI++;
1182
+ count = num;
1183
+ }
1184
+ const arg = args[argI];
1185
+ if (arg === "--code") {
1186
+ config.useCodeUpdater = args[argI + 1] !== "false";
1187
+ }
1188
+ if (!config.useCodeUpdater)
1189
+ delete config.appCodeUpdater;
1190
+ const appCodeUpdaterCache = {};
1153
1191
  for (const opts of toArray(options)) {
1154
1192
  const db = new Adapter(opts);
1155
1193
  const migratedVersions = await getMigratedVersionsMap(db, config);
@@ -1160,18 +1198,19 @@ const migrateOrRollback = async (options, config, args, up) => {
1160
1198
  }
1161
1199
  if (count-- <= 0)
1162
1200
  break;
1163
- await processMigration(db, up, file, config);
1201
+ await processMigration(db, up, file, config, opts, appCodeUpdaterCache);
1164
1202
  (_a = config.logger) == null ? void 0 : _a.log(`${file.path} ${up ? "migrated" : "rolled back"}`);
1165
1203
  }
1166
1204
  } finally {
1167
1205
  await db.close();
1168
1206
  }
1207
+ delete config.appCodeUpdater;
1169
1208
  }
1170
1209
  };
1171
1210
  const changeCache = {};
1172
- const processMigration = async (db, up, file, config) => {
1211
+ const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
1173
1212
  await db.transaction(async (tx) => {
1174
- const db2 = new Migration(tx, up, config);
1213
+ const db2 = new Migration(tx, up, config, options, appCodeUpdaterCache);
1175
1214
  setCurrentMigration(db2);
1176
1215
  setCurrentMigrationUp(up);
1177
1216
  const callback = changeCache[file.path];
@@ -1394,22 +1433,35 @@ Commands:
1394
1433
  drop drop databases
1395
1434
  reset drop, create and migrate databases
1396
1435
  g, generate generate migration file, see below
1397
- migrate migrate all pending migrations
1436
+ migrate migrate pending migrations
1398
1437
  rollback rollback the last migrated
1399
1438
  no or unknown command prints this message
1400
1439
 
1440
+ Migrate arguments:
1441
+ no arguments run all pending migrations
1442
+ number run specific number of pending migrations
1443
+
1444
+ Rollback arguments:
1445
+ no arguments rollback one last applied migration
1446
+ number rollback specific number of applied migrations
1447
+ all rollback all applied migrations
1448
+
1449
+ Migrate and rollback common arguments:
1450
+ --code run code updater, overrides \`useCodeUpdater\` option
1451
+ --code false do not run code updater
1452
+
1401
1453
  Generate arguments:
1402
1454
  - (required) first argument is migration name
1403
- * create* template for create table
1404
- * change* template for change table
1405
- * add*To* template for add columns
1406
- * remove*From* template for remove columns
1407
- * drop* template for drop table
1455
+ * create* template for create table
1456
+ * change* template for change table
1457
+ * add*To* template for add columns
1458
+ * remove*From* template for remove columns
1459
+ * drop* template for drop table
1408
1460
 
1409
1461
  - other arguments considered as columns with types and optional methods:
1410
1462
  rake-db g createTable id:serial.primaryKey name:text.nullable
1411
1463
  `
1412
1464
  );
1413
1465
 
1414
- export { Migration, change, createDb, dropDb, generate, migrate, rakeDb, resetDb, rollback };
1466
+ export { Migration, change, createDb, dropDb, generate, migrate, rakeDb, resetDb, rollback, runCodeUpdater };
1415
1467
  //# sourceMappingURL=index.esm.js.map