Skip to content

fix(Migrator): harden DDL matching and error reporting - #246

Open
h2zi wants to merge 2 commits into
go-gorm:masterfrom
h2zi:fix-migrator-hardening
Open

fix(Migrator): harden DDL matching and error reporting#246
h2zi wants to merge 2 commits into
go-gorm:masterfrom
h2zi:fix-migrator-hardening

Conversation

@h2zi

@h2zi h2zi commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

A batch of small, related fixes to how the migrator matches DDL and reports failures. Each is independently small; grouped because they share the theme (and the tests).

  • GetTables: exclude sqlite_* internal tables — sqlite_sequence appeared in the list as soon as any table used AUTOINCREMENT.
  • HasConstraint: match the exact constraint name via the parsed DDL and the existing compileConstraintRegexp instead of LIKE substrings, which matched prefixes (age_pos matched age_positive).
  • DropColumn: report an error when the column is not present in the DDL — removeColumn returned false silently and the table was rebuilt unchanged, so callers saw success without any effect.
  • removeColumn: match unquoted column names — the regexp required a quote or space before the name, so columns of hand-written DDL like CREATE TABLE t (id integer, name text) were never matched (same silent no-op as above).
  • getRawDDL: report table not found instead of passing an empty string to parseDDL, which surfaced as a confusing invalid DDL.
  • uniqueRegexp: recognize lowercase / no-space table-level unique(...) constraints; every other DDL regexp is already case-insensitive. Written to not mistake a column named uniqueid for a constraint (requires the parenthesized column list).

Tests: TestGetTablesExcludesInternal, TestConstraintAndColumnMatching, TestParseDDL_LowercaseUnique.

No dependency on the other open PRs — merges cleanly in any order.

🤖 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.

Five related fixes to how the migrator matches DDL and reports
failures:

- GetTables: exclude sqlite_* internal tables; sqlite_sequence appeared
  in the list as soon as any table used AUTOINCREMENT.
- HasConstraint: match the exact constraint name via the parsed DDL and
  compileConstraintRegexp instead of LIKE substrings, which matched
  prefixes and names embedded in other text.
- DropColumn: report an error when the column is not present in the
  DDL; removeColumn returned false silently and the table was rebuilt
  unchanged.
- removeColumn: match unquoted column names; the regexp required a
  quote or space before the name, so columns of hand-written DDL like
  "CREATE TABLE t (id integer, name text)" were never matched.
- getRawDDL: report "table not found" instead of passing an empty
  string to parseDDL, which surfaced as a confusing "invalid DDL".
- uniqueRegexp: recognize lowercase and no-space table-level
  unique(...) constraints; every other DDL regexp is already
  case-insensitive.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens the SQLite migrator’s DDL parsing/matching logic to avoid false positives and silent no-ops, and improves error reporting around missing tables/columns.

Changes:

  • Filter GetTables() results to exclude sqlite_* internal tables.
  • Rework HasConstraint() to parse table DDL and match exact constraint names; make DropColumn() error when a column is not found in the parsed DDL; improve getRawDDL() “table not found” reporting.
  • Extend DDL parsing to recognize lowercase / no-space table-level unique(...) constraints; loosen column-removal matching to handle unquoted column definitions.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
migrator.go Updates table listing, constraint detection, drop-column behavior, and DDL retrieval error handling.
migrator_test.go Adds tests covering internal-table exclusion, constraint-name matching, and drop-column failure modes.
ddlmod.go Adjusts DDL regexes for UNIQUE parsing and improves column removal matching for unquoted DDL.
ddlmod_test.go Adds a regression test for lowercase table-level unique(...) parsing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread migrator.go
Comment thread ddlmod_test.go
Address review feedback: the LIKE patterns that HasConstraint used
before explicitly handled [name], which compileConstraintRegexp did
not. It now accepts backquotes, double quotes, single quotes, brackets
or no quoting, covered by TestConstraintNameQuoting across all forms.
Also fail TestParseDDL_LowercaseUnique when the column is not parsed
at all.
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.

2 participants