Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating migration from existing database in Yii or Laravel

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?

like image 235
DamirDiz Avatar asked Nov 28 '12 13:11

DamirDiz


2 Answers

Instructions for accomplishing this in Yii:

  1. Add your database connection settings to protected/config/console.php.

  2. Run yiic migrate create initial to create the stub code for the migration.

  3. Copy contents of this gist to protected/commands/InitialDbMigrationCommand.php.

  4. Run yiic initialdbmigration 'name_of_your_database' > initial_migration.php to generate up() and down() methods for initial database migration.

  5. Copy and paste up() and down() methods from initial_migration.php to the file created in the protected/migrations folder in step 2.

like image 200
bmarston Avatar answered Oct 07 '22 18:10

bmarston


'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
like image 22
SuperDuck Avatar answered Oct 07 '22 19:10

SuperDuck