diff --git a/__fixtures__/plpgsql-pretty/trigger-function.sql b/__fixtures__/plpgsql-pretty/trigger-function.sql new file mode 100644 index 00000000..a8e1ea7c --- /dev/null +++ b/__fixtures__/plpgsql-pretty/trigger-function.sql @@ -0,0 +1,22 @@ +CREATE FUNCTION trigger_with_special_vars() RETURNS trigger +LANGUAGE plpgsql +AS $$ +BEGIN + IF TG_OP = 'INSERT' THEN + NEW.created_at := now(); + RETURN NEW; + ELSIF TG_OP = 'UPDATE' THEN + IF OLD.id IS DISTINCT FROM NEW.id THEN + RAISE EXCEPTION 'IMMUTABLE_FIELD'; + END IF; + NEW.updated_at := now(); + RETURN NEW; + ELSIF TG_OP = 'DELETE' THEN + RETURN OLD; + END IF; + IF NOT FOUND THEN + RAISE EXCEPTION 'NOT_FOUND'; + END IF; + RETURN NEW; +END; +$$ diff --git a/packages/plpgsql-deparser/__tests__/pretty/__snapshots__/plpgsql-pretty.test.ts.snap b/packages/plpgsql-deparser/__tests__/pretty/__snapshots__/plpgsql-pretty.test.ts.snap index afe0e983..23713970 100644 --- a/packages/plpgsql-deparser/__tests__/pretty/__snapshots__/plpgsql-pretty.test.ts.snap +++ b/packages/plpgsql-deparser/__tests__/pretty/__snapshots__/plpgsql-pretty.test.ts.snap @@ -208,6 +208,27 @@ exports[`lowercase: simple-function.sql 1`] = ` end" `; +exports[`lowercase: trigger-function.sql 1`] = ` +"begin + if TG_OP = 'INSERT' then + NEW.created_at := now(); + return NEW; + elsif TG_OP = 'UPDATE' then + if OLD.id IS DISTINCT FROM NEW.id then + raise exception 'IMMUTABLE_FIELD'; + end if; + NEW.updated_at := now(); + return NEW; + elsif TG_OP = 'DELETE' then + return OLD; + end if; + if NOT FOUND then + raise exception 'NOT_FOUND'; + end if; + return NEW; +end" +`; + exports[`uppercase: big-function.sql 1`] = ` "DECLARE v_orders_scanned int := 0; @@ -415,3 +436,24 @@ exports[`uppercase: simple-function.sql 1`] = ` RETURN a + b; END" `; + +exports[`uppercase: trigger-function.sql 1`] = ` +"BEGIN + IF TG_OP = 'INSERT' THEN + NEW.created_at := now(); + RETURN NEW; + ELSIF TG_OP = 'UPDATE' THEN + IF OLD.id IS DISTINCT FROM NEW.id THEN + RAISE EXCEPTION 'IMMUTABLE_FIELD'; + END IF; + NEW.updated_at := now(); + RETURN NEW; + ELSIF TG_OP = 'DELETE' THEN + RETURN OLD; + END IF; + IF NOT FOUND THEN + RAISE EXCEPTION 'NOT_FOUND'; + END IF; + RETURN NEW; +END" +`; diff --git a/packages/plpgsql-deparser/__tests__/pretty/plpgsql-pretty.test.ts b/packages/plpgsql-deparser/__tests__/pretty/plpgsql-pretty.test.ts index 1cf7c434..be593b73 100644 --- a/packages/plpgsql-deparser/__tests__/pretty/plpgsql-pretty.test.ts +++ b/packages/plpgsql-deparser/__tests__/pretty/plpgsql-pretty.test.ts @@ -5,6 +5,7 @@ const prettyTest = new PlpgsqlPrettyTest([ 'simple-function.sql', 'if-else-function.sql', 'loop-function.sql', + 'trigger-function.sql', ]); prettyTest.generateTests();