postgres-migrations icon indicating copy to clipboard operation
postgres-migrations copied to clipboard

Migrations can't be run on multiple schemas in the same database

Open slifty opened this issue 2 years ago • 2 comments

I am using schemas to parallelize integration tests (one schema per test worker).

Before each test I want to run the following setup:

  1. Create a new schema
  2. Set the search_path to be the schema
  3. Run my migrations

Unfortunately right now postgres-migrations only allows migrations to be once per database (NOT per schema). It looks like this is because in migrate.ts the check for the migrations table doesn't account for the idea that more than one schema can exist (and that migrations existing in one doesn't mean migrations exists in the current search_path).

async function doesTableExist(client: BasicPgClient, tableName: string) {
  const result = await client.query(SQL`SELECT EXISTS (
  SELECT 1
  FROM   pg_catalog.pg_class c
  WHERE  c.relname = ${tableName}
  AND    c.relkind = 'r'
);`)

As an example, if I manually create a migrations table in two different schemas this query would return more than one row:

image

I hope this makes sense! It would be wonderful if this tool could support the parallel-test-schema use case.

slifty avatar Jul 13 '22 15:07 slifty

I see there is an open PR from two years ago that would solve this issue: https://github.com/ThomWright/postgres-migrations/pull/33

What are the odds of being able to merge that in sometime...

Edit: I see the request on the PR! Working on that now.

slifty avatar Jul 13 '22 15:07 slifty

I've implemented this here, and released to npm. There are a few years of history on this feature in this repo, so I didn't try to make a PR for it here, but I'm happy to help get it in if desired.

zakpatterson avatar May 30 '23 17:05 zakpatterson