-
Notifications
You must be signed in to change notification settings - Fork 26
Migration
Yo-An Lin edited this page Feb 5, 2017
·
11 revisions
Maghead ORM provides a powerful migration feature, which let you migrate your schema changes without writing any code.
The automatic migration command is pretty simple, simply run:
php vendor/bin/maghead migration auto
You can also use command alias to execute the automatic migration:
php vendor/bin/maghead m auto
Sometimes you need to migrate the data in your database instead of just changing the table definitions. you use the command below to generate a migration script file:
$ php vendor/bin/maghead m new CopyColumnData
Creating migration script for 'CopyColumnData'
Migration script is generated: db/migrations/2015-12-01_CopyColumnData.php
The migration script looks like this:
<?php
class CopyColumnData_1448943278
extends LazyRecord\Migration\Migration
{
public function upgrade()
{
}
public function downgrade()
{
}
}Then you fill your sql queries into the upgrade method and the downgrade method:
<?php
class CopyColumnData_1448943278
extends LazyRecord\Migration\Migration
{
public function upgrade()
{
$this->query('UPDATE roles SET LOWER(identity)');
}
public function downgrade()
{
$this->query('UPDATE roles SET UPPER(identity)');
}
}$this->importSchema(new OrderSchema);
$this->improtSchema(new OrderItemSchema);$this->createTable(function($s) {
$s->column('title')->varchar(120);
});$column = new DeclareColumn('cnt');
$column->integer()->unsigned();
$this->addColumn('order_items', $column);$column = new DeclareColumn('cnt');
$column->integer()->unsigned();
$this->modifyColumn('order_items', $column);$column = new DeclareColumn('first_name');
$column->integer()->unsigned();
$this->renameColumn('order_items', 'name', $column);$schema = new ProductSchema;
$this->renameColumn('order_items', 'old_name', $schema->getColumn('first_name'));$column = new DeclareColumn('cnt');
$column->integer()->unsigned();
$this->dropColumn('order_items', $column);$this->dropColumn('order_items', 'unused_column');