-
Notifications
You must be signed in to change notification settings - Fork 30
fix: quote reserved keywords after schema prefix stripping in function bodies (#320) #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tianzhou
merged 1 commit into
main
from
fix/issue-320-quote-reserved-keywords-after-schema-strip
Feb 25, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
testdata/dump/issue_320_plpgsql_reserved_keyword_type/manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "issue_320_plpgsql_reserved_keyword_type", | ||
| "description": "Test case for reserved keywords becoming unquoted after schema prefix stripping in function/procedure bodies (GitHub issue #320)", | ||
| "source": "https://github.com/pgplex/pgschema/issues/320", | ||
| "notes": [ | ||
| "Reproduces the bug where schema-qualified reserved keywords (e.g., app.user) lose quoting when the schema prefix is stripped", | ||
| "Tests that reserved keyword identifiers are properly double-quoted after schema prefix removal in DECLARE sections and SQL statements", | ||
| "Tests that non-reserved identifiers remain unquoted after schema prefix removal" | ||
| ] | ||
| } |
86 changes: 86 additions & 0 deletions
86
testdata/dump/issue_320_plpgsql_reserved_keyword_type/pgdump.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| -- | ||
| -- PostgreSQL database dump | ||
| -- | ||
|
|
||
| SET statement_timeout = 0; | ||
| SET lock_timeout = 0; | ||
| SET client_encoding = 'UTF8'; | ||
| SET standard_conforming_strings = on; | ||
| SET check_function_bodies = false; | ||
| SET client_min_messages = warning; | ||
| SET row_security = off; | ||
|
|
||
| -- | ||
| -- Name: user; Type: TABLE; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| CREATE TABLE public."user" ( | ||
| id integer NOT NULL, | ||
| name text NOT NULL, | ||
| email text | ||
| ); | ||
|
|
||
| -- | ||
| -- Name: user_id_seq; Type: SEQUENCE; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| CREATE SEQUENCE public.user_id_seq | ||
| AS integer | ||
| START WITH 1 | ||
| INCREMENT BY 1 | ||
| NO MINVALUE | ||
| NO MAXVALUE | ||
| CACHE 1; | ||
|
|
||
| -- | ||
| -- Name: user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| ALTER SEQUENCE public.user_id_seq OWNED BY public."user".id; | ||
|
|
||
| -- | ||
| -- Name: user id; Type: DEFAULT; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| ALTER TABLE ONLY public."user" ALTER COLUMN id SET DEFAULT nextval('public.user_id_seq'::regclass); | ||
|
|
||
| -- | ||
| -- Name: user user_pkey; Type: CONSTRAINT; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| ALTER TABLE ONLY public."user" | ||
| ADD CONSTRAINT user_pkey PRIMARY KEY (id); | ||
|
|
||
| -- | ||
| -- Name: get_first_user(); Type: FUNCTION; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| CREATE FUNCTION public.get_first_user() RETURNS text | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| account public.user; | ||
| BEGIN | ||
| SELECT * INTO account FROM public.user LIMIT 1; | ||
| RETURN account.name; | ||
| END; | ||
| $$; | ||
|
|
||
| -- | ||
| -- Name: count_users(); Type: FUNCTION; Schema: public; Owner: - | ||
| -- | ||
|
|
||
| CREATE FUNCTION public.count_users() RETURNS integer | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| total_count integer; | ||
| BEGIN | ||
| SELECT count(*)::integer INTO total_count FROM public."user"; | ||
| RETURN total_count; | ||
| END; | ||
| $$; | ||
|
|
||
| -- | ||
| -- PostgreSQL database dump complete | ||
| -- |
53 changes: 53 additions & 0 deletions
53
testdata/dump/issue_320_plpgsql_reserved_keyword_type/pgschema.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| -- | ||
| -- pgschema database dump | ||
| -- | ||
|
|
||
| -- Dumped from database version PostgreSQL 18.0 | ||
| -- Dumped by pgschema version 1.7.2 | ||
|
|
||
|
|
||
| -- | ||
| -- Name: user; Type: TABLE; Schema: -; Owner: - | ||
| -- | ||
|
|
||
| CREATE TABLE IF NOT EXISTS "user" ( | ||
| id SERIAL, | ||
| name text NOT NULL, | ||
| email text, | ||
| CONSTRAINT user_pkey PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| -- | ||
| -- Name: count_users(); Type: FUNCTION; Schema: -; Owner: - | ||
| -- | ||
|
|
||
| CREATE OR REPLACE FUNCTION count_users() | ||
| RETURNS integer | ||
| LANGUAGE plpgsql | ||
| VOLATILE | ||
| AS $$ | ||
| DECLARE | ||
| total_count integer; | ||
| BEGIN | ||
| SELECT count(*)::integer INTO total_count FROM "user"; | ||
| RETURN total_count; | ||
| END; | ||
| $$; | ||
|
|
||
| -- | ||
| -- Name: get_first_user(); Type: FUNCTION; Schema: -; Owner: - | ||
| -- | ||
|
|
||
| CREATE OR REPLACE FUNCTION get_first_user() | ||
| RETURNS text | ||
| LANGUAGE plpgsql | ||
| VOLATILE | ||
| AS $$ | ||
| DECLARE | ||
| account "user"; | ||
| BEGIN | ||
| SELECT * INTO account FROM "user" LIMIT 1; | ||
| RETURN account.name; | ||
| END; | ||
| $$; | ||
|
|
39 changes: 39 additions & 0 deletions
39
testdata/dump/issue_320_plpgsql_reserved_keyword_type/raw.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| -- | ||
| -- Test case for GitHub issue #320: Reserved keywords after schema prefix stripping | ||
| -- | ||
| -- When a function body contains schema-qualified references like public.user, | ||
| -- stripping the schema prefix should produce "user" (quoted) since user is | ||
| -- a reserved keyword. Without quoting, the dumped SQL is syntactically invalid. | ||
| -- | ||
|
|
||
| CREATE TABLE "user" ( | ||
| id serial PRIMARY KEY, | ||
| name text NOT NULL, | ||
| email text | ||
| ); | ||
|
|
||
| -- Function using schema-qualified reserved keyword type in DECLARE and body | ||
| CREATE OR REPLACE FUNCTION get_first_user() | ||
| RETURNS text | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| account public.user; | ||
| BEGIN | ||
| SELECT * INTO account FROM public.user LIMIT 1; | ||
| RETURN account.name; | ||
| END; | ||
| $$; | ||
|
|
||
| -- Function with non-reserved type name (should NOT be affected) | ||
| CREATE OR REPLACE FUNCTION count_users() | ||
| RETURNS integer | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| total_count integer; | ||
| BEGIN | ||
| SELECT count(*)::integer INTO total_count FROM public."user"; | ||
| RETURN total_count; | ||
| END; | ||
| $$; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding unit tests for this new reserved keyword quoting logic in
ir/normalize_test.go. While the integration test is comprehensive, unit tests would provide faster feedback and better test isolation. Suggested test cases:public.user→"user"(reserved keyword)public.order→"order"(another reserved keyword)public.users→users(non-reserved, should not be quoted)public."user"→"user"(already quoted, should remain quoted)account public.user→account "user"(in DECLARE context)public.user.name→"user".name(field access)