Peter Kurfer
9ea9a8f658
Some checks failed
Go build / build (push) Failing after 1m58s
- setup ent scheme - add command to create users - document API - add helpers to create migrations - add command to run migrations - add basic compose file
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package migrate_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"code.icb4dc0.de/prskr/searcherside/core/ports"
|
|
"code.icb4dc0.de/prskr/searcherside/infrastructure/db/migrate"
|
|
_ "code.icb4dc0.de/prskr/searcherside/internal/db"
|
|
"code.icb4dc0.de/prskr/searcherside/internal/ent"
|
|
"code.icb4dc0.de/prskr/searcherside/scripts/migrations"
|
|
|
|
"entgo.io/ent/dialect"
|
|
)
|
|
|
|
func TestAtlasMigrator_Migrate_SQLite(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "gophershare.db")
|
|
|
|
req := ports.MigrationRequest{
|
|
Driver: ports.DriverSQLite,
|
|
URL: fmt.Sprintf("file://%s?_pragma=foreign_keys(1)", dbPath),
|
|
}
|
|
|
|
client, err := ent.Open(dialect.SQLite, req.URL)
|
|
if err != nil {
|
|
t.Fatalf("failed to open sqlite3 client: %v", err)
|
|
}
|
|
|
|
migrator := migrate.AtlasMigrator{
|
|
MigrationsFS: migrations.Fs,
|
|
RevisionRW: migrate.NewDBRevisionRW(client),
|
|
}
|
|
|
|
if err := migrator.Migrate(context.Background(), req); err != nil {
|
|
t.Fatalf("failed to migrate: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAtlasMigrator_Migrate_Postgres(t *testing.T) {
|
|
connString, closer, err := dbtest.TestPostgresDatabase(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("failed to setup Postgres test DB: %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
if err := closer.Close(context.Background()); err != nil {
|
|
t.Errorf("failed to close Postgres test DB: %v", err)
|
|
}
|
|
})
|
|
|
|
req := ports.MigrationRequest{
|
|
Driver: ports.DriverPostgres,
|
|
URL: connString,
|
|
}
|
|
|
|
client, err := ent.Open(dialect.Postgres, req.URL)
|
|
if err != nil {
|
|
t.Fatalf("failed to open sqlite3 client: %v", err)
|
|
}
|
|
|
|
migrator := migrate.AtlasMigrator{
|
|
MigrationsFS: migrations.Fs,
|
|
RevisionRW: migrate.NewDBRevisionRW(client),
|
|
}
|
|
|
|
if err = migrator.Migrate(context.Background(), req); err != nil {
|
|
t.Fatalf("failed to migrate: %v", err)
|
|
}
|
|
}
|