go/sqliteutil/migrate_test.go

45 lines
748 B
Go
Raw Normal View History

2024-11-11 05:36:55 +00:00
package sqliteutil
import (
"database/sql"
"embed"
"testing"
)
//go:embed test-migrations
var testMigrationFS embed.FS
func TestMigrate(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
if err := Migrate(db, testMigrationFS); err != nil {
t.Fatal(err)
}
// Shouldn't have any effect.
if err := Migrate(db, testMigrationFS); err != nil {
t.Fatal(err)
}
query := `SELECT EXISTS(SELECT 1 FROM users WHERE UserID=$1)`
var exists bool
if err = db.QueryRow(query, 1).Scan(&exists); err != nil {
t.Fatal(err)
}
if exists {
t.Fatal("1 shouldn't exist")
}
if err = db.QueryRow(query, 2).Scan(&exists); err != nil {
t.Fatal(err)
}
if !exists {
t.Fatal("2 should exist")
}
}