Migration
, (*1)
The migration packages lets you manage and execute your migrations., (*2)
, (*3)
Installation
If you are working on an Asgard project you don't need to install this library as it is already part of the standard libraries., (*4)
composer require asgard/migration 0.*
, (*5)
Overview
All migrations must extend \Asgard\Migration\Migration or \Asgard\Migration\DBMigration (which extends \Asgard\Migration\Migration)., (*6)
\Asgard\Migration\DBMigration can be useful for database migrations as it will automatically begin a transaction before the migration is executed and commit it only if the migration was successful., (*7)
Migrations must implements the up() or/and down() method., (*8)
For example:, (*9)
<?php
class News extends \Asgard\Migration\DBMigration {
public function up() {
$this->schema->create('news', function($table) {
$table->addColumn('id', 'integer', [
'length' => 11,
'autoincrement' => true,
]);
$table->addColumn('text', 'string', [
'length' => '255',
]);
$table->addColumn('meta_title', 'text', [
'length' => '65535',
]);
$table->setPrimaryKey(['id']);
});
}
public function down() {
$this->schema->drop('news');
}
}
All the migration files must be located in the same folder. In a Asgard project, the migration folder is at migrations/., (*10)
This folder contains one json file:, (*11)
- migrations.json contains all active migrations
The migration statutes are store in the _migrations table., (*12)
Examples:, (*13)
migrations.json:, (*14)
{
"Data": {
"added": 1401943722.9835
}
}
The Data migration must be in a file called Data.php at migrations/Data.php., (*15)
, (*16)
MigrationManager
Usage in the Asgard Framework
$migrationManager = $container['migrationManager'];
The container is often accessible as a method parameter or through a ContainerAware object. You can also use the singleton but it is not recommended., (*17)
Usage outside the Asgard Framework
$migrationManager = new \Asgard\Migration\MigrationManager('/path/to/migrations/', $container /*optional*/);
Methods
Add a migration:, (*18)
$migrationManager->add('/path/to/Migration.php');
Will copy the file to the migrations directory and add it to migrations.json., (*19)
Check if a migration already exists:, (*20)
$migrationManager->has('Migrationname');
Remove a migration:, (*21)
$migrationManager->remove('Migrationname');
Execute a migration:, (*22)
$migrationManager->migrate('Migrationname');
Migrate a file:, (*23)
$migrationManager->migrateFile('/path/to/Migration.php');
Migrate all migrations:, (*24)
$migrationManager->migrateAll();
Reset migrations (rollback and re-migrate all migrations):, (*25)
$migrationManager->reset();
Unmigrate a specific migration:, (*26)
$migrationManager->unmigrate('Migrationname');
Rollback the last migration:, (*27)
$migrationManager->rollback();
Rollback up to a specific migration:, (*28)
$migrationManager->rollbackUntil('Migrationname');
Create a new migration:, (*29)
$up = "$this->schema->create('news', function($table) {
$table->addColumn('id', 'integer', [
'length' => 11,
'autoincrement' => true,
]);
$table->addColumn('text', 'string', [
'length' => '255',
]);
$table->addColumn('meta_title', 'text', [
'length' => '65535',
]);
$table->setPrimaryKey(['id']);
});";
$down = "$this->schema->drop('news');";
$migrationManager->create($up, $down, 'Migrationname', $class='\Asgard\Migration\Migration');
, (*30)
Tracker
The tracker is helpful to track the statuses of your migrations., (*31)
Usage in the Asgard Framework
$tracker = $container['migrationManager']->getTracker();
Usage outside the Asgard Framework
$tracker = $migrationManager->getTracker();
The container is often accessible as a method parameter or through a ContainerAware object. You can also use the singleton but it is not recommended., (*32)
Methods
Get all migrations:, (*33)
$tracker->getList();
Get all non-executed migrations:, (*34)
$tracker->getDownList();
Get all executed migrations:, (*35)
$tracker->getUpList();
Check if a migration exists:, (*36)
$tracker->has('migrationName');
Get the next migration to be executed:, (*37)
$tracker->getNext();
Get the last executed migration:, (*38)
$tracker->getLast();
Get all migrations up to a specific migration:, (*39)
$tracker->getUntil('migrationName');
Get all migrated migrations up to a specific migration in reverse order:, (*40)
$tracker->getRevereMigratedUntil('migrationName');
Add a migration to migrations.json (the migration file must already be in the migrations folder):, (*41)
$tracker->add('migrationName');
Remove a migration from migrations.json (without deleting the file):, (*42)
$tracker->remove('migrationName');
Mark a migration as unmigrated:, (*43)
$tracker->unmigrate('migrationName');
Mark a migration as migrated:, (*44)
$tracker->migrate('migrationName');
Check if a migration is up:, (*45)
$tracker->isUp('migrationName');
, (*46)
Commands
AddCommand
Add a new migration to the list, (*47)
Usage:, (*48)
php console migrations:add [src]
src: The migration file, (*49)
ListCommand
Displays the list of migrations, (*50)
Usage:, (*51)
php console migrations:list
MigrateCommand
Run the migrations, (*52)
Usage:, (*53)
php console migrate
MigrateOneCommand
Run a migration, (*54)
Usage:, (*55)
php console migrations:migrate [migration]
migration: The migration name, (*56)
RefreshCommand
Reset and re-run all migrations, (*57)
Usage:, (*58)
php console migrations:refresh
RemoveCommand
Remove a migration, (*59)
Usage:, (*60)
php console migrations:remove [migration]
migration: The migration name, (*61)
RollbackCommand
Rollback the last database migration, (*62)
Usage:, (*63)
php console migrations:rollback
UnmigrateCommand
Unmigrate a migration, (*64)
Usage:, (*65)
php console migrations:unmigrate [migration]
migration: The migration name, (*66)
Contributing
Please submit all issues and pull requests to the asgardphp/asgard repository., (*67)
License
The Asgard framework is open-sourced software licensed under the MIT license, (*68)