fix(Migrator): don't panic in AlterColumn/DropColumn when value is a table-name string - #234
fix(Migrator): don't panic in AlterColumn/DropColumn when value is a table-name string#234h2zi wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a nil-pointer panic in the SQLite GORM migrator when AlterColumn/DropColumn are called with a table-name string (i.e., stmt.Schema == nil), aligning behavior with GORM’s base migrator and other dialects.
Changes:
- Guard
DropColumn’s schema field lookup behindstmt.Schema != nilto prevent panics when called with a table-name string. - Make
AlterColumnreturn a regular error (instead of panicking) whenstmt.Schema == nil, since it requires schema to compute the new column type. - Add a regression test covering both
DropColumn(success) andAlterColumn(error) for the table-name string calling style.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| migrator.go | Prevents nil dereference in DropColumn and converts AlterColumn’s schema-nil panic into a regular error. |
| migrator_test.go | Adds regression coverage for table-name-string calls to DropColumn/AlterColumn. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
f7a0fc8 to
601033c
Compare
|
Rebased onto master and resolved the same |
|
The failing CI check here is unrelated to this PR — the matrix still tests Go 1.19, which no longer compiles |
…table-name string
The base GORM migrator accepts a table-name string as the value for
migrator methods (RunWithValue sets stmt.Table and leaves stmt.Schema
nil), and the other dialects support this. The sqlite driver
dereferenced stmt.Schema unconditionally in AlterColumn and DropColumn,
so both panicked with a nil pointer dereference:
db.Migrator().DropColumn("my_table", "my_column")
DropColumn works fine without a schema (the column name is used as-is),
so it now just skips the field lookup. AlterColumn cannot build the new
column type without the model schema and returns an error instead of
panicking.
Keeps repeated runs (go test -count=N) starting from a clean state, matching the review feedback on the sibling PRs.
601033c to
6a98322
Compare
What this PR does
The base GORM migrator accepts a table-name string as the
valueargument (RunWithValuesetsstmt.Tableand leavesstmt.Schemanil), and the other official dialects support this calling style. The sqlite driver dereferencesstmt.Schemaunconditionally inAlterColumnandDropColumn, so both crash with a nil pointer dereference:The fix
DropColumnworks fine without a model schema (the given column name is used as-is), so it now just skips the field lookup, mirroring thestmt.Schema != nilguard thatHasColumnalready has.AlterColumncannot build the new column type without the model schema, so it returns a regular error instead of panicking.Tests
TestMigratorStringTableNamecovers both paths:DropColumnwith a table-name string succeeds and actually drops the column,AlterColumnreturns an error instead of panicking.🤖 Generated with Claude Code