Skip to content

Don't treat [ as an identifier quote on PostgreSQL or MySQL - #11

Merged
quinnj-bot[bot] merged 2 commits into
mainfrom
claude/postgres-array-brackets
Nov 25, 2026
Merged

quinnj-bot[bot] merged 2 commits into
mainfrom
claude/postgres-array-brackets

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

[comment generated via AI]

What went wrong: on PostgreSQL, a valid migration like this one failed:

CREATE TABLE codes (code text CHECK (code = ANY (ARRAY['x]', 'y'])));
CREATE TABLE after_it (id int);
Postgres.Error: Code: 42601
  Message: cannot insert multiple commands into a prepared statement

Root cause: the statement splitter treats [...] as a quoted identifier, as in SQLite and SQL Server. In PostgreSQL, brackets are array syntax. The ] inside 'x]' closed the bracket "quote", the following ' opened a string that never closed, and everything after it went to the server as one statement. The same happens with any array literal whose strings contain ], such as regex patterns like ARRAY['[0-9]+']. With LibPQ's simple-query path the joined text would still run, but Postgres.jl prepares each statement and rejects it.

Fix: splitsqlstatements gets a bracketquotes flag. runmigrations turns it off for PostgreSQL connections (LibPQ, Postgres.jl) and MySQL, neither of which quotes identifiers with brackets. SQLite, ODBC and other drivers keep bracket quoting. The one-argument splitsqlstatements(sql) is unchanged.

Verified:

  • A new test runs that migration through the Postgres.jl stand-in and checks that both statements execute separately. It fails on main and passes with the fix. The full suite passes on Julia 1.12 (284 tests).
  • Ran the migration through Postgres.jl against a local PostgreSQL 14. On main: the error above. With the fix: both tables are created and the history row is recorded.

🤖 Generated with Claude Code

quinnj and others added 2 commits November 25, 2026 06:36
In PostgreSQL, brackets are array syntax. A ] inside a string in an ARRAY
literal (ARRAY['x]']) ended the bracket "quote" early, and the next ' opened
a string that ran to the end of the file, so the rest of the migration went
to the server as one statement. Postgres.jl then rejected it with "cannot
insert multiple commands into a prepared statement".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MySQL has no bracket arrays; it simply never quotes identifiers with
brackets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@quinnj-bot quinnj-bot Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[review generated via AI]

Verdict: PASS. I pushed one comment-only commit (5557660).

Checked

  • With main's src, the new test fails 2 of 2: the whole file reaches the driver as one statement. On the head it passes.
  • The root cause is right. [ started a "quoted identifier" that closed at the ] inside 'x]', so the next ' opened a string that never closed.
  • SQLite and ODBC are unchanged. bracketquotes = !mysqlcomments && !usesdollarmarkers(conn) is true for SQLite, ODBC and any unrecognized driver, and the public one-argument splitsqlstatements(sql) still bracket-quotes. A real SQLite run through runmigrations on the head with CREATE TABLE [we;ird] ...; INSERT INTO [we;ird] ...; CREATE TABLE [it's] ...; INSERT INTO [it's] ... applies cleanly and reads back 1 and 2. That shows both ; and ' inside brackets are still protected.
  • Dropping bracket quoting on MySQL is right. MySQL quotes identifiers with backticks (or double quotes under ANSI_QUOTES) and has no bracket syntax outside string literals such as JSON paths, so [ never had a quoting meaning there.
  • Real PostgreSQL 14 through Postgres.jl on the head: a migration with ARRAY['x]', 'y'], DEFAULT ARRAY['[0-9]+;', 'a]b'] (a ; and a ] inside array strings), an insert, and an array slice (ARRAY[1,2,3])[2:3] applies, and the stored value reads back as {[0-9]+;,a]b}.
  • There are no other internal callers of the splitter. The README and runmigrations docstring say "quoted identifiers" generically, which stays accurate.

Pushed: the new code comment said "PostgreSQL and MySQL use brackets for array subscripts and literals". MySQL has no arrays; the reason is that it never quotes with brackets. I reworded that comment only. The suite still passes (284/284).

Ran: full Pkg.test() on Julia 1.12, on the head and with main's src. CI passes on 1.6 (the floor), 1 and nightly. The only failing check is codecov/project, the known baseline mismatch.

Not covered, not a blocker: an ODBC connection to PostgreSQL or Snowflake (both use [...] for arrays) still bracket-quotes, because the driver is detected by package, not backend. splitstatements=false is the workaround there.

@quinnj-bot
quinnj-bot Bot merged commit 5f51dab into main Nov 25, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant