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