pgutil/migrate_test.go

51 lines
870 B
Go
Raw Normal View History

2024-10-29 15:00:22 +00:00
package pgutil
import (
"database/sql"
"embed"
"testing"
)
//go:embed test-migrations
var testMigrationFS embed.FS
func TestMigrate(t *testing.T) {
db, err := sql.Open(
"postgres",
"host=127.0.0.1 dbname=test sslmode=disable user=test password=test")
if err != nil {
t.Fatal(err)
}
if err := DropAllTables(db); 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")
}
}