Fix DDL bugs in schema, enums, indexes, and triggers tools - #7
Closed
bq011 wants to merge 1 commit into
Closed
Conversation
Five bugs uncovered by smoke-testing pg_manage_schema, pg_manage_indexes
and pg_manage_triggers against PostgreSQL 15:
1. create_table / alter_table ignored the `schema` parameter and always
wrote to public. Added schema to the input zod schema and qualified
identifiers as "schema"."table".
2. create_enum used `$1` placeholders inside CREATE TYPE, which
PostgreSQL rejects in DDL ("syntax error at or near $1"). Switched
to escaped string literals.
3. create_enum with ifNotExists=true emitted `CREATE TYPE IF NOT EXISTS`,
which PostgreSQL does not support. Wrapped the statement in a DO
block that checks pg_type/pg_namespace before EXECUTE.
4. get_indexes / analyze_index_usage queried pg_stat_user_indexes using
`tablename` and `indexname` columns that do not exist on that view
(the actual columns are `relname` / `indexrelname`). Aliased them
correctly and qualified WHERE clauses with `psi.`.
5. create_trigger wrapped the entire function name in a single pair of
double quotes, so `public.touch_updated_at` became the literal
identifier `"public.touch_updated_at"`. Quote each dotted part
separately, and default bare names to the trigger's own schema.
Verified by re-running the failing scenarios against PostgreSQL 15.17
in an isolated schema.
Owner
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Five bugs uncovered by smoke-testing
pg_manage_schema,pg_manage_indexesandpg_manage_triggersagainst PostgreSQL 15. All five reproduce onmainand pass after this PR.1.
create_table/alter_tableignored theschemaparameterThe Zod schema for the schema-management operations omitted
schema, and the SQL builders hardcoded unqualified table names. Tables silently landed inpubliceven when the caller asked for another schema. Fixed by addingschemato the input schema and qualifying identifiers as"schema"."table".2.
create_enumused$1placeholders insideCREATE TYPEPostgreSQL does not accept parameter placeholders in DDL — the server returned
syntax error at or near "$1". Replaced with escaped string literals (single-quote doubling for safety).3.
create_enumwithifNotExists=trueemittedCREATE TYPE IF NOT EXISTSPostgreSQL has no
IF NOT EXISTSclause forCREATE TYPE. Wrapped the statement in aDOblock that checkspg_type/pg_namespacebeforeEXECUTE-ing the create.4.
get_indexes/analyze_index_usagereferenced non-existent columnsThe queries selected
tablenameandindexnamefrompg_stat_user_indexes, but that view exposesrelnameandindexrelname. The result wascolumn "tablename" does not exist. Aliased topsi.relname AS tablename/psi.indexrelname AS indexnameand qualified theWHEREpredicates withpsi..5.
create_triggermis-quoted schema-qualified function namesconst qualifiedFunctionName = \"${functionName}"`turnedpublic.touch_updated_atinto the literal identifier"public.touch_updated_at", so PostgreSQL reportedfunction "public.touch_updated_at" does not exist`. Now each dotted part is quoted independently, and bare names default to the trigger's own schema.Test plan
npm install && npm run buildcleanmcp_smokeschema against PostgreSQL 15.17 — all passpg_stat_user_indexesquery returns the expected row shape after creating an indexCREATE TRIGGER ... EXECUTE FUNCTION "schema"."fn"()resolves the function correctly