Skip to content

Commit

Permalink
fix(triggers): list triggers on captitalized schema namespaces
Browse files Browse the repository at this point in the history
fix an issue where listing triggers declared over a schema with capitalized name
resulted into an error 3F000
  • Loading branch information
avallete authored and soedirgo committed Jan 8, 2025
1 parent 8b6c7e3 commit fc6120a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/sql/triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +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
AND pg_c.relnamespace = (quote_ident(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
48 changes: 48 additions & 0 deletions test/lib/triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,51 @@ create schema s2; create table s2.t(); create trigger tr before insert on s2.t e

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

test('triggers on capitalized schema and table names', async () => {
await pgMeta.query(`
CREATE SCHEMA "MySchema";
CREATE TABLE "MySchema"."MyTable" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP
);
CREATE OR REPLACE FUNCTION "MySchema"."my_trigger_function"()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER "my_trigger"
BEFORE INSERT ON "MySchema"."MyTable"
FOR EACH ROW
EXECUTE FUNCTION "MySchema"."my_trigger_function"();
`)

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": "my_trigger_function",
"function_schema": "MySchema",
"name": "my_trigger",
"orientation": "ROW",
"schema": "MySchema",
"table": "MyTable",
},
]
`)

await pgMeta.query('drop schema "MySchema" cascade;')
})

0 comments on commit fc6120a

Please sign in to comment.