rake-db 2.3.2 → 2.3.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.js CHANGED
@@ -6,32 +6,31 @@ var pqb = require('pqb');
6
6
  var Enquirer = require('enquirer');
7
7
  var path = require('path');
8
8
  var promises = require('fs/promises');
9
- var pluralize = require('pluralize');
10
9
 
11
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
11
 
13
12
  var Enquirer__default = /*#__PURE__*/_interopDefaultLegacy(Enquirer);
14
13
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
15
14
 
16
- var __defProp$7 = Object.defineProperty;
17
- var __defProps$6 = Object.defineProperties;
18
- var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
19
- var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
20
- var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
21
- var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
22
- var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
23
- var __spreadValues$7 = (a, b) => {
15
+ var __defProp$6 = Object.defineProperty;
16
+ var __defProps$5 = Object.defineProperties;
17
+ var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
18
+ var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
19
+ var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
20
+ var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
21
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
22
+ var __spreadValues$6 = (a, b) => {
24
23
  for (var prop in b || (b = {}))
25
- if (__hasOwnProp$7.call(b, prop))
26
- __defNormalProp$7(a, prop, b[prop]);
27
- if (__getOwnPropSymbols$7)
28
- for (var prop of __getOwnPropSymbols$7(b)) {
29
- if (__propIsEnum$7.call(b, prop))
30
- __defNormalProp$7(a, prop, b[prop]);
24
+ if (__hasOwnProp$6.call(b, prop))
25
+ __defNormalProp$6(a, prop, b[prop]);
26
+ if (__getOwnPropSymbols$6)
27
+ for (var prop of __getOwnPropSymbols$6(b)) {
28
+ if (__propIsEnum$6.call(b, prop))
29
+ __defNormalProp$6(a, prop, b[prop]);
31
30
  }
32
31
  return a;
33
32
  };
34
- var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
33
+ var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
35
34
  const migrationConfigDefaults = {
36
35
  migrationsPath: path__default["default"].resolve("src", "migrations"),
37
36
  migrationsTable: "schemaMigrations",
@@ -41,7 +40,7 @@ const migrationConfigDefaults = {
41
40
  useCodeUpdater: true
42
41
  };
43
42
  const getMigrationConfigWithDefaults = (config) => {
44
- return __spreadValues$7(__spreadValues$7({}, migrationConfigDefaults), config);
43
+ return __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
45
44
  };
46
45
  const getDatabaseAndUserFromOptions = (options) => {
47
46
  if (options.databaseURL) {
@@ -69,9 +68,9 @@ const setAdapterOptions = (options, set) => {
69
68
  if (set.password !== void 0) {
70
69
  url.password = set.password;
71
70
  }
72
- return __spreadProps$6(__spreadValues$7({}, options), { databaseURL: url.toString() });
71
+ return __spreadProps$5(__spreadValues$6({}, options), { databaseURL: url.toString() });
73
72
  } else {
74
- return __spreadValues$7(__spreadValues$7({}, options), set);
73
+ return __spreadValues$6(__spreadValues$6({}, options), set);
75
74
  }
76
75
  };
77
76
  const askAdminCredentials = async () => {
@@ -173,12 +172,6 @@ const getMigrationFiles = async (config, up) => {
173
172
  };
174
173
  const sortAsc = (arr) => arr.sort();
175
174
  const sortDesc = (arr) => arr.sort((a, b) => a > b ? -1 : 1);
176
- const joinWords = (...words) => {
177
- return words.slice(1).reduce(
178
- (acc, word) => acc + word[0].toUpperCase() + word.slice(1),
179
- words[0]
180
- );
181
- };
182
175
  const joinColumns = (columns) => {
183
176
  return columns.map((column) => `"${column}"`).join(", ");
184
177
  };
@@ -218,25 +211,25 @@ const setCurrentMigrationUp = (up) => {
218
211
  const getCurrentPromise = () => currentPromise;
219
212
  const getCurrentChangeCallback = () => currentChangeCallback;
220
213
 
221
- var __defProp$6 = Object.defineProperty;
222
- var __defProps$5 = Object.defineProperties;
223
- var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
224
- var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
225
- var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
226
- var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
227
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
228
- var __spreadValues$6 = (a, b) => {
214
+ var __defProp$5 = Object.defineProperty;
215
+ var __defProps$4 = Object.defineProperties;
216
+ var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
217
+ var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
218
+ var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
219
+ var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
220
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
221
+ var __spreadValues$5 = (a, b) => {
229
222
  for (var prop in b || (b = {}))
230
- if (__hasOwnProp$6.call(b, prop))
231
- __defNormalProp$6(a, prop, b[prop]);
232
- if (__getOwnPropSymbols$6)
233
- for (var prop of __getOwnPropSymbols$6(b)) {
234
- if (__propIsEnum$6.call(b, prop))
235
- __defNormalProp$6(a, prop, b[prop]);
223
+ if (__hasOwnProp$5.call(b, prop))
224
+ __defNormalProp$5(a, prop, b[prop]);
225
+ if (__getOwnPropSymbols$5)
226
+ for (var prop of __getOwnPropSymbols$5(b)) {
227
+ if (__propIsEnum$5.call(b, prop))
228
+ __defNormalProp$5(a, prop, b[prop]);
236
229
  }
237
230
  return a;
238
231
  };
239
- var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
232
+ var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
240
233
  const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
241
234
  const line = [`"${key}" ${item.toSQL()}`];
242
235
  if (item.data.compression) {
@@ -275,7 +268,7 @@ const addColumnIndex = (indexes, key, item) => {
275
268
  if (item.data.indexes) {
276
269
  indexes.push(
277
270
  ...item.data.indexes.map((index) => ({
278
- columns: [__spreadProps$5(__spreadValues$6({}, index), { column: key })],
271
+ columns: [__spreadProps$4(__spreadValues$5({}, index), { column: key })],
279
272
  options: index
280
273
  }))
281
274
  );
@@ -293,8 +286,11 @@ const getForeignKeyTable = (fnOrTable) => {
293
286
  const item = new (fnOrTable())();
294
287
  return [item.schema, item.table];
295
288
  };
289
+ const getForeignKeyName = (table, columns) => {
290
+ return `${table}_${columns.join("_")}_fkey`;
291
+ };
296
292
  const constraintToSql = ({ name }, up, foreignKey) => {
297
- const constraintName = foreignKey.options.name || `${name}_${foreignKey.columns.join("_")}_fkey`;
293
+ const constraintName = foreignKey.options.name || getForeignKeyName(name, foreignKey.columns);
298
294
  if (!up) {
299
295
  const { dropMode } = foreignKey.options;
300
296
  return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
@@ -326,13 +322,17 @@ const referencesToSql = (schema, table, columns, foreignKey) => {
326
322
  }
327
323
  return sql.join(" ");
328
324
  };
325
+ const getIndexName = (table, columns) => {
326
+ return `${table}_${columns.map(
327
+ (it) => {
328
+ var _a;
329
+ return "column" in it ? it.column : ((_a = it.expression.match(/\w+/g)) == null ? void 0 : _a.join("_")) || "expression";
330
+ }
331
+ ).join("_")}_idx`;
332
+ };
329
333
  const indexesToQuery = (up, { schema, name }, indexes) => {
330
334
  return indexes.map(({ columns, options }) => {
331
- const indexName = options.name || joinWords(
332
- name,
333
- ...columns.filter((it) => "column" in it).map((it) => it.column),
334
- "index"
335
- );
335
+ const indexName = options.name || getIndexName(name, columns);
336
336
  if (!up) {
337
337
  return {
338
338
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -399,48 +399,26 @@ const primaryKeyToSql = (primaryKey) => {
399
399
  primaryKey.columns
400
400
  )})`;
401
401
  };
402
- const getPrimaryKeysOfTable = async (db, tableName) => {
403
- const { rows } = await db.query(
404
- {
405
- text: `SELECT
406
- pg_attribute.attname AS name,
407
- format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type
408
- FROM pg_index, pg_class, pg_attribute, pg_namespace
409
- WHERE
410
- pg_class.oid = $1::regclass AND
411
- indrelid = pg_class.oid AND
412
- nspname = 'public' AND
413
- pg_class.relnamespace = pg_namespace.oid AND
414
- pg_attribute.attrelid = pg_class.oid AND
415
- pg_attribute.attnum = any(pg_index.indkey) AND
416
- indisprimary`,
417
- values: [tableName]
418
- },
419
- db.types,
420
- void 0
421
- );
422
- return rows;
423
- };
424
402
 
425
- var __defProp$5 = Object.defineProperty;
426
- var __defProps$4 = Object.defineProperties;
427
- var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
428
- var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
429
- var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
430
- var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
431
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
432
- var __spreadValues$5 = (a, b) => {
403
+ var __defProp$4 = Object.defineProperty;
404
+ var __defProps$3 = Object.defineProperties;
405
+ var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
406
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
407
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
408
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
409
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
410
+ var __spreadValues$4 = (a, b) => {
433
411
  for (var prop in b || (b = {}))
434
- if (__hasOwnProp$5.call(b, prop))
435
- __defNormalProp$5(a, prop, b[prop]);
436
- if (__getOwnPropSymbols$5)
437
- for (var prop of __getOwnPropSymbols$5(b)) {
438
- if (__propIsEnum$5.call(b, prop))
439
- __defNormalProp$5(a, prop, b[prop]);
412
+ if (__hasOwnProp$4.call(b, prop))
413
+ __defNormalProp$4(a, prop, b[prop]);
414
+ if (__getOwnPropSymbols$4)
415
+ for (var prop of __getOwnPropSymbols$4(b)) {
416
+ if (__propIsEnum$4.call(b, prop))
417
+ __defNormalProp$4(a, prop, b[prop]);
440
418
  }
441
419
  return a;
442
420
  };
443
- var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
421
+ var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
444
422
  const types = Object.assign(Object.create(pqb.columnTypes), {
445
423
  raw: pqb.raw
446
424
  });
@@ -471,14 +449,14 @@ const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
471
449
  }
472
450
  const primaryKey = tableData.primaryKey;
473
451
  const [schema, table] = getSchemaAndTableFromName(tableName);
474
- return __spreadProps$4(__spreadValues$5(__spreadProps$4(__spreadValues$5({
452
+ return __spreadProps$3(__spreadValues$4(__spreadProps$3(__spreadValues$4({
475
453
  type: "table",
476
454
  action: up ? "create" : "drop",
477
455
  schema,
478
456
  name: table,
479
457
  shape
480
458
  }, tableData), {
481
- primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$4(__spreadValues$5({}, primaryKey), { columns: [...shapePKeys, ...primaryKey.columns] }) : { columns: shapePKeys }
459
+ primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$3(__spreadValues$4({}, primaryKey), { columns: [...shapePKeys, ...primaryKey.columns] }) : { columns: shapePKeys }
482
460
  }), options), {
483
461
  noPrimaryKey: options.noPrimaryKey ? "ignore" : noPrimaryKey || "error"
484
462
  });
@@ -552,25 +530,25 @@ const astToQueries$1 = (ast) => {
552
530
  return result;
553
531
  };
554
532
 
555
- var __defProp$4 = Object.defineProperty;
556
- var __defProps$3 = Object.defineProperties;
557
- var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
558
- var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
559
- var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
560
- var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
561
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
562
- var __spreadValues$4 = (a, b) => {
533
+ var __defProp$3 = Object.defineProperty;
534
+ var __defProps$2 = Object.defineProperties;
535
+ var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
536
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
537
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
538
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
539
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
540
+ var __spreadValues$3 = (a, b) => {
563
541
  for (var prop in b || (b = {}))
564
- if (__hasOwnProp$4.call(b, prop))
565
- __defNormalProp$4(a, prop, b[prop]);
566
- if (__getOwnPropSymbols$4)
567
- for (var prop of __getOwnPropSymbols$4(b)) {
568
- if (__propIsEnum$4.call(b, prop))
569
- __defNormalProp$4(a, prop, b[prop]);
542
+ if (__hasOwnProp$3.call(b, prop))
543
+ __defNormalProp$3(a, prop, b[prop]);
544
+ if (__getOwnPropSymbols$3)
545
+ for (var prop of __getOwnPropSymbols$3(b)) {
546
+ if (__propIsEnum$3.call(b, prop))
547
+ __defNormalProp$3(a, prop, b[prop]);
570
548
  }
571
549
  return a;
572
550
  };
573
- var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
551
+ var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
574
552
  const newChangeTableData = () => ({
575
553
  add: { indexes: [], foreignKeys: [] },
576
554
  drop: { indexes: [], foreignKeys: [] }
@@ -586,7 +564,7 @@ const mergeTableData = (a, b) => {
586
564
  } else {
587
565
  a.primaryKey = {
588
566
  columns: [...a.primaryKey.columns, ...b.primaryKey.columns],
589
- options: __spreadValues$4(__spreadValues$4({}, a.primaryKey.options), b.primaryKey.options)
567
+ options: __spreadValues$3(__spreadValues$3({}, a.primaryKey.options), b.primaryKey.options)
590
568
  };
591
569
  }
592
570
  }
@@ -637,7 +615,7 @@ const columnTypeToColumnChange = (item) => {
637
615
  if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
638
616
  throw new Error("Callback in foreignKey is not allowed in migration");
639
617
  }
640
- return __spreadProps$3(__spreadValues$4({
618
+ return __spreadProps$2(__spreadValues$3({
641
619
  column: item,
642
620
  type: item.toSQL(),
643
621
  nullable: item.data.isNullable,
@@ -653,7 +631,7 @@ const tableChangeMethods = {
653
631
  add,
654
632
  drop,
655
633
  change(from, to, options) {
656
- return __spreadValues$4({
634
+ return __spreadValues$3({
657
635
  type: "change",
658
636
  from: columnTypeToColumnChange(from),
659
637
  to: columnTypeToColumnChange(to)
@@ -703,18 +681,18 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
703
681
  const item = changeData[key];
704
682
  if ("type" in item) {
705
683
  if (up) {
706
- shape[key] = item.type === "change" && item.usingUp ? __spreadProps$3(__spreadValues$4({}, item), { using: item.usingUp }) : item;
684
+ shape[key] = item.type === "change" && item.usingUp ? __spreadProps$2(__spreadValues$3({}, item), { using: item.usingUp }) : item;
707
685
  } else {
708
686
  if (item.type === "rename") {
709
- shape[item.name] = __spreadProps$3(__spreadValues$4({}, item), { name: key });
687
+ shape[item.name] = __spreadProps$2(__spreadValues$3({}, item), { name: key });
710
688
  } else {
711
- shape[key] = item.type === "add" ? __spreadProps$3(__spreadValues$4({}, item), { type: "drop" }) : item.type === "drop" ? __spreadProps$3(__spreadValues$4({}, item), { type: "add" }) : item.type === "change" ? __spreadProps$3(__spreadValues$4({}, item), { from: item.to, to: item.from, using: item.usingDown }) : item;
689
+ 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;
712
690
  }
713
691
  }
714
692
  }
715
693
  }
716
694
  const [schema, table] = getSchemaAndTableFromName(name);
717
- return __spreadValues$4({
695
+ return __spreadValues$3({
718
696
  type: "changeTable",
719
697
  schema,
720
698
  name: table,
@@ -731,10 +709,10 @@ const astToQueries = (ast) => {
731
709
  values: []
732
710
  });
733
711
  }
734
- const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$4({}, ast.add.primaryKey) : {
712
+ const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$3({}, ast.add.primaryKey) : {
735
713
  columns: []
736
714
  };
737
- const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$4({}, ast.drop.primaryKey) : {
715
+ const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$3({}, ast.drop.primaryKey) : {
738
716
  columns: []
739
717
  };
740
718
  for (const key in ast.shape) {
@@ -842,7 +820,7 @@ const astToQueries = (ast) => {
842
820
  if (fromIndex) {
843
821
  dropIndexes.push({
844
822
  columns: [
845
- __spreadValues$4({
823
+ __spreadValues$3({
846
824
  column: key
847
825
  }, fromIndex)
848
826
  ],
@@ -852,7 +830,7 @@ const astToQueries = (ast) => {
852
830
  if (toIndex) {
853
831
  addIndexes.push({
854
832
  columns: [
855
- __spreadValues$4({
833
+ __spreadValues$3({
856
834
  column: key
857
835
  }, toIndex)
858
836
  ],
@@ -902,98 +880,6 @@ const astToQueries = (ast) => {
902
880
  return result;
903
881
  };
904
882
 
905
- var __defProp$3 = Object.defineProperty;
906
- var __defProps$2 = Object.defineProperties;
907
- var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
908
- var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
909
- var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
910
- var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
911
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
912
- var __spreadValues$3 = (a, b) => {
913
- for (var prop in b || (b = {}))
914
- if (__hasOwnProp$3.call(b, prop))
915
- __defNormalProp$3(a, prop, b[prop]);
916
- if (__getOwnPropSymbols$3)
917
- for (var prop of __getOwnPropSymbols$3(b)) {
918
- if (__propIsEnum$3.call(b, prop))
919
- __defNormalProp$3(a, prop, b[prop]);
920
- }
921
- return a;
922
- };
923
- var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
924
- class UnknownColumn extends pqb.ColumnType {
925
- constructor(dataType) {
926
- super();
927
- this.dataType = dataType;
928
- this.operators = pqb.Operators.any;
929
- }
930
- toCode() {
931
- return "unknown";
932
- }
933
- }
934
- const createJoinTable = async (migration, up, tables, options, fn) => {
935
- const tableName = options.tableName || joinWords(...tables);
936
- if (!up) {
937
- return createTable$1(
938
- migration,
939
- up,
940
- tableName,
941
- __spreadProps$2(__spreadValues$3({}, options), { noPrimaryKey: true }),
942
- () => ({})
943
- );
944
- }
945
- const tablesWithPrimaryKeys = await Promise.all(
946
- tables.map(async (table) => {
947
- const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
948
- (items) => items.map((item) => __spreadProps$2(__spreadValues$3({}, item), {
949
- joinedName: joinWords(pluralize.singular(table), item.name)
950
- }))
951
- );
952
- const [schema, name] = getSchemaAndTableFromName(table);
953
- if (!primaryKeys.length) {
954
- throw new Error(
955
- `Primary key for table ${quoteWithSchema({
956
- schema,
957
- name
958
- })} is not defined`
959
- );
960
- }
961
- return [schema, table, primaryKeys];
962
- })
963
- );
964
- return createTable$1(migration, up, tableName, options, (t) => {
965
- const result = {};
966
- tablesWithPrimaryKeys.forEach(([schema, table, primaryKeys]) => {
967
- if (primaryKeys.length === 1) {
968
- const [{ type, joinedName, name }] = primaryKeys;
969
- const column = new UnknownColumn(type);
970
- result[joinedName] = column.foreignKey(
971
- schema ? `${schema}.${table}` : table,
972
- name
973
- );
974
- return;
975
- }
976
- primaryKeys.forEach(({ joinedName, type }) => {
977
- result[joinedName] = new UnknownColumn(type);
978
- });
979
- t.foreignKey(
980
- primaryKeys.map((key) => key.joinedName),
981
- table,
982
- primaryKeys.map((key) => key.name)
983
- );
984
- });
985
- if (fn) {
986
- Object.assign(result, fn(t));
987
- }
988
- t.primaryKey(
989
- tablesWithPrimaryKeys.flatMap(
990
- ([, , primaryKeys]) => primaryKeys.map((item) => item.joinedName)
991
- )
992
- );
993
- return result;
994
- });
995
- };
996
-
997
883
  var __defProp$2 = Object.defineProperty;
998
884
  var __defProps$1 = Object.defineProperties;
999
885
  var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
@@ -1038,16 +924,6 @@ class Migration extends pqb.TransactionAdapter {
1038
924
  const fn = cb || cbOrOptions;
1039
925
  return createTable$1(this, !this.up, tableName, options, fn);
1040
926
  }
1041
- async createJoinTable(tables, cbOrOptions, cb) {
1042
- const options = typeof cbOrOptions === "function" ? {} : cbOrOptions || {};
1043
- const fn = cb || cbOrOptions;
1044
- return createJoinTable(this, this.up, tables, options, fn);
1045
- }
1046
- async dropJoinTable(tables, cbOrOptions, cb) {
1047
- const options = typeof cbOrOptions === "function" ? {} : cbOrOptions || {};
1048
- const fn = cb || cbOrOptions;
1049
- return createJoinTable(this, !this.up, tables, options, fn);
1050
- }
1051
927
  changeTable(tableName, cbOrOptions, cb) {
1052
928
  const [fn, options] = typeof cbOrOptions === "function" ? [cbOrOptions, {}] : [cb, cbOrOptions];
1053
929
  return changeTable(this, this.up, tableName, options, fn);
@@ -1846,7 +1722,7 @@ const structureToAst = async (db) => {
1846
1722
  collate: options.collate,
1847
1723
  opclass: options.opclass,
1848
1724
  order: options.order,
1849
- name: index.name,
1725
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1850
1726
  using: index.using === "btree" ? void 0 : index.using,
1851
1727
  unique: index.isUnique,
1852
1728
  include: index.include,
@@ -1863,7 +1739,7 @@ const structureToAst = async (db) => {
1863
1739
  foreignKey.foreignTableName,
1864
1740
  foreignKey.foreignColumnNames[0],
1865
1741
  {
1866
- name: foreignKey.name,
1742
+ name: foreignKey.name && foreignKey.name !== getForeignKeyName(name, foreignKey.columnNames) ? foreignKey.name : void 0,
1867
1743
  match: matchMap[foreignKey.match],
1868
1744
  onUpdate: fkeyActionMap[foreignKey.onUpdate],
1869
1745
  onDelete: fkeyActionMap[foreignKey.onDelete]
@@ -1893,7 +1769,7 @@ const structureToAst = async (db) => {
1893
1769
  order: it.order
1894
1770
  })),
1895
1771
  options: {
1896
- name: index.name,
1772
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1897
1773
  using: index.using === "btree" ? void 0 : index.using,
1898
1774
  unique: index.isUnique,
1899
1775
  include: index.include,
@@ -1907,7 +1783,7 @@ const structureToAst = async (db) => {
1907
1783
  fnOrTable: it.foreignTableName,
1908
1784
  foreignColumns: it.foreignColumnNames,
1909
1785
  options: {
1910
- name: it.name,
1786
+ name: it.name && it.name !== getForeignKeyName(name, it.columnNames) ? it.name : void 0,
1911
1787
  match: matchMap[it.match],
1912
1788
  onUpdate: fkeyActionMap[it.onUpdate],
1913
1789
  onDelete: fkeyActionMap[it.onDelete]