Generating migration from existing database in Yii

2019-03-08 16:16发布

I'm working on a project that has a fairly complex database (150+ tables). In order to be able to maintain changes, I've decided to add migrations, preferably using Yii or Laravel.

Does anybody know, if it is possible to generate a initial migration from an existing database?

Creating it by hand would:

  • take for ever and
  • be very error-prone.

If there is no way, does anybody know a good PHP-based framework, that supports such functionality?

9条回答
Summer. ? 凉城
2楼-- · 2019-03-08 16:27

'Doctrine Project' (aka Doctrine) has the ability to create DB migrations for existing DB structures, so you can recreate the existing structure. It can be easily implemented in Symfony, Laravel, also in Yii and many frameworks.

Sample from:
http://symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations

From Database

If you have an existing database you can build a set of migration classes that will re-create your database by running the following command.

$ ./symfony doctrine:generate-migrations-db

From Models

If you have an existing set of models you can build a set of migration classes that will create your database by running the following command.

$ ./symfony doctrine:generate-migrations-models
查看更多
叼着烟拽天下
3楼-- · 2019-03-08 16:30

After doing some research, here's what you're going to need for Laravel: https://github.com/XCMer/larry-four-generator

(version 4 at least, who knows how long this will work, Laravel changes too fast and has too many breaking changes)

You'll want to run php artisan larry:fromdb and it'll show you the tables...You can also exclude or only process certain tables (look at the readme).

Again, super super useful if you like to build your schema in something like MySQL Workbench. I also saw mention of a package that would parse the workbench files...But the link was dead.

You may also wish to use this larry package with: https://github.com/JeffreyWay/Laravel-4-Generators

You can then create scaffolding a la CakePHP style.

Alternatively, try this package: https://github.com/barryvdh/laravel-migration-generator

查看更多
Rolldiameter
4楼-- · 2019-03-08 16:31

I think that the answer is: https://github.com/jamband/yii2-schemadump for Yii2 "This command to generate the schema from an existing database."

查看更多
Ridiculous、
5楼-- · 2019-03-08 16:35

As for Yii 1.x, schmunk has created a wonderful database-command yiic command.

This command covers only up migrations. You must write your own down migrations.

To use it:

  1. Get the newest version from GitHub and put it's contents into /protected/commands folder (create one, if it does not exist). Note, that you need to put contents as is (without subfolder for this particular command), which is contrary to what we do for example for extensions.

  2. Rename EDatabaseCommand.php file (and class inside) to DatabaseCommand.php, if you want to use yiic database command (as suggested in docs). Without this fix, you'll have to use yiic edatabase command, as there's slight inconsistency between docs and the code (at least in the newest version, as of writing this; maybe schmunk is going to fix this).

  3. Having this, navigate back to protected folder in your console and execute yiic database dump migration_name --prefix=table_name.

This will create a migration protected/runtime/migration_name.php file with proper date and time in the beginning of file name, filled with series of CDbMigration commands to recreate your database schema. Visit "Usage" section in the docs to read more about customizing command.

查看更多
贪生不怕死
6楼-- · 2019-03-08 16:39

Here is a Laravel package I created that does exactly that. It automatically generates clean and accurate Laravel migrations from your existing database.

As it doesn't make any assumptions of the database, it should work on any database structure while even keeping the original index and foreign key names.

https://github.com/Xethron/migrations-generator

查看更多
神经病院院长
7楼-- · 2019-03-08 16:40

There is one now for Yii:

This allows a distributed team to easily update the db locally and then distribute it's updates with thee other developers automatically with the rest of the code via a versioning control system (I used git). It also performs a full initial db dump to xml and to a migration file.

project home: https://code.google.com/p/yii-automatically-generated-migration-files/

source code: https://code.google.com/p/yii-automatically-generated-migration-files/source/checkout

I've created it from scratch as I was annoyed with the fact that I had to do this manually in order to distribute it to my team.

Hope it helps!

Feel free to share bugs, improvements and comments.

查看更多
登录 后发表回答