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}; `)