Yii2 migrations provide a structured way to manage database schema changes using PHP code. Instead of manually creating tables, adding columns, or modifying indexes, migrations allow you to define these changes in code and apply them using console commands.
Run the following command:
php yii migrate/create create_table_studentThis generates a migration file inside the migrations folder (@app/migrations). The file will be named something like:
m230101_123456_create_table_student.phpModify the generated file by defining schema changes in up() and down() methods.
use yii\db\Migration;
class m230101_123456_create_table_student extends Migration
{
public function up()
{
$this->createTable('student', [
'id' => $this->primaryKey(),
'name' => $this->string(255)->notNull(),
'email' => $this->string(255)->unique(),
'created_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
]);
}
public function down()
{
$this->dropTable('student');
}
}To apply migrations:
php yii migrateTo revert the last migration:
php yii migrate/down 1To apply only a specific migration:
php yii migrate/up m230101_123456_create_table_studentYii2 does not have built-in seeding, but you can create migrations that insert initial data.
php yii migrate/create insert_dummy_dataModify the generated file:
use yii\db\Migration;
class m230101_234567_insert_dummy_data extends Migration
{
public function up()
{
$this->batchInsert('student', ['name', 'email'], [
['Rahul', 'rahul@example.com'],
['Mohit', 'mohit@example.com'],
['Vivek', 'vivek@example.com'],
]);
}
public function down()
{
$this->delete('student', ['email' => ['rahul@example.com', 'mohit@example.com', 'vivek@example.com']]);
}
}Run:
php yii migrateIn Yii2 modules, migrations should be placed inside the migrations directory of the module.
modules/
location/
migrations/
m230101_123456_create_table_location.php
models/
controllers/To run migrations inside a module, specify the path:
php yii migrate --migrationPath=@app/modules/location/migrationsTo revert:
php yii migrate/down --migrationPath=@app/modules/location/migrationsAlthough Yii2 migrations are useful, they have some limitations:
| Scenario | Use Migrations? | Reason |
|---|---|---|
| Small to Medium Applications | Yes | Keeps database schema changes under version control. |
| Large Applications with Frequent Changes | Use with caution | If database structure changes too often, developers spend too much time writing migrations. |
| Production Environments | Yes | Ensures consistent schema across all servers. |
| Large Tables with 1000+ Columns | No | Writing migrations for large tables is time-consuming and may lead to performance issues. Instead, use raw SQL scripts. |
| Minor Schema Changes (e.g., Adding a Single Column) | Yes | Easy to manage and roll back if needed. |
| Major Schema Overhaul (Dropping/Recreating Large Tables) | No | Use database-specific scripts instead. |
If migrations are too slow or difficult for large projects, consider these alternatives:
Use Database Dumps for Schema Management
mysqldump -u root -p database_name > schema.sqlInstead of writing Yii2 migrations, you can store the SQL dump in version control.
Raw SQL Scripts for Large Tables
ALTER TABLE large_table ADD COLUMN new_column INT(11) NOT NULL;Use a Dedicated Database Migration Tool
| Feature | Command / Method |
|---|---|
| Create Migration | php yii migrate/create migration_name |
| Run All Migrations | php yii migrate |
| Rollback Last Migration | php yii migrate/down 1 |
| Run Specific Migration | php yii migrate/up migration_name |
| Insert Data (Seeder) | Use $this->batchInsert() in a migration |
| Module Migration | php yii migrate --migrationPath=@app/modules/module_name/migrations |
| Large Database Alternative | Use SQL dumps or manual scripts |
Sign in to join the discussion and post comments.
Sign in