1

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.

Dan
  • 974
  • 8
  • 12

2 Answers2

2

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?

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • I agree that you should have tests that exercise the MySQL code paths, but there are different levels of tests that may depend on data layer code (for convenience vs mocks) and it's nice for devs to be able to use sqlite. – Dan Feb 07 '18 at 01:30
1

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.

leaf bebop
  • 7,643
  • 2
  • 17
  • 27