2020-11-18 04:14:20 +05:30
|
|
|
// +build cgo
|
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-11-23 05:05:46 +05:30
|
|
|
"os"
|
2016-09-15 06:41:57 +05:30
|
|
|
"testing"
|
2016-11-23 05:05:46 +05:30
|
|
|
|
2017-02-22 04:30:22 +05:30
|
|
|
sqlite3 "github.com/mattn/go-sqlite3"
|
2017-07-26 02:15:17 +05:30
|
|
|
"github.com/sirupsen/logrus"
|
2016-09-15 06:41:57 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func TestMigrate(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2016-11-23 05:05:46 +05:30
|
|
|
logger := &logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: &logrus.TextFormatter{DisableColors: true},
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:30:22 +05:30
|
|
|
errCheck := func(err error) bool {
|
|
|
|
sqlErr, ok := err.(sqlite3.Error)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sqlErr.ExtendedCode == sqlite3.ErrConstraintUnique
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:43:38 +05:30
|
|
|
var sqliteMigrations []migration
|
|
|
|
for _, m := range migrations {
|
|
|
|
if m.flavor == nil || m.flavor == &flavorSQLite3 {
|
|
|
|
sqliteMigrations = append(sqliteMigrations, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &conn{db, &flavorSQLite3, logger, errCheck}
|
|
|
|
for _, want := range []int{len(sqliteMigrations), 0} {
|
2016-09-15 06:41:57 +05:30
|
|
|
got, err := c.migrate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("expected %d migrations, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|