Yii 2.0: yii2-schemadump

Generate the schema from an existing database.
14 followers

Demo

gif

Installation

php composer.phar require --dev --prefer-dist jamband/yii2-schemadump "*"

or add in composer.json (require-dev section)

"jamband/yii2-schemadump": "*"

Usage

Add the following in config/console.php:

return [
    ...
    'components' => [
        ...
    ],
    'controllerMap' => [
        'migrate' => [
            'class' => yii\console\controllers\MigrateController::class,
            'templateFile' => '@jamband/schemadump/template.php',
        ],
        'schemadump' => [
            'class' => jamband\schemadump\SchemaDumpController::class,
            'db' => [
                'class' => yii\db\Connection::class,
                'dsn' => 'mysql:host=localhost;dbname=existing_database_name',
                'username' => 'your_username',
                'password' => 'your_password',
            ],
        ],
    ],
    ...
];

And run schemadump command.

cd /path/to/project
./yii schemadump

Example output:

// user
$this->createTable('{{%user}}', [
    'id' => $this->primaryKey()->comment('主キー'),
    'username' => $this->string(20)->notNull()->unique()->comment('ユーザ名'),
    'email' => $this->string(255)->notNull()->unique()->comemnt('メールアドレス'),
    'password' => $this->string(255)->notNull()->comment('パスワード'),
], $this->tableOptions);

Copy the output code and paste it into a migration file.

Commands

Generates the 'createTable' code. (default)

./yii schemadump
./yii schemadump/create

Generates the 'dropTable' code.

./yii schemadump/drop

Useful commands (for macOS user):

./yii schemadump | pbcopy
./yii schemadump/drop | pbcopy

Check help.

./yii help schemadump

Supports

  • Types
  • Size
  • Unsigned
  • NOT NULL
  • DEFAULT value
  • COMMENT
  • Unique key
  • Foreign key
  • Composite primary keys
  • Primary key without AUTO_INCREMENT
  • ENUM type (for MySQL)

Resources

https://github.com/jamband/yii2-schemadump

Total 7 comments

#19826 report it
gcarvalho at 2016/04/06 10:26pm
Great Tool!

Thank you for this great tool! Works perfectly and saved me a lot of time.

#19260 report it
jamband at 2015/05/02 09:46am
Re: dustbin1_uk

Thank you for comment. if it is possible, Could you create the issue in GutHub?

#19253 report it
dustbin1_uk at 2015/04/28 05:38pm
Thank you!

This is a wonderful tool! Thank you!

A feature request: A differential version

Check the DB for migrations already run and compare the physical migration files with the db. If the object is in the DB, but not in an already run migration file, then add it to the next migration. Then generate new migrations for only new columns, keys\indexs ect....

Object drops could be done the same way. If they are in a migration file that is already run, but not in the DB any-more, they need to be added to the next migration as a drop.

This is a massive task I know, but it would save a ton of time if we had that! :)

In the mean time though, Thank you for this awesome tool!

#19041 report it
jamband at 2015/03/03 12:53am
@Highmastdon

For example, @property annotation is automatically generated by Gii. This annotation is generated to complement the column attributes in the IDE. IMO, I think that's tricky to use it.

Also, In order to get the information of some columns must be declared schema. Please check yii\db\ColumnSchema class.

#19039 report it
Highmastdon at 2015/03/02 02:24pm
@jamband

Couldn't that be done using annotations in some way? Or inside a property inside the model?

#19038 report it
jamband at 2015/03/02 01:40pm
@Highmastdon

In that case, is there a way to get such as data types and size of some columns?Although I think that it is difficult in many ways.

#19035 report it
Highmastdon at 2015/03/01 01:40am
Schema creation from Model

Do you know if there is a module available that can generate schema (database migrations) based on the Model? This would be a great way to implement Domain Driven Design instead of Database Driven Development.

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet