Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automigration fails for existing table if model has a boolean field. #6923

Closed
wants to merge 5 commits into from

Conversation

ayakut
Copy link

@ayakut ayakut commented Mar 26, 2024

  • Do only one thing
  • Non breaking API changes
  • Tested: project's unit tests and on connection to real Cockroach DB.

What did this pull request do?

The PR fixes how the MigrateColumn() method calculates an initial value of the isSameType flag.

User Case Description

Environment:
Go 1.21
DB: Cockroach DB ()
used versions

github.com/jackc/pgx/v5 v5.5.5
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.8

Issue reproduction:

If you have a model that contains a boolean field, e.g.

Model struct {
  Id int
  TestBool bool
}

And call

gorm.AutoMigrate(&Model{})

then
If the table does not exist in a DB yet - then the auto-migration logic just creates it and it works fine,
BUT
If the table exists in a DB: the auto-migration logic analyzes new vs. existing column configurations. And in this case, it crashes if the model contains boolean fields. Here is except the trace

panic({0xc44060?, 0x1432f00?})

/usr/local/go/src/runtime/panic.go:914 +0x21f

database/sql.(*ColumnType).DecimalSize(...)

/usr/local/go/src/database/sql/sql.go:3175

gorm.io/gorm/migrator.ColumnType.DecimalSize(...)

/home/sio/go/pkg/mod/gorm.io/gorm@v1.25.8/migrator/column_type.go:75

gorm.io/gorm/migrator.Migrator.MigrateColumn({{0x40?, 0xc000328330?, {0xe8e948?, 0xc0002bc900?}}}, {0xd1ea00, 0xc00039c050}, 0xc0004663c0, {0xe8fc88, 0xc0002e72b0})

/home/sio/go/pkg/mod/gorm.io/gorm@v1.25.8/migrator/migrator.go:497 +0x402

gorm.io/driver/postgres.Migrator.MigrateColumn({{{0xa0?, 0xc000328330?, {0xe8e948?, 0xc0002bc900?}}}}, {0xd1ea00, 0xc00039c050}, 0xc0004663c0, {0xe8fc88?, 0xc0002e72b0?})

/home/sio/go/pkg/mod/gorm.io/driver/postgres@v1.5.7/migrator.go:268 +0x89

gorm.io/gorm/migrator.Migrator.AutoMigrate.func1(0xc00046e8c0)

/home/sio/go/pkg/mod/gorm.io/gorm@v1.25.8/migrator/migrator.go:156 +0x231

gorm.io/gorm/migrator.Migrator.RunWithValue({{0x20?, 0xc000290750?, {0xe8e948?, 0xc0002bc900?}}}, {0xd1ea00?, 0xc00039c050}, 0xc0002afb70)

/home/sio/go/pkg/mod/gorm.io/gorm@v1.25.8/migrator/migrator.go:74 +0x137

gorm.io/gorm/migrator.Migrator.AutoMigrate({{0x0?, 0xc000290750?, {0xe8e948?, 0xc0002bc900?}}}, {0xc0002b4300?, 0x0?, 0x0?})

/home/sio/go/pkg/mod/gorm.io/gorm@v1.25.8/migrator/migrator.go:129 +0x1be

gorm.io/gorm.(*DB).AutoMigrate(0xc000328cf0?, {0xc0002b4300, 0x1, 0x1})

/home/sio/go/pkg/mod/gorm.io/gorm@v1.25.8/migrator.go:24 +0x42

@ayakut
Copy link
Author

ayakut commented Mar 26, 2024

By the way,

When I run unit-tests on my local they pass OK 100%

sio@localhost [/home/.../ayakut-gorm/tests] [master]$  go test ./...
ok      gorm.io/gorm/tests 

Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

We can't seem to assume that the same prefix means the same type because they may have different sizes and precisions.

@ayakut
Copy link
Author

ayakut commented Mar 28, 2024 via email

@a631807682
Copy link
Member

a631807682 commented Mar 28, 2024

Can you provide a replica code in the playground? It seems to have been fixed in go-gorm/postgres#256

@ayakut
Copy link
Author

ayakut commented Apr 2, 2024 via email

@ayakut
Copy link
Author

ayakut commented Apr 2, 2024

Can you provide a replica code in the playground? It seems to have been fixed in go-gorm/postgres#256

Please check - I refactored the solution to minimize the risk of regression (all UTs on my local are passed OK)

@ayakut ayakut requested a review from a631807682 April 2, 2024 19:12
Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

  1. Please add some unit tests describing what was fixed.
  2. Can you describe why different prefixes must be the same type?

@ayakut ayakut closed this Apr 8, 2024
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