Skip to content

Commit

Permalink
fix: triggers w/ the same name on different schemas are duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Mar 21, 2024
1 parent a736ab5 commit dd47c6d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/sql/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ON pg_t.tgrelid = pg_c.oid
JOIN information_schema.triggers AS is_t
ON is_t.trigger_name = pg_t.tgname
AND pg_c.relname = is_t.event_object_table
AND pg_c.relnamespace = is_t.event_object_schema::regnamespace
JOIN pg_proc AS pg_p
ON pg_t.tgfoid = pg_p.oid
JOIN pg_namespace AS pg_n
Expand Down
47 changes: 47 additions & 0 deletions test/lib/triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,50 @@ test('multi event', async () => {
)
await pgMeta.triggers.remove(res.data!.id)
})

test('triggers with the same name on different schemas', async () => {
await pgMeta.query(`
create function tr_f() returns trigger language plpgsql as 'begin end';
create schema s1; create table s1.t(); create trigger tr before insert on s1.t execute function tr_f();
create schema s2; create table s2.t(); create trigger tr before insert on s2.t execute function tr_f();
`)

const res = await pgMeta.triggers.list()
const triggers = res.data?.map(({ id, table_id, ...trigger }) => trigger)
expect(triggers).toMatchInlineSnapshot(`
[
{
"activation": "BEFORE",
"condition": null,
"enabled_mode": "ORIGIN",
"events": [
"INSERT",
],
"function_args": [],
"function_name": "tr_f",
"function_schema": "public",
"name": "tr",
"orientation": "STATEMENT",
"schema": "s1",
"table": "t",
},
{
"activation": "BEFORE",
"condition": null,
"enabled_mode": "ORIGIN",
"events": [
"INSERT",
],
"function_args": [],
"function_name": "tr_f",
"function_schema": "public",
"name": "tr",
"orientation": "STATEMENT",
"schema": "s2",
"table": "t",
},
]
`)

await pgMeta.query('drop schema s1 cascade; drop schema s2 cascade;')
})

0 comments on commit dd47c6d

Please sign in to comment.