rake-db 2.4.16 → 2.4.17

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
@@ -35,6 +35,7 @@ declare type TableOptions = {
35
35
  dropMode?: DropMode;
36
36
  comment?: string;
37
37
  noPrimaryKey?: boolean;
38
+ snakeCase?: boolean;
38
39
  };
39
40
  declare type TextColumnCreator = () => TextColumn;
40
41
  declare type MigrationColumnTypes = Omit<ColumnTypes, 'text' | 'string' | 'enum'> & {
@@ -47,6 +48,7 @@ declare type ColumnsShapeCallback = (t: MigrationColumnTypes & {
47
48
  raw: typeof raw;
48
49
  }) => ColumnsShape;
49
50
  declare type ChangeTableOptions = {
51
+ snakeCase?: boolean;
50
52
  comment?: string | [string, string] | null;
51
53
  };
52
54
  declare type ChangeTableCallback = (t: TableChanger) => TableChangeData;
package/dist/index.js CHANGED
@@ -305,8 +305,11 @@ var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
305
305
  const columnTypeToSql = (item) => {
306
306
  return item.data.isOfCustomType ? `"${item.toSQL()}"` : item.toSQL();
307
307
  };
308
- const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
309
- const line = [`"${item.data.name || key}" ${columnTypeToSql(item)}`];
308
+ const getColumnName = (item, key, snakeCase) => {
309
+ return item.data.name || (snakeCase ? orchidCore.toSnakeCase(key) : key);
310
+ };
311
+ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
312
+ const line = [`"${name}" ${columnTypeToSql(item)}`];
310
313
  if (item.data.compression) {
311
314
  line.push(`COMPRESSION ${item.data.compression}`);
312
315
  }
@@ -337,24 +340,32 @@ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
337
340
  if (foreignKey.name) {
338
341
  line.push(`CONSTRAINT "${foreignKey.name}"`);
339
342
  }
340
- line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
343
+ line.push(
344
+ referencesToSql(
345
+ schema,
346
+ table,
347
+ foreignKey.columns,
348
+ foreignKey,
349
+ snakeCase
350
+ )
351
+ );
341
352
  }
342
353
  }
343
354
  return line.join(" ");
344
355
  };
345
- const addColumnIndex = (indexes, key, item) => {
356
+ const addColumnIndex = (indexes, name, item) => {
346
357
  if (item.data.indexes) {
347
358
  indexes.push(
348
359
  ...item.data.indexes.map((index) => ({
349
- columns: [__spreadProps$4(__spreadValues$5({}, index), { column: key })],
360
+ columns: [__spreadProps$4(__spreadValues$5({}, index), { column: name })],
350
361
  options: index
351
362
  }))
352
363
  );
353
364
  }
354
365
  };
355
- const addColumnComment = (comments, key, item) => {
366
+ const addColumnComment = (comments, name, item) => {
356
367
  if (item.data.comment) {
357
- comments.push({ column: key, comment: item.data.comment });
368
+ comments.push({ column: name, comment: item.data.comment });
358
369
  }
359
370
  };
360
371
  const getForeignKeyTable = (fnOrTable) => {
@@ -367,7 +378,7 @@ const getForeignKeyTable = (fnOrTable) => {
367
378
  const getForeignKeyName = (table, columns) => {
368
379
  return `${table}_${columns.join("_")}_fkey`;
369
380
  };
370
- const constraintToSql = ({ name }, up, foreignKey) => {
381
+ const constraintToSql = ({ name }, up, foreignKey, snakeCase) => {
371
382
  const constraintName = foreignKey.options.name || getForeignKeyName(name, foreignKey.columns);
372
383
  if (!up) {
373
384
  const { dropMode } = foreignKey.options;
@@ -380,13 +391,14 @@ const constraintToSql = ({ name }, up, foreignKey) => {
380
391
  schema,
381
392
  table,
382
393
  foreignKey.foreignColumns,
383
- foreignKey.options
394
+ foreignKey.options,
395
+ snakeCase
384
396
  )}`;
385
397
  };
386
- const referencesToSql = (schema, table, columns, foreignKey) => {
398
+ const referencesToSql = (schema, table, columns, foreignKey, snakeCase) => {
387
399
  const sql = [
388
400
  `REFERENCES ${quoteWithSchema({ schema, name: table })}(${joinColumns(
389
- columns
401
+ snakeCase ? columns.map(orchidCore.toSnakeCase) : columns
390
402
  )})`
391
403
  ];
392
404
  if (foreignKey.match) {
@@ -525,7 +537,8 @@ var __objRest$1 = (source, exclude) => {
525
537
  };
526
538
  const types = Object.assign(Object.create(pqb.columnTypes), tableMethods);
527
539
  const createTable$1 = async (migration, up, tableName, options, fn) => {
528
- types[orchidCore.snakeCaseKey] = migration.options.snakeCase;
540
+ const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
541
+ types[orchidCore.snakeCaseKey] = snakeCase;
529
542
  const shape = pqb.getColumnTypes(types, fn);
530
543
  const tableData = pqb.getTableData();
531
544
  const ast = makeAst$1(
@@ -537,7 +550,7 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
537
550
  migration.options.noPrimaryKey
538
551
  );
539
552
  validatePrimaryKey(ast);
540
- const queries = astToQueries$1(ast);
553
+ const queries = astToQueries$1(ast, snakeCase);
541
554
  for (const _a of queries) {
542
555
  const _b = _a, { then } = _b, query = __objRest$1(_b, ["then"]);
543
556
  const result = await migration.adapter.arrays(query);
@@ -548,11 +561,12 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
548
561
  const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
549
562
  const shapePKeys = [];
550
563
  for (const key in shape) {
551
- if (shape[key].data.isPrimaryKey) {
564
+ const column = shape[key];
565
+ if (column.data.isPrimaryKey) {
552
566
  shapePKeys.push(key);
553
567
  }
554
568
  }
555
- const primaryKey = tableData.primaryKey;
569
+ const { primaryKey } = tableData;
556
570
  const [schema, table] = getSchemaAndTableFromName(tableName);
557
571
  return __spreadProps$3(__spreadValues$4(__spreadProps$3(__spreadValues$4({
558
572
  type: "table",
@@ -591,10 +605,11 @@ You can suppress this error by setting { noPrimaryKey: true } after a table name
591
605
  }
592
606
  }
593
607
  };
594
- const astToQueries$1 = (ast) => {
608
+ const astToQueries$1 = (ast, snakeCase) => {
595
609
  const queries = [];
596
- for (const key in ast.shape) {
597
- const item = ast.shape[key];
610
+ const { shape } = ast;
611
+ for (const key in shape) {
612
+ const item = shape[key];
598
613
  if (!(item instanceof pqb.EnumColumn))
599
614
  continue;
600
615
  queries.push(makePopulateEnumQuery(item));
@@ -609,22 +624,49 @@ const astToQueries$1 = (ast) => {
609
624
  const values = [];
610
625
  const indexes = [];
611
626
  const comments = [];
612
- for (const key in ast.shape) {
613
- const item = ast.shape[key];
614
- addColumnIndex(indexes, key, item);
615
- addColumnComment(comments, key, item);
616
- lines.push(`
617
- ${columnToSql(key, item, values, !!ast.primaryKey)}`);
627
+ for (const key in shape) {
628
+ const item = shape[key];
629
+ const name = getColumnName(item, key, snakeCase);
630
+ addColumnIndex(indexes, name, item);
631
+ addColumnComment(comments, name, item);
632
+ lines.push(
633
+ `
634
+ ${columnToSql(name, item, values, !!ast.primaryKey, snakeCase)}`
635
+ );
618
636
  }
619
637
  if (ast.primaryKey) {
620
- lines.push(`
621
- ${primaryKeyToSql(ast.primaryKey)}`);
638
+ lines.push(
639
+ `
640
+ ${primaryKeyToSql({
641
+ options: ast.primaryKey.options,
642
+ columns: ast.primaryKey.columns.map(
643
+ (key) => getColumnName(shape[key], key, snakeCase)
644
+ )
645
+ })}`
646
+ );
622
647
  }
623
648
  ast.foreignKeys.forEach((foreignKey) => {
624
- lines.push(`
625
- ${constraintToSql(ast, true, foreignKey)}`);
649
+ lines.push(
650
+ `
651
+ ${constraintToSql(
652
+ ast,
653
+ true,
654
+ __spreadProps$3(__spreadValues$4({}, foreignKey), {
655
+ columns: foreignKey.columns.map(
656
+ (column) => getColumnName(shape[column], column, snakeCase)
657
+ )
658
+ }),
659
+ snakeCase
660
+ )}`
661
+ );
626
662
  });
627
- indexes.push(...ast.indexes);
663
+ indexes.push(
664
+ ...ast.indexes.map((index) => __spreadProps$3(__spreadValues$4({}, index), {
665
+ columns: index.columns.map((item) => __spreadValues$4(__spreadValues$4({}, item), "column" in item ? {
666
+ column: getColumnName(shape[item.column], item.column, snakeCase)
667
+ } : {}))
668
+ }))
669
+ );
628
670
  queries.push(
629
671
  {
630
672
  text: `CREATE TABLE ${quoteWithSchema(ast)} (${lines.join(",")}
@@ -808,10 +850,11 @@ const changeTable = async (migration, up, tableName, options, fn) => {
808
850
  resetChangeTableData();
809
851
  const tableChanger = Object.create(pqb.columnTypes);
810
852
  Object.assign(tableChanger, tableChangeMethods);
811
- tableChanger[orchidCore.snakeCaseKey] = migration.options.snakeCase;
853
+ const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
854
+ tableChanger[orchidCore.snakeCaseKey] = snakeCase;
812
855
  const changeData = (fn == null ? void 0 : fn(tableChanger)) || {};
813
856
  const ast = makeAst(up, tableName, changeData, changeTableData, options);
814
- const queries = astToQueries(ast);
857
+ const queries = astToQueries(ast, snakeCase);
815
858
  for (const query of queries) {
816
859
  const result = await migration.adapter.arrays(query);
817
860
  (_a = query.then) == null ? void 0 : _a.call(query, result);
@@ -844,7 +887,7 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
844
887
  shape
845
888
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
846
889
  };
847
- const astToQueries = (ast) => {
890
+ const astToQueries = (ast, snakeCase) => {
848
891
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
849
892
  const queries = [];
850
893
  if (ast.comment !== void 0) {
@@ -852,10 +895,10 @@ const astToQueries = (ast) => {
852
895
  text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${pqb.quote(ast.comment)}`
853
896
  });
854
897
  }
855
- const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$3({}, ast.add.primaryKey) : {
898
+ const addPrimaryKeys = {
856
899
  columns: []
857
900
  };
858
- const dropPrimaryKeys = ast.drop.primaryKey ? __spreadValues$3({}, ast.drop.primaryKey) : {
901
+ const dropPrimaryKeys = {
859
902
  columns: []
860
903
  };
861
904
  for (const key in ast.shape) {
@@ -868,11 +911,11 @@ const astToQueries = (ast) => {
868
911
  }
869
912
  if (item.type === "add") {
870
913
  if (item.item.data.isPrimaryKey) {
871
- addPrimaryKeys.columns.push(key);
914
+ addPrimaryKeys.columns.push(getColumnName(item.item, key, snakeCase));
872
915
  }
873
916
  } else if (item.type === "drop") {
874
917
  if (item.item.data.isPrimaryKey) {
875
- dropPrimaryKeys.columns.push(key);
918
+ dropPrimaryKeys.columns.push(getColumnName(item.item, key, snakeCase));
876
919
  }
877
920
  } else if (item.type === "change") {
878
921
  if (item.from.column instanceof pqb.EnumColumn) {
@@ -882,73 +925,104 @@ const astToQueries = (ast) => {
882
925
  queries.push(makePopulateEnumQuery(item.to.column));
883
926
  }
884
927
  if (item.from.primaryKey) {
885
- dropPrimaryKeys.columns.push(key);
928
+ dropPrimaryKeys.columns.push(
929
+ item.from.column ? getColumnName(item.from.column, key, snakeCase) : snakeCase ? orchidCore.toSnakeCase(key) : key
930
+ );
886
931
  dropPrimaryKeys.change = true;
887
932
  }
888
933
  if (item.to.primaryKey) {
889
- addPrimaryKeys.columns.push(key);
934
+ addPrimaryKeys.columns.push(
935
+ item.to.column ? getColumnName(item.to.column, key, snakeCase) : snakeCase ? orchidCore.toSnakeCase(key) : key
936
+ );
890
937
  addPrimaryKeys.change = true;
891
938
  }
892
939
  }
893
940
  }
941
+ if (ast.add.primaryKey) {
942
+ addPrimaryKeys.options = ast.add.primaryKey.options;
943
+ addPrimaryKeys.columns.push(...ast.add.primaryKey.columns);
944
+ }
945
+ if (ast.drop.primaryKey) {
946
+ dropPrimaryKeys.options = ast.drop.primaryKey.options;
947
+ dropPrimaryKeys.columns.push(...ast.drop.primaryKey.columns);
948
+ }
894
949
  const alterTable = [];
895
950
  const values = [];
896
- const addIndexes = [...ast.add.indexes];
897
- const dropIndexes = [...ast.drop.indexes];
898
- const addForeignKeys = [...ast.add.foreignKeys];
899
- const dropForeignKeys = [...ast.drop.foreignKeys];
951
+ const addIndexes = mapIndexesForSnakeCase(
952
+ ast.add.indexes,
953
+ snakeCase
954
+ );
955
+ const dropIndexes = mapIndexesForSnakeCase(
956
+ ast.drop.indexes,
957
+ snakeCase
958
+ );
959
+ const addForeignKeys = mapForeignKeysForSnakeCase(
960
+ ast.add.foreignKeys,
961
+ snakeCase
962
+ );
963
+ const dropForeignKeys = mapForeignKeysForSnakeCase(
964
+ ast.drop.foreignKeys,
965
+ snakeCase
966
+ );
900
967
  const comments = [];
901
968
  for (const key in ast.shape) {
902
969
  const item = ast.shape[key];
903
970
  if (item.type === "add") {
904
971
  const column = item.item;
905
- addColumnIndex(addIndexes, key, column);
906
- addColumnComment(comments, key, column);
972
+ const name = getColumnName(column, key, snakeCase);
973
+ addColumnIndex(addIndexes, name, column);
974
+ addColumnComment(comments, name, column);
907
975
  alterTable.push(
908
976
  `ADD COLUMN ${columnToSql(
909
- key,
977
+ name,
910
978
  column,
911
979
  values,
912
- addPrimaryKeys.columns.length > 1
980
+ addPrimaryKeys.columns.length > 1,
981
+ snakeCase
913
982
  )}`
914
983
  );
915
984
  } else if (item.type === "drop") {
916
- addColumnIndex(dropIndexes, key, item.item);
985
+ const name = getColumnName(item.item, key, snakeCase);
986
+ addColumnIndex(dropIndexes, name, item.item);
917
987
  alterTable.push(
918
- `DROP COLUMN "${item.item.data.name || key}"${item.dropMode ? ` ${item.dropMode}` : ""}`
988
+ `DROP COLUMN "${name}"${item.dropMode ? ` ${item.dropMode}` : ""}`
919
989
  );
920
990
  } else if (item.type === "change") {
921
991
  const { from, to } = item;
992
+ const name = getChangeColumnName(item, key, snakeCase);
922
993
  if (to.type && (from.type !== to.type || from.collate !== to.collate)) {
923
994
  const type = !to.column || to.column.data.isOfCustomType ? `"${to.type}"` : to.type;
924
995
  alterTable.push(
925
- `ALTER COLUMN "${item.name || key}" TYPE ${type}${to.collate ? ` COLLATE ${pqb.quote(to.collate)}` : ""}${item.using ? ` USING ${pqb.getRaw(item.using, values)}` : ""}`
996
+ `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${pqb.quote(to.collate)}` : ""}${item.using ? ` USING ${pqb.getRaw(item.using, values)}` : ""}`
926
997
  );
927
998
  }
928
999
  if (from.default !== to.default) {
929
1000
  const value = typeof to.default === "object" && to.default && orchidCore.isRaw(to.default) ? pqb.getRaw(to.default, values) : pqb.quote(to.default);
930
1001
  const expr = value === void 0 ? "DROP DEFAULT" : `SET DEFAULT ${value}`;
931
- alterTable.push(`ALTER COLUMN "${item.name || key}" ${expr}`);
1002
+ alterTable.push(`ALTER COLUMN "${name}" ${expr}`);
932
1003
  }
933
1004
  if (from.nullable !== to.nullable) {
934
1005
  alterTable.push(
935
- `ALTER COLUMN "${item.name || key}" ${to.nullable ? "DROP" : "SET"} NOT NULL`
1006
+ `ALTER COLUMN "${name}" ${to.nullable ? "DROP" : "SET"} NOT NULL`
936
1007
  );
937
1008
  }
938
1009
  if (from.compression !== to.compression) {
939
1010
  alterTable.push(
940
- `ALTER COLUMN "${item.name || key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
1011
+ `ALTER COLUMN "${name}" SET COMPRESSION ${to.compression || "DEFAULT"}`
941
1012
  );
942
1013
  }
943
1014
  if (from.check !== to.check) {
944
- const name = `${ast.name}_${item.name || key}_check`;
1015
+ const checkName = `${ast.name}_${name}_check`;
945
1016
  if (from.check) {
946
- alterTable.push(`DROP CONSTRAINT "${name}"`);
1017
+ alterTable.push(`DROP CONSTRAINT "${checkName}"`);
947
1018
  }
948
1019
  if (to.check) {
949
1020
  alterTable.push(
950
- `ADD CONSTRAINT "${name}"
951
- CHECK (${pqb.getRaw(to.check, values)})`
1021
+ `ADD CONSTRAINT "${checkName}"
1022
+ CHECK (${pqb.getRaw(
1023
+ to.check,
1024
+ values
1025
+ )})`
952
1026
  );
953
1027
  }
954
1028
  }
@@ -962,17 +1036,17 @@ const astToQueries = (ast) => {
962
1036
  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(","))) {
963
1037
  if (fromFkey) {
964
1038
  dropForeignKeys.push({
965
- columns: [key],
1039
+ columns: [name],
966
1040
  fnOrTable: fromFkey.table,
967
- foreignColumns: fromFkey.columns,
1041
+ foreignColumns: snakeCase ? fromFkey.columns.map(orchidCore.toSnakeCase) : fromFkey.columns,
968
1042
  options: fromFkey
969
1043
  });
970
1044
  }
971
1045
  if (toFkey) {
972
1046
  addForeignKeys.push({
973
- columns: [key],
1047
+ columns: [name],
974
1048
  fnOrTable: toFkey.table,
975
- foreignColumns: toFkey.columns,
1049
+ foreignColumns: snakeCase ? toFkey.columns.map(orchidCore.toSnakeCase) : toFkey.columns,
976
1050
  options: toFkey
977
1051
  });
978
1052
  }
@@ -990,7 +1064,7 @@ const astToQueries = (ast) => {
990
1064
  dropIndexes.push({
991
1065
  columns: [
992
1066
  __spreadValues$3({
993
- column: key
1067
+ column: name
994
1068
  }, fromIndex)
995
1069
  ],
996
1070
  options: fromIndex
@@ -1000,7 +1074,7 @@ const astToQueries = (ast) => {
1000
1074
  addIndexes.push({
1001
1075
  columns: [
1002
1076
  __spreadValues$3({
1003
- column: key
1077
+ column: name
1004
1078
  }, toIndex)
1005
1079
  ],
1006
1080
  options: toIndex
@@ -1009,10 +1083,12 @@ const astToQueries = (ast) => {
1009
1083
  }
1010
1084
  }
1011
1085
  if (from.comment !== to.comment) {
1012
- comments.push({ column: key, comment: to.comment || null });
1086
+ comments.push({ column: name, comment: to.comment || null });
1013
1087
  }
1014
1088
  } else if (item.type === "rename") {
1015
- alterTable.push(`RENAME COLUMN "${key}" TO "${item.name}"`);
1089
+ alterTable.push(
1090
+ `RENAME COLUMN "${snakeCase ? orchidCore.toSnakeCase(key) : key}" TO "${snakeCase ? orchidCore.toSnakeCase(item.name) : item.name}"`
1091
+ );
1016
1092
  }
1017
1093
  }
1018
1094
  const prependAlterTable = [];
@@ -1023,17 +1099,24 @@ const astToQueries = (ast) => {
1023
1099
  prependAlterTable.push(
1024
1100
  ...dropForeignKeys.map(
1025
1101
  (foreignKey) => `
1026
- DROP ${constraintToSql(ast, false, foreignKey)}`
1102
+ DROP ${constraintToSql(ast, false, foreignKey, snakeCase)}`
1027
1103
  )
1028
1104
  );
1029
1105
  alterTable.unshift(...prependAlterTable);
1030
1106
  if (ast.add.primaryKey || addPrimaryKeys.change || addPrimaryKeys.columns.length > 1) {
1031
- alterTable.push(`ADD ${primaryKeyToSql(addPrimaryKeys)}`);
1107
+ alterTable.push(
1108
+ `ADD ${primaryKeyToSql(
1109
+ snakeCase ? {
1110
+ options: addPrimaryKeys.options,
1111
+ columns: addPrimaryKeys.columns.map(orchidCore.toSnakeCase)
1112
+ } : addPrimaryKeys
1113
+ )}`
1114
+ );
1032
1115
  }
1033
1116
  alterTable.push(
1034
1117
  ...addForeignKeys.map(
1035
1118
  (foreignKey) => `
1036
- ADD ${constraintToSql(ast, true, foreignKey)}`
1119
+ ADD ${constraintToSql(ast, true, foreignKey, snakeCase)}`
1037
1120
  )
1038
1121
  );
1039
1122
  if (alterTable.length) {
@@ -1048,6 +1131,22 @@ const astToQueries = (ast) => {
1048
1131
  queries.push(...commentsToQuery(ast, comments));
1049
1132
  return queries;
1050
1133
  };
1134
+ const getChangeColumnName = (change, key, snakeCase) => {
1135
+ return change.name || (change.to.column ? getColumnName(change.to.column, key, snakeCase) : snakeCase ? orchidCore.toSnakeCase(key) : key);
1136
+ };
1137
+ const mapIndexesForSnakeCase = (indexes, snakeCase) => {
1138
+ return indexes.map((index) => ({
1139
+ options: index.options,
1140
+ columns: snakeCase ? index.columns.map(
1141
+ (item) => "column" in item ? __spreadProps$2(__spreadValues$3({}, item), { column: orchidCore.toSnakeCase(item.column) }) : item
1142
+ ) : index.columns
1143
+ }));
1144
+ };
1145
+ const mapForeignKeysForSnakeCase = (foreignKeys, snakeCase) => {
1146
+ return foreignKeys.map((foreignKey) => __spreadProps$2(__spreadValues$3({}, foreignKey), {
1147
+ columns: snakeCase ? foreignKey.columns.map(orchidCore.toSnakeCase) : foreignKey.columns
1148
+ }));
1149
+ };
1051
1150
 
1052
1151
  var __defProp$2 = Object.defineProperty;
1053
1152
  var __defProps$1 = Object.defineProperties;
@@ -2051,7 +2150,7 @@ const fkeyActionMap = {
2051
2150
  n: "SET NULL",
2052
2151
  d: "SET DEFAULT"
2053
2152
  };
2054
- const structureToAst = async (unsupportedTypes, db) => {
2153
+ const structureToAst = async (ctx, db) => {
2055
2154
  const ast = [];
2056
2155
  const data = await getData(db);
2057
2156
  for (const name of data.schemas) {
@@ -2079,24 +2178,19 @@ const structureToAst = async (unsupportedTypes, db) => {
2079
2178
  }
2080
2179
  const domains = {};
2081
2180
  for (const it of data.domains) {
2082
- domains[`${it.schemaName}.${it.name}`] = getColumn(
2083
- unsupportedTypes,
2084
- data,
2085
- domains,
2086
- {
2087
- schemaName: it.schemaName,
2088
- name: it.name,
2089
- type: it.type,
2090
- typeSchema: it.typeSchema,
2091
- isArray: it.isArray,
2092
- isSerial: false
2093
- }
2094
- );
2181
+ domains[`${it.schemaName}.${it.name}`] = getColumn(ctx, data, domains, {
2182
+ schemaName: it.schemaName,
2183
+ name: it.name,
2184
+ type: it.type,
2185
+ typeSchema: it.typeSchema,
2186
+ isArray: it.isArray,
2187
+ isSerial: false
2188
+ });
2095
2189
  }
2096
2190
  for (const key in pendingTables) {
2097
2191
  const { table, dependsOn } = pendingTables[key];
2098
2192
  if (!dependsOn.size) {
2099
- pushTableAst(unsupportedTypes, ast, data, domains, table, pendingTables);
2193
+ pushTableAst(ctx, ast, data, domains, table, pendingTables);
2100
2194
  }
2101
2195
  }
2102
2196
  const outerFKeys = [];
@@ -2144,15 +2238,7 @@ const structureToAst = async (unsupportedTypes, db) => {
2144
2238
  outerFKeys.push([fkey, table]);
2145
2239
  }
2146
2240
  }
2147
- pushTableAst(
2148
- unsupportedTypes,
2149
- ast,
2150
- data,
2151
- domains,
2152
- table,
2153
- pendingTables,
2154
- innerFKeys
2155
- );
2241
+ pushTableAst(ctx, ast, data, domains, table, pendingTables, innerFKeys);
2156
2242
  }
2157
2243
  for (const [fkey, table] of outerFKeys) {
2158
2244
  ast.push(__spreadProps(__spreadValues({}, foreignKeyToAst(fkey)), {
@@ -2212,7 +2298,7 @@ const getIsSerial = (item) => {
2212
2298
  }
2213
2299
  return false;
2214
2300
  };
2215
- const getColumn = (unsupportedTypes, data, domains, _a) => {
2301
+ const getColumn = (ctx, data, domains, _a) => {
2216
2302
  var _b = _a, {
2217
2303
  schemaName,
2218
2304
  tableName,
@@ -2230,7 +2316,7 @@ const getColumn = (unsupportedTypes, data, domains, _a) => {
2230
2316
  "isArray",
2231
2317
  "isSerial"
2232
2318
  ]);
2233
- var _a2;
2319
+ var _a2, _b2;
2234
2320
  let column;
2235
2321
  const klass = pqb.columnsByType[getColumnType(type, isSerial)];
2236
2322
  if (klass) {
@@ -2247,7 +2333,7 @@ const getColumn = (unsupportedTypes, data, domains, _a) => {
2247
2333
  column = new RakeDbEnumColumn({}, type, enumType.values);
2248
2334
  } else {
2249
2335
  column = new pqb.CustomTypeColumn({}, type);
2250
- ((_a2 = unsupportedTypes[type]) != null ? _a2 : unsupportedTypes[type] = []).push(
2336
+ ((_b2 = (_a2 = ctx.unsupportedTypes)[type]) != null ? _b2 : _a2[type] = []).push(
2251
2337
  `${schemaName}${tableName ? `.${tableName}` : ""}.${name}`
2252
2338
  );
2253
2339
  }
@@ -2260,7 +2346,7 @@ const getColumnType = (type, isSerial) => {
2260
2346
  return type;
2261
2347
  return type === "int2" ? "smallserial" : type === "int4" ? "serial" : "bigserial";
2262
2348
  };
2263
- const pushTableAst = (unsupportedTypes, ast, data, domains, table, pendingTables, innerFKeys = data.foreignKeys) => {
2349
+ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerFKeys = data.foreignKeys) => {
2264
2350
  const { schemaName, name } = table;
2265
2351
  const key = `${schemaName}.${table.name}`;
2266
2352
  delete pendingTables[key];
@@ -2283,7 +2369,7 @@ const pushTableAst = (unsupportedTypes, ast, data, domains, table, pendingTables
2283
2369
  if (isSerial) {
2284
2370
  item = __spreadProps(__spreadValues({}, item), { default: void 0 });
2285
2371
  }
2286
- let column = getColumn(unsupportedTypes, data, domains, __spreadProps(__spreadValues({}, item), {
2372
+ let column = getColumn(ctx, data, domains, __spreadProps(__spreadValues({}, item), {
2287
2373
  type: item.type,
2288
2374
  isArray: item.isArray,
2289
2375
  isSerial
@@ -2328,8 +2414,14 @@ const pushTableAst = (unsupportedTypes, ast, data, domains, table, pendingTables
2328
2414
  if (check) {
2329
2415
  column.data.check = orchidCore.raw(check.expression);
2330
2416
  }
2331
- delete column.data.name;
2332
- shape[item.name] = column;
2417
+ const camelCaseName = orchidCore.toCamelCase(item.name);
2418
+ if (ctx.snakeCase) {
2419
+ const snakeCaseName = orchidCore.toSnakeCase(camelCaseName);
2420
+ column.data.name = snakeCaseName === item.name ? void 0 : item.name;
2421
+ } else {
2422
+ column.data.name = camelCaseName === item.name ? void 0 : item.name;
2423
+ }
2424
+ shape[camelCaseName] = column;
2333
2425
  }
2334
2426
  ast.push({
2335
2427
  type: "table",
@@ -2366,14 +2458,7 @@ const pushTableAst = (unsupportedTypes, ast, data, domains, table, pendingTables
2366
2458
  for (const otherKey in pendingTables) {
2367
2459
  const item = pendingTables[otherKey];
2368
2460
  if (item.dependsOn.delete(key) && item.dependsOn.size === 0) {
2369
- pushTableAst(
2370
- unsupportedTypes,
2371
- ast,
2372
- data,
2373
- domains,
2374
- item.table,
2375
- pendingTables
2376
- );
2461
+ pushTableAst(ctx, ast, data, domains, item.table, pendingTables);
2377
2462
  }
2378
2463
  }
2379
2464
  };
@@ -2490,15 +2575,18 @@ const createDomain = (ast) => {
2490
2575
  return code;
2491
2576
  };
2492
2577
  const createTable = (config, ast) => {
2578
+ var _a, _b, _c, _d, _e, _f;
2493
2579
  const code = [];
2494
2580
  orchidCore.addCode(code, `await db.createTable(${quoteSchemaTable(ast)}, (t) => ({`);
2495
- const hasTimestamps = !config.snakeCase && isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
2496
- const hasTimestampsSnake = isTimestamp(ast.shape.created_at) && isTimestamp(ast.shape.updated_at);
2581
+ let hasTimestamps = isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
2582
+ const camelCaseTimestamps = !config.snakeCase && hasTimestamps && !((_a = ast.shape.createdAt) == null ? void 0 : _a.data.name) && !((_b = ast.shape.updatedAt) == null ? void 0 : _b.data.name);
2583
+ const snakeCaseTimestamps = hasTimestamps && !camelCaseTimestamps && (!config.snakeCase && ((_c = ast.shape.createdAt) == null ? void 0 : _c.data.name) === "created_at" && ((_d = ast.shape.updatedAt) == null ? void 0 : _d.data.name) === "updated_at" || config.snakeCase && !((_e = ast.shape.createdAt) == null ? void 0 : _e.data.name) && !((_f = ast.shape.updatedAt) == null ? void 0 : _f.data.name));
2584
+ if (!camelCaseTimestamps && !snakeCaseTimestamps) {
2585
+ hasTimestamps = false;
2586
+ }
2497
2587
  for (const key in ast.shape) {
2498
2588
  if (hasTimestamps && (key === "createdAt" || key === "updatedAt"))
2499
2589
  continue;
2500
- if (hasTimestampsSnake && (key === "created_at" || key === "updated_at"))
2501
- continue;
2502
2590
  const line = [`${orchidCore.quoteObjectKey(key)}: `];
2503
2591
  for (const part of ast.shape[key].toCode("t")) {
2504
2592
  orchidCore.addCode(line, part);
@@ -2506,11 +2594,10 @@ const createTable = (config, ast) => {
2506
2594
  orchidCore.addCode(line, ",");
2507
2595
  code.push(line);
2508
2596
  }
2509
- if (hasTimestamps || config.snakeCase && hasTimestampsSnake) {
2510
- code.push(["...t.timestamps(),"]);
2511
- }
2512
- if (hasTimestampsSnake && !config.snakeCase) {
2513
- code.push(["...t.timestampsSnakeCase(),"]);
2597
+ if (hasTimestamps) {
2598
+ code.push([
2599
+ `...t.${camelCaseTimestamps || config.snakeCase ? "timestamps" : "timestampsSnakeCase"}(),`
2600
+ ]);
2514
2601
  }
2515
2602
  if (ast.primaryKey) {
2516
2603
  code.push([pqb.primaryKeyToCode(ast.primaryKey, "t")]);
@@ -2548,8 +2635,11 @@ const pullDbStructure = async (options, config) => {
2548
2635
  var _a, _b, _c;
2549
2636
  const adapter = new pqb.Adapter(options);
2550
2637
  const db = new DbStructure(adapter);
2551
- const unsupportedTypes = {};
2552
- const ast = await structureToAst(unsupportedTypes, db);
2638
+ const ctx = {
2639
+ unsupportedTypes: {},
2640
+ snakeCase: config.snakeCase
2641
+ };
2642
+ const ast = await structureToAst(ctx, db);
2553
2643
  await adapter.close();
2554
2644
  const result = astToMigration(config, ast);
2555
2645
  if (!result)
@@ -2567,7 +2657,7 @@ const pullDbStructure = async (options, config) => {
2567
2657
  logger: config.logger
2568
2658
  }));
2569
2659
  }
2570
- const unsupportedEntries = Object.entries(unsupportedTypes);
2660
+ const unsupportedEntries = Object.entries(ctx.unsupportedTypes);
2571
2661
  const len = unsupportedEntries.length;
2572
2662
  if (len) {
2573
2663
  let count = 0;