I have a prisma db and I am trying to run the deploy command for production.
When running yarn rw prisma migrate deploy , I get an error saying:
1 migration found in prisma/migrations
Error: P3005
The database schema is not empty. Read more about how to baseline an existing production database: https://pris.ly/d/migrate-baseline
What am I doing wrong and how can I fix it?
If you setted up Prisma on an existing database you must skip initial migration as such:
yarn prisma migrate resolve --applied <migration_name>
Its called baselining.
If it doesnt work due to some existing data, you can either rollback changes or fix the data problem and apply a fix with yarn prisma migrate diff
Details are here.
I ran the diff and applied the resulting file as such:
yarn prisma migrate diff --from-url <DATABASE_URL_PROD> --to-schema-datamodel <path to schema.prisma> --script > forward.sql
yarn prisma db execute --url <DATABASE_URL_PROD> --file forward.sql
Related
I am connecting my existing database to Prisma. I am followed the documentation (https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/relational-databases/baseline-your-database-typescript-sqlserver). But the baseline step is throwing an error that is given below,
the command I tried is "npx prisma migrate resolve --applied 0_init". Anybody have the same issue?
In CI(GitHub Actions), I want to check if schema changed without migration generated.
There is exact command what I want in Django, ./manage.py makemigrations --check, but I cannot found such command in Prisma.
https://docs.djangoproject.com/en/4.1/ref/django-admin/#cmdoption-makemigrations-check
What would be the best way to check ungenerated migration with Prisma?
Depending on what exactly you want to compare you can use prisma migrate diff for this.
prisma migrate diff --from-schema-datamodel --to-schema-datasource --exit-code
prisma migrate diff --from-migrations --to-schema-datasource --exit-code
prisma migrate diff --from-migrations --to-schema-datamodel --exit-code
This will compare the state of the two sides with each other. If they are the same it will exit with 0 if there are differences with 2.
See the command docs here: https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-diff
When I changed prisma.schema and run:
npx prisma migrate dev --name init
My Postgres lost all data. Can I run another command which doesn't remove my data? What is best for me in this situation?
From the prisma.io documentation:
Prisma Migrate is an imperative database schema migration tool that enables you to keep your database schema in sync with your Prisma schema as it evolves and
main existing data in your database
Prisma Migrate generates a history of .sql migration files, and plays a role in both development and deployment.
This means that the prisma migrate command is supposed to be syncing the data from the model with the one in the database, adding/removing rows if needed, etc.
Perhaps the command you are looking for is prisma db push? They both have similar uses and aspects, one is for dev, one isn't.
Given a team composed of data scientists and developers.
Developers want to use schema.prisma but data scientists don't and want to freely edit the DB directly...
What happen if a data scientist alter the DB directly? Will prisma migrate dev/deploy continue to work correctly?
If the data scientists alter the database directly then there would be a drift between the prisma schema and the database schema.
So next time when someone invokes prisma migrate dev command, prisma would prompt you to reset the database.
From Documentation:
The migrate dev command 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
It is advisable to stick to only one approach, either updating database only through migrations or just directly updating the database, combining both would lead to an undesirable state.
In addition to #Nurul Sundarani's answer, here is what I did:
$ vim .env # Points `DATABASE_URL` to the data-scientists' DB
$ npx prisma db pull # Pull changes from it and update `schema.prisma`
$ vim .env # Points `DATABASE_URL` back to localhost
$ npx prisma migrate dev # Generate the migration and apply changes tolocal DB
Name of your migration: my_sneaky_new_migration
$ git add prisma/schema.prisma prisma/migrations/20220603XXXXX_my_sneaky_new_migration
$ git commit
$ vim .env # Points `DATABASE_URL` to the data-scientists' DB
$ npx prisma migrate resolve --applied "my_sneaky_new_migration" # Mark the migration as already applied
$ vim .env # Points `DATABASE_URL` back to localhost
When I try to do a migration of my database in heroku using vapor I get the following error when I run heroku run Run -- migrate --env production
FluentKit/Databases.swift:160: Fatal error: No default database configured.
I execute heroku config and created a database before migration.
Local migration works for me without problem. From a database management software if I can access the database without problem.
Thanks