Database Migration
Learn how to generate and manage database migrations in StackRender, including creating, applying, and rolling back schema changes safely across environments.
What is a Database Migration?
A database migration is a versioned change to your database schema. It allows you to modify your database structure over time in a controlled and consistent way.
Migrations are commonly used to:
- Create or update tables
- Add, modify, or remove columns
- Define relationships and constraints
Each migration represents a specific step in your database evolution and can be applied or rolled back when needed. This makes it easier to keep your database in sync across different environments like development, staging, and production.
In StackRender, migrations are generated and tracked automatically from the ER Diagram changes, helping you safely manage schema changes without losing control of your data.
Creating a Migration
Once you design your schema or make changes to it, head to the Migrations page to generate a new migration.
-
First Migration
If no migrations exist yet, you can create your first migration.
StackRender will automatically generate the full SQL (DDL) required to initialize your database schema. -
Subsequent Migrations
If you already have an existing migration, you must apply (close) it before creating a new one.
Once the previous migration is applied, you can generate a new migration.
This new migration will include only the SQL differences (diff) based on the changes you made to your schema.This ensures that each migration represents a clear and incremental step in your database evolution.
Applying and Rolling Back Migrations
Each migration in StackRender consists of two scripts:
- UP script → used to apply the migration
- DOWN script → used to roll back the migration
By default, when you open the Migrations page, the current migration is already selected. StackRender keeps you focused on the active migration so you can stay on track.
-
Applying a Migration
To apply a migration:
- Copy the UP script
- Run it against your database
- Click Deploy to close the migration
Once a migration is closed, it is no longer used to track schema changes. StackRender will then move to the next available migration (if any).
-
Rolling Back a Migration
To roll back a migration:
- Copy the DOWN script
- Run it against your database
- Click Rollback
This will revert the applied changes and move the current migration back to the previous one (if available).