There is a problem in latest release with gorm schema generator. I use this package - https://github.com/ariga/atlas-provider-gorm/blob/master/gormschema/gorm.go and using v1.30.0 it was generating expected output with a bunch of CREATE TABLE statements, but updating to 1.30.1 contains some sort of degradation that results in generating ALTER SCHEMA instead, while tables are not present, which is a bug.
Here is the minimum repro:
cat <<EOF > go.mod
module gormbug
go 1.23.10
require ariga.io/atlas-provider-gorm v0.5.3
require (
ariga.io/atlas-go-sdk v0.7.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/text v0.21.0 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/postgres v1.5.11 // indirect
gorm.io/driver/sqlite v1.5.7 // indirect
gorm.io/driver/sqlserver v1.5.4 // indirect
gorm.io/gorm v1.30.1 // indirect
)
EOF
cat << 'EOF' > main.go
package main
import (
"fmt"
"os"
"ariga.io/atlas-provider-gorm/gormschema"
)
type Entity struct {
ID string `gorm:"primaryKey"`
Name string `gorm:"not null,default:''"`
}
func main() {
stmts, err := gormschema.New("postgres").Load(
&Entity{},
)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load gorm schema: %v\n", err)
os.Exit(1)
}
_, err = os.Stdout.WriteString(stmts)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write gorm schema: %v\n", err)
os.Exit(1)
}
}
EOF
go mod tidy
go run main.go
Expected one CREATE TABLE statement, result are two ALTER TABLE statements instead:
ALTER TABLE "entities" ADD "id" text;
ALTER TABLE "entities" ADD "name" text;
However, if you replace
gorm.io/gorm v1.30.1 // indirect
to gorm.io/gorm v1.30.0 // indirect
and run go mod tidy && go run main.go
, then you will see this:
CREATE TABLE "entities" ("id" text,"name" text,PRIMARY KEY ("id"));
, as expected.
This shows that degradation occurred somewhere here, but I do not understand gorm well enough to suggest where exactly.
I know that you'd probably prefer if I found how exactly that "atlas-provider-gorm" calls "gorm", but I have no idea about that either, I only have noticed that our processes started to fail after auto update to this new version of gorm.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: strowk
Ah, I have dichotomied a particular commit that broke this.
Given repro above, running:
go get gorm.io/gorm@2f4925e017f020909c061c3feb8ba54a0c5d002c
go mod tidy && go run main.go
gives this:
CREATE TABLE "entities" ("id" text,"name" text,PRIMARY KEY ("id"));
and next commit
go get gorm.io/gorm@751a6dde7a4510c105bb3fb21ea8cc0f26d3aebd
go mod tidy && go run main.go
gives wrong result:
ALTER TABLE "entities" ADD "id" text;
ALTER TABLE "entities" ADD "name" text;
, hence the bug is here: https://github.com/go-gorm/gorm/commit/751a6dde7a4510c105bb3fb21ea8cc0f26d3aebd
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨