Migrations

Learn how to manage database schema changes

The prisma/migrations folder stores migration files that track changes to your database schema (e.g., adding tables or fields). Prisma’s migration commands synchronize your schema.prisma with your Supabase database, ensuring consistency between your code and database structure.

Running Migrations

Update Your Schema

  • Modify schema.prisma to add or change models (e.g., adding a Post model or a new field to the Event).

Create a Migration

  • Generate a new migration to reflect schema changes:
Terminal
npx prisma migrate dev --name migration-name
  • Replace migration-name with a descriptive name (e.g., add-post-model). This creates a migration file in prisma/migrations and applies it to your Supabase database.

Apply Migrations in Production

  • For production, use:
Terminal
npx prisma migrate deploy
  • This applies pending migrations without generating new ones, ensuring your Supabase database aligns with your schema.

Verify Changes

  • Use Supabase’s dashboard or a SQL client to confirm the schema updates (e.g., new tables or columns).
  • Test your application to ensure queries work as expected.

Notes

  • Environment Variables: Ensure DATABASE_URL is set in both development and production environments. For production, configure it in your deployment platform (e.g., Vercel).
  • Backup Your Database: Before running migrations, back up your Supabase database via the dashboard to avoid data loss in case of errors.
  • Conflict Resolution: If migrations fail due to schema conflicts, reset your database with npx prisma migrate reset (use cautiously, as it drops and recreates the database).
  • Version Control: Commit migration files in prisma/migrations to track schema history, but never commit .env files with sensitive DATABASE_URL values.

By following these steps, you can set up, connect, and manage your Supabase PostgreSQL database with Prisma ORM, keeping your Plainform project’s data layer robust and scalable.

For further details, consult the Supabase Database Overview or Prisma Documentation.

How is this guide ?

Last updated on