Maciek K
10/07/2021, 11:35 AMnpx prisma migrate dev
on my development branch and db. When doing the npx prisma migrate deploy
against staging environemtn it errored out on this part
Database error:
ERROR: relation "User.email_unique" does not exist
So exactly on this sql statement:
-- RenameIndex
ALTER INDEX "User.email_unique" RENAME TO "User_email_key";
From schema.prisma:
model User {
//deleted unnecessary stuff
email String @unique
}
So only then I did notice the text at the bottom (Dealing with cases where more than one database environment is used for the same application). π€
So I have pulled schemas from other database environments and I have a problem, because they do differ indeed. In example on production the index does not have the name User.email_unique
, but:
model User {
email String @unique(map: "booking-production$prod.User.email._UNIQUE")
}
It has also the postgres schema name prepended (!).
So what should I do now? I would also very much like to use the default Prisma 3 naming convention for all environments (User_email_key
)?Ryan
10/07/2021, 11:43 AMMaciek K
10/07/2021, 11:44 AMMaciek K
10/07/2021, 11:59 AMtom
10/07/2021, 2:36 PMtom
10/07/2021, 2:38 PMtom
10/07/2021, 2:38 PMMaciek K
10/07/2021, 2:54 PMtom
10/07/2021, 3:26 PM.sql
script that would sync constraint names between your prisma schema and your staging db. If you are open to that, I can either send you a compiled binary or you can compile from source (if you have a rust toolchain installed).Maciek K
10/07/2021, 3:34 PMtom
10/07/2021, 3:37 PMtom
10/07/2021, 3:40 PMtom
10/07/2021, 3:41 PMtom
10/07/2021, 3:44 PMtest-cli
binary I just sent
β’ Point your prisma.schema
to your staging database (either string literal or env var in your datasource
block)
β’ Make sure the prisma.schema
is the version you actually want.
β’ Run ./test-cli migrate-diff prisma/schema.prisma --output-type=ddl
β this command does not write anything to the database, it just reads, so it's safe. You can also remove the --output-type=ddl
to have a more readable diff summary. The output should be the SQL script you need.
This is only a very very rough version of the command we want, but the actual machinery is there and well tested. The full command would let you diff between two schemas from any source (a database, a prisma schema, a migrations directory) and produce the migration migrate would use to sync them. It should be super useful for debugging, recovering from failed migrations, etc.Maciek K
10/07/2021, 3:47 PMError: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database.
Migration name: 20210914183711_prisma_3_upgrade
Database error code: 42P01
Database error:
ERROR: relation "User.email_unique" does not exist```
Maciek K
10/07/2021, 3:48 PMtom
10/07/2021, 3:49 PMmigrate diff
is going to be part of the solution.
In your case, once you have generated the script, reviewed that it makes sense, and applied it, you can run prisma migrate resolve --applied '20210914183711_prisma_3_upgrade'
on your staging database, and it should be back on track.Maciek K
10/07/2021, 4:02 PM17:59 $ ./test-cli migrate-diff prisma/schema.prisma --output-type=ddl
reading the prisma schema from prisma/schema.prisma
Error: error: Environment variable not found: DATABASE_URL.
--> schema.prisma:7
|
6 | provider = "postgresql"
7 | url = env("DATABASE_URL")
|
Maciek K
10/07/2021, 4:02 PMMaciek K
10/07/2021, 4:04 PM-- RenameIndex
ALTER INDEX "booking-production$prod.User.email._UNIQUE" RENAME TO "User_email_key";
Maciek K
10/07/2021, 4:07 PMtom
10/07/2021, 4:18 PMMaciek K
10/07/2021, 5:02 PMmigrate resolve --applied
this is how it looks in db. Is this correct with the applied_steps_count
at 0?
Running prisma migrate deploy
after this gives no errors. No pending migrations to apply.
tom
10/07/2021, 5:27 PMMaciek K
10/08/2021, 8:19 AMapplied_steps_count
should not be 0. Anyway I'm reporting that I have succesfully applied all migrations with this tool all up to production with no issues. Smooth as butter. Thank you again, classy as always π π₯ prisma rainbowtom
10/08/2021, 10:57 AMOk I have thought thatYes, that's a bit misleading, but it's totally fine. Migrate writes toshould not be 0.applied_steps_count
applied_steps_count
, but in practice we are not using it.Frederik
12/08/2021, 2:51 PMFrederik
02/18/2022, 11:55 AMMaciek K
02/18/2022, 1:07 PM