rake-db 2.1.15 → 2.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.1.17
4
+
5
+ ### Patch Changes
6
+
7
+ - Support composite primary key by setting primaryKey on multiple columns
8
+
9
+ ## 2.1.16
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies
14
+ - pqb@0.7.12
15
+
3
16
  ## 2.1.15
4
17
 
5
18
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -28,10 +28,10 @@ declare type ChangeOptions = {
28
28
  usingUp?: RawExpression;
29
29
  usingDown?: RawExpression;
30
30
  };
31
- declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null] | ['compression', string] | ['foreignKey', ForeignKey<string, string[]>] | ['index', Omit<SingleColumnIndexOptions, 'column'>];
31
+ declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null] | ['compression', string] | ['primaryKey', boolean] | ['foreignKey', ForeignKey<string, string[]>] | ['index', Omit<SingleColumnIndexOptions, 'column'>];
32
32
  declare type TableChangeMethods = typeof tableChangeMethods;
33
33
  declare const tableChangeMethods: {
34
- raw: (sql: string, ...values: unknown[]) => RawExpression<ColumnType<unknown, pqb.Operators, unknown>>;
34
+ raw: (sql: string, values?: false | Record<string, unknown> | undefined) => RawExpression<ColumnType<unknown, pqb.Operators, unknown>>;
35
35
  add: typeof add;
36
36
  drop: typeof add;
37
37
  change(from: ChangeArg, to: ChangeArg, options?: ChangeOptions): ChangeItem;
package/dist/index.esm.js CHANGED
@@ -219,7 +219,7 @@ var __spreadValues$3 = (a, b) => {
219
219
  return a;
220
220
  };
221
221
  var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
222
- const columnToSql = (key, item, { values }) => {
222
+ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
223
223
  const line = [`"${key}" ${item.toSQL()}`];
224
224
  if (item.data.compression) {
225
225
  line.push(`COMPRESSION ${item.data.compression}`);
@@ -227,7 +227,7 @@ const columnToSql = (key, item, { values }) => {
227
227
  if (item.data.collate) {
228
228
  line.push(`COLLATE ${quote(item.data.collate)}`);
229
229
  }
230
- if (item.isPrimaryKey) {
230
+ if (item.isPrimaryKey && !hasMultiplePrimaryKeys) {
231
231
  line.push("PRIMARY KEY");
232
232
  } else if (!item.isNullable) {
233
233
  line.push("NOT NULL");
@@ -479,29 +479,9 @@ const types = Object.assign(Object.create(columnTypes), {
479
479
  raw
480
480
  });
481
481
  const createTable = async (migration, up, tableName, options, fn) => {
482
- var _a, _b;
483
482
  const shape = getColumnTypes(types, fn);
484
483
  const tableData = getTableData();
485
- const { noPrimaryKey } = migration.options;
486
- if (!options.noPrimaryKey && (!noPrimaryKey || noPrimaryKey !== "ignore")) {
487
- let hasPrimaryKey = !!((_b = (_a = tableData.primaryKey) == null ? void 0 : _a.columns) == null ? void 0 : _b.length);
488
- if (!hasPrimaryKey) {
489
- for (const key in shape) {
490
- if (shape[key].isPrimaryKey) {
491
- hasPrimaryKey = true;
492
- break;
493
- }
494
- }
495
- }
496
- if (!hasPrimaryKey) {
497
- const message = `Table ${tableName} has no primary key`;
498
- if (!noPrimaryKey || noPrimaryKey === "error") {
499
- throw new Error(message);
500
- } else {
501
- console.warn(message);
502
- }
503
- }
504
- }
484
+ validatePrimaryKey(migration, options, tableData, shape, tableName);
505
485
  if (!up) {
506
486
  const { dropMode } = options;
507
487
  await migration.query(
@@ -510,19 +490,23 @@ const createTable = async (migration, up, tableName, options, fn) => {
510
490
  return;
511
491
  }
512
492
  const lines = [];
493
+ applyMultiplePrimaryKeys(shape, tableData);
513
494
  const state = {
514
495
  migration,
515
496
  tableName,
516
497
  values: [],
517
498
  indexes: [],
518
- comments: []
499
+ comments: [],
500
+ tableData
519
501
  };
520
502
  for (const key in shape) {
521
503
  const item = shape[key];
522
504
  addColumnIndex(state.indexes, key, item);
523
505
  addColumnComment(state.comments, key, item);
524
- lines.push(`
525
- ${columnToSql(key, item, state)}`);
506
+ lines.push(
507
+ `
508
+ ${columnToSql(key, item, state.values, tableData.primaryKey)}`
509
+ );
526
510
  }
527
511
  if (tableData.primaryKey) {
528
512
  lines.push(`
@@ -546,6 +530,44 @@ const createTable = async (migration, up, tableName, options, fn) => {
546
530
  );
547
531
  }
548
532
  };
533
+ const validatePrimaryKey = (migration, options, tableData, shape, tableName) => {
534
+ var _a, _b;
535
+ const { noPrimaryKey } = migration.options;
536
+ if (!options.noPrimaryKey && (!noPrimaryKey || noPrimaryKey !== "ignore")) {
537
+ let hasPrimaryKey = !!((_b = (_a = tableData.primaryKey) == null ? void 0 : _a.columns) == null ? void 0 : _b.length);
538
+ if (!hasPrimaryKey) {
539
+ for (const key in shape) {
540
+ if (shape[key].isPrimaryKey) {
541
+ hasPrimaryKey = true;
542
+ break;
543
+ }
544
+ }
545
+ }
546
+ if (!hasPrimaryKey) {
547
+ const message = `Table ${tableName} has no primary key`;
548
+ if (!noPrimaryKey || noPrimaryKey === "error") {
549
+ throw new Error(message);
550
+ } else {
551
+ console.warn(message);
552
+ }
553
+ }
554
+ }
555
+ };
556
+ const applyMultiplePrimaryKeys = (shape, tableData) => {
557
+ const columns = [];
558
+ for (const key in shape) {
559
+ if (shape[key].isPrimaryKey) {
560
+ columns.push(key);
561
+ }
562
+ }
563
+ if (columns.length <= 1)
564
+ return;
565
+ if (!tableData.primaryKey) {
566
+ tableData.primaryKey = { columns };
567
+ } else {
568
+ tableData.primaryKey.columns.unshift(...columns);
569
+ }
570
+ };
549
571
 
550
572
  var __defProp$1 = Object.defineProperty;
551
573
  var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
@@ -629,11 +651,33 @@ const tableChangeMethods = {
629
651
  }
630
652
  };
631
653
  const changeTable = async (migration, up, tableName, options, fn) => {
654
+ var _a;
632
655
  resetTableData();
633
656
  resetChangeTableData();
634
657
  const tableChanger = Object.create(columnTypes);
635
658
  Object.assign(tableChanger, tableChangeMethods);
636
659
  const changeData = (fn == null ? void 0 : fn(tableChanger)) || {};
660
+ const addPrimaryKeys = {
661
+ columns: []
662
+ };
663
+ const dropPrimaryKeys = {
664
+ columns: []
665
+ };
666
+ for (const key in changeData) {
667
+ const item = changeData[key];
668
+ if (Array.isArray(item)) {
669
+ const [action] = item;
670
+ if (action === "add") {
671
+ if (item[1].isPrimaryKey) {
672
+ addPrimaryKeys.columns.push(key);
673
+ }
674
+ } else if (action === "drop") {
675
+ if (item[1].isPrimaryKey) {
676
+ dropPrimaryKeys.columns.push(key);
677
+ }
678
+ }
679
+ }
680
+ }
637
681
  const state = {
638
682
  migration,
639
683
  up,
@@ -642,37 +686,70 @@ const changeTable = async (migration, up, tableName, options, fn) => {
642
686
  values: [],
643
687
  indexes: [],
644
688
  dropIndexes: [],
645
- comments: []
689
+ comments: [],
690
+ addPrimaryKeys,
691
+ dropPrimaryKeys
646
692
  };
647
693
  if (options.comment !== void 0) {
648
694
  await changeActions.tableComment(state, tableName, options.comment);
649
695
  }
650
696
  for (const key in changeData) {
651
- const result = changeData[key];
652
- if (Array.isArray(result)) {
653
- const [action] = result;
697
+ const item = changeData[key];
698
+ if (Array.isArray(item)) {
699
+ const [action] = item;
654
700
  if (action === "change") {
655
- const [, from, to, options2] = result;
701
+ const [, from, to, options2] = item;
656
702
  changeActions.change(state, up, key, from, to, options2);
657
703
  } else if (action === "rename") {
658
- const [, name] = result;
704
+ const [, name] = item;
659
705
  changeActions.rename(state, up, key, name);
660
706
  } else if (action) {
661
- const [action2, item, options2] = result;
662
- changeActions[action2](state, up, key, item, options2);
707
+ const [action2, columnType, options2] = item;
708
+ changeActions[action2](state, up, key, columnType, options2);
663
709
  }
664
710
  }
665
711
  }
712
+ const prependAlterTable = [];
713
+ let addedDownKey = false;
666
714
  changeTableData[up ? "drop" : "add"].forEach((tableData) => {
667
- handleTableData(state, false, tableName, tableData);
715
+ if (tableData.primaryKey) {
716
+ addedDownKey = true;
717
+ const keys = state[up ? "dropPrimaryKeys" : "addPrimaryKeys"];
718
+ keys.columns.push(...tableData.primaryKey.columns);
719
+ keys.options = tableData.primaryKey.options;
720
+ }
721
+ state.dropIndexes.push(...tableData.indexes);
722
+ prependAlterTable.push(...getForeignKeysLines(state, false, tableData));
668
723
  });
724
+ const dropKeys = state[up ? "dropPrimaryKeys" : "addPrimaryKeys"];
725
+ if (addedDownKey || dropKeys.change || dropKeys.columns.length > 1) {
726
+ const name = ((_a = dropKeys.options) == null ? void 0 : _a.name) || `${tableName}_pkey`;
727
+ prependAlterTable.push(`DROP CONSTRAINT "${name}"`);
728
+ }
729
+ let addedUpKey = false;
669
730
  changeTableData[up ? "add" : "drop"].forEach((tableData) => {
670
- handleTableData(state, true, tableName, tableData);
731
+ if (tableData.primaryKey) {
732
+ addedUpKey = true;
733
+ const keys = state[up ? "addPrimaryKeys" : "dropPrimaryKeys"];
734
+ keys.columns.push(...tableData.primaryKey.columns);
735
+ keys.options = tableData.primaryKey.options;
736
+ }
737
+ state.indexes.push(...tableData.indexes);
738
+ state.alterTable.push(...getForeignKeysLines(state, true, tableData));
671
739
  });
672
- if (state.alterTable.length) {
740
+ const addKeys = state[up ? "addPrimaryKeys" : "dropPrimaryKeys"];
741
+ if (addedUpKey || addKeys.change || addKeys.columns.length > 1) {
742
+ state.alterTable.push(`ADD ${primaryKeyToSql(addKeys)}`);
743
+ }
744
+ if (prependAlterTable.length || state.alterTable.length) {
673
745
  await migration.query(
674
746
  `ALTER TABLE ${quoteTable(tableName)}
675
- ${state.alterTable.join(",\n ")}`
747
+ ${[
748
+ ...prependAlterTable,
749
+ ...state.alterTable.map(
750
+ (item) => typeof item === "string" ? item : item(state)
751
+ )
752
+ ].join(",\n ")}`
676
753
  );
677
754
  }
678
755
  await migrateIndexes(state, state.dropIndexes, false);
@@ -697,7 +774,14 @@ const changeActions = {
697
774
  addColumnComment(state.comments, key, item);
698
775
  }
699
776
  if (up) {
700
- state.alterTable.push(`ADD COLUMN ${columnToSql(key, item, state)}`);
777
+ state.alterTable.push(
778
+ (state2) => `ADD COLUMN ${columnToSql(
779
+ key,
780
+ item,
781
+ state2.values,
782
+ (state2.up ? state2.addPrimaryKeys : state2.dropPrimaryKeys).columns.length > 1
783
+ )}`
784
+ );
701
785
  } else {
702
786
  state.alterTable.push(
703
787
  `DROP COLUMN "${key}"${(options == null ? void 0 : options.dropMode) ? ` ${options.dropMode}` : ""}`
@@ -732,6 +816,11 @@ const changeActions = {
732
816
  `ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
733
817
  );
734
818
  }
819
+ if (from.primaryKey || to.primaryKey) {
820
+ const primaryKey = state[up && to.primaryKey || !up && from.primaryKey ? "addPrimaryKeys" : "dropPrimaryKeys"];
821
+ primaryKey.columns.push(key);
822
+ primaryKey.change = true;
823
+ }
735
824
  const fromFkey = from.foreignKey;
736
825
  const toFkey = to.foreignKey;
737
826
  if (fromFkey || toFkey) {
@@ -813,6 +902,7 @@ const getChangeProperties = (item) => {
813
902
  nullable: item.isNullable,
814
903
  comment: item.data.comment,
815
904
  compression: item.data.compression,
905
+ primaryKey: item.isPrimaryKey,
816
906
  foreignKey: item.data.foreignKey,
817
907
  index: item.data.index
818
908
  };
@@ -824,33 +914,21 @@ const getChangeProperties = (item) => {
824
914
  nullable: item[0] === "nullable" ? item[1] : void 0,
825
915
  comment: item[0] === "comment" ? item[1] : void 0,
826
916
  compression: item[0] === "compression" ? item[1] : void 0,
917
+ primaryKey: item[0] === "primaryKey" ? item[1] : void 0,
827
918
  foreignKey: item[0] === "foreignKey" ? item[1] : void 0,
828
919
  index: item[0] === "index" ? item[1] : void 0
829
920
  };
830
921
  }
831
922
  };
832
- const handleTableData = (state, up, tableName, tableData) => {
833
- var _a;
834
- if (tableData.primaryKey) {
835
- if (up) {
836
- state.alterTable.push(`ADD ${primaryKeyToSql(tableData.primaryKey)}`);
837
- } else {
838
- const name = ((_a = tableData.primaryKey.options) == null ? void 0 : _a.name) || `${tableName}_pkey`;
839
- state.alterTable.push(`DROP CONSTRAINT "${name}"`);
840
- }
841
- }
842
- if (tableData.indexes.length) {
843
- state[up ? "indexes" : "dropIndexes"].push(...tableData.indexes);
844
- }
845
- if (tableData.foreignKeys.length) {
846
- tableData.foreignKeys.forEach((foreignKey) => {
847
- const action = up ? "ADD" : "DROP";
848
- state.alterTable.push(
849
- `
850
- ${action} ${constraintToSql(state.tableName, up, foreignKey)}`
851
- );
852
- });
853
- }
923
+ const getForeignKeysLines = (state, up, tableData) => {
924
+ return tableData.foreignKeys.map(
925
+ (foreignKey) => `
926
+ ${up ? "ADD" : "DROP"} ${constraintToSql(
927
+ state.tableName,
928
+ up,
929
+ foreignKey
930
+ )}`
931
+ );
854
932
  };
855
933
  const getRawOrValue = (item, values) => {
856
934
  return typeof item === "object" && item && isRaw(item) ? getRaw(item, values) : quote(item);