Switch to migrations after db push

Hello , I got a new project from a client , and they are using SQLite , I noticed they do not have any migrations , but rather db push directly , as this is not safe , I want to switch them to migrations , however how can we manage that without losing data as firing the migration will issue a drift
1 Reply
Prisma AI Help
Prisma AI Helpā€¢2w ago
You selected the bot's instantaneous insights. Let me compile your answer right away! To switch from using db push to migrations without losing data, you can follow these steps: 1. Create an initial migration that represents your current database state:
mkdir -p prisma/migrations/init
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/init/migration.sql
npx prisma migrate resolve --applied init
mkdir -p prisma/migrations/init
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/init/migration.sql
npx prisma migrate resolve --applied init
2. After this, run npx prisma migrate dev. It should indicate that everything is in sync. This process is known as baselining, which is the recommended method to sync your migration history with your current database schema without resetting the database or losing data. For future development, it's recommended to consistently use prisma migrate dev for creating and applying migrations, rather than mixing db push and migrate dev, as this can lead to drift. Github Issue Comment provides these steps, and Prisma Docs on Baselining offer more detailed information on this process. Remember, after switching to migrations, you should use npx prisma migrate deploy in production environments to ensure your database won't get reset in any case. āœ… Great, thanks for letting us know that this solved your question!

Did you find this page helpful?