kubo0/yii2-migrate-array

Yii2 migration as array helper and template


Keywords
migration, yii2
License
BSD-3-Clause-Clear

Contributors

KUbo0


See all contributors


Documentation

yii2-migrate-array

Yii2 up and down migration as array helper and template.

Supported commands:

  • createTable with columns and options
  • renameTable
  • addColumn
  • renameColumn
  • alterColumn
  • createIndex

Installation

Configure config/console.php or console/config/main.php:

return [
...
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'templateFile' => 'kubo0/migratearray/Migration_array.php',
        ],
    ],
...
];

then ./yii migrate/create will use our template instead of standard one.

modify $migration array to your purpose:

private $migration = [
    'table1'=>[
        'createTable' => [
            'options'=>'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB',
            'columns'=>[ // colum name => schema type
                'id' => Schema::TYPE_BIGPK,
                'name' => Schema::TYPE_STRING,
                'type' => Schema::TYPE_INTEGER,
                'status' => 'int(1) null',
                'text' => Schema::TYPE_TEXT . ' null',
                ]
            ]
        ],
    [
    'tableName' => 'table1'
    // command => new name
    'renameTable'=>'table2',
    ],
    'table2'=>[
        'renameTable'=>'table22', // command => new name
        'renameColumn'=>[
            'oldColumnName1' => 'newColumnName1',
            'oldColumnName2' => 'newColumnName2',
            'oldColumnName3' => 'newColumnName3',
        ],
        'alterColumn'=>[ // column name => [old type => new type]
            'columnName1'=>[Schema::TYPE_INTEGER => Schema::TYPE_STRING],
            'columnName2'=>['int(1) null' => 'string'],
        ],
        'addColumn'=>[ // colum name => schema type
            'newColumnName3'=>Schema::TYPE_INTEGER,
        ],
        'createIndex'=>[ // index name => uniq bool
            'indexName' => true,
        ]
    ],
];
  • Don't use same keys as table name in $migration array. If you need to rename one table and on it's place create other with same name use 'tableName' insted of key;

and ./yii migrate/up or down will apply and rollback migration.

tips used from https://yii2-cookbook.readthedocs.org/using-custom-migration-template/