Generating migration from existing database in Yii or Laravel
Solution 1
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.
Solution 2
'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
Solution 3
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
Solution 4
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.
php artisan migrate:make add_firstname_to_user
Now go into application/migrations
and find the migration file, add this
public function up()
{
Schema::table('user', function($table)
{
$table->string('firstname');
});
}
public function down() {
Schema::table('user', function($table)
{
$table->drop_column('firstname');
});
}
Now you can add migrate it
php artisan migrate:install // if you haven't run this, should only be once
php artisan migrate
.. and rollback using
php artisan migrate:rollback
This will add or drop the column firstname, without affecting your table in any other way.
Solution 5
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:
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.Rename
EDatabaseCommand.php
file (and class inside) toDatabaseCommand.php
, if you want to useyiic database
command (as suggested in docs). Without this fix, you'll have to useyiic 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).Having this, navigate back to
protected
folder in your console and executeyiic 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.
DamirDiz
Updated on June 05, 2022Comments
-
DamirDiz almost 2 years
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?
-
DamirDiz over 11 yearsi wrote script mysqlf in yii that generates a migration from an existing database.
-
Mihai P. over 11 yearsPlease share it :), I would love to take a look at it. Expecially in Yii.
-
DarkWingDuck almost 11 yearsanyway, it would be nice to have such a facility, for generating the initial migration script for a new project which you already have a complete db structure for. EG: You may be migrating from another platform, or the db may have been changed since the last migration script, or you may have not used migration scripts before and wish to begin using them for an existing project. There are always special cases, and they are not always that special...
-
Tom about 10 yearsIt would be quite nice...Other frameworks do this (like CakePHP) and the reason why is because it's FAR easier to build your schema in something like MySQL Workbench, etc. Though it's FAR easier to build your schema in PHP if you plan on changing databases (so it's agnostic). There is this package: github.com/JeffreyWay/Laravel-4-Generators ... Which makes things a lot nicer (adds some CakePHP like functionality to Laravel).
-
trejder over 9 yearsYour answer led us into an interesting discussion on meta. Conlusions are, that your answer would be perfect, if you could include entire code from your gist below this answer (edit it and add this code, plese). I did it myself, but had to remove my change, because I was warned that I'm breaking license and only you (gist author) can add this code to your own answer. Will you, please, consider adding it?
-
Sisko78 over 9 yearsAnd what is so wrong with exporting the schema in a tool like mysql workbench and loading this script initially, when setting up a new environment (maybe even within the first migration)? And after that starting to use migrations.
-
tumultous_rooster over 8 yearsAs it understand it, on SO, link-only answers are frowned upon due to linkrot.