https://github.com/tuliren/prisma-template
Prisma template with better rollback functionality
https://github.com/tuliren/prisma-template
migration prisma rollback template
Last synced: 8 months ago
JSON representation
Prisma template with better rollback functionality
- Host: GitHub
- URL: https://github.com/tuliren/prisma-template
- Owner: tuliren
- License: mit
- Created: 2024-10-03T06:32:21.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-20T00:01:06.000Z (9 months ago)
- Last Synced: 2025-01-20T01:18:54.653Z (9 months ago)
- Topics: migration, prisma, rollback, template
- Language: Shell
- Homepage:
- Size: 40 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Prisma Template
This is a Prisma template with better migration rollback support.
Native Prisma migration system has limited rollback functionality:
- A separate command is required to create a down migration.
- It can only roll back a migration when the migration fails.See documentation [here](https://www.prisma.io/docs/orm/prisma-migrate/workflows/generating-down-migrations) for details.
Of course, down migrations should not be used in production. However, it is a common operation during development. When working on database schema change, one can seldom get everything right the first time. And it is undesirable to create multiple migrations for a single schema change, or to manually edit the migration files and database schema to fix the mistake.
The golden migration experience is from Rails, in which one can always create an up migration and a down migration for each schema change. During development, when a mistake is made, just rollback the last migration and write a new one.
This template aims to provide a similar experience for Prisma as follows:
- Create a rollback SQL script for each migration.
- Add a rollback command to roll back the last migration.
- Add a schema dump command to pull the current schema from the database, and persist it to `prisma/schema_dump.sql`.## Commands
| Command | Description |
| --- | --- |
| `npm run db:check-drift` | Check the difference between the schema in the database vs in the Prisma schema file. |
| `npm run db:create-migration` | Create a new migration, together with the rollback script. |
| `npm run db:deploy` | Apply the migration, and update the schema dump. |
| `npm run db:rollback` | Roll back the last migration, delete the migration file, and update the schema dump. |
| `npm run db:generate` | Generate and update Prisma client code. |See [`package.json`](./package.json) for more details.
## Database migration workflow
- Run `yarn db:create-migration` to create a new migration.
- The normal `migration.sql` file is created under `-` directory.
- A `down.sql` file for down migration is generated under the same directory.
- Run `yarn db:deploy` to apply the migration.
- The `db:deploy` also runs `db:dump-schema` to create or update the SQL schema file at `prisma/schema_dump.sql`
- The schema dump is updated to reflect the current schema of the database, and should be checked into source control for reference.
- Run `yarn db:rollback` to roll back the last migration.
- The `down.sql` file is executed to revert the schema change in the database.
- The record in `_prisma_migrations` table for the reverted migration is deleted.
- The `-` directory is deleted.
- The schema dump is updated.
- Change the `schema.prisma` file, and create a new migration.## LICENSE
[MIT](LICENSE)