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?
Instructions for accomplishing this in Yii:
Add your database connection settings to
protected/config/console.php
.Run
yiic migrate create initial
to create the stub code for the migration.Copy contents of this gist to
protected/commands/InitialDbMigrationCommand.php
.Run
yiic initialdbmigration 'name_of_your_database' > initial_migration.php
to generateup()
anddown()
methods for initial database migration.Copy and paste
up()
anddown()
methods frominitial_migration.php
to the file created in theprotected/migrations
folder in step 2.I use both Yii and Laravel and I could not find what you require for either of them. They both create empty files and you need to create the migration script yourself. For a table of 150 tables it will be challenge to create the migrations yourself, but it is not quite as hard as you imagine. Because you already have the information on the fields it should not take so long to create.
Well since migration is about setting up your database structure and make changes to it, not to reflect a current database there is no such way.
And this is also not a step you have to make. You can start from where you are at the moment, which will make you able to rollback up to this point. Which means you can make migrations for your current tables without having to specify their entire structure, but just the changes only.
Let's say you have a table called user and want to add their firstname to it.
Now go into
application/migrations
and find the migration file, add thisNow you can add migrate it
.. and rollback using
This will add or drop the column firstname, without affecting your table in any other way.