fix(Migrator): don't panic in AlterColumn/DropColumn when value is a table-name string#234
Open
h2zi wants to merge 2 commits into
Open
fix(Migrator): don't panic in AlterColumn/DropColumn when value is a table-name string#234h2zi wants to merge 2 commits into
h2zi wants to merge 2 commits into
Conversation
…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.
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.
Keeps repeated runs (go test -count=N) starting from a clean state, matching the review feedback on the sibling PRs.
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.
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