Skip to content

Commit

Permalink
fix: few tweaks and fixes. drop db beforehand.
Browse files Browse the repository at this point in the history
  • Loading branch information
gak committed Jul 10, 2024
1 parent 15a6169 commit 1356d61
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
12 changes: 7 additions & 5 deletions backend/controller/sql/database_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package sql_test

import (
"os"
"fmt"
"testing"

in "github.com/TBD54566975/ftl/integration"
Expand All @@ -26,15 +26,17 @@ func TestDatabase(t *testing.T) {
}

func TestMigrate(t *testing.T) {
os.Setenv("DATABASE_URL", "postgres://postgres:secret@localhost:15432/ftl_test?sslmode=disable")
dbName := "ftl_test"
dbUri := fmt.Sprintf("postgres://postgres:secret@localhost:15432/%s?sslmode=disable", dbName)

q := func() in.Action {
return in.QueryRow("ftl_test", "SELECT version FROM schema_migrations WHERE version = '20240704103403'", "20240704103403")
return in.QueryRow(dbName, "SELECT version FROM schema_migrations WHERE version = '20240704103403'", "20240704103403")
}

in.RunWithoutController(t, "",
in.Fail(q(), "Should fail because the table does not exist."),
in.Exec("ftl", "migrate"),
in.DropDBAction(t, dbName),
in.Fail(q(), "Should fail because the database does not exist."),
in.Exec("ftl", "migrate", "--dsn", dbUri),
q(),
)
}
2 changes: 1 addition & 1 deletion backend/controller/sql/databasetesting/devel.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func CreateForDevel(ctx context.Context, dsn string, recreate bool) (*pgxpool.Po

_, _ = conn.Exec(ctx, fmt.Sprintf("CREATE DATABASE %q", config.Database)) //nolint:errcheck // PG doesn't support "IF NOT EXISTS" so instead we just ignore any error.

err = sql.Migrate(ctx, dsn)
err = sql.Migrate(ctx, dsn, log.Debug)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/controller/sql/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var migrationSchema embed.FS

// Migrate the database.
func Migrate(ctx context.Context, dsn string) error {
func Migrate(ctx context.Context, dsn string, logLevel log.Level) error {
u, err := url.Parse(dsn)
if err != nil {
return fmt.Errorf("invalid DSN: %w", err)
Expand All @@ -31,7 +31,7 @@ func Migrate(ctx context.Context, dsn string) error {

db := dbmate.New(u)
db.FS = migrationSchema
db.Log = log.FromContext(ctx).Scope("migrate").WriterAt(log.Debug)
db.Log = log.FromContext(ctx).Scope("migrate").WriterAt(logLevel)
db.MigrationsDir = []string{"schema"}
err = db.CreateAndMigrate()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl/cmd_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type migrateCmd struct {
func (c *migrateCmd) Run(ctx context.Context) error {
logger := log.FromContext(ctx)
logger.Infof("Migrating database")
err := sql.Migrate(ctx, c.DSN)
err := sql.Migrate(ctx, c.DSN, log.Info)
if err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
Expand Down
41 changes: 34 additions & 7 deletions integration/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ func CreateDBAction(module, dbName string, isTest bool) Action {
}
}

func terminateDanglingConnections(t testing.TB, db *sql.DB, dbName string) {
t.Helper()

_, err := db.Exec(`
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = $1 AND pid <> pg_backend_pid()`,
dbName)
assert.NoError(t, err)
}

func CreateDB(t testing.TB, module, dbName string, isTestDb bool) {
// insert test suffix if needed when actually setting up db
if isTestDb {
Expand All @@ -368,18 +379,34 @@ func CreateDB(t testing.TB, module, dbName string, isTestDb bool) {
assert.NoError(t, err, "failed to create database")

t.Cleanup(func() {
// Terminate any dangling connections.
_, err := db.Exec(`
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = $1 AND pid <> pg_backend_pid()`,
dbName)
assert.NoError(t, err)
terminateDanglingConnections(t, db, dbName)
_, err = db.Exec("DROP DATABASE " + dbName)
assert.NoError(t, err)
})
}

func DropDBAction(t testing.TB, dbName string) Action {
return func(t testing.TB, ic TestContext) {
DropDB(t, dbName)
}
}

func DropDB(t testing.TB, dbName string) {
Infof("Dropping database %s", dbName)
db, err := sql.Open("pgx", "postgres://postgres:secret@localhost:15432/postgres?sslmode=disable")
assert.NoError(t, err, "failed to open database connection")

terminateDanglingConnections(t, db, dbName)

_, err = db.Exec("DROP DATABASE IF EXISTS " + dbName)
assert.NoError(t, err, "failed to delete existing database")

t.Cleanup(func() {
err := db.Close()
assert.NoError(t, err)
})
}

// Create a directory in the working directory
func Mkdir(dir string) Action {
return func(t testing.TB, ic TestContext) {
Expand Down

0 comments on commit 1356d61

Please sign in to comment.