execut/yii2-migration

Yii2 helper to simplify of writing migration

Installs: 752

Dependents: 8

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 1

Open Issues: 0

Type:yii2-extension

1.8.2 2020-07-20 09:02 UTC

This package is auto-updated.

Last update: 2024-10-24 17:33:24 UTC


README

This is a typical migration for yii2:

    public function safeUp()
    {
        $this->createTable('characteristics_units', [
            'id' => $this->primaryKey(),
            'name' => $this->string()->notNull(),
            'short_name' => $this->string()->notNull(),
            'created' => $this->dateTime()->notNull()->defaultExpression('now()'),
            'updated' => $this->dateTime(),
        ]);

        $this->createTable('characteristics', [
            'id' => $this->primaryKey(),
            'characteristics_unit_id' => $this->integer()->notNull(),
            'name' => $this->string()->notNull(),
            'created' => $this->dateTime()->notNull()->defaultExpression('now()'),
            'updated' => $this->dateTime(),
        ]);

        $this->addForeignKey('characteristics_unit_id_characteristics_fk', 'characteristics', 'characteristics_unit_id', 'characteristics_units', 'id');
    }

    public function safeDown()
    {
        $this->dropTable('characteristics');
        $this->dropTable('characteristics_units');
    }

Why write more? If you use execut yii2-migration helper, you can write it faster and more compact:

    public function initInverter(\execut\yii\migration\Inverter $i)
    {
        $i->table('characteristics')->create(array_merge($this->defaultColumns(), [
            'name' => $this->string()->notNull(),
            'short_name' => $this->string()->notNull(),
        ]));

        $i->table('characteristics_units')->create(array_merge($this->defaultColumns(), [
            'name' => $this->string()->notNull(),
        ]))->addForeignColumn('characteristics');
    }

Installation

The preferred way to install this extension is through composer.

Install

Either run

$ php composer.phar require execut/yii2-migration "dev-master"

or add

"execut/yii2-migration": "dev-master"

to the require section of your composer.json file.

Usage

To use yii2 migration, simply expand the migration class from the execut\yii\migration\Migration class and override the abstract method:

     public function initInverter(\execut\yii\migration\Inverter $i)
     {
     }

$i has all the methods that normal migration has, but allows you to write actions up and down at a time.

To permanently do not rewrite the migration, you can define a new template for the yii migrate\create command.

    'controllerMap' => [
        'migrate' => [
            'templateFile' => '@vendor/execut/yii2-migration/views/template.php',
        ],

Supported databases

Currently only supported PostgreSQL and MySQL. Also you can use BDR plugin for PostgreSQL.