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

DDL checking is not case-insensitive #87

Closed
muety opened this issue Apr 3, 2022 · 0 comments
Closed

DDL checking is not case-insensitive #87

muety opened this issue Apr 3, 2022 · 0 comments
Assignees

Comments

@muety
Copy link

muety commented Apr 3, 2022

Sorry for skipping the playground PR, but I think the below snippet should explain the issue well enough.

Description

It took me a couple of hours to figure out the following issue. When parsing the DDL as part of AutoMigrate, an error will occur if the original SQL statement wasn't fully uppercase.

package main

import (
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type Foo struct {
	ID     uint64 `gorm:"primary_key"`
	Field1 string
	Field2 string
}

func main() {
	db, err := gorm.Open(sqlite.Open("gorm_db"), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}

	if err := db.AutoMigrate(&Foo{}); err != nil {
		panic(err)
	}

	// note "on" vs "ON" here
	if err := db.Exec("CREATE INDEX idx_field1_field2 on foos (field1, field2)").Error; err != nil {
		panic(err)
	}

	if err := db.AutoMigrate(&Foo{}); err != nil {
		panic(err)
	}
}

Here's the output:

2022/04/03 17:07:23 /home/ferdinand/go/pkg/mod/gorm.io/driver/sqlite@v1.3.1/migrator.go:33
[0.025ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="foos"

2022/04/03 17:07:23 /home/ferdinand/go/pkg/mod/gorm.io/driver/sqlite@v1.3.1/migrator.go:106
[0.062ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "foos" AND sql IS NOT NULL order by type = "table" desc

2022/04/03 17:07:23 /tmp/gorm-bug/main.go:23 duplicate column name: id
[0.012ms] [rows:0] ALTER TABLE `foos` ADD `id` integer
panic: duplicate column name: id

goroutine 1 [running]:
main.main()
        /tmp/gorm-bug/main.go:24 +0x23a

Process finished with the exit code 2

So when manually running a DDL statement that is valid, yet not correctly formatted, auto migrations for that table will break forever until you explicitly fix it.

Would be very helpful if the regexes were case-insensitive and only check for correct syntax instead.

go.mod

module gorm-bug

go 1.18

require (
	gorm.io/driver/sqlite v1.3.1
	gorm.io/gorm v1.23.4
)

require (
	github.com/jinzhu/inflection v1.0.0 // indirect
	github.com/jinzhu/now v1.1.4 // indirect
	github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
)
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

No branches or pull requests

2 participants