From 7234384afb6d18e1ca3d6c291ebe7db111773c06 Mon Sep 17 00:00:00 2001 From: Kyle Scott Date: Tue, 7 Jan 2025 15:44:12 -0500 Subject: [PATCH] fix duplicate triggers --- .changeset/modern-ads-mate.md | 5 +++++ packages/core/src/database/index.test.ts | 21 +++++++++++++++++++++ packages/core/src/database/index.ts | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .changeset/modern-ads-mate.md diff --git a/.changeset/modern-ads-mate.md b/.changeset/modern-ads-mate.md new file mode 100644 index 000000000..3afb7ef33 --- /dev/null +++ b/.changeset/modern-ads-mate.md @@ -0,0 +1,5 @@ +--- +"ponder": patch +--- + +Fixed a bug causing the error: `trigger "_reorg__xyz" for relation "xyz" already exists`. diff --git a/packages/core/src/database/index.test.ts b/packages/core/src/database/index.test.ts index d1e30ce39..fcb5149e2 100644 --- a/packages/core/src/database/index.test.ts +++ b/packages/core/src/database/index.test.ts @@ -648,6 +648,27 @@ test("createTriggers()", async (context) => { await database.kill(); }); +test("createTriggers() duplicate", async (context) => { + const database = createDatabase({ + common: context.common, + preBuild: { + databaseConfig: context.databaseConfig, + namespace: "public", + }, + schemaBuild: { + schema: { account }, + statements: buildSchema({ schema: { account } }).statements, + }, + }); + + await database.setup({ buildId: "abc" }); + await database.createTriggers(); + await database.createTriggers(); + + await database.unlock(); + await database.kill(); +}); + test("complete()", async (context) => { const database = createDatabase({ common: context.common, diff --git a/packages/core/src/database/index.ts b/packages/core/src/database/index.ts index 44cd32929..321615394 100644 --- a/packages/core/src/database/index.ts +++ b/packages/core/src/database/index.ts @@ -1032,7 +1032,7 @@ $$ LANGUAGE plpgsql await sql .raw(` - CREATE TRIGGER "${tableName.trigger}" + CREATE OR REPLACE TRIGGER "${tableName.trigger}" AFTER INSERT OR UPDATE OR DELETE ON "${preBuild.namespace}"."${tableName.sql}" FOR EACH ROW EXECUTE FUNCTION ${tableName.triggerFn}; `)