mirror of
https://github.com/hanzoai/xorm.git
synced 2026-10-01 00:49:40 +00:00
Four test files named a SQLite engine directly, and two named two of them — mattn beside modernc in one binary. They register different names so nothing panicked, but a suite linking two implementations is not exercising the one a consumer gets. hanzoai/sqlite is the one import, and mattn leaves the module entirely. Since v0.5.6 the driver registers "sqlite" AND aliases "sqlite3" to the same value on both builds, so core/db_test.go keeps opening "sqlite3" — and now does so on the cgo-free build, where that name did not resolve at all. Also passes the error the log line already asked for: the table-inference failure declared %s and %v and supplied one argument, so the reason a table could not be inferred rendered as MISSING. go vet is clean.
143 lines
2.8 KiB
Go
143 lines
2.8 KiB
Go
package migrate
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
_ "github.com/hanzoai/sqlite" // registers BOTH "sqlite" and "sqlite3"; the caller picks
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/hanzoai/xorm"
|
|
)
|
|
|
|
type Person struct {
|
|
ID int64
|
|
Name string
|
|
}
|
|
|
|
type Pet struct {
|
|
ID int64
|
|
Name string
|
|
PersonID int
|
|
}
|
|
|
|
const (
|
|
dbName = "testdb.sqlite3"
|
|
)
|
|
|
|
var migrations = []*Migration{
|
|
{
|
|
ID: "201608301400",
|
|
Migrate: func(tx *xorm.Engine) error {
|
|
return tx.Sync(&Person{})
|
|
},
|
|
Rollback: func(tx *xorm.Engine) error {
|
|
return tx.DropTables(&Person{})
|
|
},
|
|
},
|
|
{
|
|
ID: "201608301430",
|
|
Migrate: func(tx *xorm.Engine) error {
|
|
return tx.Sync(&Pet{})
|
|
},
|
|
Rollback: func(tx *xorm.Engine) error {
|
|
return tx.DropTables(&Pet{})
|
|
},
|
|
},
|
|
}
|
|
|
|
func TestMigration(t *testing.T) {
|
|
_ = os.Remove(dbName)
|
|
|
|
db, err := xorm.NewEngine("sqlite3", dbName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
if err = db.Ping(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
m := New(db, DefaultOptions, migrations)
|
|
|
|
err = m.Migrate()
|
|
assert.NoError(t, err)
|
|
exists, _ := db.IsTableExist(&Person{})
|
|
assert.True(t, exists)
|
|
exists, _ = db.IsTableExist(&Pet{})
|
|
assert.True(t, exists)
|
|
assert.Equal(t, 2, tableCount(db, "migrations"))
|
|
|
|
err = m.RollbackLast()
|
|
assert.NoError(t, err)
|
|
exists, _ = db.IsTableExist(&Person{})
|
|
assert.True(t, exists)
|
|
exists, _ = db.IsTableExist(&Pet{})
|
|
assert.False(t, exists)
|
|
assert.Equal(t, 1, tableCount(db, "migrations"))
|
|
|
|
err = m.RollbackLast()
|
|
assert.NoError(t, err)
|
|
exists, _ = db.IsTableExist(&Person{})
|
|
assert.False(t, exists)
|
|
exists, _ = db.IsTableExist(&Pet{})
|
|
assert.False(t, exists)
|
|
assert.Equal(t, 0, tableCount(db, "migrations"))
|
|
}
|
|
|
|
func TestInitSchema(t *testing.T) {
|
|
os.Remove(dbName)
|
|
|
|
db, err := xorm.NewEngine("sqlite3", dbName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if err = db.Ping(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
m := New(db, DefaultOptions, migrations)
|
|
m.InitSchema(func(tx *xorm.Engine) error {
|
|
if err := tx.Sync(&Person{}); err != nil {
|
|
return err
|
|
}
|
|
return tx.Sync(&Pet{})
|
|
})
|
|
|
|
err = m.Migrate()
|
|
assert.NoError(t, err)
|
|
exists, _ := db.IsTableExist(&Person{})
|
|
assert.True(t, exists)
|
|
exists, _ = db.IsTableExist(&Pet{})
|
|
assert.True(t, exists)
|
|
assert.Equal(t, 2, tableCount(db, "migrations"))
|
|
}
|
|
|
|
func TestMissingID(t *testing.T) {
|
|
os.Remove(dbName)
|
|
|
|
db, err := xorm.NewEngine("sqlite3", dbName)
|
|
assert.NoError(t, err)
|
|
if db != nil {
|
|
defer db.Close()
|
|
}
|
|
assert.NoError(t, db.Ping())
|
|
|
|
migrationsMissingID := []*Migration{
|
|
{
|
|
Migrate: func(tx *xorm.Engine) error {
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
|
|
m := New(db, DefaultOptions, migrationsMissingID)
|
|
assert.Equal(t, ErrMissingID, m.Migrate())
|
|
}
|
|
|
|
func tableCount(db *xorm.Engine, tableName string) (count int) {
|
|
_, _ = db.SQL("SELECT COUNT(*) FROM " + tableName).Get(&count)
|
|
return count
|
|
}
|