orchid-orm 1.72.4 → 1.72.5
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/migrations/index.js
CHANGED
|
@@ -572,7 +572,7 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
|
|
|
572
572
|
if (generatorIgnore?.schemas?.includes(dbEnum.schemaName) || generatorIgnore?.enums?.includes(dbEnum.name)) continue;
|
|
573
573
|
const codeEnum = enums.get(`${dbEnum.schemaName}.${dbEnum.name}`);
|
|
574
574
|
if (codeEnum) {
|
|
575
|
-
changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
|
|
575
|
+
await changeEnum(ast, dbEnum, codeEnum, pendingDbTypes, verifying);
|
|
576
576
|
continue;
|
|
577
577
|
}
|
|
578
578
|
const i = createEnums.findIndex((x) => x.name === dbEnum.name);
|
|
@@ -591,7 +591,7 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
|
|
|
591
591
|
to: dbEnum.name
|
|
592
592
|
});
|
|
593
593
|
pendingDbTypes.add(toSchema, dbEnum.name);
|
|
594
|
-
changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
|
|
594
|
+
await changeEnum(ast, dbEnum, codeEnum, pendingDbTypes, verifying);
|
|
595
595
|
continue;
|
|
596
596
|
}
|
|
597
597
|
dropEnums.push(dbEnum);
|
|
@@ -617,7 +617,7 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
|
|
|
617
617
|
to
|
|
618
618
|
});
|
|
619
619
|
pendingDbTypes.add(toSchema, to);
|
|
620
|
-
changeEnum(ast, dbEnum, codeEnum, pendingDbTypes);
|
|
620
|
+
await changeEnum(ast, dbEnum, codeEnum, pendingDbTypes, verifying);
|
|
621
621
|
continue;
|
|
622
622
|
}
|
|
623
623
|
}
|
|
@@ -636,34 +636,63 @@ const processEnums = async (ast, dbStructure, { codeItems: { enums }, currentSch
|
|
|
636
636
|
values: dbEnum.values
|
|
637
637
|
});
|
|
638
638
|
};
|
|
639
|
-
const changeEnum = (ast, dbEnum, codeEnum, pendingDbTypes) => {
|
|
639
|
+
const changeEnum = async (ast, dbEnum, codeEnum, pendingDbTypes, verifying) => {
|
|
640
640
|
const { values: dbValues } = dbEnum;
|
|
641
641
|
const { values: codeValues, schema, name } = codeEnum;
|
|
642
|
+
const addValues = codeValues.filter((value) => !dbValues.includes(value));
|
|
643
|
+
const dropValues = dbValues.filter((value) => !codeValues.includes(value));
|
|
642
644
|
if (dbValues.length < codeValues.length) {
|
|
643
|
-
if (!
|
|
645
|
+
if (!dropValues.length) {
|
|
644
646
|
ast.push({
|
|
645
647
|
type: "enumValues",
|
|
646
648
|
action: "add",
|
|
647
649
|
schema,
|
|
648
650
|
name,
|
|
649
|
-
values:
|
|
651
|
+
values: addValues
|
|
650
652
|
});
|
|
651
653
|
pendingDbTypes.add(schema, name);
|
|
652
654
|
return;
|
|
653
655
|
}
|
|
654
656
|
} else if (dbValues.length > codeValues.length) {
|
|
655
|
-
if (!
|
|
657
|
+
if (!addValues.length) {
|
|
656
658
|
ast.push({
|
|
657
659
|
type: "enumValues",
|
|
658
660
|
action: "drop",
|
|
659
661
|
schema,
|
|
660
662
|
name,
|
|
661
|
-
values:
|
|
663
|
+
values: dropValues
|
|
662
664
|
});
|
|
663
665
|
pendingDbTypes.add(schema, name);
|
|
664
666
|
return;
|
|
665
667
|
}
|
|
666
|
-
} else if (!
|
|
668
|
+
} else if (!dropValues.length) return;
|
|
669
|
+
const enumValueChanges = await promptEnumValueChanges(name, dbValues, codeValues, addValues, dropValues, verifying);
|
|
670
|
+
if (enumValueChanges) {
|
|
671
|
+
let changed = false;
|
|
672
|
+
if (Object.keys(enumValueChanges.renamedValues).length) {
|
|
673
|
+
ast.push({
|
|
674
|
+
type: "renameEnumValues",
|
|
675
|
+
schema,
|
|
676
|
+
name,
|
|
677
|
+
values: enumValueChanges.renamedValues
|
|
678
|
+
});
|
|
679
|
+
changed = true;
|
|
680
|
+
}
|
|
681
|
+
if (enumValueChanges.fromValues) {
|
|
682
|
+
ast.push({
|
|
683
|
+
type: "changeEnumValues",
|
|
684
|
+
schema,
|
|
685
|
+
name,
|
|
686
|
+
fromValues: enumValueChanges.fromValues,
|
|
687
|
+
toValues: enumValueChanges.toValues
|
|
688
|
+
});
|
|
689
|
+
changed = true;
|
|
690
|
+
}
|
|
691
|
+
if (changed) {
|
|
692
|
+
pendingDbTypes.add(schema, name);
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
667
696
|
ast.push({
|
|
668
697
|
type: "changeEnumValues",
|
|
669
698
|
schema,
|
|
@@ -673,6 +702,33 @@ const changeEnum = (ast, dbEnum, codeEnum, pendingDbTypes) => {
|
|
|
673
702
|
});
|
|
674
703
|
pendingDbTypes.add(schema, name);
|
|
675
704
|
};
|
|
705
|
+
const promptEnumValueChanges = async (enumName, dbValues, codeValues, addValues, dropValues, verifying) => {
|
|
706
|
+
if (!addValues.length || !dropValues.length) return;
|
|
707
|
+
const renamedValues = {};
|
|
708
|
+
const remainingDropValues = [...dropValues];
|
|
709
|
+
for (const value of addValues) if (remainingDropValues.length) {
|
|
710
|
+
if (verifying) throw new AbortSignal();
|
|
711
|
+
const i = await (0, rake_db.promptSelect)({
|
|
712
|
+
message: `Add or rename ${pqb_internal.colors.blueBold(value)} enum value in ${pqb_internal.colors.blueBold(enumName)}?`,
|
|
713
|
+
options: [`${pqb_internal.colors.greenBold("+")} ${value} ${pqb_internal.colors.pale("add enum value")}`, ...remainingDropValues.map((dropValue) => `${pqb_internal.colors.yellowBold("~")} ${dropValue} ${pqb_internal.colors.yellowBold("=>")} ${value} ${pqb_internal.colors.pale("rename enum value")}`)]
|
|
714
|
+
});
|
|
715
|
+
if (i) {
|
|
716
|
+
const dropValue = remainingDropValues[i - 1];
|
|
717
|
+
remainingDropValues.splice(i - 1, 1);
|
|
718
|
+
renamedValues[dropValue] = value;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
const fromValues = dbValues.map((value) => renamedValues[value] ?? value);
|
|
722
|
+
const toValues = codeValues;
|
|
723
|
+
return fromValues.some((value, i) => value !== toValues[i]) ? {
|
|
724
|
+
renamedValues,
|
|
725
|
+
fromValues,
|
|
726
|
+
toValues
|
|
727
|
+
} : {
|
|
728
|
+
renamedValues,
|
|
729
|
+
toValues
|
|
730
|
+
};
|
|
731
|
+
};
|
|
676
732
|
const renameColumnsTypeSchema = (dbStructure, from, to) => {
|
|
677
733
|
for (const table of dbStructure.tables) for (const column of table.columns) if (column.typeSchema === from) column.typeSchema = to;
|
|
678
734
|
};
|
|
@@ -2610,16 +2666,21 @@ const report = (ast, config, currentSchema) => {
|
|
|
2610
2666
|
case "enumValues":
|
|
2611
2667
|
code.push(`${a.action === "add" ? green("+ add values to enum") : red("- remove values from enum")} ${dbItemName(a, currentSchema)}: ${a.values.join(", ")}`);
|
|
2612
2668
|
break;
|
|
2613
|
-
case "changeEnumValues":
|
|
2614
|
-
|
|
2615
|
-
|
|
2669
|
+
case "changeEnumValues": {
|
|
2670
|
+
const fromValues = a.fromValues.filter((value) => !a.toValues.includes(value));
|
|
2671
|
+
const toValues = a.toValues.filter((value) => !a.fromValues.includes(value));
|
|
2672
|
+
if (fromValues.length) code.push(`${red("- remove values from enum")} ${dbItemName(a, currentSchema)}: ${fromValues.join(", ")}`);
|
|
2673
|
+
if (toValues.length) code.push(`${green("+ add values to enum")} ${dbItemName(a, currentSchema)}: ${toValues.join(", ")}`);
|
|
2674
|
+
break;
|
|
2675
|
+
}
|
|
2676
|
+
case "renameEnumValues":
|
|
2677
|
+
code.push(`${yellow("~ rename values in enum")} ${dbItemName(a, currentSchema)}: ${Object.entries(a.values).map(([from, to]) => `${from} ${yellow("=>")} ${to}`).join(", ")}`);
|
|
2616
2678
|
break;
|
|
2617
2679
|
case "domain":
|
|
2618
2680
|
code.push(`${a.action === "create" ? green("+ create domain") : red("- drop domain")} ${dbItemName(a, currentSchema)}`);
|
|
2619
2681
|
break;
|
|
2620
2682
|
case "view":
|
|
2621
2683
|
case "collation":
|
|
2622
|
-
case "renameEnumValues":
|
|
2623
2684
|
case "constraint": break;
|
|
2624
2685
|
case "renameTableItem":
|
|
2625
2686
|
code.push(`${yellow(`~ rename ${a.kind.toLowerCase()}`)} on table ${dbItemName({
|