Skip to content
Yo-An Lin edited this page Feb 5, 2017 · 11 revisions

Automatic Migration

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

Manual Migration

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)');
    }
}

Importing new schemas

$this->importSchema(new OrderSchema);
$this->improtSchema(new OrderItemSchema);

Creating new table

$this->createTable(function($s) {
    $s->column('title')->varchar(120);
});

Adding new column

$column = new DeclareColumn('cnt');
$column->integer()->unsigned();
$this->addColumn('order_items', $column);

Modifying column

$column = new DeclareColumn('cnt');
$column->integer()->unsigned();
$this->modifyColumn('order_items', $column);

Renaming 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'));

Dropping column

$column = new DeclareColumn('cnt');
$column->integer()->unsigned();
$this->dropColumn('order_items', $column);

Dropping column by column name

$this->dropColumn('order_items', 'unused_column');

Clone this wiki locally