rake-db 2.33.10 → 2.33.11

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
@@ -521,6 +521,7 @@ const addOrDropChanges = [];
521
521
  function add(item, options) {
522
522
  consumeColumnName();
523
523
  setName(this, item);
524
+ if (isNoForeignKeyChangeInput(item)) throw new Error("t.noForeignKey() is only supported in t.change(...)");
524
525
  if (item instanceof Column) {
525
526
  const result = addOrDrop("add", item, options);
526
527
  if (result.type === "change") return result;
@@ -544,6 +545,7 @@ function add(item, options) {
544
545
  const drop = function(item, options) {
545
546
  consumeColumnName();
546
547
  setName(this, item);
548
+ if (isNoForeignKeyChangeInput(item)) throw new Error("t.noForeignKey() is only supported in t.change(...)");
547
549
  if (item instanceof Column) {
548
550
  const result = addOrDrop("drop", item, options);
549
551
  if (result.type === "change") return result;
@@ -587,6 +589,28 @@ const addOrDrop = (type, item, options) => {
587
589
  dropMode: options?.dropMode
588
590
  };
589
591
  };
592
+ const isColumnForeignKeyChangeInput = (item) => {
593
+ return "columnForeignKey" in item;
594
+ };
595
+ const isNoForeignKeyChangeInput = (item) => {
596
+ return "noForeignKey" in item;
597
+ };
598
+ const isCheckConstraintItem = (item) => {
599
+ return !!item.constraint?.check;
600
+ };
601
+ const changeInputToColumnChange = (item, opposite) => {
602
+ if (item instanceof Column || "type" in item) return columnTypeToColumnChange(item);
603
+ if (isColumnForeignKeyChangeInput(item)) return { foreignKeys: [item.columnForeignKey] };
604
+ if (isNoForeignKeyChangeInput(item)) {
605
+ if (!isColumnForeignKeyChangeInput(opposite)) throw new Error("t.noForeignKey() in t.change(...) must be paired with t.foreignKey(...)");
606
+ return { foreignKeys: [] };
607
+ }
608
+ if (isCheckConstraintItem(item)) return { checks: [{
609
+ sql: item.constraint.check,
610
+ name: item.constraint.name
611
+ }] };
612
+ throw new Error("t.change(...) supports t.check(...) only for check constraints in this form");
613
+ };
590
614
  const columnTypeToColumnChange = (item, name) => {
591
615
  if (item instanceof Column) {
592
616
  let column = item;
@@ -618,6 +642,18 @@ const setName = (self, item) => {
618
642
  else if (item instanceof Column) item.data.name ??= name;
619
643
  else item.name ??= name;
620
644
  };
645
+ function foreignKey(...args) {
646
+ if (Array.isArray(args[0])) {
647
+ const [columns, fnOrTable, foreignColumns, options] = args;
648
+ return tableDataMethods.foreignKey(columns, fnOrTable, foreignColumns, options);
649
+ }
650
+ const [fnOrTable, foreignColumn, options] = args;
651
+ return { columnForeignKey: {
652
+ fnOrTable,
653
+ foreignColumns: [foreignColumn],
654
+ options
655
+ } };
656
+ }
621
657
  const tableChangeMethods = {
622
658
  ...tableMethods,
623
659
  ...tableDataMethods,
@@ -629,10 +665,14 @@ const tableChangeMethods = {
629
665
  },
630
666
  add,
631
667
  drop,
668
+ foreignKey,
669
+ noForeignKey() {
670
+ return { noForeignKey: true };
671
+ },
632
672
  change(from, to, using) {
633
673
  consumeColumnName();
634
- const f = columnTypeToColumnChange(from);
635
- const t = columnTypeToColumnChange(to);
674
+ const f = changeInputToColumnChange(from, to);
675
+ const t = changeInputToColumnChange(to, from);
636
676
  setName(this, f);
637
677
  setName(this, t);
638
678
  return {