ThomWright / postgres-migrations

🐦 A Stack Overflow-inspired PostgreSQL migration library with strict ordering and immutable migrations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Check for existence of database

ThomWright opened this issue · comments

createDb attempts to create a database. If it already exists, it detects the 'duplicate' error and resolves appropriately.

However, on a readonly replica, it gets a different error.

Now, arguably migrations shouldn't be running on a replica... but let's move past that for now. If there are no new migrations to run then I think this library should be happy being run against a read replica.

So, what we could do is:

  • create a connection, then on that connection:
    • check if the database exists
    • if not, then try to create
  • close the connection

It looks like SELECT 1 FROM pg_database WHERE datname='DB_NAME' might do it.

I think the API could probably do with changing too... it is more ensureDbExists than createDb. Would have to keep createDb around for backwards compatiblity.