Overview
The Juadah API uses Prisma as its ORM, which provides a robust migration system to manage database schema changes. Migrations are version-controlled SQL files that track all changes to your database schema.Prerequisites
Before running migrations, ensure you have:- PostgreSQL installed and running
- Node.js v20 (as specified in the project README)
- Environment variables configured
Environment Setup
1. Configure Environment Variables
Copy the.env.example file to .env and configure your database connection:
2. Database Connection URLs
Add the following PostgreSQL connection strings to your.env file:
DATABASE_URL is used for connection pooling, while DIRECT_URL is used for direct connections during migrations.
Connection String Format
PostgreSQL username
PostgreSQL password
Database host (e.g., localhost or remote host)
PostgreSQL port number
Database name (e.g., juadah)
Prisma Commands
The project includes several npm scripts for managing Prisma and migrations:Generate Prisma Client
Generate the Prisma Client after schema changes:The Prisma Client is automatically generated after running
npm install via the postinstall script.Build the Project
Build includes Prisma Client generation and TypeScript compilation:prisma generate- Generates Prisma Clienttsc- Compiles TypeScript to JavaScript
Migration Workflow
Understanding Existing Migrations
The project has existing migrations inprisma/migrations/:
View Initial Migration SQL
View Initial Migration SQL
Running Migrations
Apply Existing Migrations
To apply all pending migrations to your database:- Applies all migrations in the
prisma/migrations/folder - Does not generate new migrations
- Ideal for production deployments
Development Migrations
During development, use:- Creates a new migration from schema changes
- Applies the migration to the database
- Generates Prisma Client
- Prompts for a migration name
Reset Database
To reset your database and reapply all migrations:Creating New Migrations
1. Modify the Schema
Editprisma/schema.prisma to add or modify models:
2. Create Migration
Generate a new migration:3. Review Generated SQL
Always review the generated SQL before applying:4. Apply Migration
The migration is automatically applied when usingmigrate dev. For production:
Migration Best Practices
1. Version Control
1. Version Control
Always commit migration files to version control (Git). This ensures:
- Team members have the same database schema
- Migrations can be rolled back if needed
- Production deployments are consistent
2. Never Edit Applied Migrations
2. Never Edit Applied Migrations
Once a migration is applied and committed:
- Never modify the SQL file
- Create a new migration instead
- Editing applied migrations can cause inconsistencies
3. Test Migrations Locally
3. Test Migrations Locally
Before deploying to production:
- Test migrations on a local database
- Verify data integrity
- Check for breaking changes
- Test rollback procedures if needed
4. Use Descriptive Names
4. Use Descriptive Names
Name migrations clearly:
5. Backup Before Major Changes
5. Backup Before Major Changes
Before running migrations that modify or delete data:
Troubleshooting
Migration Failed
If a migration fails:- Check the error message - It usually indicates the issue
- Resolve the issue in your schema or database
- Mark migration as rolled back:
- Try again:
Schema Out of Sync
If your schema doesn’t match the database:Reset Development Database
If your development database is in a bad state:Prisma Studio
Prisma Studio is a visual database browser:http://localhost:5555 where you can:
- View all tables and data
- Edit records
- Run queries
- Test relationships
Prisma Studio is helpful for debugging and exploring your database during development.
Production Deployment
CI/CD Pipeline
In your deployment pipeline:Environment Variables
Ensure your production environment has:Additional Resources
Database Schema
View complete database schema documentation
Prisma Documentation
Official Prisma documentation
PostgreSQL Docs
PostgreSQL official documentation
Migration Reference
Prisma Migrate reference guide