Skip to content

Commit

Permalink
Attempt to continue when encountering errors in createDBIfNotExist (#258
Browse files Browse the repository at this point in the history
)

* Attempt to continue when encountering errors in createDBIfNotExist

The database might already exist even if we encounter errors in createDBIfNotExist, so instead of erroring out, log warnings and attempt to continue.

Signed-off-by: Morten Lied Johansen <[email protected]>
Co-authored-by: Brad Davidson <[email protected]>
  • Loading branch information
mortenlj and brandond authored Dec 13, 2023
1 parent 5328e60 commit a03b5c9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/drivers/pgsql/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ func createDBIfNotExist(dataSourceName string) error {
u.Path = "/postgres"
db, err := sql.Open("pgx", u.String())
if err != nil {
return err
logrus.Warnf("failed to ensure existence of database %s: unable to connect to default postgres database: %v", dbName, err)
return nil
}
defer db.Close()

var exists bool
err = db.QueryRow("SELECT 1 FROM pg_database WHERE datname = $1", dbName).Scan(&exists)
if err != nil && err != sql.ErrNoRows {
return err
logrus.Warnf("failed to check existence of database %s, going to attempt create: %v", dbName, err)
}

stmt := createDB + dbName + ";"
Expand All @@ -153,9 +154,10 @@ func createDBIfNotExist(dataSourceName string) error {
logrus.Tracef("SETUP EXEC : %v", util.Stripped(stmt))
_, err = db.Exec(stmt)
if err != nil {
return err
logrus.Warnf("failed to create database %s: %v", dbName, err)
} else {
logrus.Tracef("created database: %s", dbName)
}
logrus.Tracef("created database: %s", dbName)
}
return nil
}
Expand Down

0 comments on commit a03b5c9

Please sign in to comment.