rake-db 2.18.0 → 2.19.0

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
@@ -120,6 +120,7 @@ const getColumnName = (item, key, snakeCase) => {
120
120
  return item.data.name || (snakeCase ? orchidCore.toSnakeCase(key) : key);
121
121
  };
122
122
  const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
123
+ var _a, _b;
123
124
  const line = [`"${name}" ${columnTypeToSql(item)}`];
124
125
  if (item.data.compression) {
125
126
  line.push(`COMPRESSION ${item.data.compression}`);
@@ -151,17 +152,14 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
151
152
  const { foreignKeys } = item.data;
152
153
  if (foreignKeys) {
153
154
  for (const foreignKey of foreignKeys) {
154
- if (foreignKey.name) {
155
- line.push(`CONSTRAINT "${foreignKey.name}"`);
155
+ if ((_a = foreignKey.options) == null ? void 0 : _a.name) {
156
+ line.push(`CONSTRAINT "${(_b = foreignKey.options) == null ? void 0 : _b.name}"`);
156
157
  }
157
158
  line.push(
158
159
  referencesToSql(
159
- {
160
- columns: foreignKey.columns,
161
- fnOrTable: "fn" in foreignKey ? foreignKey.fn : foreignKey.table,
162
- foreignColumns: foreignKey.columns,
163
- options: foreignKey
164
- },
160
+ __spreadValues$9({
161
+ columns: [name]
162
+ }, foreignKey),
165
163
  snakeCase
166
164
  )
167
165
  );
@@ -208,10 +206,9 @@ const sequenceOptionsToSql = (item) => {
208
206
  const addColumnIndex = (indexes, name, item) => {
209
207
  if (item.data.indexes) {
210
208
  indexes.push(
211
- ...item.data.indexes.map((index) => ({
212
- columns: [__spreadProps$7(__spreadValues$9({}, index), { column: name })],
213
- options: index
214
- }))
209
+ ...item.data.indexes.map((index) => __spreadValues$9({
210
+ columns: [__spreadProps$7(__spreadValues$9({}, index.options), { column: name })]
211
+ }, index))
215
212
  );
216
213
  }
217
214
  };
@@ -282,9 +279,9 @@ const referencesToSql = (references, snakeCase) => {
282
279
  const getIndexName = (table, columns) => {
283
280
  return `${table}_${columns.map((it) => "column" in it ? it.column : "expression").join("_")}_idx`;
284
281
  };
285
- const indexesToQuery = (up, { schema, name }, indexes, language) => {
286
- return indexes.map(({ columns, options }) => {
287
- const indexName = options.name || getIndexName(name, columns);
282
+ const indexesToQuery = (up, { schema, name: tableName }, indexes, language) => {
283
+ return indexes.map(({ columns, options, name }) => {
284
+ const indexName = name || getIndexName(tableName, columns);
288
285
  if (!up) {
289
286
  return {
290
287
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -296,7 +293,7 @@ const indexesToQuery = (up, { schema, name }, indexes, language) => {
296
293
  if (options.unique) {
297
294
  sql.push("UNIQUE");
298
295
  }
299
- sql.push(`INDEX "${indexName}" ON ${quoteTable(schema, name)}`);
296
+ sql.push(`INDEX "${indexName}" ON ${quoteTable(schema, tableName)}`);
300
297
  const u = options.using || options.tsVector && "GIN";
301
298
  if (u) {
302
299
  sql.push(`USING ${u}`);
@@ -367,11 +364,7 @@ const commentsToQuery = (schemaTable, comments) => {
367
364
  }));
368
365
  };
369
366
  const primaryKeyToSql = (primaryKey) => {
370
- var _a;
371
- const name = (_a = primaryKey.options) == null ? void 0 : _a.name;
372
- return `${name ? `CONSTRAINT "${name}" ` : ""}PRIMARY KEY (${joinColumns(
373
- primaryKey.columns
374
- )})`;
367
+ return `${primaryKey.name ? `CONSTRAINT "${primaryKey.name}" ` : ""}PRIMARY KEY (${joinColumns(primaryKey.columns)})`;
375
368
  };
376
369
 
377
370
  const tableMethods = {
@@ -409,7 +402,7 @@ var __spreadValues$8 = (a, b) => {
409
402
  return a;
410
403
  };
411
404
  var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
412
- var __objRest = (source, exclude) => {
405
+ var __objRest$1 = (source, exclude) => {
413
406
  var target = {};
414
407
  for (var prop in source)
415
408
  if (__hasOwnProp$8.call(source, prop) && exclude.indexOf(prop) < 0)
@@ -421,8 +414,20 @@ var __objRest = (source, exclude) => {
421
414
  }
422
415
  return target;
423
416
  };
424
- const createTable = async (migration, up, tableName, options, fn) => {
417
+ const createTable = async (migration, up, tableName, first, second, third) => {
425
418
  var _a;
419
+ let options;
420
+ let fn;
421
+ let dataFn;
422
+ if (typeof first === "object") {
423
+ options = first;
424
+ fn = second;
425
+ dataFn = third;
426
+ } else {
427
+ options = orchidCore.emptyObject;
428
+ fn = first;
429
+ dataFn = second;
430
+ }
426
431
  const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
427
432
  const language = "language" in options ? options.language : migration.options.language;
428
433
  const types = Object.assign(
@@ -439,7 +444,7 @@ const createTable = async (migration, up, tableName, options, fn) => {
439
444
  (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL,
440
445
  language
441
446
  );
442
- tableData = pqb.getTableData();
447
+ tableData = pqb.parseTableData(dataFn);
443
448
  } else {
444
449
  shape = tableData = orchidCore.emptyObject;
445
450
  }
@@ -454,17 +459,22 @@ const createTable = async (migration, up, tableName, options, fn) => {
454
459
  fn && validatePrimaryKey(ast);
455
460
  const queries = astToQueries$1(ast, snakeCase, language);
456
461
  for (const _b of queries) {
457
- const _c = _b, { then } = _c, query = __objRest(_c, ["then"]);
462
+ const _c = _b, { then } = _c, query = __objRest$1(_c, ["then"]);
458
463
  const result = await migration.adapter.arrays(query);
459
464
  then == null ? void 0 : then(result);
460
465
  }
461
466
  let table;
462
467
  return {
463
468
  get table() {
464
- return table != null ? table : table = migration(tableName, shape, {
465
- noPrimaryKey: options.noPrimaryKey ? "ignore" : void 0,
466
- snakeCase: options.snakeCase
467
- });
469
+ return table != null ? table : table = migration(
470
+ tableName,
471
+ shape,
472
+ void 0,
473
+ {
474
+ noPrimaryKey: options.noPrimaryKey ? "ignore" : void 0,
475
+ snakeCase: options.snakeCase
476
+ }
477
+ );
468
478
  }
469
479
  };
470
480
  };
@@ -549,7 +559,7 @@ const astToQueries$1 = (ast, snakeCase, language) => {
549
559
  lines.push(
550
560
  `
551
561
  ${primaryKeyToSql({
552
- options: ast.primaryKey.options,
562
+ name: ast.primaryKey.name,
553
563
  columns: ast.primaryKey.columns.map(
554
564
  (key) => getColumnName(shape[key], key, snakeCase)
555
565
  )
@@ -625,20 +635,6 @@ let changeTableData = newChangeTableData();
625
635
  const resetChangeTableData = () => {
626
636
  changeTableData = newChangeTableData();
627
637
  };
628
- const mergeTableData = (a, b) => {
629
- if (b.primaryKey) {
630
- if (!a.primaryKey) {
631
- a.primaryKey = b.primaryKey;
632
- } else {
633
- a.primaryKey = {
634
- columns: [...a.primaryKey.columns, ...b.primaryKey.columns],
635
- options: __spreadValues$7(__spreadValues$7({}, a.primaryKey.options), b.primaryKey.options)
636
- };
637
- }
638
- }
639
- a.indexes = [...a.indexes || [], ...b.indexes || []];
640
- a.constraints = [...a.constraints || [], ...b.constraints || []];
641
- };
642
638
  const addOrDropChanges = [];
643
639
  function add(item, options) {
644
640
  if (item instanceof pqb.ColumnType) {
@@ -647,21 +643,23 @@ function add(item, options) {
647
643
  return result;
648
644
  addOrDropChanges.push(result);
649
645
  return addOrDropChanges.length - 1;
650
- } else if (item === orchidCore.emptyObject) {
651
- mergeTableData(changeTableData.add, pqb.getTableData());
652
- pqb.resetTableData();
653
- return orchidCore.emptyObject;
654
- } else {
655
- const result = {};
656
- for (const key in item) {
657
- result[key] = {
658
- type: "add",
659
- item: item[key],
660
- dropMode: options == null ? void 0 : options.dropMode
661
- };
646
+ }
647
+ for (const key in item) {
648
+ if (item[key] instanceof orchidCore.ColumnTypeBase) {
649
+ const result = {};
650
+ for (const key2 in item) {
651
+ result[key2] = {
652
+ type: "add",
653
+ item: item[key2],
654
+ dropMode: options == null ? void 0 : options.dropMode
655
+ };
656
+ }
657
+ return result;
662
658
  }
663
- return result;
659
+ pqb.parseTableDataInput(changeTableData.add, item);
660
+ break;
664
661
  }
662
+ return void 0;
665
663
  }
666
664
  const drop = function(item, options) {
667
665
  if (item instanceof pqb.ColumnType) {
@@ -670,21 +668,23 @@ const drop = function(item, options) {
670
668
  return result;
671
669
  addOrDropChanges.push(result);
672
670
  return addOrDropChanges.length - 1;
673
- } else if (item === orchidCore.emptyObject) {
674
- mergeTableData(changeTableData.drop, pqb.getTableData());
675
- pqb.resetTableData();
676
- return orchidCore.emptyObject;
677
- } else {
678
- const result = {};
679
- for (const key in item) {
680
- result[key] = {
681
- type: "drop",
682
- item: item[key],
683
- dropMode: options == null ? void 0 : options.dropMode
684
- };
671
+ }
672
+ for (const key in item) {
673
+ if (item[key] instanceof orchidCore.ColumnTypeBase) {
674
+ const result = {};
675
+ for (const key2 in item) {
676
+ result[key2] = {
677
+ type: "drop",
678
+ item: item[key2],
679
+ dropMode: options == null ? void 0 : options.dropMode
680
+ };
681
+ }
682
+ return result;
685
683
  }
686
- return result;
684
+ pqb.parseTableDataInput(changeTableData.drop, item);
685
+ break;
687
686
  }
687
+ return void 0;
688
688
  };
689
689
  const addOrDrop = (type, item, options) => {
690
690
  const name = orchidCore.consumeColumnName();
@@ -734,7 +734,7 @@ const columnTypeToColumnChange = (item) => {
734
734
  return item.to;
735
735
  };
736
736
  const nameKey = Symbol("name");
737
- const tableChangeMethods = __spreadProps$5(__spreadValues$7({}, tableMethods), {
737
+ const tableChangeMethods = __spreadProps$5(__spreadValues$7(__spreadValues$7({}, tableMethods), pqb.tableDataMethods), {
738
738
  name(name) {
739
739
  orchidCore.setCurrentColumnName(name);
740
740
  const types = Object.create(this);
@@ -781,7 +781,6 @@ const changeTable = async (migration, up, tableName, options, fn) => {
781
781
  const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
782
782
  const language = "language" in options ? options.language : migration.options.language;
783
783
  orchidCore.setDefaultLanguage(language);
784
- pqb.resetTableData();
785
784
  resetChangeTableData();
786
785
  const tableChanger = Object.create(
787
786
  migration.columnTypes
@@ -852,7 +851,6 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
852
851
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
853
852
  };
854
853
  const astToQueries = (ast, snakeCase, language) => {
855
- var _a;
856
854
  const queries = [];
857
855
  if (ast.comment !== void 0) {
858
856
  queries.push({
@@ -890,11 +888,11 @@ const astToQueries = (ast, snakeCase, language) => {
890
888
  }
891
889
  }
892
890
  if (ast.add.primaryKey) {
893
- addPrimaryKeys.options = ast.add.primaryKey.options;
891
+ addPrimaryKeys.name = ast.add.primaryKey.name;
894
892
  addPrimaryKeys.columns.push(...ast.add.primaryKey.columns);
895
893
  }
896
894
  if (ast.drop.primaryKey) {
897
- dropPrimaryKeys.options = ast.drop.primaryKey.options;
895
+ dropPrimaryKeys.name = ast.drop.primaryKey.name;
898
896
  dropPrimaryKeys.columns.push(...ast.drop.primaryKey.columns);
899
897
  }
900
898
  const alterTable = [];
@@ -951,7 +949,7 @@ const astToQueries = (ast, snakeCase, language) => {
951
949
  }
952
950
  const prependAlterTable = [];
953
951
  if (ast.drop.primaryKey || dropPrimaryKeys.change || dropPrimaryKeys.columns.length > 1) {
954
- const name = ((_a = dropPrimaryKeys.options) == null ? void 0 : _a.name) || `${ast.name}_pkey`;
952
+ const name = dropPrimaryKeys.name || `${ast.name}_pkey`;
955
953
  prependAlterTable.push(`DROP CONSTRAINT "${name}"`);
956
954
  }
957
955
  prependAlterTable.push(
@@ -965,7 +963,7 @@ const astToQueries = (ast, snakeCase, language) => {
965
963
  alterTable.push(
966
964
  `ADD ${primaryKeyToSql(
967
965
  snakeCase ? {
968
- options: addPrimaryKeys.options,
966
+ name: addPrimaryKeys.name,
969
967
  columns: addPrimaryKeys.columns.map(orchidCore.toSnakeCase)
970
968
  } : addPrimaryKeys
971
969
  )}`
@@ -1031,7 +1029,7 @@ const handlePrerequisitesForTableItem = (key, item, queries, addPrimaryKeys, dro
1031
1029
  }
1032
1030
  };
1033
1031
  const handleTableItemChange = (key, item, ast, alterTable, renameItems, values, addPrimaryKeys, addIndexes, dropIndexes, addConstraints, dropConstraints, comments, snakeCase) => {
1034
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1032
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
1035
1033
  if (item.type === "add") {
1036
1034
  const column = item.item;
1037
1035
  const name = getColumnName(column, key, snakeCase);
@@ -1115,60 +1113,56 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
1115
1113
  for (let i = 0; i < foreignKeysLen; i++) {
1116
1114
  const fromFkey = (_d = from.foreignKeys) == null ? void 0 : _d[i];
1117
1115
  const toFkey = (_e = to.foreignKeys) == null ? void 0 : _e[i];
1118
- if ((fromFkey || toFkey) && (!fromFkey || !toFkey || fromFkey.name !== toFkey.name || fromFkey.match !== toFkey.match || fromFkey.onUpdate !== toFkey.onUpdate || fromFkey.onDelete !== toFkey.onDelete || fromFkey.dropMode !== toFkey.dropMode || fromFkey.table !== toFkey.table || fromFkey.columns.join(",") !== toFkey.columns.join(","))) {
1116
+ if ((fromFkey || toFkey) && (!fromFkey || !toFkey || ((_f = fromFkey.options) == null ? void 0 : _f.name) !== ((_g = toFkey.options) == null ? void 0 : _g.name) || ((_h = fromFkey.options) == null ? void 0 : _h.match) !== ((_i = toFkey.options) == null ? void 0 : _i.match) || ((_j = fromFkey.options) == null ? void 0 : _j.onUpdate) !== ((_k = toFkey.options) == null ? void 0 : _k.onUpdate) || ((_l = fromFkey.options) == null ? void 0 : _l.onDelete) !== ((_m = toFkey.options) == null ? void 0 : _m.onDelete) || ((_n = fromFkey.options) == null ? void 0 : _n.dropMode) !== ((_o = toFkey.options) == null ? void 0 : _o.dropMode) || fromFkey.fnOrTable !== toFkey.fnOrTable)) {
1119
1117
  if (fromFkey) {
1120
1118
  dropConstraints.push({
1121
- name: fromFkey.name,
1122
- dropMode: fromFkey.dropMode,
1123
- references: {
1124
- columns: [name],
1125
- fnOrTable: fromFkey.table,
1126
- foreignColumns: snakeCase ? fromFkey.columns.map(orchidCore.toSnakeCase) : fromFkey.columns,
1127
- options: fromFkey
1128
- }
1119
+ name: (_p = fromFkey.options) == null ? void 0 : _p.name,
1120
+ dropMode: (_q = fromFkey.options) == null ? void 0 : _q.dropMode,
1121
+ references: __spreadProps$5(__spreadValues$7({
1122
+ columns: [name]
1123
+ }, fromFkey), {
1124
+ foreignColumns: snakeCase ? fromFkey.foreignColumns.map(orchidCore.toSnakeCase) : fromFkey.foreignColumns
1125
+ })
1129
1126
  });
1130
1127
  }
1131
1128
  if (toFkey) {
1132
1129
  addConstraints.push({
1133
- name: toFkey.name,
1134
- dropMode: toFkey.dropMode,
1135
- references: {
1136
- columns: [name],
1137
- fnOrTable: toFkey.table,
1138
- foreignColumns: snakeCase ? toFkey.columns.map(orchidCore.toSnakeCase) : toFkey.columns,
1139
- options: toFkey
1140
- }
1130
+ name: (_r = toFkey.options) == null ? void 0 : _r.name,
1131
+ dropMode: (_s = toFkey.options) == null ? void 0 : _s.dropMode,
1132
+ references: __spreadProps$5(__spreadValues$7({
1133
+ columns: [name]
1134
+ }, toFkey), {
1135
+ foreignColumns: snakeCase ? toFkey.foreignColumns.map(orchidCore.toSnakeCase) : toFkey.foreignColumns
1136
+ })
1141
1137
  });
1142
1138
  }
1143
1139
  }
1144
1140
  }
1145
1141
  const indexesLen = Math.max(
1146
- ((_f = from.indexes) == null ? void 0 : _f.length) || 0,
1147
- ((_g = to.indexes) == null ? void 0 : _g.length) || 0
1142
+ ((_t = from.indexes) == null ? void 0 : _t.length) || 0,
1143
+ ((_u = to.indexes) == null ? void 0 : _u.length) || 0
1148
1144
  );
1149
1145
  for (let i = 0; i < indexesLen; i++) {
1150
- const fromIndex = (_h = from.indexes) == null ? void 0 : _h[i];
1151
- const toIndex = (_i = to.indexes) == null ? void 0 : _i[i];
1152
- if ((fromIndex || toIndex) && (!fromIndex || !toIndex || fromIndex.collate !== toIndex.collate || fromIndex.opclass !== toIndex.opclass || fromIndex.order !== toIndex.order || fromIndex.name !== toIndex.name || fromIndex.unique !== toIndex.unique || fromIndex.using !== toIndex.using || fromIndex.include !== toIndex.include || Array.isArray(fromIndex.include) && Array.isArray(toIndex.include) && fromIndex.include.join(",") !== toIndex.include.join(",") || fromIndex.with !== toIndex.with || fromIndex.tablespace !== toIndex.tablespace || fromIndex.where !== toIndex.where || fromIndex.dropMode !== toIndex.dropMode)) {
1146
+ const fromIndex = (_v = from.indexes) == null ? void 0 : _v[i];
1147
+ const toIndex = (_w = to.indexes) == null ? void 0 : _w[i];
1148
+ if ((fromIndex || toIndex) && (!fromIndex || !toIndex || fromIndex.options.collate !== toIndex.options.collate || fromIndex.options.opclass !== toIndex.options.opclass || fromIndex.options.order !== toIndex.options.order || fromIndex.name !== toIndex.name || fromIndex.options.unique !== toIndex.options.unique || fromIndex.options.using !== toIndex.options.using || fromIndex.options.include !== toIndex.options.include || Array.isArray(fromIndex.options.include) && Array.isArray(toIndex.options.include) && fromIndex.options.include.join(",") !== toIndex.options.include.join(",") || fromIndex.options.with !== toIndex.options.with || fromIndex.options.tablespace !== toIndex.options.tablespace || fromIndex.options.where !== toIndex.options.where || fromIndex.options.dropMode !== toIndex.options.dropMode)) {
1153
1149
  if (fromIndex) {
1154
- dropIndexes.push({
1150
+ dropIndexes.push(__spreadProps$5(__spreadValues$7({}, fromIndex), {
1155
1151
  columns: [
1156
1152
  __spreadValues$7({
1157
1153
  column: name
1158
- }, fromIndex)
1159
- ],
1160
- options: fromIndex
1161
- });
1154
+ }, fromIndex.options)
1155
+ ]
1156
+ }));
1162
1157
  }
1163
1158
  if (toIndex) {
1164
- addIndexes.push({
1159
+ addIndexes.push(__spreadProps$5(__spreadValues$7({}, toIndex), {
1165
1160
  columns: [
1166
1161
  __spreadValues$7({
1167
1162
  column: name
1168
- }, toIndex)
1169
- ],
1170
- options: toIndex
1171
- });
1163
+ }, toIndex.options)
1164
+ ]
1165
+ }));
1172
1166
  }
1173
1167
  }
1174
1168
  }
@@ -1189,8 +1183,7 @@ const renameColumnSql = (from, to, snakeCase) => {
1189
1183
  return `RENAME COLUMN "${snakeCase ? orchidCore.toSnakeCase(from) : from}" TO "${snakeCase ? orchidCore.toSnakeCase(to) : to}"`;
1190
1184
  };
1191
1185
  const mapIndexesForSnakeCase = (indexes, snakeCase) => {
1192
- return (indexes == null ? void 0 : indexes.map((index) => ({
1193
- options: index.options,
1186
+ return (indexes == null ? void 0 : indexes.map((index) => __spreadProps$5(__spreadValues$7({}, index), {
1194
1187
  columns: snakeCase ? index.columns.map(
1195
1188
  (item) => "column" in item ? __spreadProps$5(__spreadValues$7({}, item), { column: orchidCore.toSnakeCase(item.column) }) : item
1196
1189
  ) : index.columns
@@ -1318,15 +1311,11 @@ const createMigrationInterface = (tx, up, config) => {
1318
1311
  });
1319
1312
  };
1320
1313
  class Migration {
1321
- createTable(tableName, cbOrOptions, cb) {
1322
- const options = !cbOrOptions || typeof cbOrOptions === "function" ? {} : cbOrOptions;
1323
- const fn = typeof cbOrOptions === "function" ? cbOrOptions : cb;
1324
- return createTable(this, this.up, tableName, options, fn);
1314
+ createTable(tableName, first, second, third) {
1315
+ return createTable(this, this.up, tableName, first, second, third);
1325
1316
  }
1326
- dropTable(tableName, cbOrOptions, cb) {
1327
- const options = !cbOrOptions || typeof cbOrOptions === "function" ? {} : cbOrOptions;
1328
- const fn = typeof cbOrOptions === "function" ? cbOrOptions : cb;
1329
- return createTable(this, !this.up, tableName, options, fn);
1317
+ dropTable(tableName, first, second, third) {
1318
+ return createTable(this, !this.up, tableName, first, second, third);
1330
1319
  }
1331
1320
  changeTable(tableName, cbOrOptions, cb) {
1332
1321
  const [fn, options] = typeof cbOrOptions === "function" ? [cbOrOptions, {}] : [cb, cbOrOptions];
@@ -1400,7 +1389,7 @@ class Migration {
1400
1389
  return addColumn(this, this.up, tableName, columnName, fn);
1401
1390
  }
1402
1391
  /**
1403
- * Drop the schema, create it on rollback. See {@link addIndex}.
1392
+ * Drop the schema, create it on rollback. See {@link addColumn}.
1404
1393
  *
1405
1394
  * @param tableName - name of the table to add the column to
1406
1395
  * @param columnName - name of the column to add
@@ -1432,20 +1421,20 @@ class Migration {
1432
1421
  *
1433
1422
  * @param tableName - name of the table to add the index for
1434
1423
  * @param columns - indexed columns
1435
- * @param options - index options
1424
+ * @param args - index options, or an index name and then options
1436
1425
  */
1437
- addIndex(tableName, columns, options) {
1438
- return addIndex(this, this.up, tableName, columns, options);
1426
+ addIndex(tableName, columns, ...args) {
1427
+ return addIndex(this, this.up, tableName, columns, args);
1439
1428
  }
1440
1429
  /**
1441
1430
  * Drop the schema, create it on rollback. See {@link addIndex}.
1442
1431
  *
1443
1432
  * @param tableName - name of the table to add the index for
1444
1433
  * @param columns - indexed columns
1445
- * @param options - index options
1434
+ * @param args - index options, or an index name and then options
1446
1435
  */
1447
- dropIndex(tableName, columns, options) {
1448
- return addIndex(this, !this.up, tableName, columns, options);
1436
+ dropIndex(tableName, columns, ...args) {
1437
+ return addIndex(this, !this.up, tableName, columns, args);
1449
1438
  }
1450
1439
  /**
1451
1440
  * Rename index:
@@ -1560,20 +1549,20 @@ class Migration {
1560
1549
  *
1561
1550
  * @param tableName - name of the table
1562
1551
  * @param columns - array of the columns
1563
- * @param options - object with a constraint name
1552
+ * @param name - optionally, set a primary key constraint name
1564
1553
  */
1565
- addPrimaryKey(tableName, columns, options) {
1566
- return addPrimaryKey(this, this.up, tableName, columns, options);
1554
+ addPrimaryKey(tableName, columns, name) {
1555
+ return addPrimaryKey(this, this.up, tableName, columns, name);
1567
1556
  }
1568
1557
  /**
1569
1558
  * Drop the schema, create it on rollback. See {@link addPrimaryKey}.
1570
1559
  *
1571
1560
  * @param tableName - name of the table
1572
1561
  * @param columns - array of the columns
1573
- * @param options - object with a constraint name
1562
+ * @param name - optionally, set a primary key constraint name
1574
1563
  */
1575
- dropPrimaryKey(tableName, columns, options) {
1576
- return addPrimaryKey(this, !this.up, tableName, columns, options);
1564
+ dropPrimaryKey(tableName, columns, name) {
1565
+ return addPrimaryKey(this, !this.up, tableName, columns, name);
1577
1566
  }
1578
1567
  /**
1579
1568
  * Add or drop a check for multiple columns.
@@ -1593,7 +1582,7 @@ class Migration {
1593
1582
  return addCheck(this, this.up, tableName, check);
1594
1583
  }
1595
1584
  /**
1596
- * Drop the schema, create it on rollback. See {@link addConstraint}.
1585
+ * Drop the schema, create it on rollback. See {@link addCheck}.
1597
1586
  *
1598
1587
  * @param tableName - name of the table to add the check into
1599
1588
  * @param check - raw SQL for the check
@@ -1602,39 +1591,7 @@ class Migration {
1602
1591
  return addCheck(this, !this.up, tableName, check);
1603
1592
  }
1604
1593
  /**
1605
- * Add or drop a constraint with check and a foreign key references.
1606
- *
1607
- * See foreign key details in [foreign key](/guide/migration-column-methods.html#composite-foreign-key).
1608
- *
1609
- * ```ts
1610
- * import { change } from '../dbScript';
1611
- *
1612
- * change(async (db) => {
1613
- * await db.addConstraint('tableName', {
1614
- * name: 'constraintName',
1615
- * check: db.sql`column > 123`,
1616
- * references: [['id', 'name'], 'otherTable', ['otherId', 'otherName']],
1617
- * });
1618
- * });
1619
- * ```
1620
- *
1621
- * @param tableName - name of the table to add the constraint to
1622
- * @param constraint - constraint config object
1623
- */
1624
- addConstraint(tableName, constraint) {
1625
- return addConstraint(this, this.up, tableName, constraint);
1626
- }
1627
- /**
1628
- * Drop the schema, create it on rollback. See {@link addConstraint}.
1629
- *
1630
- * @param tableName - name of the table to add the constraint to
1631
- * @param constraint - constraint config object
1632
- */
1633
- dropConstraint(tableName, constraint) {
1634
- return addConstraint(this, !this.up, tableName, constraint);
1635
- }
1636
- /**
1637
- * Rename a table constraint, such as primary key, or check.
1594
+ * Rename a table constraint such as a primary key or a database check.
1638
1595
  *
1639
1596
  * ```ts
1640
1597
  * import { change } from '../dbScript';
@@ -2196,21 +2153,18 @@ const addColumn = (migration, up, tableName, columnName, fn) => {
2196
2153
  [columnName]: t.add(fn(t))
2197
2154
  }));
2198
2155
  };
2199
- const addIndex = (migration, up, tableName, columns, options) => {
2200
- return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.index(columns, options))));
2156
+ const addIndex = (migration, up, tableName, columns, args) => {
2157
+ return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.index(columns, ...args))));
2201
2158
  };
2202
2159
  const addForeignKey = (migration, up, tableName, columns, foreignTable, foreignColumns, options) => {
2203
2160
  return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.foreignKey(columns, foreignTable, foreignColumns, options))));
2204
2161
  };
2205
- const addPrimaryKey = (migration, up, tableName, columns, options) => {
2206
- return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.primaryKey(columns, options))));
2162
+ const addPrimaryKey = (migration, up, tableName, columns, name) => {
2163
+ return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.primaryKey(columns, name))));
2207
2164
  };
2208
2165
  const addCheck = (migration, up, tableName, check) => {
2209
2166
  return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.check(check))));
2210
2167
  };
2211
- const addConstraint = (migration, up, tableName, constraint) => {
2212
- return changeTable(migration, up, tableName, {}, (t) => __spreadValues$6({}, t.add(t.constraint(constraint))));
2213
- };
2214
2168
  const createSchema = async (migration, up, name) => {
2215
2169
  const ast = {
2216
2170
  type: "schema",
@@ -4055,6 +4009,18 @@ var __spreadValues$2 = (a, b) => {
4055
4009
  return a;
4056
4010
  };
4057
4011
  var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
4012
+ var __objRest = (source, exclude) => {
4013
+ var target = {};
4014
+ for (var prop in source)
4015
+ if (__hasOwnProp$2.call(source, prop) && exclude.indexOf(prop) < 0)
4016
+ target[prop] = source[prop];
4017
+ if (source != null && __getOwnPropSymbols$2)
4018
+ for (var prop of __getOwnPropSymbols$2(source)) {
4019
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$2.call(source, prop))
4020
+ target[prop] = source[prop];
4021
+ }
4022
+ return target;
4023
+ };
4058
4024
  const matchMap = {
4059
4025
  s: void 0,
4060
4026
  f: "FULL",
@@ -4241,13 +4207,15 @@ const tableToAst = (ctx, data, table, action, domains) => {
4241
4207
  primaryKey: primaryKey && primaryKey.columns.length > 1 ? primaryKey : void 0,
4242
4208
  indexes: indexes.reduce((acc, index) => {
4243
4209
  if (index.columns.length > 1 || index.columns.some((it) => "expression" in it)) {
4210
+ const _a = makeIndexOptions(tableName, index), { name } = _a, options = __objRest(_a, ["name"]);
4244
4211
  acc.push({
4245
4212
  columns: index.columns.map((it) => __spreadProps$1(__spreadValues$2({}, "column" in it ? { column: it.column } : { expression: it.expression }), {
4246
4213
  collate: it.collate,
4247
4214
  opclass: it.opclass,
4248
4215
  order: it.order
4249
4216
  })),
4250
- options: makeIndexOptions(tableName, index)
4217
+ options,
4218
+ name
4251
4219
  });
4252
4220
  }
4253
4221
  return acc;
@@ -4269,7 +4237,7 @@ const getDbStructureTableData = (data, { name, schemaName }) => {
4269
4237
  return {
4270
4238
  primaryKey: (primaryKey == null ? void 0 : primaryKey.primaryKey) ? {
4271
4239
  columns: primaryKey.primaryKey,
4272
- options: primaryKey.name === `${name}_pkey` ? void 0 : { name: primaryKey.name }
4240
+ name: primaryKey.name === `${name}_pkey` ? void 0 : primaryKey.name
4273
4241
  } : void 0,
4274
4242
  indexes: data.indexes.filter(
4275
4243
  (it) => it.tableName === name && it.schemaName === schemaName
@@ -4366,7 +4334,7 @@ const getDbTableColumnsChecks = (tableData) => tableData.constraints.reduce((acc
4366
4334
  return acc;
4367
4335
  }, {});
4368
4336
  const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, checks) => {
4369
- var _a, _b, _c, _d, _e, _f, _g;
4337
+ var _a, _b, _c, _d, _f, _g, _h, _i, _j;
4370
4338
  let column = instantiateDbColumn(ctx, data, domains, item);
4371
4339
  if (item.identity) {
4372
4340
  column.data.identity = item.identity;
@@ -4381,24 +4349,28 @@ const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, ch
4381
4349
  (it) => it.columns.length === 1 && "column" in it.columns[0] && it.columns[0].column === item.name
4382
4350
  );
4383
4351
  for (const index of columnIndexes) {
4384
- const options = index.columns[0];
4385
- column = column.index(__spreadValues$2({
4386
- collate: options.collate,
4387
- opclass: options.opclass,
4388
- order: options.order
4389
- }, makeIndexOptions(tableName, index)));
4352
+ const columnOptions = index.columns[0];
4353
+ const _e = makeIndexOptions(tableName, index), { name } = _e, indexOptions = __objRest(_e, ["name"]);
4354
+ ((_g = (_f = column.data).indexes) != null ? _g : _f.indexes = []).push({
4355
+ options: __spreadValues$2({
4356
+ collate: columnOptions.collate,
4357
+ opclass: columnOptions.opclass,
4358
+ order: columnOptions.order
4359
+ }, indexOptions),
4360
+ name
4361
+ });
4390
4362
  }
4391
4363
  }
4392
4364
  if (table) {
4393
4365
  for (const it of data.constraints) {
4394
- if (it.tableName !== table.name || it.schemaName !== table.schemaName || it.check || ((_e = it.references) == null ? void 0 : _e.columns.length) !== 1 || it.references.columns[0] !== item.name || checkIfIsOuterRecursiveFkey(data, table, it.references)) {
4366
+ if (it.tableName !== table.name || it.schemaName !== table.schemaName || it.check || ((_h = it.references) == null ? void 0 : _h.columns.length) !== 1 || it.references.columns[0] !== item.name || checkIfIsOuterRecursiveFkey(data, table, it.references)) {
4395
4367
  continue;
4396
4368
  }
4397
4369
  const c = dbConstraintToTableConstraint(ctx, table, it);
4398
4370
  column = column.foreignKey(
4399
- (_f = c.references) == null ? void 0 : _f.fnOrTable,
4371
+ (_i = c.references) == null ? void 0 : _i.fnOrTable,
4400
4372
  it.references.foreignColumns[0],
4401
- (_g = c.references) == null ? void 0 : _g.options
4373
+ (_j = c.references) == null ? void 0 : _j.options
4402
4374
  );
4403
4375
  }
4404
4376
  }
@@ -4613,7 +4585,7 @@ const astToGenerateItem = (ast, currentSchema) => {
4613
4585
  };
4614
4586
  };
4615
4587
  const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, columns) => {
4616
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
4588
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
4617
4589
  for (const [keys, name, change] of columns) {
4618
4590
  const { column } = change;
4619
4591
  if (column) {
@@ -4652,29 +4624,25 @@ const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, co
4652
4624
  if (foreignKeys) {
4653
4625
  for (const fkey of foreignKeys) {
4654
4626
  keys.push(
4655
- fkey.name ? `${schema}.${fkey.name}` : getConstraintName(table, {
4656
- references: { columns: [(_i = (_h = change.column) == null ? void 0 : _h.data.name) != null ? _i : name] }
4627
+ ((_h = fkey.options) == null ? void 0 : _h.name) ? `${schema}.${fkey.options.name}` : getConstraintName(table, {
4628
+ references: { columns: [(_j = (_i = change.column) == null ? void 0 : _i.data.name) != null ? _j : name] }
4657
4629
  })
4658
4630
  );
4659
- const [s = currentSchema, t] = getForeignKeyTable(
4660
- "table" in fkey ? fkey.table : fkey.fn
4661
- );
4631
+ const [s = currentSchema, t] = getForeignKeyTable(fkey.fnOrTable);
4662
4632
  deps.push(`${s}.${t}`);
4663
4633
  }
4664
4634
  }
4665
4635
  }
4666
4636
  };
4667
4637
  const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
4668
- var _a, _b;
4669
4638
  if (data.primaryKey) {
4670
- const name = (_a = data.primaryKey.options) == null ? void 0 : _a.name;
4639
+ const name = data.primaryKey.name;
4671
4640
  keys.push(name ? `${schema}.${name}` : `${table}_pkey`);
4672
4641
  }
4673
4642
  if (data.indexes) {
4674
4643
  for (const index of data.indexes) {
4675
- const name = (_b = index.options) == null ? void 0 : _b.name;
4676
4644
  keys.push(
4677
- name ? `${schema}.${name}` : getIndexName(table, index.columns)
4645
+ index.name ? `${schema}.${index.name}` : getIndexName(table, index.columns)
4678
4646
  );
4679
4647
  }
4680
4648
  }
@@ -4816,20 +4784,28 @@ ${group.map(
4816
4784
  };
4817
4785
  const astEncoders = {
4818
4786
  table(ast, config) {
4787
+ var _a, _b;
4819
4788
  let code = [];
4820
4789
  const result = code;
4821
4790
  const hasOptions = Boolean(ast.comment || ast.noPrimaryKey === "ignore");
4822
- if (hasOptions) {
4791
+ const hasTableData = Boolean(
4792
+ ast.primaryKey || ((_a = ast.indexes) == null ? void 0 : _a.length) || ((_b = ast.constraints) == null ? void 0 : _b.length)
4793
+ );
4794
+ const isShifted = hasOptions || hasTableData;
4795
+ if (isShifted) {
4823
4796
  orchidCore.addCode(code, `await db.${ast.action}Table(`);
4824
4797
  const inner = [`${quoteSchemaTable(ast)},`];
4825
4798
  code.push(inner);
4826
4799
  code = inner;
4827
- const options = [];
4828
- if (ast.comment)
4829
- options.push(`comment: ${JSON.stringify(ast.comment)},`);
4830
- if (ast.noPrimaryKey === "ignore")
4831
- options.push(`noPrimaryKey: true,`);
4832
- code.push("{", options, "},", "(t) => ({");
4800
+ if (hasOptions) {
4801
+ const options = [];
4802
+ if (ast.comment)
4803
+ options.push(`comment: ${JSON.stringify(ast.comment)},`);
4804
+ if (ast.noPrimaryKey === "ignore")
4805
+ options.push(`noPrimaryKey: true,`);
4806
+ code.push("{", options, "},");
4807
+ }
4808
+ code.push("(t) => ({");
4833
4809
  } else {
4834
4810
  orchidCore.addCode(
4835
4811
  code,
@@ -4854,21 +4830,10 @@ const astEncoders = {
4854
4830
  if (timestamps.hasAnyTimestamps) {
4855
4831
  code.push([`...${timestampsToCode(config, timestamps)},`]);
4856
4832
  }
4857
- if (ast.primaryKey) {
4858
- code.push([pqb.primaryKeyToCode(ast.primaryKey, "t")]);
4859
- }
4860
- if (ast.indexes) {
4861
- for (const index of ast.indexes) {
4862
- code.push(pqb.indexToCode(index, "t"));
4863
- }
4864
- }
4865
- if (ast.constraints) {
4866
- for (const constraint of ast.constraints) {
4867
- code.push(pqb.constraintToCode(constraint, "t", true));
4868
- }
4869
- }
4870
- if (hasOptions) {
4833
+ if (isShifted) {
4871
4834
  orchidCore.addCode(code, "}),");
4835
+ if (hasTableData)
4836
+ pqb.pushTableDataCode(code, ast);
4872
4837
  orchidCore.addCode(result, ");");
4873
4838
  } else {
4874
4839
  orchidCore.addCode(result, "}));");
@@ -5084,25 +5049,20 @@ const astEncoders = {
5084
5049
  ];
5085
5050
  },
5086
5051
  constraint(ast) {
5087
- const kind = pqb.getConstraintKind(ast);
5088
5052
  const table = quoteSchemaTable({
5089
5053
  schema: ast.tableSchema,
5090
5054
  name: ast.tableName
5091
5055
  });
5092
- if (kind === "foreignKey" && ast.references) {
5056
+ if (ast.references) {
5093
5057
  return [
5094
5058
  `await db.addForeignKey(`,
5095
5059
  [`${table},`, ...pqb.referencesArgsToCode(ast.references, ast.name, true)],
5096
5060
  ");"
5097
5061
  ];
5098
5062
  }
5099
- if (kind === "check" && ast.check) {
5100
- return [`await db.addCheck(${table}, ${ast.check.toCode("t")});`];
5101
- }
5063
+ const check = ast.check;
5102
5064
  return [
5103
- `await db.addConstraint(${table}, {`,
5104
- pqb.constraintPropsToCode("t", ast, true),
5105
- "});"
5065
+ `await db.addCheck(${table}, ${check.toCode("t")}${ast.name ? `, ${orchidCore.singleQuote(ast.name)}` : ""});`
5106
5066
  ];
5107
5067
  },
5108
5068
  renameTableItem(ast) {