Migrations
Database migrations are essential for managing changes to your schema over time in a controlled manner. In CQL, migrations are handled through the Migration
and Migrator
classes. This guide will help you understand how to create, apply, rollback, and manage migrations using CQL::Migrator
in your projects.
Why Use Migrations?
Migrations allow you to:
Apply changes to your database schema over time.
Roll back changes in case of errors or updates.
Track applied and pending changes, ensuring consistency across environments.
Real-World Example: Creating and Applying Migrations
Let’s start with a simple example. Suppose we need to add a users
table to our database with two columns: name
and age
.
Explanation
The
up
method: defines the changes to apply when the migration is run (e.g., adding new columns).The
down
method: defines how to revert the changes (e.g., dropping columns).Versioning: Each migration is assigned a version number, which ensures migrations are run in the correct order.
Initializing the Schema and Migrator
Before applying migrations, you need to set up the schema and create an instance of the Migrator
.
The migrator, upon initialization, automatically creates a schema_migrations
table to track which migrations have been applied.
Applying Migrations
To apply all pending migrations, simply call the up
method on the migrator
object:
This will apply all pending migrations in order of their version numbers.
Applying Migrations Up to a Specific Version
You can also apply migrations up to a specific version:
This will apply all migrations up to version 1_i64
.
Rolling Back Migrations
To roll back the last migration, use the down
method:
You can also roll back to a specific migration version:
This rolls back all migrations down to version 1_i64
.
Redoing Migrations
If you want to rollback and then re-apply the last migration, use the redo
method:
This first rolls back the last migration and then re-applies it.
Listing Migrations
You can list applied, pending, and rolled-back migrations with the following commands:
List Applied Migrations:
List Pending Migrations:
List Rolled Back Migrations:
These commands provide a clear view of the current state of your migrations, making it easy to track progress and issues.
Managing Migrations
Checking the Last Applied Migration
You can retrieve information about the last applied migration using:
This gives you details about the last migration that was successfully applied.
Advanced Example: Managing Multiple Migrations
Here’s an example where we define multiple migrations and apply them sequentially:
Versioning ensures that migrations are applied in the correct order.
Each migration can be applied and rolled back independently, offering flexibility in managing your database schema.
Conclusion
The CQL::Migrator
class makes it easy to manage database migrations in a structured and version-controlled manner. By following this guide, you can:
Create and apply migrations to modify your schema.
Roll back changes if needed.
Track applied and pending migrations to keep your database consistent across environments.
This approach is essential for teams working on large applications where database changes need to be applied safely and consistently over time.
Last updated