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

Run a migration by hand and update the migration table manually #9154

Open
kevinsimper opened this issue Sep 7, 2021 · 13 comments
Open

Run a migration by hand and update the migration table manually #9154

kevinsimper opened this issue Sep 7, 2021 · 13 comments
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: CREATE INDEX CONCURRENTLY topic: indexes topic: prisma migrate deploy CLI: prisma migrate deploy

Comments

@kevinsimper
Copy link

Problem

Adding indexes should be done concurrently in postgres

Prisma generates indexes in the migration.

Indexes concurrently can't be added in a transaction

I run the migration manually by hand and comment out the add indexes while running prisma migrate deploy

Afterwards Prisma deploy will say

WARNING The following migrations have been modified since they were applied:

Suggested solution

One solution could be:

Output the checksum so that I can update it manually

@tomhoule tomhoule added the domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. label Sep 8, 2021
@janpio janpio added the kind/feature A request for a new feature. label Sep 17, 2021
@janpio
Copy link
Contributor

janpio commented Sep 17, 2021

Adding indexes should be done concurrently in postgres

Prisma generates indexes in the migration.

Indexes concurrently can't be added in a transaction

Can you please elaborate a bit what you mean by that exactly? What does "should be done concurrently" mean?

@kevinsimper
Copy link
Author

kevinsimper commented Sep 21, 2021

Hi @janpio 😄

What does "should be done concurrently" mean?

If you add a index to a tabel it will lock writes to it. Postgres supports writing a index CONCURRENTLY. It takes longer, but does not lock the table. https://www.postgresql.org/docs/9.1/sql-createindex.html

a standard index build locks out writes (but not reads) on the table until it's done.

But a index CONCURRENTLY can not be added in a transaction block.

Adding a column to a table with milions of rows took for me 5 mins adding it concurrently. Adding it without concurrent would have been quicker, but I did not want not to write to the tables at the same time.

@janpio
Copy link
Contributor

janpio commented Sep 21, 2021

Ok. So you wanted to modify the generated migration vs. what migrate dev had created for you, then apply it via migrate dev or migrate deploy? But afterwards you got a message that the migration file was modified?

@kevinsimper
Copy link
Author

kevinsimper commented Sep 21, 2021

@janpio Yes correct!

@janpio
Copy link
Contributor

janpio commented Sep 21, 2021

Ok, so to recap:

  1. Create migration with migrate dev
  2. Realize migration contains index changes
  3. Run modified index changes (adding CONCURRENTLY) by hand (as does not work in transaction)
  4. Comment out indexes in migration file
  5. Run migrate deploy
  6. Future migrate commands will complain about a migration being edited

Is that correct?

@janpio janpio added the topic: prisma migrate deploy CLI: prisma migrate deploy label Sep 21, 2021
@kevinsimper
Copy link
Author

@janpio Close, it is this steps

  1. Create migration with migrate dev
  2. Realize migration contains index changes
  3. Comment out create indexes, run migration that adds columns.
  4. Run modified index changes (adding CONCURRENTLY) by hand (as does not work in transaction)
  5. Commit migration as it was initially generated
  6. Future migrate commands will complain about a migration being edited

@janpio
Copy link
Contributor

janpio commented Sep 30, 2021

  1. Comment out create indexes, run migration that adds columns.

What command are you using to do this?

If you commit a different migration (step 5) than what was run (step 3), I think it is probably expected to get a complaint because the checksum of the migration file is written in step 3, but then on next run compared to what was committed in step 5 - and those won't match. Does that make sense?

(Of course does not help you to figure out a way how to run it the way you expect and want it to run)

@tomhoule tomhoule self-assigned this Nov 4, 2021
@tomhoule tomhoule removed their assignment Nov 4, 2021
@jho-gm
Copy link

jho-gm commented Nov 23, 2021

(For context, I work with Kevin, so I'm not just barging in :-))

@janpio

I think your last message sums up the problem; the checksum in the table is based on the migration run in step 3, but the file on disk after step 5 generates a different checksum, so Prisma complains about those not matching ('The following migrations have been modified since they were applied').

The solution would seem to be to manually updating the checksum in _prisma_migrations (so that it matches the generated file, i.e., step 5). I think this is a reasonable compromise, but that would still require knowing how the checksum is constructed (and ideally in an easily accessible fashion, e.g. some sort of prisma checksum <file> CLI).

@tomhoule
Copy link
Contributor

Candidating this issue for discussion (at least), since we're working on migrate DX, and we may want to just remove these warnings (telling you if a migration has been edited is a job for migrate status).

@aledalgrande
Copy link

@janpio is CREATE INDEX CONCURRENTLY in the roadmap for Postgres?

@janpio
Copy link
Contributor

janpio commented May 16, 2022

Everything that is not supported is on the roadmap - the question is when exactly. Right now CREATE INDEX CONCURRENTLY is not super high on that list as we have more basic functionality (see feature requests).

@aledalgrande
Copy link

Thanks for the update. That feature would really help with databases that have tables with millions of rows with a lot of traffic, as creating an index with locking is not feasible.

@luissquall
Copy link

@jho-gm did you find a simple way to generate the checksum (via bash, another command, etc.) of the updated migration? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: CREATE INDEX CONCURRENTLY topic: indexes topic: prisma migrate deploy CLI: prisma migrate deploy
Projects
None yet
Development

No branches or pull requests

7 participants