Fix wait command for servers with no postgres database (#90)

Currently the `dbmate wait` command fails if the `postgres` system database does not exist (which is common on DigitalOcean, and perhaps other hosting providers).

This command is intended to verify that the postgres server is available, and not whether or not the user's database exists. Therefore, we will update this command to simply ignore the `database "foo" does not exist` error and treat this as the server being ready.

Fixes #78
This commit is contained in:
Adrian Macneil 2019-09-19 09:36:34 +12:00 committed by GitHub
parent aa72a39a29
commit b69a3d487a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -177,11 +177,25 @@ func (drv PostgresDriver) DeleteMigration(db Transaction, version string) error
// Ping verifies a connection to the database server. It does not verify whether the
// specified database exists.
func (drv PostgresDriver) Ping(u *url.URL) error {
db, err := drv.openPostgresDB(u)
// attempt connection to primary database, not "postgres" database
// to support servers with no "postgres" database
// (see https://github.com/amacneil/dbmate/issues/78)
db, err := drv.Open(u)
if err != nil {
return err
}
defer mustClose(db)
return db.Ping()
err = db.Ping()
if err == nil {
return nil
}
// ignore 'database "foo" does not exist' error
pqErr, ok := err.(*pq.Error)
if ok && pqErr.Code == "3D000" {
return nil
}
return err
}