Skip to content

Fix recurring crash when Connection deinitializes - #1373

Open
vjymisal0 wants to merge 3 commits into
stephencelis:masterfrom
vjymisal0:codex/close-v2-deinit-fix
Open

Fix recurring crash when Connection deinitializes#1373
vjymisal0 wants to merge 3 commits into
stephencelis:masterfrom
vjymisal0:codex/close-v2-deinit-fix

Conversation

@vjymisal0

Copy link
Copy Markdown

Fixes #1276nnUse sqlite3_close_v2 from deinit so an out-of-scope Connection does not crash when statements are still outstanding. I could not run Swift tests in this environment because the Swift toolchain is unavailable.

vjymisal0 and others added 3 commits August 10, 2026 21:36
The `~=` operator overloads used for Range/ClosedRange/PartialRange
pattern matching (e.g. `0...26 ~= column`) build their SQL fragment
without wrapping it in parentheses, unlike every other comparison
operator (`>`, `<`, `==`, etc.), which all self-wrap via
`Operator.infix(...)`.

This is harmless standalone (e.g. inside `.filter(...)`), but it
produces invalid SQL when used as a column-level CHECK constraint via
`table.create { t in t.column(col, check: 0...26 ~= col) }`, because
Schema.swift's per-column `definition()` relies on the check
expression already being parenthesized (it just does
`"CHECK" + " " + check`, same as the table-level `check()` builder
relies on `.prefix` to add its own parens). Since BETWEEN/range
expressions weren't self-wrapped, the generated SQL came out as:

    CREATE TABLE "t" ("type" INTEGER NOT NULL CHECK "type" BETWEEN 0 AND 26)

which SQLite rejects with a syntax error near the CHECK condition,
exactly as reported in stephencelis#1056.

Fixes stephencelis#1056.

Fix: wrap the `~=` operators' generated SQL in parens, matching the
convention already used by every other comparison operator. Updated
the existing BETWEEN/range operator tests to expect the parenthesized
output, and added a regression test in SchemaTests that reproduces the
exact column-level CHECK scenario from the issue and asserts the
generated CREATE TABLE statement is now well-formed.

Verified by tracing the SQL-generation code path end-to-end (Operator
`infix`/`wrap` helpers, the `~=` overloads, and Schema.swift's
`definition()`/`check()`) since a full Swift toolchain wasn't
available in this environment to run `swift test` directly; the
updated/added unit tests exercise this exact path and should be run
by CI.
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.

Recurring crash when connection object goes out of scope

2 participants