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

Help users when they need to use pg_dump: when a baselining is needed for a brownfield project. #4906

Open
Jolg42 opened this issue Jun 13, 2023 · 4 comments
Labels
docs Documentation creation, updates or corrections topic: migrate Issues relating to Prisma Migrate

Comments

@Jolg42
Copy link
Contributor

Jolg42 commented Jun 13, 2023

We mention pg_dump in

- If the changes are significant, it can be easier to replace the entire migration file with the result of a database dump ([`mysqldump`](https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html), [`pg_dump`](https://www.postgresql.org/docs/12/app-pgdump.html))

In a brownfield project when a user wants to manage their database with Prisma Migrate, a baseline migration is required (because there is an existing database).

pg_dump can be used to create a reliable baseline migration, example

pg_dump --file=migration.sql --schema-only --dbname=mydb --host=localhost --username=myuser --password

⚠️ The migration.sql dump needs to be edited and this following line removed.

SELECT pg_catalog.set_config('search_path', '', false);

If it is not removed, the migration will fail with the following error message:

Applying migration `20220510114355_baseline`
Error: P1014

The underlying table for model `_prisma_migrations` does not exist.

We could mention this somewhere in our docs.

@janpio
Copy link
Contributor

janpio commented Jun 14, 2023

What is the explanation behind this?

If it is not removed, the migration will fail with the following error message:
The underlying table for model _prisma_migrations does not exist.

How does that 1 SQL command lead to this error message?

@Jolg42
Copy link
Contributor Author

Jolg42 commented Jun 14, 2023

Migrate relies on the search_path which is set to the current schema behind the scenes when connecting to the db.
Since the search path is modified during the migration, here to nothing, it causes this.

@janpio
Copy link
Contributor

janpio commented Jun 14, 2023

Doesn't that imply a potential bug in our logic reading _prisma_migrations, that we should specify the schema explicitly instead of relying on the search paths? (That would still work, correct?) Or alternatively, that we should detect such search path modifications and warn about them and maybe even prevent running migrate on such SQL files?

@Sjlver
Copy link
Contributor

Sjlver commented Mar 16, 2024

Detecting a change to the search path, as @janpio suggested, would be great. I had exactly the problem described in this issue, and it took a while to find the cause. It might be worth detecting since this is the default behavior of pg_dump.

That said, migrations could probably do a number of things that screw up Prisma. However, changes from pg_catalog.set_config are only valid in the current session. So a safer (albeit slower) way would be to run each migration in its own session. That is, create a new database connection for each. This makes migrations more isolated, and thus there are fewer assumptions about what they can and cannot do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation creation, updates or corrections topic: migrate Issues relating to Prisma Migrate
Projects
None yet
Development

No branches or pull requests

3 participants