We're using gorm and I'd like to be able to specify database specific annotations. For convenience, in development/test we use an sqlite3 database, then MySQL in production.
Unfortunately sqlite3 doesn't accept CHARACTER SET
and COLLATE
keywords. Which means that the following breaks:
type User struct {
Name string `gorm:"primary_key;type:varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci"`
}
Has anyone found a work around for this? I'd rather not use mysql in test and I'd also rather not manually manage the columns.
The best thing here is to test with MySQL. If you were able to specify different tags for test you would never know if you code will actually work in production because you would never test the code thats running there.
You should try to keep as much parity between your tests and production as possible. How else will you know if your annotations are actually correct?
I think a solution is to use build tags.
Declare two different User
in two different files, like user_sqlite3.go
and user_mysql.go
. And in the sqlite3 file, before package
declaration, Put the build tag:
// +build test
And when building on the local machine: go buiid -tags=test
.