What is the good practice for migrations and keeping them on GitHub?
Hello, I started my full stack project and currently we are in dilemma. Here is the scenario, our database will update accordingly with the project, we want to release some feature later let's use Reddit karma for example which is (I assume) only one table column added, if our project already has a lot of online users is there a way we can update our database without resetting and how to do it?
I assume it is with prisma migrations, if that is the case, should we keep the migration files on our GitHub repository or are we allowed to either delete them or add them to .gitignore?
Thank you in advance.
2 Replies
You selected the bot's instantaneous insights. Let me compile your answer right away!
Yes, you're correct that Prisma Migrate is the appropriate tool for managing database schema changes in your scenario. Let me address your questions based on the provided knowledge sources:
1. Updating the database without resetting:
You can indeed update your database schema without resetting it, even when you have online users. Prisma Migrate allows you to make incremental changes to your database schema, such as adding a new column (like Reddit karma in your example).
2. Keeping migration files on GitHub:
It is strongly recommended to keep your migration files in your GitHub repository. According to the Prisma documentation:
Committing the migration history to source control states:
"You must commit the entire
prisma/migrations
folder to source control. This includes the prisma/migrations/migration_lock.toml
file, which is used to detect if you have attempted to change providers."
The reasons for this are:
- Customized migrations contain information that cannot be represented in the Prisma schema alone.
- The prisma migrate deploy
command, used for deploying changes to production environments, only runs migration files.
3. Process for updating the database:
To update your database with a new feature (like adding a karma column), you would follow these steps:
1. Update your Prisma schema to include the new column.
2. Run npx prisma migrate dev
in your development environment. This will create a new migration file.
3. Commit the new migration file along with your schema changes to your GitHub repository.
4. When deploying to production, use npx prisma migrate deploy
to apply the new migration.
This process allows you to make schema changes safely without resetting your database or losing data from existing users.
[Hassle-Free Database Migrations with Prisma Migrate](https://www.prisma.io/blog/pri
sma-migrate-ga-b5eno5g08d0b#database-schema-migrations-with-prisma-migrate) explains that Prisma Migrate generates SQL migration files based on your schema changes, allowing you to evolve your database schema alongside your application.
In conclusion, use Prisma Migrate for your database updates, keep your migration files in your GitHub repository, and don't add them to .gitignore or delete them. This practice ensures that your team can collaborate effectively and that you can safely deploy schema changes to your production environment.Hey 👋
Let us know if you still need any help 🙂