et-soft/yii2-migrations-create

Console script for Yii2, for create migrations files from existing database.

v0.1.2 2016-07-29 13:59 UTC

This package is auto-updated.

Last update: 2024-03-29 03:31:40 UTC


README

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads

Console script for Yii2, for create Yii2 migrations files from existing database.

Install

Either run

$ php composer.phar require et-soft/yii2-migrations-create "*"

or add

"et-soft/yii2-migrations-create": "*"

to the require section of your composer.json file.

In config/console.php add section:

'controllerMap' => [
    'migrations' => 'etsoft\yii2migrations\MigrationsController',
]

Using

In console, types:

./yii migrations

For create migration files for all tables from database in directory app\migrations.

Also in console your may type:

./yii migrations/table table_name

For create migration file selected table from database in directory app\migrations.

Examples

For existing MySQL table with scheme:

 CREATE TABLE test
 (
     id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
     user_id INT(11) COMMENT 'ID user',
     name VARCHAR(255) NOT NULL COMMENT 'Name',
     phone VARCHAR(50) NOT NULL COMMENT 'Phone',
     created DATETIME NOT NULL COMMENT 'Date created'
 );

Migration script create in app\migrations directory? file m_test.php with content:

<?php

use yii\db\Migration;

/**
 * Handles the creation for table `{{%test}}`.
 */
class m_test extends Migration
{
    /**
     * @inheritdoc
     */
    public function up()
    {
        $this->createTable('{{%test}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()->comment('ID user'),
            'name' => $this->string(255)->notNull()->comment('Name'),
            'phone' => $this->string(50)->notNull()->comment('Phone'),
            'created' => $this->dateTime()->notNull()->comment('Date created'),
        ]);
    }

    /**
     * @inheritdoc
     */
    public function down()
    {
        $this->dropTable('{{%test}}');
    }
}

TODO in next releases

  • Support Foreign keys
  • Support ENUM fields