1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-10 00:43:36 +02:00
pocketbase/tools/migrate/runner_test.go

167 lines
4.2 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package migrate
import (
"context"
"database/sql"
2023-01-07 22:25:56 +02:00
"encoding/json"
2022-07-06 23:19:05 +02:00
"testing"
"time"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/tools/list"
_ "modernc.org/sqlite"
)
func TestNewRunner(t *testing.T) {
testDB, err := createTestDB()
if err != nil {
t.Fatal(err)
}
defer testDB.Close()
l := MigrationsList{}
l.Register(nil, nil, "1_test.go")
l.Register(nil, nil, "2_test.go")
l.Register(nil, nil, "3_test.go")
r, err := NewRunner(testDB.DB, l)
if err != nil {
t.Fatal(err)
}
if len(r.migrationsList.Items()) != len(l.Items()) {
t.Fatalf("Expected the same migrations list to be assigned, got \n%#v", r.migrationsList)
}
expectedQueries := []string{
"CREATE TABLE IF NOT EXISTS `_migrations` (file VARCHAR(255) PRIMARY KEY NOT NULL, applied INTEGER NOT NULL)",
}
if len(expectedQueries) != len(testDB.CalledQueries) {
t.Fatalf("Expected %d queries, got %d: \n%v", len(expectedQueries), len(testDB.CalledQueries), testDB.CalledQueries)
}
for _, q := range expectedQueries {
if !list.ExistInSlice(q, testDB.CalledQueries) {
t.Fatalf("Query %s was not found in \n%v", q, testDB.CalledQueries)
}
}
}
func TestRunnerUpAndDown(t *testing.T) {
testDB, err := createTestDB()
if err != nil {
t.Fatal(err)
}
defer testDB.Close()
2023-01-07 22:25:56 +02:00
callsOrder := []string{}
2022-07-06 23:19:05 +02:00
l := MigrationsList{}
l.Register(func(db dbx.Builder) error {
2023-01-07 22:25:56 +02:00
callsOrder = append(callsOrder, "up2")
2022-07-06 23:19:05 +02:00
return nil
}, func(db dbx.Builder) error {
2023-01-07 22:25:56 +02:00
callsOrder = append(callsOrder, "down2")
2022-07-06 23:19:05 +02:00
return nil
2023-01-07 22:25:56 +02:00
}, "2_test")
2022-07-06 23:19:05 +02:00
l.Register(func(db dbx.Builder) error {
2023-01-07 22:25:56 +02:00
callsOrder = append(callsOrder, "up3")
2022-07-06 23:19:05 +02:00
return nil
}, func(db dbx.Builder) error {
2023-01-07 22:25:56 +02:00
callsOrder = append(callsOrder, "down3")
2022-07-06 23:19:05 +02:00
return nil
2023-01-07 22:25:56 +02:00
}, "3_test")
l.Register(func(db dbx.Builder) error {
callsOrder = append(callsOrder, "up1")
return nil
}, func(db dbx.Builder) error {
callsOrder = append(callsOrder, "down1")
return nil
}, "1_test")
2022-07-06 23:19:05 +02:00
r, err := NewRunner(testDB.DB, l)
if err != nil {
t.Fatal(err)
}
2023-01-07 22:25:56 +02:00
// simulate partially out-of-order run migration
r.saveAppliedMigration(testDB, "2_test")
2022-07-06 23:19:05 +02:00
2023-01-07 22:25:56 +02:00
// ---------------------------------------------------------------
2022-07-06 23:19:05 +02:00
// Up()
2023-01-07 22:25:56 +02:00
// ---------------------------------------------------------------
2022-07-06 23:19:05 +02:00
if _, err := r.Up(); err != nil {
t.Fatal(err)
}
2023-01-07 22:25:56 +02:00
expectedUpCallsOrder := `["up1","up3"]` // skip up2 since it was applied previously
upCallsOrder, err := json.Marshal(callsOrder)
if err != nil {
t.Fatal(err)
2022-07-06 23:19:05 +02:00
}
2023-01-07 22:25:56 +02:00
if v := string(upCallsOrder); v != expectedUpCallsOrder {
t.Fatalf("Expected Up() calls order %s, got %s", expectedUpCallsOrder, upCallsOrder)
2022-07-06 23:19:05 +02:00
}
2023-01-07 22:25:56 +02:00
// ---------------------------------------------------------------
// reset callsOrder
callsOrder = []string{}
2022-07-06 23:19:05 +02:00
// simulate unrun migration
r.migrationsList.Register(nil, func(db dbx.Builder) error {
2023-01-07 22:25:56 +02:00
callsOrder = append(callsOrder, "down4")
2022-07-06 23:19:05 +02:00
return nil
2023-01-07 22:25:56 +02:00
}, "4_test")
2022-07-06 23:19:05 +02:00
2023-01-07 22:25:56 +02:00
// ---------------------------------------------------------------
// ---------------------------------------------------------------
2022-07-06 23:19:05 +02:00
// Down()
2023-01-07 22:25:56 +02:00
// ---------------------------------------------------------------
if _, err := r.Down(2); err != nil {
2022-07-06 23:19:05 +02:00
t.Fatal(err)
}
2023-01-07 22:25:56 +02:00
expectedDownCallsOrder := `["down3","down1"]` // revert in the applied order
2022-07-06 23:19:05 +02:00
2023-01-07 22:25:56 +02:00
downCallsOrder, err := json.Marshal(callsOrder)
if err != nil {
t.Fatal(err)
2022-07-18 22:00:54 +02:00
}
2023-01-07 22:25:56 +02:00
if v := string(downCallsOrder); v != expectedDownCallsOrder {
t.Fatalf("Expected Down() calls order %s, got %s", expectedDownCallsOrder, downCallsOrder)
2022-07-06 23:19:05 +02:00
}
}
// -------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------
type testDB struct {
*dbx.DB
CalledQueries []string
}
// NB! Don't forget to call `db.Close()` at the end of the test.
func createTestDB() (*testDB, error) {
sqlDB, err := sql.Open("sqlite", ":memory:")
if err != nil {
return nil, err
}
db := testDB{DB: dbx.NewFromDB(sqlDB, "sqlite")}
db.QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) {
db.CalledQueries = append(db.CalledQueries, sql)
}
db.ExecLogFunc = func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error) {
db.CalledQueries = append(db.CalledQueries, sql)
}
return &db, nil
}