yii2-dump
Generate migration file from an existing database, (*1)
github: https://github.com/Hzhihua/yii2-dump
oschina: http://git.oschina.net/hzhihua/yii2-dump, (*2)
Demo
, (*3)
Test Environment
- PHP >= 5.5.0
- MySQL(5.6.36)
Installation
composer require --prefer-dist "hzhihua/yii2-dump:1.0.3"
Configuration
Add the following in console/config/main.php:, (*4)
Simple Configuration
return [
'controllerMap' => [
'dump' => [
'class' => 'hzhihua\dump\DumpController',
'filePrefix' => '123456_654321',
'tableOptions' => 'ENGINE=InnoDB CHARACTER SET=utf8 COLLATE=utf8_unicode_ci', // if mysql >= 5.7, you can set āENGINE=InnoDB CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ciā,
],
],
];
Detail Configuration Ā see
return [
'controllerMap' => [
'dump' => [
'class' => 'hzhihua\\dump\\DumpController',
Ā Ā Ā Ā Ā Ā 'db' => 'db', // Connection
'templateFile' => '@vendor/hzhihua/yii2-dump/templates/migration.php',
'generatePath' => '@console/migrations',
'table' => 'table1,table2', // select which table will be dump(default filter migration table)
'filter' => 'table3,table4', // table3 and table4 will be filtered when generating migration file
'limit' => '0,1000', // select * from tableName limit 0,1000
'filePrefix' => '123456_654321',
'tableOptions' => 'ENGINE=InnoDB CHARACTER SET=utf8 COLLATE=utf8_unicode_ci', // if mysql >= 5.7, you can set āENGINE=InnoDB CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ciā,
// ... ...
],
],
];
Default Table Options
ENGINE=InnoDB CHARACTER SET=utf8 COLLATE=utf8_unicode_ci
it was defined at "@vendor/hzhihua/yii2-dump/src/Migration.php" file see, (*5)
Default Limit
0,1000
Default Filter Table
migration, (*6)
Tips
You may neet to remove the migration file that its name is "*_init.php" before you run "./yii migrate" commond. because it will generate "{{%user}}" table file when you run "./yii dump" commond and the file name with "*_init.php" also create "{{%user}}" table. it will tip you "{{%user}}" has always exits., (*7)
Simple Usage
run dump
command., (*8)
cd /path/to/your-project
./yii dump # default action: generate, it will gerate migration file
Commands
Check help, (*9)
./yii help dump
print all over the table name without table prefix, (*10)
./yii dump/list
generate all over the table migration file(default filter migration table), (*11)
./yii dump
or
./yii dump/generate
generate all over the table migration file but it only had some data between 0 and 10, (*12)
./yii dump -limit=10
only generate table1,table2 migration file, (*13)
./yii dump -table=table1,table2
only generate table1,table2 migration file and only table1 will be dumped table data, (*14)
./yii dump -table=table1,table2 -data=table1
generate all over the migration table file without table1,table2, (*15)
./yii dump -filter=table1,table2
print all over the code of migration table file(default filter migration table), (*16)
./yii dump/create
Display the 'createTable' code at the terminal., (*17)
./yii dump/create
Display the 'dropTable' code at the terminal., (*18)
./yii dump/drop
-type params, (*19)
./yii dump -type=0/1/2/3
>>> -type=0 generate table migration file,
>>> -type=1 generate table data migration file,
>>> -type=2 generate add key migration file,
>>> -type=3 generate add foreign key migration file
Useful commands (for macOS user):, (*20)
./yii dump | pbcopy
./yii dump/drop | pbcopy
Supports
- Types
- Size
- Unsigned
- NOT NULL
- DEFAULT Value
- COMMENT
- Unique key
- Foreign key
- Primary key
- ENUM type (for MySQL)
- AUTO_INCREMENT
Not Supports
~~AUTO_INCREMENT~~, (*21)