Hello @lumenwrites
npx prisma db push is meant for development use only. The development db is considered "resettable" and "seedable" as its used to develop your migrations and to test/tweak them if needed. Looks like you have used it in your production environment. In production, you should use npx prisma migrate deploy and it would ensure your db won't get reset in any case.
Now in order to resolve your current situation we need to tweak the migrations history using some primitives which migrate provides like the diff command(https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-diff)
- You will need to generate a new migration which will be the difference between migrations history stored in your disk in the migrations folder and your production database. The
--to-schema-datasourceflag here point to the database stored in your.envfile but you can also change this flag to--to-urlif you prefer to give the database url in the command itself.
mkdir -p prisma/migrations/add-comments-and-post/
npx prisma migrate diff --from-migrations prisma/migrations --to-schema-datasource prisma/schema.prisma --shadow-database-url --script > prisma/migrations/add-comments-and-post/migration.sql
The --shadow-database-url parameter is required and you will need to point it to any empty postgres database either a local one or hosted one. Prisma will use that to reply migration history and calculate the diff.
This will create a new migration in prisma/migrations/add-comments-and-post/migration.sql which will have the SQL for the model you have added via db push.
- Now you have the migration ready, we will need to mark it as applied. This is because those tables already exists in the database and we can't run the actual
CREATEsql statements to the database. We will just mark the migration as applied as it was already applied when you rannpx prisma db push.
npx prisma migrate resolve --applied add-comments-and-post
- Now, you should be ready to go. Run
npx prisma migrate devonce now and it should say everything is in sync.
Difference between prisma db push and prisma migrate dev - Stack Overflow
Where do you actually run prisma migrate deploy?
`npx prisma migrate dev --name init` fails in starter project
`npx prisma migrate dev` hangs up
They serve two different environments. The prisma db push is not to be used in your production environment, as stated in the docs
db pushuses the same engine as Prisma Migrate to synchronize your Prisma schema with your database schema, and is best suited for schema prototyping. Thedb pushcommand:
Introspects the database to infer and executes the changes required to make your database schema reflect the state of your Prisma schema.
By default, after changes have been applied to the database schema, generators are triggered (for example, Prisma Client). You do not need to manually invoke
prisma generate.If
db pushanticipates that the changes could result in data loss, it will:
- Throw an error
- Require the
--accept-data-lossoption if you still want to make the changesNote:
db pushdoes not interact with or rely on migrations. The migrations table will not be updated, and no migration files will be generated.
The prisma migrate dev is used in you local environment, as explained in the docs
migrate devis a development command and should never be used in a production environment.This command:
- Replays the existing migration history in the shadow database in order to detect schema drift (edited or deleted migration file, or a manual changes to the database schema)
- Applies pending migrations to the shadow database (for example, new migrations created by colleagues)
- Generates a new migration from any changes you made to the Prisma schema before running
migrate dev- Applies all unapplied migrations to the development database and updates the
_prisma_migrationstable- Triggers the generation of artifacts (for example, the Prisma Client)
The
migrate devcommand will prompt you to reset the database in the following scenarios:
- Migration history conflicts caused by modified or missing migrations
- The database schema has drifted away from the end-state of the migration history
If you have any other question regarding this, there is this comparison in the docs explaining when to use one or the other.
- Prisma db push syncs (and formats) your prisma schema to that of your database schema.
- While prisma migrate dev "command automatically generates SQL migration files (saved in /prisma/migrations) and applies them to the database".
The main difference between both is the generation of the migration files.
I'm working on a little project with supabase and prisma, I've just been using one dev database which I update with prisma migrate dev. For production migrations, you're supposed to use prisma migrate deploy, which the prisma docs say to put in a CI/CD pipeline.
-
I don't have a CI/CD pipeline
-
I can't run it locally from my project directory, cause prisma uses the
DATABASE_URLenvironment, which is pointing to my dev database
So I'm wondering where y'all would actually run prisma migrate deploy from? Should I just bite the bullet and learn some github actions? Or is there an easier way?
EDIT: The prisma docs recommend using dotenv-cli to use switch between .env files, but I'm kinda worried that I might fuck up somehow and end up running prisma migrate reset on my prod database, and delete everything. But, seems like that might be my plan until I figure out some CI/CD stuff.
Still definitely open to any advice though!