fix(schema-diff): keep deploy script preview in sync with selection
This commit is contained in:
1 parent
1302c0a27b
commit
1637948023
6 files changed
+225
-61
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { convertToSchemaDiffObjects, selectSchemaDiffInput, selectSchemaDiffInputForObject, setSchemaDiffObjectSelected, type SchemaDiffPreparation } from "../../schema/schemaDiff";
|
||||
|
||||
function createPreparation(): SchemaDiffPreparation {
|
||||
return {
|
||||
diffs: [
|
||||
{
|
||||
type: "modified",
|
||||
objectType: "table",
|
||||
name: "table_a",
|
||||
columns: [
|
||||
{ type: "added", name: "a_col" },
|
||||
{ type: "added", name: "a_other_col" },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "modified",
|
||||
objectType: "table",
|
||||
name: "table_b",
|
||||
columns: [{ type: "added", name: "b_col" }],
|
||||
},
|
||||
],
|
||||
syncSql: "-- table_a and table_b",
|
||||
};
|
||||
}
|
||||
|
||||
describe("schema diff SQL selection projections", () => {
|
||||
it("keeps the focused table SQL separate from the full selected SQL", () => {
|
||||
const result = createPreparation();
|
||||
const objects = convertToSchemaDiffObjects(result.diffs);
|
||||
|
||||
const selectedInput = selectSchemaDiffInput(result, objects);
|
||||
const focusedInput = selectSchemaDiffInputForObject(result, objects, "table-table_a");
|
||||
|
||||
expect(selectedInput.diffs.map((diff) => diff.name)).toEqual(["table_a", "table_b"]);
|
||||
expect(focusedInput.diffs.map((diff) => diff.name)).toEqual(["table_a"]);
|
||||
expect(focusedInput.diffs[0]?.columns?.map((column) => column.name)).toEqual(["a_col", "a_other_col"]);
|
||||
});
|
||||
|
||||
it("projects a child object without including sibling or parent changes", () => {
|
||||
const result = createPreparation();
|
||||
const objects = convertToSchemaDiffObjects(result.diffs);
|
||||
|
||||
const focusedInput = selectSchemaDiffInputForObject(result, objects, "col-table_a-a_col");
|
||||
|
||||
expect(focusedInput.diffs.map((diff) => diff.name)).toEqual(["table_a"]);
|
||||
expect(focusedInput.diffs[0]?.columns?.map((column) => column.name)).toEqual(["a_col"]);
|
||||
});
|
||||
|
||||
it("updates the selected input after a checkbox change while leaving focused projection independent", () => {
|
||||
const result = createPreparation();
|
||||
const objects = convertToSchemaDiffObjects(result.diffs);
|
||||
|
||||
setSchemaDiffObjectSelected(objects, "table-table_b", false);
|
||||
setSchemaDiffObjectSelected(objects, "col-table_a-a_col", false);
|
||||
|
||||
const selectedInput = selectSchemaDiffInput(result, objects);
|
||||
const focusedInput = selectSchemaDiffInputForObject(result, objects, "table-table_a");
|
||||
|
||||
expect(selectedInput.diffs.map((diff) => diff.name)).toEqual(["table_a"]);
|
||||
expect(selectedInput.diffs[0]?.columns?.map((column) => column.name)).toEqual(["a_other_col"]);
|
||||
expect(focusedInput.diffs[0]?.columns?.map((column) => column.name)).toEqual(["a_col", "a_other_col"]);
|
||||
});
|
||||
|
||||
it("returns an empty input for an object that is no longer present", () => {
|
||||
const result = createPreparation();
|
||||
const objects = convertToSchemaDiffObjects(result.diffs);
|
||||
|
||||
expect(selectSchemaDiffInputForObject(result, objects, "missing-object")).toEqual({
|
||||
diffs: [],
|
||||
functionDiffs: [],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -802,6 +802,26 @@ export function selectSchemaDiffInput(result: SchemaDiffPreparation, objects: Sc
|
||||
};
|
||||
}
|
||||
|
||||
function projectSchemaDiffObjectSelection(object: SchemaDiffObject, focusedObjectId: string, selectDescendants: boolean): SchemaDiffObject {
|
||||
const isFocused = object.id === focusedObjectId;
|
||||
const selectChildren = selectDescendants || isFocused;
|
||||
return {
|
||||
...object,
|
||||
selected: isFocused || selectDescendants,
|
||||
children: object.children?.map((child) => projectSchemaDiffObjectSelection(child, focusedObjectId, selectChildren)),
|
||||
};
|
||||
}
|
||||
|
||||
/** Project one focused tree object onto a fresh selection without mutating checkbox state. */
|
||||
export function selectSchemaDiffInputForObject(result: SchemaDiffPreparation, objects: SchemaDiffObject[], objectId: string): SelectedSchemaDiffInput {
|
||||
if (!findSchemaDiffObject(objects, objectId)) {
|
||||
return { diffs: [], functionDiffs: [], sequenceDiffs: [], ruleDiffs: [], ownerDiffs: [] };
|
||||
}
|
||||
|
||||
const focusedSelection = objects.map((object) => projectSchemaDiffObjectSelection(object, objectId, false));
|
||||
return selectSchemaDiffInput(result, focusedSelection);
|
||||
}
|
||||
|
||||
export function buildDeploySqlForObjects(objects: SchemaDiffObject[]): string {
|
||||
const selected = objects.filter((o) => {
|
||||
return o.selected && o.operationType !== "none" && !o.parentId;
|
||||
|
||||
Reference in new issue
Block a user