Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/dump/dump_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func TestDumpCommand_Issue307ViewDependencyOrder(t *testing.T) {
runExactMatchTest(t, "issue_307_view_dependency_order")
}

func TestDumpCommand_Issue320PlpgsqlReservedKeywordType(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_320_plpgsql_reserved_keyword_type")
}

func TestDumpCommand_Issue318CrossSchemaComment(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
Expand Down
14 changes: 14 additions & 0 deletions ir/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,20 @@ func stripSchemaPrefixFromBody(body, schema string) string {
// Ensure this is a schema qualifier, not part of a longer identifier
// (e.g., "not_public.users" should not match)
if i == 0 || !isIdentChar(body[i-1]) {
// After stripping the schema prefix, check if the remaining identifier
// is a reserved keyword that needs quoting.
// e.g., public.user → "user", public.order → "order"
afterPrefix := i + prefixLen
identEnd := afterPrefix
for identEnd < len(body) && isIdentChar(body[identEnd]) {
identEnd++
}
ident := body[afterPrefix:identEnd]
if needsQuoting(ident) {
result.WriteString(QuoteIdentifier(ident))
i = identEnd - 1
continue
}
Comment on lines +391 to +404
Copy link

Copilot AI Feb 25, 2026

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.usersusers (non-reserved, should not be quoted)
  • public."user""user" (already quoted, should remain quoted)
  • account public.useraccount "user" (in DECLARE context)
  • public.user.name"user".name (field access)

Copilot uses AI. Check for mistakes.
// Skip the schema prefix, keep everything after it
i += prefixLen - 1
continue
Expand Down
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 testdata/dump/issue_320_plpgsql_reserved_keyword_type/pgdump.sql
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 testdata/dump/issue_320_plpgsql_reserved_keyword_type/pgschema.sql
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 testdata/dump/issue_320_plpgsql_reserved_keyword_type/raw.sql
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;
$$;