orchid-orm 1.72.4 → 1.72.6

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.
@@ -549,7 +549,7 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
549
549
  if (generatorIgnore?.schemas?.includes(dbEnum.schemaName) || generatorIgnore?.enums?.includes(dbEnum.name)) continue;
550
550
  const codeEnum = enums.get(`${dbEnum.schemaName}.${dbEnum.name}`);
551
551
  if (codeEnum) {
552
- changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
552
+ await changeEnum(ast, dbEnum, codeEnum, pendingDbTypes, verifying);
553
553
  continue;
554
554
  }
555
555
  const i = createEnums.findIndex((x) => x.name === dbEnum.name);
@@ -568,7 +568,7 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
568
568
  to: dbEnum.name
569
569
  });
570
570
  pendingDbTypes.add(toSchema, dbEnum.name);
571
- changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
571
+ await changeEnum(ast, dbEnum, codeEnum, pendingDbTypes, verifying);
572
572
  continue;
573
573
  }
574
574
  dropEnums.push(dbEnum);
@@ -594,7 +594,7 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
594
594
  to
595
595
  });
596
596
  pendingDbTypes.add(toSchema, to);
597
- changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
597
+ await changeEnum(ast, dbEnum, codeEnum, pendingDbTypes, verifying);
598
598
  continue;
599
599
  }
600
600
  }
@@ -613,34 +613,63 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
613
613
  values: dbEnum.values
614
614
  });
615
615
  };
616
- const changeEnum = (ast, dbEnum, codeEnum, pendingDbTypes) => {
616
+ const changeEnum = async (ast, dbEnum, codeEnum, pendingDbTypes, verifying) => {
617
617
  const { values: dbValues } = dbEnum;
618
618
  const { values: codeValues, schema, name } = codeEnum;
619
+ const addValues = codeValues.filter((value) => !dbValues.includes(value));
620
+ const dropValues = dbValues.filter((value) => !codeValues.includes(value));
619
621
  if (dbValues.length < codeValues.length) {
620
- if (!dbValues.some((value) => !codeValues.includes(value))) {
622
+ if (!dropValues.length) {
621
623
  ast.push({
622
624
  type: "enumValues",
623
625
  action: "add",
624
626
  schema,
625
627
  name,
626
- values: codeValues.filter((value) => !dbValues.includes(value))
628
+ values: addValues
627
629
  });
628
630
  pendingDbTypes.add(schema, name);
629
631
  return;
630
632
  }
631
633
  } else if (dbValues.length > codeValues.length) {
632
- if (!codeValues.some((value) => !dbValues.includes(value))) {
634
+ if (!addValues.length) {
633
635
  ast.push({
634
636
  type: "enumValues",
635
637
  action: "drop",
636
638
  schema,
637
639
  name,
638
- values: dbValues.filter((value) => !codeValues.includes(value))
640
+ values: dropValues
639
641
  });
640
642
  pendingDbTypes.add(schema, name);
641
643
  return;
642
644
  }
643
- } else if (!dbValues.some((value) => !codeValues.includes(value))) return;
645
+ } else if (!dropValues.length) return;
646
+ const enumValueChanges = await promptEnumValueChanges(name, dbValues, codeValues, addValues, dropValues, verifying);
647
+ if (enumValueChanges) {
648
+ let changed = false;
649
+ if (Object.keys(enumValueChanges.renamedValues).length) {
650
+ ast.push({
651
+ type: "renameEnumValues",
652
+ schema,
653
+ name,
654
+ values: enumValueChanges.renamedValues
655
+ });
656
+ changed = true;
657
+ }
658
+ if (enumValueChanges.fromValues) {
659
+ ast.push({
660
+ type: "changeEnumValues",
661
+ schema,
662
+ name,
663
+ fromValues: enumValueChanges.fromValues,
664
+ toValues: enumValueChanges.toValues
665
+ });
666
+ changed = true;
667
+ }
668
+ if (changed) {
669
+ pendingDbTypes.add(schema, name);
670
+ return;
671
+ }
672
+ }
644
673
  ast.push({
645
674
  type: "changeEnumValues",
646
675
  schema,
@@ -650,6 +679,33 @@ const changeEnum = (ast, dbEnum, codeEnum, pendingDbTypes) => {
650
679
  });
651
680
  pendingDbTypes.add(schema, name);
652
681
  };
682
+ const promptEnumValueChanges = async (enumName, dbValues, codeValues, addValues, dropValues, verifying) => {
683
+ if (!addValues.length || !dropValues.length) return;
684
+ const renamedValues = {};
685
+ const remainingDropValues = [...dropValues];
686
+ for (const value of addValues) if (remainingDropValues.length) {
687
+ if (verifying) throw new AbortSignal();
688
+ const i = await promptSelect({
689
+ message: `Add or rename ${colors.blueBold(value)} enum value in ${colors.blueBold(enumName)}?`,
690
+ options: [`${colors.greenBold("+")} ${value} ${colors.pale("add enum value")}`, ...remainingDropValues.map((dropValue) => `${colors.yellowBold("~")} ${dropValue} ${colors.yellowBold("=>")} ${value} ${colors.pale("rename enum value")}`)]
691
+ });
692
+ if (i) {
693
+ const dropValue = remainingDropValues[i - 1];
694
+ remainingDropValues.splice(i - 1, 1);
695
+ renamedValues[dropValue] = value;
696
+ }
697
+ }
698
+ const fromValues = dbValues.map((value) => renamedValues[value] ?? value);
699
+ const toValues = codeValues;
700
+ return fromValues.some((value, i) => value !== toValues[i]) ? {
701
+ renamedValues,
702
+ fromValues,
703
+ toValues
704
+ } : {
705
+ renamedValues,
706
+ toValues
707
+ };
708
+ };
653
709
  const renameColumnsTypeSchema = (dbStructure, from, to) => {
654
710
  for (const table of dbStructure.tables) for (const column of table.columns) if (column.typeSchema === from) column.typeSchema = to;
655
711
  };
@@ -2587,16 +2643,21 @@ const report = (ast, config, currentSchema) => {
2587
2643
  case "enumValues":
2588
2644
  code.push(`${a.action === "add" ? green("+ add values to enum") : red("- remove values from enum")} ${dbItemName(a, currentSchema)}: ${a.values.join(", ")}`);
2589
2645
  break;
2590
- case "changeEnumValues":
2591
- if (a.fromValues) code.push(`${red("- remove values from enum")} ${dbItemName(a, currentSchema)}: ${a.fromValues.join(", ")}`);
2592
- if (a.toValues) code.push(`${green("+ add values to enum")} ${dbItemName(a, currentSchema)}: ${a.toValues.join(", ")}`);
2646
+ case "changeEnumValues": {
2647
+ const fromValues = a.fromValues.filter((value) => !a.toValues.includes(value));
2648
+ const toValues = a.toValues.filter((value) => !a.fromValues.includes(value));
2649
+ if (fromValues.length) code.push(`${red("- remove values from enum")} ${dbItemName(a, currentSchema)}: ${fromValues.join(", ")}`);
2650
+ if (toValues.length) code.push(`${green("+ add values to enum")} ${dbItemName(a, currentSchema)}: ${toValues.join(", ")}`);
2651
+ break;
2652
+ }
2653
+ case "renameEnumValues":
2654
+ code.push(`${yellow("~ rename values in enum")} ${dbItemName(a, currentSchema)}: ${Object.entries(a.values).map(([from, to]) => `${from} ${yellow("=>")} ${to}`).join(", ")}`);
2593
2655
  break;
2594
2656
  case "domain":
2595
2657
  code.push(`${a.action === "create" ? green("+ create domain") : red("- drop domain")} ${dbItemName(a, currentSchema)}`);
2596
2658
  break;
2597
2659
  case "view":
2598
2660
  case "collation":
2599
- case "renameEnumValues":
2600
2661
  case "constraint": break;
2601
2662
  case "renameTableItem":
2602
2663
  code.push(`${yellow(`~ rename ${a.kind.toLowerCase()}`)} on table ${dbItemName({