Demo ¶
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)
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.
@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.
@jamband
Couldn't that be done using annotations in some way? Or inside a property inside the model?
@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.
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!
Re: dustbin1_uk
Thank you for comment. if it is possible, Could you create the issue in GutHub?
Great Tool!
Thank you for this great tool! Works perfectly and saved me a lot of time.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.