Skip to content

Commit

Permalink
store errctx as context if next operation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
choonkeat committed Dec 3, 2023
1 parent c3ea684 commit 8a95b2f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
47 changes: 24 additions & 23 deletions cmd/dbmigrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@ import (
_ "github.com/lib/pq"
)

var (
serverReadyWait time.Duration
doCreateDB bool
dbSchema *string
doCreateMigration bool
doPendingVersions bool
doMigrateUp bool
doMigrateDown int
dirname string
databaseURL string
driverName string
timeout time.Duration
)

func main() {
if err := _main(); err != nil {
log.Fatalln(err.Error())
}
}

func _main() error {
var (
serverReadyWait time.Duration
doCreateDB bool
dbSchema *string
doCreateMigration bool
doPendingVersions bool
doMigrateUp bool
doMigrateDown int
dirname string
databaseURL string
driverName string
timeout time.Duration
errctx error
)

// options
flag.DurationVar(&serverReadyWait,
"server-ready", 0, "wait until database server is ready, then continue")
Expand Down Expand Up @@ -78,12 +79,12 @@ func _main() error {
return nil
}

driverName, databaseURL, _ = dbmigrate.SanitizeDriverNameURL(driverName, databaseURL)
driverName, databaseURL, errctx = dbmigrate.SanitizeDriverNameURL(driverName, databaseURL)

if doServerReadyWait := serverReadyWait > 0; doServerReadyWait || doCreateDB || dbSchema != nil {
adapter, err := dbmigrate.AdapterFor(driverName)
if err != nil {
return err
return errors.Wrap(err, errctx.Error())
}

if doServerReadyWait {
Expand All @@ -92,12 +93,12 @@ func _main() error {
}
connString, _, err := adapter.BaseDatabaseURL(databaseURL)
if err != nil {
return err
return errors.Wrap(err, errctx.Error())
}
ctx, cancel := context.WithTimeout(context.Background(), serverReadyWait)
defer cancel()
if err := dbmigrate.ReadyWait(ctx, driverName, []string{databaseURL, connString}, log.Println); err != nil {
return err
return errors.Wrap(err, errctx.Error())
}
}

Expand All @@ -110,14 +111,14 @@ func _main() error {
}
connString, dbName, err := adapter.BaseDatabaseURL(databaseURL)
if err != nil {
return err
return errors.Wrap(err, errctx.Error())
}
db, err := sql.Open(driverName, connString)
if err != nil {
return errors.Wrapf(err, "connect to db")
}
// leave errors for subsequent actions
_, _ = db.Exec(adapter.CreateDatabaseQuery(dbName))
_, errctx = db.Exec(adapter.CreateDatabaseQuery(dbName))
_ = db.Close()
}

Expand All @@ -130,14 +131,14 @@ func _main() error {
return errors.Wrapf(err, "connect to db")
}
// leave errors for subsequent actions
_, _ = db.Exec(adapter.CreateSchemaQuery(*dbSchema))
_, errctx = db.Exec(adapter.CreateSchemaQuery(*dbSchema))
_ = db.Close()
}
}

m, err := dbmigrate.New(os.DirFS(dirname), driverName, databaseURL)
if err != nil {
return err
return errors.Wrap(err, errctx.Error())
}
defer m.CloseDB()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand All @@ -147,7 +148,7 @@ func _main() error {
if doPendingVersions {
versions, err := m.PendingVersions(ctx, dbSchema)
if err != nil {
return err
return errors.Wrap(err, errctx.Error())
}
fmt.Println(strings.Join(versions, "\n"))
return nil
Expand Down
4 changes: 2 additions & 2 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func (c *Config) CloseDB() error {

func (c *Config) existingVersions(ctx context.Context, schema *string) (*trie.Trie, error) {
// best effort create before we select; if the table is not there, next query will fail anyway
_, _ = c.db.ExecContext(ctx, c.adapter.CreateVersionsTable(schema))
_, errctx := c.db.ExecContext(ctx, c.adapter.CreateVersionsTable(schema))
rows, err := c.db.QueryContext(ctx, c.adapter.SelectExistingVersions(schema))
if err != nil {
return nil, err
return nil, errors.Wrap(err, errctx.Error())
}
defer rows.Close()

Expand Down
5 changes: 1 addition & 4 deletions tests/scenario.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# abort on any failure
set -e
set -euxo pipefail
source `dirname $0`/lib.sh

# setup
Expand All @@ -12,9 +12,6 @@ trap finish EXIT
mkdir -p ${DB_MIGRATIONS_DIR}
echo "testing ${DATABASE_DRIVER}..."

# echo commands that we run
# set -x

# `-create` should work
assert "should create new migration" ${DBMIGRATE_CMD} -dir ${DB_MIGRATIONS_DIR} -create finally 'do! nothing??' 2>/dev/null
assert "should create .up.sql" test -f ${DB_MIGRATIONS_DIR}/*_finally-do-nothing.up.sql
Expand Down

0 comments on commit 8a95b2f

Please sign in to comment.