rake-db 2.21.8 → 2.22.1

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.d.ts CHANGED
@@ -1688,7 +1688,7 @@ declare const getConstraintName: (table: string, constraint: {
1688
1688
  };
1689
1689
  check?: unknown;
1690
1690
  identity?: unknown;
1691
- }) => string;
1691
+ }, snakeCase: boolean | undefined) => string;
1692
1692
  declare const constraintToSql: ({ name }: {
1693
1693
  schema?: string;
1694
1694
  name: string;
@@ -1702,7 +1702,7 @@ declare const getIndexName: (table: string, columns: ({
1702
1702
  declare const indexesToQuery: (up: boolean, { schema, name: tableName }: {
1703
1703
  schema?: string;
1704
1704
  name: string;
1705
- }, indexes: TableData.Index[], language?: string) => SingleSql[];
1705
+ }, indexes: TableData.Index[], snakeCase: boolean | undefined, language?: string) => SingleSql[];
1706
1706
  declare const commentsToQuery: (schemaTable: {
1707
1707
  schema?: string;
1708
1708
  name: string;
package/dist/index.js CHANGED
@@ -132,7 +132,10 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
132
132
  line.push(identityToSql(item.data.identity));
133
133
  } else if (item.data.generated) {
134
134
  line.push(
135
- `GENERATED ALWAYS AS (${item.data.generated.toSQL({ values })}) STORED`
135
+ `GENERATED ALWAYS AS (${item.data.generated({
136
+ values,
137
+ snakeCase
138
+ })}) STORED`
136
139
  );
137
140
  }
138
141
  if (item.data.primaryKey && !hasMultiplePrimaryKeys) {
@@ -224,9 +227,13 @@ const getForeignKeyTable = (fnOrTable) => {
224
227
  const item = new (fnOrTable())();
225
228
  return [item.schema, item.table];
226
229
  };
227
- const getConstraintName = (table, constraint) => {
228
- if (constraint.references)
229
- return `${table}_${constraint.references.columns.join("_")}_fkey`;
230
+ const getConstraintName = (table, constraint, snakeCase) => {
231
+ if (constraint.references) {
232
+ const { columns } = constraint.references;
233
+ return `${table}_${(snakeCase ? columns.map(orchidCore.toSnakeCase) : columns).join(
234
+ "_"
235
+ )}_fkey`;
236
+ }
230
237
  if (constraint.check)
231
238
  return `${table}_check`;
232
239
  if (constraint.identity)
@@ -234,7 +241,7 @@ const getConstraintName = (table, constraint) => {
234
241
  return `${table}_constraint`;
235
242
  };
236
243
  const constraintToSql = ({ name }, up, constraint, values, snakeCase) => {
237
- const constraintName = constraint.name || getConstraintName(name, constraint);
244
+ const constraintName = constraint.name || getConstraintName(name, constraint, snakeCase);
238
245
  if (!up) {
239
246
  const { dropMode } = constraint;
240
247
  return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
@@ -252,10 +259,9 @@ const checkToSql = (check, values) => {
252
259
  return `CHECK (${check.toSQL({ values })})`;
253
260
  };
254
261
  const foreignKeyToSql = (item, snakeCase) => {
255
- return `FOREIGN KEY (${joinColumns(item.columns)}) ${referencesToSql(
256
- item,
257
- snakeCase
258
- )}`;
262
+ return `FOREIGN KEY (${joinColumns(
263
+ snakeCase ? item.columns.map(orchidCore.toSnakeCase) : item.columns
264
+ )}) ${referencesToSql(item, snakeCase)}`;
259
265
  };
260
266
  const referencesToSql = (references, snakeCase) => {
261
267
  const [schema, table] = getForeignKeyTable(references.fnOrTable);
@@ -279,8 +285,16 @@ const referencesToSql = (references, snakeCase) => {
279
285
  const getIndexName = (table, columns) => {
280
286
  return `${table}_${columns.map((it) => "column" in it ? it.column : "expression").join("_")}_idx`;
281
287
  };
282
- const indexesToQuery = (up, { schema, name: tableName }, indexes, language) => {
288
+ const indexesToQuery = (up, { schema, name: tableName }, indexes, snakeCase, language) => {
283
289
  return indexes.map(({ columns, options, name }) => {
290
+ let include = options.include ? orchidCore.toArray(options.include) : void 0;
291
+ if (snakeCase) {
292
+ columns = columns.map(
293
+ (c) => "column" in c ? __spreadProps$7(__spreadValues$9({}, c), { column: orchidCore.toSnakeCase(c.column) }) : c
294
+ );
295
+ if (include)
296
+ include = include.map(orchidCore.toSnakeCase);
297
+ }
284
298
  const indexName = name || getIndexName(tableName, columns);
285
299
  if (!up) {
286
300
  return {
@@ -335,7 +349,7 @@ const indexesToQuery = (up, { schema, name: tableName }, indexes, language) => {
335
349
  sql.push(`(${columnList})`);
336
350
  if (options.include) {
337
351
  sql.push(
338
- `INCLUDE (${orchidCore.toArray(options.include).map((column) => `"${column}"`).join(", ")})`
352
+ `INCLUDE (${orchidCore.toArray(include).map((column) => `"${column}"`).join(", ")})`
339
353
  );
340
354
  }
341
355
  if (options.nullsNotDistinct) {
@@ -599,7 +613,7 @@ const astToQueries$1 = (ast, snakeCase, language) => {
599
613
  )`,
600
614
  values
601
615
  },
602
- ...indexesToQuery(true, ast, indexes, language),
616
+ ...indexesToQuery(true, ast, indexes, snakeCase, language),
603
617
  ...commentsToQuery(ast, comments)
604
618
  );
605
619
  if (ast.comment) {
@@ -639,6 +653,8 @@ const resetChangeTableData = () => {
639
653
  };
640
654
  const addOrDropChanges = [];
641
655
  function add(item, options) {
656
+ orchidCore.consumeColumnName();
657
+ setName(this, item);
642
658
  if (item instanceof pqb.ColumnType) {
643
659
  const result = addOrDrop("add", item, options);
644
660
  if (result.type === "change")
@@ -664,6 +680,8 @@ function add(item, options) {
664
680
  return void 0;
665
681
  }
666
682
  const drop = function(item, options) {
683
+ orchidCore.consumeColumnName();
684
+ setName(this, item);
667
685
  if (item instanceof pqb.ColumnType) {
668
686
  const result = addOrDrop("drop", item, options);
669
687
  if (result.type === "change")
@@ -689,10 +707,6 @@ const drop = function(item, options) {
689
707
  return void 0;
690
708
  };
691
709
  const addOrDrop = (type, item, options) => {
692
- const name = orchidCore.consumeColumnName();
693
- if (name) {
694
- item.data.name = name;
695
- }
696
710
  if (item instanceof pqb.UnknownColumn) {
697
711
  const empty = columnTypeToColumnChange({
698
712
  type: "change",
@@ -718,24 +732,42 @@ const addOrDrop = (type, item, options) => {
718
732
  dropMode: options == null ? void 0 : options.dropMode
719
733
  };
720
734
  };
721
- const columnTypeToColumnChange = (item) => {
735
+ const columnTypeToColumnChange = (item, name) => {
722
736
  if (item instanceof pqb.ColumnType) {
723
- const foreignKeys = item.data.foreignKeys;
737
+ let column = item;
738
+ const foreignKeys = column.data.foreignKeys;
724
739
  if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
725
740
  throw new Error("Callback in foreignKey is not allowed in migration");
726
741
  }
742
+ if (name && !column.data.name) {
743
+ column = Object.create(column);
744
+ column.data = __spreadProps$5(__spreadValues$7({}, column.data), { name });
745
+ }
727
746
  return __spreadProps$5(__spreadValues$7({
728
- column: item,
729
- type: item.toSQL(),
730
- nullable: item.data.isNullable
731
- }, item.data), {
732
- primaryKey: item.data.primaryKey === void 0 ? void 0 : true,
747
+ column,
748
+ type: column.toSQL(),
749
+ nullable: column.data.isNullable
750
+ }, column.data), {
751
+ primaryKey: column.data.primaryKey === void 0 ? void 0 : true,
733
752
  foreignKeys
734
753
  });
735
754
  }
736
755
  return item.to;
737
756
  };
738
757
  const nameKey = Symbol("name");
758
+ const setName = (self, item) => {
759
+ var _a, _b, _c, _d, _e;
760
+ const name = self[nameKey];
761
+ if (!name)
762
+ return;
763
+ if ("column" in item && item.column instanceof pqb.ColumnType) {
764
+ (_b = (_a = item.column.data).name) != null ? _b : _a.name = name;
765
+ } else if (item instanceof pqb.ColumnType) {
766
+ (_d = (_c = item.data).name) != null ? _d : _c.name = name;
767
+ } else {
768
+ (_e = item.name) != null ? _e : item.name = name;
769
+ }
770
+ };
739
771
  const tableChangeMethods = __spreadProps$5(__spreadValues$7(__spreadValues$7({}, tableMethods), pqb.tableDataMethods), {
740
772
  name(name) {
741
773
  orchidCore.setCurrentColumnName(name);
@@ -746,11 +778,17 @@ const tableChangeMethods = __spreadProps$5(__spreadValues$7(__spreadValues$7({},
746
778
  add,
747
779
  drop,
748
780
  change(from, to, using) {
781
+ orchidCore.consumeColumnName();
782
+ const f = columnTypeToColumnChange(from);
783
+ const t = columnTypeToColumnChange(to);
784
+ setName(this, f);
785
+ setName(this, t);
749
786
  return {
750
787
  type: "change",
751
- name: orchidCore.consumeColumnName(),
752
- from: columnTypeToColumnChange(from),
753
- to: columnTypeToColumnChange(to),
788
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
789
+ name: this[nameKey],
790
+ from: f,
791
+ to: t,
754
792
  using
755
793
  };
756
794
  },
@@ -853,6 +891,7 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
853
891
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
854
892
  };
855
893
  const astToQueries = (ast, snakeCase, language) => {
894
+ var _a, _b, _c, _d;
856
895
  const queries = [];
857
896
  if (ast.comment !== void 0) {
858
897
  queries.push({
@@ -891,25 +930,25 @@ const astToQueries = (ast, snakeCase, language) => {
891
930
  }
892
931
  if (ast.add.primaryKey) {
893
932
  addPrimaryKeys.name = ast.add.primaryKey.name;
894
- addPrimaryKeys.columns.push(...ast.add.primaryKey.columns);
933
+ const { columns } = ast.add.primaryKey;
934
+ addPrimaryKeys.columns.push(
935
+ ...snakeCase ? columns.map(orchidCore.toSnakeCase) : columns
936
+ );
895
937
  }
896
938
  if (ast.drop.primaryKey) {
897
939
  dropPrimaryKeys.name = ast.drop.primaryKey.name;
898
- dropPrimaryKeys.columns.push(...ast.drop.primaryKey.columns);
940
+ const { columns } = ast.drop.primaryKey;
941
+ dropPrimaryKeys.columns.push(
942
+ ...snakeCase ? columns.map(orchidCore.toSnakeCase) : columns
943
+ );
899
944
  }
900
945
  const alterTable = [];
901
946
  const renameItems = [];
902
947
  const values = [];
903
- const addIndexes = mapIndexesForSnakeCase(ast.add.indexes, snakeCase);
904
- const dropIndexes = mapIndexesForSnakeCase(ast.drop.indexes, snakeCase);
905
- const addConstraints = mapConstraintsToSnakeCase(
906
- ast.add.constraints,
907
- snakeCase
908
- );
909
- const dropConstraints = mapConstraintsToSnakeCase(
910
- ast.drop.constraints,
911
- snakeCase
912
- );
948
+ const addIndexes = (_a = ast.add.indexes) != null ? _a : [];
949
+ const dropIndexes = (_b = ast.drop.indexes) != null ? _b : [];
950
+ const addConstraints = (_c = ast.add.constraints) != null ? _c : [];
951
+ const dropConstraints = (_d = ast.drop.constraints) != null ? _d : [];
913
952
  const comments = [];
914
953
  for (const key in ast.shape) {
915
954
  const item = ast.shape[key];
@@ -985,8 +1024,8 @@ const astToQueries = (ast, snakeCase, language) => {
985
1024
  if (alterTable.length) {
986
1025
  queries.push(alterTableSql(tableName, alterTable, values));
987
1026
  }
988
- queries.push(...indexesToQuery(false, ast, dropIndexes, language));
989
- queries.push(...indexesToQuery(true, ast, addIndexes, language));
1027
+ queries.push(...indexesToQuery(false, ast, dropIndexes, snakeCase, language));
1028
+ queries.push(...indexesToQuery(true, ast, addIndexes, snakeCase, language));
990
1029
  queries.push(...commentsToQuery(ast, comments));
991
1030
  return queries;
992
1031
  };
@@ -1057,7 +1096,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
1057
1096
  const name = getChangeColumnName("to", item, key, snakeCase);
1058
1097
  const fromName = getChangeColumnName("from", item, key, snakeCase);
1059
1098
  if (fromName !== name) {
1060
- renameItems.push(renameColumnSql(fromName, name, snakeCase));
1099
+ renameItems.push(renameColumnSql(fromName, name));
1061
1100
  }
1062
1101
  let changeType = false;
1063
1102
  if (to.type && (from.type !== to.type || from.collate !== to.collate)) {
@@ -1173,7 +1212,9 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
1173
1212
  comments.push({ column: name, comment: to.comment || null });
1174
1213
  }
1175
1214
  } else if (item.type === "rename") {
1176
- renameItems.push(renameColumnSql(key, item.name, snakeCase));
1215
+ renameItems.push(
1216
+ snakeCase ? renameColumnSql(orchidCore.toSnakeCase(key), orchidCore.toSnakeCase(item.name)) : renameColumnSql(key, item.name)
1217
+ );
1177
1218
  }
1178
1219
  };
1179
1220
  const getChangeColumnName = (what, change, key, snakeCase) => {
@@ -1182,22 +1223,8 @@ const getChangeColumnName = (what, change, key, snakeCase) => {
1182
1223
  getColumnName(change[what].column, key, snakeCase)
1183
1224
  ) : snakeCase ? orchidCore.toSnakeCase(key) : key);
1184
1225
  };
1185
- const renameColumnSql = (from, to, snakeCase) => {
1186
- return `RENAME COLUMN "${snakeCase ? orchidCore.toSnakeCase(from) : from}" TO "${snakeCase ? orchidCore.toSnakeCase(to) : to}"`;
1187
- };
1188
- const mapIndexesForSnakeCase = (indexes, snakeCase) => {
1189
- return (indexes == null ? void 0 : indexes.map((index) => __spreadProps$5(__spreadValues$7({}, index), {
1190
- columns: snakeCase ? index.columns.map(
1191
- (item) => "column" in item ? __spreadProps$5(__spreadValues$7({}, item), { column: orchidCore.toSnakeCase(item.column) }) : item
1192
- ) : index.columns
1193
- }))) || [];
1194
- };
1195
- const mapConstraintsToSnakeCase = (foreignKeys, snakeCase) => {
1196
- return (foreignKeys == null ? void 0 : foreignKeys.map((item) => __spreadProps$5(__spreadValues$7({}, item), {
1197
- references: item.references ? snakeCase ? __spreadProps$5(__spreadValues$7({}, item.references), {
1198
- columns: item.references.columns.map(orchidCore.toSnakeCase)
1199
- }) : item.references : void 0
1200
- }))) || [];
1226
+ const renameColumnSql = (from, to) => {
1227
+ return `RENAME COLUMN "${from}" TO "${to}"`;
1201
1228
  };
1202
1229
 
1203
1230
  const createView = async (migration, up, name, options, sql) => {
@@ -2110,7 +2137,10 @@ class Migration {
2110
2137
  async columnExists(tableName, columnName) {
2111
2138
  return queryExists(this, {
2112
2139
  text: `SELECT 1 FROM "information_schema"."columns" WHERE "table_name" = $1 AND "column_name" = $2`,
2113
- values: [tableName, columnName]
2140
+ values: [
2141
+ tableName,
2142
+ this.options.snakeCase ? orchidCore.toSnakeCase(columnName) : columnName
2143
+ ]
2114
2144
  });
2115
2145
  }
2116
2146
  /**
@@ -4277,17 +4307,20 @@ const tableToAst = (ctx, data, table, action, domains) => {
4277
4307
  name: tableName,
4278
4308
  shape: makeDbStructureColumnsShape(ctx, data, domains, table, tableData),
4279
4309
  noPrimaryKey: tableData.primaryKey ? "error" : "ignore",
4280
- primaryKey: primaryKey && primaryKey.columns.length > 1 ? primaryKey : void 0,
4310
+ primaryKey: primaryKey && primaryKey.columns.length > 1 ? __spreadProps$1(__spreadValues$2({}, primaryKey), { columns: primaryKey.columns.map(orchidCore.toCamelCase) }) : void 0,
4281
4311
  indexes: indexes.reduce((acc, index) => {
4312
+ var _b;
4282
4313
  if (index.columns.length > 1 || index.columns.some((it) => "expression" in it)) {
4283
4314
  const _a = makeIndexOptions(tableName, index), { name } = _a, options = __objRest(_a, ["name"]);
4284
4315
  acc.push({
4285
- columns: index.columns.map((it) => __spreadProps$1(__spreadValues$2({}, "column" in it ? { column: it.column } : { expression: it.expression }), {
4316
+ columns: index.columns.map((it) => __spreadProps$1(__spreadValues$2({}, "column" in it ? { column: orchidCore.toCamelCase(it.column) } : { expression: it.expression }), {
4286
4317
  collate: it.collate,
4287
4318
  opclass: it.opclass,
4288
4319
  order: it.order
4289
4320
  })),
4290
- options,
4321
+ options: __spreadProps$1(__spreadValues$2({}, options), {
4322
+ include: (_b = index.include) == null ? void 0 : _b.map(orchidCore.toCamelCase)
4323
+ }),
4291
4324
  name
4292
4325
  });
4293
4326
  }
@@ -4343,7 +4376,7 @@ const constraintToAst = (ctx, item) => {
4343
4376
  if (check) {
4344
4377
  result.check = pqb.raw({ raw: check.expression });
4345
4378
  }
4346
- if (item.name && item.name !== getConstraintName(item.tableName, result)) {
4379
+ if (item.name && item.name !== getConstraintName(item.tableName, result, ctx.snakeCase)) {
4347
4380
  result.name = item.name;
4348
4381
  if ((_a = result.references) == null ? void 0 : _a.options) {
4349
4382
  result.references.options.name = item.name;
@@ -4409,6 +4442,7 @@ const getDbTableColumnsChecks = (tableData) => tableData.constraints.reduce((acc
4409
4442
  const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, checks) => {
4410
4443
  var _a, _b, _c, _d, _f, _g, _h, _i, _j;
4411
4444
  let column = instantiateDbColumn(ctx, data, domains, item);
4445
+ column.data.name = item.name;
4412
4446
  if (item.identity) {
4413
4447
  column.data.identity = item.identity;
4414
4448
  if (!item.identity.always)
@@ -4479,7 +4513,7 @@ const dbConstraintToTableConstraint = (ctx, table, item) => {
4479
4513
  } : void 0,
4480
4514
  check: check ? pqb.raw({ raw: check.expression }) : void 0
4481
4515
  };
4482
- const name = item.name && item.name !== getConstraintName(table.name, constraint) ? item.name : void 0;
4516
+ const name = item.name && item.name !== getConstraintName(table.name, constraint, ctx.snakeCase) ? item.name : void 0;
4483
4517
  if (name) {
4484
4518
  constraint.name = name;
4485
4519
  if ((_a = constraint.references) == null ? void 0 : _a.options) {
@@ -4518,10 +4552,10 @@ const checkIfIsOuterRecursiveFkey = (data, table, references) => {
4518
4552
  return false;
4519
4553
  };
4520
4554
 
4521
- const astToGenerateItems = (asts, currentSchema) => {
4522
- return asts.map((ast) => astToGenerateItem(ast, currentSchema));
4555
+ const astToGenerateItems = (config, asts, currentSchema) => {
4556
+ return asts.map((ast) => astToGenerateItem(config, ast, currentSchema));
4523
4557
  };
4524
- const astToGenerateItem = (ast, currentSchema) => {
4558
+ const astToGenerateItem = (config, ast, currentSchema) => {
4525
4559
  var _a, _b, _c, _d, _e;
4526
4560
  const add = [];
4527
4561
  const drop = [];
@@ -4550,6 +4584,7 @@ const astToGenerateItem = (ast, currentSchema) => {
4550
4584
  ([name, column]) => [keys, name, { column }]
4551
4585
  );
4552
4586
  analyzeTableColumns(
4587
+ config,
4553
4588
  currentSchema,
4554
4589
  schema,
4555
4590
  table,
@@ -4558,7 +4593,15 @@ const astToGenerateItem = (ast, currentSchema) => {
4558
4593
  columns
4559
4594
  );
4560
4595
  if (ast.type === "table") {
4561
- analyzeTableData(currentSchema, schema, table, keys, deps, ast);
4596
+ analyzeTableData(
4597
+ config,
4598
+ currentSchema,
4599
+ schema,
4600
+ table,
4601
+ keys,
4602
+ deps,
4603
+ ast
4604
+ );
4562
4605
  } else {
4563
4606
  deps.push(
4564
4607
  ...ast.deps.map(({ schemaName, name }) => `${schemaName}.${name}`)
@@ -4581,6 +4624,7 @@ const astToGenerateItem = (ast, currentSchema) => {
4581
4624
  }
4582
4625
  }
4583
4626
  analyzeTableColumns(
4627
+ config,
4584
4628
  currentSchema,
4585
4629
  schema,
4586
4630
  table,
@@ -4588,8 +4632,24 @@ const astToGenerateItem = (ast, currentSchema) => {
4588
4632
  resolveType,
4589
4633
  columns
4590
4634
  );
4591
- analyzeTableData(currentSchema, schema, table, add, deps, ast.add);
4592
- analyzeTableData(currentSchema, schema, table, drop, deps, ast.drop);
4635
+ analyzeTableData(
4636
+ config,
4637
+ currentSchema,
4638
+ schema,
4639
+ table,
4640
+ add,
4641
+ deps,
4642
+ ast.add
4643
+ );
4644
+ analyzeTableData(
4645
+ config,
4646
+ currentSchema,
4647
+ schema,
4648
+ table,
4649
+ drop,
4650
+ deps,
4651
+ ast.drop
4652
+ );
4593
4653
  }
4594
4654
  break;
4595
4655
  }
@@ -4635,7 +4695,7 @@ const astToGenerateItem = (ast, currentSchema) => {
4635
4695
  }
4636
4696
  case "constraint": {
4637
4697
  const { tableSchema = currentSchema, tableName } = ast;
4638
- const name = `${tableSchema}.${(_e = ast.name) != null ? _e : getConstraintName(tableName, ast)}`;
4698
+ const name = `${tableSchema}.${(_e = ast.name) != null ? _e : getConstraintName(tableName, ast, config.snakeCase)}`;
4639
4699
  (ast.action === "create" ? add : drop).push(name);
4640
4700
  deps.push(tableSchema, `${tableSchema}.${tableName}`);
4641
4701
  break;
@@ -4657,7 +4717,7 @@ const astToGenerateItem = (ast, currentSchema) => {
4657
4717
  deps: new Set(deps)
4658
4718
  };
4659
4719
  };
4660
- const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, columns) => {
4720
+ const analyzeTableColumns = (config, currentSchema, schema, table, deps, resolveType, columns) => {
4661
4721
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
4662
4722
  for (const [keys, name, change] of columns) {
4663
4723
  const { column } = change;
@@ -4697,9 +4757,13 @@ const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, co
4697
4757
  if (foreignKeys) {
4698
4758
  for (const fkey of foreignKeys) {
4699
4759
  keys.push(
4700
- ((_h = fkey.options) == null ? void 0 : _h.name) ? `${schema}.${fkey.options.name}` : getConstraintName(table, {
4701
- references: { columns: [(_j = (_i = change.column) == null ? void 0 : _i.data.name) != null ? _j : name] }
4702
- })
4760
+ ((_h = fkey.options) == null ? void 0 : _h.name) ? `${schema}.${fkey.options.name}` : getConstraintName(
4761
+ table,
4762
+ {
4763
+ references: { columns: [(_j = (_i = change.column) == null ? void 0 : _i.data.name) != null ? _j : name] }
4764
+ },
4765
+ config.snakeCase
4766
+ )
4703
4767
  );
4704
4768
  const [s = currentSchema, t] = getForeignKeyTable(fkey.fnOrTable);
4705
4769
  deps.push(`${s}.${t}`);
@@ -4707,7 +4771,7 @@ const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, co
4707
4771
  }
4708
4772
  }
4709
4773
  };
4710
- const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
4774
+ const analyzeTableData = (config, currentSchema, schema, table, keys, deps, data) => {
4711
4775
  if (data.primaryKey) {
4712
4776
  const name = data.primaryKey.name;
4713
4777
  keys.push(name ? `${schema}.${name}` : `${table}_pkey`);
@@ -4722,7 +4786,7 @@ const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
4722
4786
  if (data.constraints) {
4723
4787
  for (const constraint of data.constraints) {
4724
4788
  keys.push(
4725
- constraint.name ? `${schema}.${constraint.name}` : getConstraintName(table, constraint)
4789
+ constraint.name ? `${schema}.${constraint.name}` : getConstraintName(table, constraint, config.snakeCase)
4726
4790
  );
4727
4791
  if (constraint.references) {
4728
4792
  const [s = currentSchema, t] = getForeignKeyTable(
@@ -4736,7 +4800,7 @@ const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
4736
4800
 
4737
4801
  const astToMigration = (currentSchema, config, asts) => {
4738
4802
  var _a, _b, _c;
4739
- const items = astToGenerateItems(asts, currentSchema);
4803
+ const items = astToGenerateItems(config, asts, currentSchema);
4740
4804
  const toBeAdded = /* @__PURE__ */ new Set();
4741
4805
  const toBeDropped = /* @__PURE__ */ new Set();
4742
4806
  const remainingDeps = /* @__PURE__ */ new Map();
@@ -4886,7 +4950,6 @@ const astEncoders = {
4886
4950
  );
4887
4951
  }
4888
4952
  const timestamps = getHasTimestamps(
4889
- config,
4890
4953
  ast.shape.createdAt,
4891
4954
  ast.shape.updatedAt
4892
4955
  );
@@ -4894,14 +4957,15 @@ const astEncoders = {
4894
4957
  if (timestamps.hasAnyTimestamps && (key === "createdAt" || key === "updatedAt"))
4895
4958
  continue;
4896
4959
  const line = [`${orchidCore.quoteObjectKey(key)}: `];
4897
- for (const part of ast.shape[key].toCode("t", true)) {
4960
+ const columnCode = orchidCore.columnToCode(key, ast.shape[key], config.snakeCase);
4961
+ for (const part of columnCode) {
4898
4962
  orchidCore.addCode(line, part);
4899
4963
  }
4900
4964
  orchidCore.addCode(line, ",");
4901
4965
  code.push(line);
4902
4966
  }
4903
4967
  if (timestamps.hasAnyTimestamps) {
4904
- code.push([`...${timestampsToCode(config, timestamps)},`]);
4968
+ code.push([`...${timestampsToCode(timestamps)},`]);
4905
4969
  }
4906
4970
  if (isShifted) {
4907
4971
  orchidCore.addCode(code, "}),");
@@ -4914,6 +4978,7 @@ const astEncoders = {
4914
4978
  return result;
4915
4979
  },
4916
4980
  changeTable(ast, config, currentSchema) {
4981
+ var _a;
4917
4982
  let code = [];
4918
4983
  const result = code;
4919
4984
  const schemaTable = quoteSchemaTable({
@@ -4935,10 +5000,9 @@ const astEncoders = {
4935
5000
  }
4936
5001
  const [addTimestamps, dropTimestamps] = ["add", "drop"].map(
4937
5002
  (type) => {
4938
- var _a, _b;
5003
+ var _a2, _b;
4939
5004
  return getHasTimestamps(
4940
- config,
4941
- ast.shape.createdAt && "type" in ast.shape.createdAt && ((_a = ast.shape.createdAt) == null ? void 0 : _a.type) === type ? ast.shape.createdAt.item : void 0,
5005
+ ast.shape.createdAt && "type" in ast.shape.createdAt && ((_a2 = ast.shape.createdAt) == null ? void 0 : _a2.type) === type ? ast.shape.createdAt.item : void 0,
4942
5006
  ast.shape.updatedAt && "type" in ast.shape.updatedAt && ((_b = ast.shape.updatedAt) == null ? void 0 : _b.type) === type ? ast.shape.updatedAt.item : void 0
4943
5007
  );
4944
5008
  }
@@ -4951,9 +5015,11 @@ const astEncoders = {
4951
5015
  continue;
4952
5016
  const recreate = changes.length > 1;
4953
5017
  const line = [
4954
- recreate ? `...t.${change.type}(t.name(${orchidCore.singleQuote(key)})` : `${orchidCore.quoteObjectKey(key)}: t.${change.type}(`
5018
+ recreate ? `...t.${change.type}(t.name(${orchidCore.singleQuote(
5019
+ (_a = change.item.data.name) != null ? _a : key
5020
+ )})` : `${orchidCore.quoteObjectKey(key)}: t.${change.type}(`
4955
5021
  ];
4956
- const columnCode = change.item.toCode("t", true);
5022
+ const columnCode = orchidCore.columnToCode(key, change.item, config.snakeCase);
4957
5023
  for (let i = 0; i < columnCode.length; i++) {
4958
5024
  let part = columnCode[i];
4959
5025
  if (recreate && !i)
@@ -4968,11 +5034,17 @@ const astEncoders = {
4968
5034
  const line = [
4969
5035
  `${orchidCore.quoteObjectKey(key)}: t${change.name ? `.name(${orchidCore.singleQuote(change.name)})` : ""}.change(`
4970
5036
  ];
4971
- for (const part of change.from.column.toCode("t", true)) {
5037
+ const fromCode = orchidCore.columnToCode(
5038
+ key,
5039
+ change.from.column,
5040
+ config.snakeCase
5041
+ );
5042
+ for (const part of fromCode) {
4972
5043
  orchidCore.addCode(line, part);
4973
5044
  }
4974
5045
  orchidCore.addCode(line, ", ");
4975
- for (const part of change.to.column.toCode("t", true)) {
5046
+ const toCode = orchidCore.columnToCode(key, change.to.column, config.snakeCase);
5047
+ for (const part of toCode) {
4976
5048
  orchidCore.addCode(line, part);
4977
5049
  }
4978
5050
  if (change.using) {
@@ -5001,9 +5073,7 @@ const astEncoders = {
5001
5073
  for (const key of ["drop", "add"]) {
5002
5074
  const timestamps = key === "add" ? addTimestamps : dropTimestamps;
5003
5075
  if (timestamps.hasAnyTimestamps) {
5004
- orchidCore.addCode(code, [
5005
- `...t.${key}(${timestampsToCode(config, timestamps)}),`
5006
- ]);
5076
+ orchidCore.addCode(code, [`...t.${key}(${timestampsToCode(timestamps)}),`]);
5007
5077
  }
5008
5078
  const { primaryKey, indexes, constraints } = ast[key];
5009
5079
  if (primaryKey) {
@@ -5193,41 +5263,24 @@ const isTimestamp = (column, type) => {
5193
5263
  column instanceof type && !column.data.isNullable && def && typeof def === "object" && orchidCore.isRawSQL(def) && (typeof def._sql === "object" ? def._sql[0][0] : def._sql) === "now()"
5194
5264
  );
5195
5265
  };
5196
- const getHasTimestamps = (config, createdAt, updatedAt) => {
5197
- const timestamps = getTimestampsInfo(
5198
- config,
5199
- createdAt,
5200
- updatedAt,
5201
- pqb.TimestampTZColumn
5202
- );
5266
+ const getHasTimestamps = (createdAt, updatedAt) => {
5267
+ const timestamps = getTimestampsInfo(createdAt, updatedAt, pqb.TimestampTZColumn);
5203
5268
  const timestampsNoTZ = getTimestampsInfo(
5204
- config,
5205
5269
  createdAt,
5206
5270
  updatedAt,
5207
5271
  pqb.TimestampColumn
5208
5272
  );
5209
5273
  return {
5210
- hasTZTimestamps: timestamps.hasTimestamps,
5211
- hasAnyTimestamps: timestamps.hasTimestamps || timestampsNoTZ.hasTimestamps,
5212
- hasAnyCamelCaseTimestamps: timestamps.camelCaseTimestamps || timestampsNoTZ.camelCaseTimestamps
5274
+ hasTZTimestamps: timestamps,
5275
+ hasAnyTimestamps: timestamps || timestampsNoTZ
5213
5276
  };
5214
5277
  };
5215
- const getTimestampsInfo = (config, createdAt, updatedAt, type) => {
5216
- let hasTimestamps = isTimestamp(createdAt, type) && isTimestamp(updatedAt, type);
5217
- const camelCaseTimestamps = !config.snakeCase && hasTimestamps && !(createdAt == null ? void 0 : createdAt.data.name) && !(updatedAt == null ? void 0 : updatedAt.data.name);
5218
- const snakeCaseTimestamps = hasTimestamps && !camelCaseTimestamps && (!config.snakeCase && (createdAt == null ? void 0 : createdAt.data.name) === "created_at" && (updatedAt == null ? void 0 : updatedAt.data.name) === "updated_at" || config.snakeCase && !(createdAt == null ? void 0 : createdAt.data.name) && !(updatedAt == null ? void 0 : updatedAt.data.name));
5219
- if (!camelCaseTimestamps && !snakeCaseTimestamps) {
5220
- hasTimestamps = false;
5221
- }
5222
- return {
5223
- hasTimestamps,
5224
- camelCaseTimestamps,
5225
- snakeCaseTimestamps
5226
- };
5278
+ const getTimestampsInfo = (createdAt, updatedAt, type) => {
5279
+ return isTimestamp(createdAt, type) && isTimestamp(updatedAt, type) && (!(createdAt == null ? void 0 : createdAt.data.name) || (createdAt == null ? void 0 : createdAt.data.name) === "created_at") && (!(updatedAt == null ? void 0 : updatedAt.data.name) || (updatedAt == null ? void 0 : updatedAt.data.name) === "updated_at");
5227
5280
  };
5228
- const timestampsToCode = (config, { hasTZTimestamps, hasAnyCamelCaseTimestamps }) => {
5281
+ const timestampsToCode = ({ hasTZTimestamps }) => {
5229
5282
  const key = hasTZTimestamps ? "timestamps" : "timestampsNoTZ";
5230
- return `t.${hasAnyCamelCaseTimestamps || config.snakeCase ? key : `${key}SnakeCase`}()`;
5283
+ return `t.${key}()`;
5231
5284
  };
5232
5285
 
5233
5286
  const pullDbStructure = async (options, config) => {