Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
slifty opened this issue Jul 13, 2022 · 2 comments · May be fixed by #93
Open

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

slifty opened this issue Jul 13, 2022 · 2 comments · May be fixed by #93

Comments

@slifty
Copy link

slifty commented Jul 13, 2022

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 slifty changed the title Migrations can't be run on multiple schemas Migrations can't be run on multiple schemas in the same database Jul 13, 2022
@slifty
Copy link
Author

slifty commented Jul 13, 2022

I see there is an open PR from two years ago that would solve this issue: #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 added a commit to slifty/postgres-migrations that referenced this issue Jul 13, 2022
* If a `migrations` table did exist in a schema outside of the
  search path, `doesTableExist` would return true but the migration
  would then fail with 'relation "migrations" does not exist'
* See https://dba.stackexchange.com/a/86098 for the details on the query
* This new query makes it possible to have one `migrations` table per
  schema

Issue ThomWright#92 Migrations can't be run on multiple schemas in the same database

Co-authored-by: Remi Koenig <r.koenig@agricool.co>
slifty added a commit to slifty/postgres-migrations that referenced this issue Jul 13, 2022
* If a `migrations` table did exist in a schema outside of the
  search path, `doesTableExist` would return true but the migration
  would then fail with 'relation "migrations" does not exist'
* See https://dba.stackexchange.com/a/86098 for the details on the query
* This new query makes it possible to have one `migrations` table per
  schema

Issue ThomWright#92 Migrations can't be run on multiple schemas in the same database

Co-authored-by: Remi Koenig <r.koenig@agricool.co>
@slifty slifty linked a pull request Jul 13, 2022 that will close this issue
slifty added a commit to PhilanthropyDataCommons/service that referenced this issue Jul 13, 2022
Integration tests are slower than unit tests because they rely on
a database connection, so we want the ability to run them separately
depending on our environment.

Jest allows us to define different config files.  By creating a `base`
config we can define all of the common settings between unit and
integration tests, and then customize for each type in the respective
extended configs.

In addition to configuration this begins to define define some hooks
that make it possible to run tests in parallel.  We can't actually use
these hooks properly until an upstream bug is fixed in our migration
package:

ThomWright/postgres-migrations#92

Issue #43 Support integration tests
@zakpatterson
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants