rake-db 2.2.3 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.2.4
4
+
5
+ ### Patch Changes
6
+
7
+ - Add --code cli argument to rake-db
8
+ - Improve codegen
9
+ - Updated dependencies
10
+ - Updated dependencies
11
+ - pqb@0.8.4
12
+
3
13
  ## 2.2.3
4
14
 
5
15
  ### Patch Changes
package/db.ts CHANGED
@@ -2,6 +2,7 @@ import { config } from 'dotenv';
2
2
  import path from 'path';
3
3
  import { rakeDb } from './src/rakeDb';
4
4
  import { AdapterOptions } from 'pqb';
5
+ import { appCodeUpdater } from '../orm/src';
5
6
 
6
7
  config({ path: path.resolve(process.cwd(), '.env.local') });
7
8
  config();
@@ -22,4 +23,11 @@ if (databaseURLTest) {
22
23
 
23
24
  rakeDb(options, {
24
25
  migrationsPath: path.resolve(process.cwd(), 'migrations'),
26
+ appCodeUpdater: appCodeUpdater({
27
+ tablePath: (tableName) => `app/tables/${tableName}.ts`,
28
+ baseTablePath: 'app/lib/baseTable.ts',
29
+ baseTableName: 'BaseTable',
30
+ mainFilePath: 'app/db.ts',
31
+ }),
32
+ useCodeUpdaterByDefault: false,
25
33
  });
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as pqb from 'pqb';
2
- import { EmptyObject, RawExpression, ColumnType, ColumnTypes, raw, ColumnsShape, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, MaybeArray, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, QueryLogOptions, AdapterOptions } from 'pqb';
2
+ import { EmptyObject, RawExpression, ColumnType, ColumnTypes, raw, ColumnsShape, TransactionAdapter, AdapterOptions, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, MaybeArray, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, QueryLogOptions } from 'pqb';
3
3
 
4
4
  declare function add(item: ColumnType, options?: {
5
5
  dropMode?: DropMode;
@@ -63,8 +63,10 @@ declare type ExtensionOptions = {
63
63
  declare class Migration extends TransactionAdapter {
64
64
  up: boolean;
65
65
  options: RakeDbConfig;
66
+ adapterOptions: AdapterOptions;
67
+ appCodeUpdaterCache: object;
66
68
  log?: QueryLogObject;
67
- constructor(tx: TransactionAdapter, up: boolean, options: RakeDbConfig);
69
+ constructor(tx: TransactionAdapter, up: boolean, options: RakeDbConfig, adapterOptions: AdapterOptions, appCodeUpdaterCache: object);
68
70
  query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers, log?: QueryLogObject | undefined): Promise<QueryResult<T>>;
69
71
  arrays<R extends any[] = any[]>(query: QueryInput, types?: TypeParsers, log?: QueryLogObject | undefined): Promise<QueryArraysResult<R>>;
70
72
  createTable(tableName: string, options: TableOptions, fn: ColumnsShapeCallback): Promise<void>;
@@ -104,6 +106,7 @@ declare class Migration extends TransactionAdapter {
104
106
  columnExists(tableName: string, columnName: string): Promise<boolean>;
105
107
  constraintExists(constraintName: string): Promise<boolean>;
106
108
  }
109
+ declare const runCodeUpdater: (migration: Migration, ast: RakeDbAst) => Promise<void> | undefined;
107
110
 
108
111
  declare type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameTable | RakeDbAst.Schema | RakeDbAst.Extension;
109
112
  declare namespace RakeDbAst {
@@ -185,8 +188,13 @@ declare type RakeDbConfig = {
185
188
  requireTs(path: string): void;
186
189
  noPrimaryKey?: NoPrimaryKeyOption;
187
190
  appCodeUpdater?: AppCodeUpdater;
191
+ useCodeUpdater?: boolean;
188
192
  } & QueryLogOptions;
189
- declare type AppCodeUpdater = (ast: RakeDbAst) => Promise<void>;
193
+ declare type AppCodeUpdater = (params: {
194
+ ast: RakeDbAst;
195
+ options: AdapterOptions;
196
+ cache: object;
197
+ }) => Promise<void>;
190
198
 
191
199
  declare const createDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig) => Promise<void>;
192
200
  declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
@@ -202,4 +210,4 @@ declare const change: (fn: ChangeCallback) => void;
202
210
 
203
211
  declare const rakeDb: (options: MaybeArray<AdapterOptions>, partialConfig?: Partial<RakeDbConfig>, args?: string[]) => Promise<void>;
204
212
 
205
- export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, ExtensionOptions, JoinTableOptions, Migration, MigrationColumnTypes, RakeDbAst, RakeDbConfig, TableOptions, change, createDb, dropDb, generate, migrate, rakeDb, resetDb, rollback };
213
+ export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, ExtensionOptions, JoinTableOptions, Migration, MigrationColumnTypes, RakeDbAst, RakeDbConfig, TableOptions, change, createDb, dropDb, generate, migrate, rakeDb, resetDb, rollback, runCodeUpdater };
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
  };
@@ -893,14 +892,14 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
893
892
  migration,
894
893
  up,
895
894
  tableName,
896
- __spreadProps$1(__spreadValues$1({}, options), { noPrimaryKey: true }),
895
+ __spreadProps$1(__spreadValues$2({}, options), { noPrimaryKey: true }),
897
896
  () => ({})
898
897
  );
899
898
  }
900
899
  const tablesWithPrimaryKeys = await Promise.all(
901
900
  tables.map(async (table) => {
902
901
  const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
903
- (items) => items.map((item) => __spreadProps$1(__spreadValues$1({}, item), {
902
+ (items) => items.map((item) => __spreadProps$1(__spreadValues$2({}, item), {
904
903
  joinedName: joinWords(singular(table), item.name)
905
904
  }))
906
905
  );
@@ -942,30 +941,32 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
942
941
  });
943
942
  };
944
943
 
945
- var __defProp = Object.defineProperty;
944
+ var __defProp$1 = Object.defineProperty;
946
945
  var __defProps = Object.defineProperties;
947
946
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
948
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
949
- var __hasOwnProp = Object.prototype.hasOwnProperty;
950
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
951
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
952
- 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) => {
953
952
  for (var prop in b || (b = {}))
954
- if (__hasOwnProp.call(b, prop))
955
- __defNormalProp(a, prop, b[prop]);
956
- if (__getOwnPropSymbols)
957
- for (var prop of __getOwnPropSymbols(b)) {
958
- if (__propIsEnum.call(b, prop))
959
- __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]);
960
959
  }
961
960
  return a;
962
961
  };
963
962
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
964
963
  class Migration extends TransactionAdapter {
965
- constructor(tx, up, options) {
964
+ constructor(tx, up, options, adapterOptions, appCodeUpdaterCache) {
966
965
  super(tx, tx.client, tx.types);
967
966
  this.up = up;
968
967
  this.options = options;
968
+ this.adapterOptions = adapterOptions;
969
+ this.appCodeUpdaterCache = appCodeUpdaterCache;
969
970
  this.log = logParamToLogObject(options.logger || console, options.log);
970
971
  }
971
972
  async query(query, types = this.types, log = this.log) {
@@ -999,7 +1000,6 @@ class Migration extends TransactionAdapter {
999
1000
  return changeTable(this, this.up, tableName, options, fn);
1000
1001
  }
1001
1002
  async renameTable(from, to) {
1002
- var _a, _b;
1003
1003
  const ast = {
1004
1004
  type: "renameTable",
1005
1005
  from: this.up ? from : to,
@@ -1008,7 +1008,7 @@ class Migration extends TransactionAdapter {
1008
1008
  await this.query(
1009
1009
  `ALTER TABLE ${quoteTable(ast.from)} RENAME TO "${ast.to}"`
1010
1010
  );
1011
- await ((_b = (_a = this.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
1011
+ await runCodeUpdater(this, ast);
1012
1012
  }
1013
1013
  addColumn(tableName, columnName, fn) {
1014
1014
  return addColumn(this, this.up, tableName, columnName, fn);
@@ -1090,7 +1090,7 @@ const wrapWithLog = async (log, query, fn) => {
1090
1090
  if (!log) {
1091
1091
  return fn();
1092
1092
  } else {
1093
- 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: [] });
1094
1094
  const logData = log.beforeQuery(sql);
1095
1095
  try {
1096
1096
  const result = await fn();
@@ -1108,16 +1108,15 @@ const addColumn = (migration, up, tableName, columnName, fn) => {
1108
1108
  }));
1109
1109
  };
1110
1110
  const addIndex = (migration, up, tableName, columns, options) => {
1111
- 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))));
1112
1112
  };
1113
1113
  const addForeignKey = (migration, up, tableName, columns, foreignTable, foreignColumns, options) => {
1114
- 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))));
1115
1115
  };
1116
1116
  const addPrimaryKey = (migration, up, tableName, columns, options) => {
1117
- 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))));
1118
1118
  };
1119
1119
  const createSchema = async (migration, up, name) => {
1120
- var _a, _b;
1121
1120
  const ast = {
1122
1121
  type: "schema",
1123
1122
  action: up ? "create" : "drop",
@@ -1126,11 +1125,10 @@ const createSchema = async (migration, up, name) => {
1126
1125
  await migration.query(
1127
1126
  `${ast.action === "create" ? "CREATE" : "DROP"} SCHEMA "${name}"`
1128
1127
  );
1129
- await ((_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
1128
+ await runCodeUpdater(migration, ast);
1130
1129
  };
1131
1130
  const createExtension = async (migration, up, name, options) => {
1132
- var _a, _b;
1133
- const ast = __spreadValues({
1131
+ const ast = __spreadValues$1({
1134
1132
  type: "extension",
1135
1133
  action: up ? "create" : "drop",
1136
1134
  name
@@ -1142,17 +1140,54 @@ const createExtension = async (migration, up, name, options) => {
1142
1140
  query = `CREATE EXTENSION${ast.ifExists ? " IF NOT EXISTS" : ""} "${ast.name}"${ast.schema ? ` SCHEMA "${ast.schema}"` : ""}${ast.version ? ` VERSION '${ast.version}'` : ""}${ast.cascade ? " CASCADE" : ""}`;
1143
1141
  }
1144
1142
  await migration.query(query);
1145
- await ((_b = (_a = migration.options).appCodeUpdater) == null ? void 0 : _b.call(_a, ast));
1143
+ await runCodeUpdater(migration, ast);
1146
1144
  };
1147
1145
  const queryExists = (db, sql) => {
1148
1146
  return db.query(sql).then(({ rowCount }) => rowCount > 0);
1149
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
+ };
1150
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
+ };
1151
1173
  const migrateOrRollback = async (options, config, args, up) => {
1152
1174
  var _a;
1175
+ config = __spreadValues({}, config);
1153
1176
  const files = await getMigrationFiles(config, up);
1154
- const argCount = args[0] === "all" ? Infinity : parseInt(args[0]);
1155
- 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 = {};
1156
1191
  for (const opts of toArray(options)) {
1157
1192
  const db = new Adapter(opts);
1158
1193
  const migratedVersions = await getMigratedVersionsMap(db, config);
@@ -1163,18 +1198,19 @@ const migrateOrRollback = async (options, config, args, up) => {
1163
1198
  }
1164
1199
  if (count-- <= 0)
1165
1200
  break;
1166
- await processMigration(db, up, file, config);
1201
+ await processMigration(db, up, file, config, opts, appCodeUpdaterCache);
1167
1202
  (_a = config.logger) == null ? void 0 : _a.log(`${file.path} ${up ? "migrated" : "rolled back"}`);
1168
1203
  }
1169
1204
  } finally {
1170
1205
  await db.close();
1171
1206
  }
1207
+ delete config.appCodeUpdater;
1172
1208
  }
1173
1209
  };
1174
1210
  const changeCache = {};
1175
- const processMigration = async (db, up, file, config) => {
1211
+ const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
1176
1212
  await db.transaction(async (tx) => {
1177
- const db2 = new Migration(tx, up, config);
1213
+ const db2 = new Migration(tx, up, config, options, appCodeUpdaterCache);
1178
1214
  setCurrentMigration(db2);
1179
1215
  setCurrentMigrationUp(up);
1180
1216
  const callback = changeCache[file.path];
@@ -1397,22 +1433,35 @@ Commands:
1397
1433
  drop drop databases
1398
1434
  reset drop, create and migrate databases
1399
1435
  g, generate generate migration file, see below
1400
- migrate migrate all pending migrations
1436
+ migrate migrate pending migrations
1401
1437
  rollback rollback the last migrated
1402
1438
  no or unknown command prints this message
1403
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
+
1404
1453
  Generate arguments:
1405
1454
  - (required) first argument is migration name
1406
- * create* template for create table
1407
- * change* template for change table
1408
- * add*To* template for add columns
1409
- * remove*From* template for remove columns
1410
- * 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
1411
1460
 
1412
1461
  - other arguments considered as columns with types and optional methods:
1413
1462
  rake-db g createTable id:serial.primaryKey name:text.nullable
1414
1463
  `
1415
1464
  );
1416
1465
 
1417
- export { Migration, change, createDb, dropDb, generate, migrate, rakeDb, resetDb, rollback };
1466
+ export { Migration, change, createDb, dropDb, generate, migrate, rakeDb, resetDb, rollback, runCodeUpdater };
1418
1467
  //# sourceMappingURL=index.esm.js.map