Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't trigger own change control on file.data write #3262

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,61 @@ test("file.data is not own change controlled as plugins handle the change contro

expect(change.content?.data).toBe(undefined);
});

test("updating file.data does not trigger own change control", async () => {
const lix = await openLixInMemory({});

const file = await lix.db
.insertInto("file")
.values({
id: "file1",
data: new TextEncoder().encode("hello world"),
path: "/mock.txt",
metadata: null,
})
.returningAll()
.executeTakeFirstOrThrow();

let changes = await lix.db
.selectFrom("change")
.where("schema_key", "=", "lix_file_table")
.where("entity_id", "=", file.id)
.selectAll()
.execute();

expect(changes.length).toBe(1);

await lix.db
.updateTable("file")
.set({ data: new TextEncoder().encode("hello world 2") })
.where("id", "=", file.id)
.execute();

changes = await lix.db
.selectFrom("change")
.where("schema_key", "=", "lix_file_table")
.where("entity_id", "=", file.id)
.selectAll()
.execute();

expect(changes.length).toBe(1);

// the metadata should be change controlled
// hence, this should trigger a change
await lix.db
.updateTable("file")
.set({
data: new TextEncoder().encode("hello world 3"),
metadata: { key: "value" },
})
.execute();

changes = await lix.db
.selectFrom("change")
.where("schema_key", "=", "lix_file_table")
.where("entity_id", "=", file.id)
.selectAll()
.execute();

expect(changes.length).toBe(2);
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ export function applyOwnEntityChangeControlTriggers(

CREATE TEMP TRIGGER IF NOT EXISTS ${table}_change_control_update
AFTER UPDATE ON ${table}
${
// ignore update trigger if the change controlled properties
// did not change (a plugin likely called apply changes on the file.data)
table === "file"
? `
WHEN (
OLD.id IS NOT NEW.id OR
OLD.path IS NOT NEW.path OR
OLD.metadata IS NOT NEW.metadata
)`
: ""
}
BEGIN
SELECT handle_lix_own_entity_change('${table}', 'update', ${tableInfo.map((c) => "NEW." + c.name).join(", ")});
END;
Expand Down
Loading