fix: quote string literals with single quotes in Explain - #242
Open
h2zi wants to merge 2 commits into
Open
Conversation
Explain wrapped string values in double quotes, which are identifier quoting in SQLite — SQL copied from the logs misparses the value as a column reference. Use standard single-quoted string literals instead. GORM also embeds string default values into CREATE TABLE through Explain, so the DDL parser now strips single quotes from parsed default values as well (double quotes are still stripped for tables created by older driver versions); without that, migrations would stop being idempotent. Covered by a default-value round-trip test. Fixes go-gorm#197
There was a problem hiding this comment.
Pull request overview
Updates the SQLite dialector’s SQL rendering to use correct SQLite string-literal quoting in Explain, and adjusts DDL default-value parsing to keep migrations idempotent when defaults are emitted via Explain.
Changes:
- Switch
Dialector.Explainto use single-quoted string literals (avoids misparsing double quotes as identifiers in SQLite). - Update
parseDDLdefault-value extraction to strip string-literal quotes for both new (single-quoted) and legacy (double-quoted) tables. - Add tests covering
Explainstring quoting and default-value round-tripping throughAutoMigrate, including legacy double-quoted defaults.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| sqlite.go | Explain now emits single-quoted string literals for SQLite correctness. |
| ddlmod.go | Default-value parsing updated to remove surrounding quotes so migrations remain idempotent across versions. |
| sqlite_test.go | Adds coverage for Explain quoting and default parsing / idempotent AutoMigrate. |
Suppressed comments (1)
sqlite_test.go:184
- The result of
db.Raw(...).Scan(&after)is not checked. If the query fails,afterstays empty and the test may pass/fail for the wrong reason. Capture and assert the error fromScan.
var after string
db.Raw("SELECT sql FROM sqlite_master WHERE type='table' AND name='explain_defaults'").Scan(&after)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced Jul 31, 2026
Address review feedback: strings.Trim removes every leading/trailing character from the set, so DEFAULT '"x"' parsed to x instead of "x". Strip a single matching pair instead, and fail the round-trip test on query errors.
h2zi
added a commit
to libtnb/sqlite
that referenced
this pull request
Jul 31, 2026
…traint names Two fixes surfaced by review feedback on the upstream ports (go-gorm/sqlite#242, #246): - strings.Trim removed every leading/trailing quote character from the set, so DEFAULT '"x"' parsed to x instead of "x". A trimQuote helper strips exactly one matching outer pair. - compileConstraintRegexp only accepted backquotes and double quotes around the constraint name; [name] and 'name' forms now match too. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Symptom
Explainwraps string values in double quotes (name = "hello"), but double quotes are identifier quoting in SQLite — SQL copied from the logs misparses the value as a column reference (#197).Fix
Use standard single-quoted string literals in
Explain.One coupled change is required: GORM embeds string default values into
CREATE TABLEDDL throughDialector.Explain(FullDataTypeOf→DEFAULT 'hello'), and the DDL parser only stripped double quotes from parsed default values. With Explain fixed in isolation,DefaultValue()would return'hello'with quotes and everyAutoMigratewould try to re-alter the column. The parser now strips single quotes too, and keeps stripping double quotes so tables created by older driver versions still parse.Tests
TestExplainQuotesStrings— literal quoting.TestDefaultValueRoundTrip— default value parses back clean, a secondAutoMigrateleaves the DDL untouched, and a legacy double-quoted default still parses.Fixes #197
🤖 Generated with Claude Code
Merge order (this batch: #241 → #242 → #243 → #244 → #245 → #246, plus #234 from the previous batch): all seven are functionally independent and merge cleanly in any order. Several append tests to the same test files, so whichever merges later may show a trivial append-only conflict in
migrator_test.go/ddlmod_test.go— I'll rebase the remaining ones promptly after each merge, as before.