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 generate up() and down() methods for initial database migration.
Copy and paste up() and down() methods from initial_migration.php to the file created in the protected/migrations folder in step 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
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
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.
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) 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).
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.
I think that the answer is: https://github.com/jamband/yii2-schemadump for Yii2
"This command to generate the schema from an existing database."
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.
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
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.
I am building a complex application but i want to know that is it safe to use doctrine migrations in production.
For eg. the site has been used for 1 year and company wants to add extra attribute to user table.
So do i straight way chnage by going in database or through doctrine migrations
This is one of the intended uses (and benefits) of migrations - to automate the changes to your database quickly and accurately. Yes, they can and in most cases should be used to update your database in production.
Edit: The Symfony2 documentation also explains clearly this is one of the purposes of migrations.
Of course, the end goal of writing migrations is to be able to use them to reliably update your database structure when you deploy your application. By running the migrations locally (or on a beta server), you can ensure that the migrations work as you expect.
...
Yes it would be safe.
I would just add an extra attribute in the User entity. Then run the doctrine:generate:entities command. That should generate the get/set methods. Then just update your database using doctrine:schema:update --force. That should add it into your database table.
Question about migrations, announced at one of the latest releases(1.1.6, am I right?)
So, i have a site working with yii 1.1.5 and i want use migrations now, cause site progressing every day. What is the best way to describe first migration, which include all my current database schema?
when you say describe first migration do you mean the class and file name?
yiic migrate create init_db_schema
EDIT
In which case I don't believe the migration tool can reverse engineer an existing db and create the migration for you. So, you'll probably have to manually do it. You could use PHPMyAdmin, MySQL Browser/Workbench to generate the CREATE commands for you and inside your migration script, create the command e.g.
$cmd = $this->getDbConnection()->createCommand($sql);
$cmd->execute();
Doctrine (on Ubuntu): What command generate models from database without delete existing models?(I add new tables soo i want update models)
Thanks
If you're using Doctrine 1, you'll need to make use of Migrations. Migrations can either be written manually, or auto-generated one of a few different commands:
./doctrine generate-migration
./doctrine generate-migrations-db
./doctrine generate-migrations-models
Migrations can be a fairly complex topic, so I would read up on the documentation. You may also want to check out this slide show.
If you're using Doctrine 2, things get a little simpler. It doesn't have a Migrations class (yet), but there an easy-to-use command to non-destructively update your db schema:
./doctrine orm:schema-tool:update
This has worked well for me, but can sometimes fail due to complex foreign key constraints.
What is the a standard way of adding new model to my app built on Symfony + Doctrine while maintaining all previous models and their meta-data (like relationships).
What am I really looking for: A command / procedure that will be equivalent of ./script/generate model FooModel in Ruby on Rails (which does not have any sort of reset db / reset models while generating)
If these two are different things, and I am chasing the wrong ghost (I would like to think I am not), please let me know.
EDIT: Updated the question.
You shouldn't be overriding the base classes, as these will be mostly be auto-generated whenever you do build:all or doctrine:build-model etc. Use the classes generated in the lib/model directory eg YourModel.class.php if you want to add new methods etc. Then your new models will be generated alongside your existing ones.
Standard process is to add the new model and any relationships it requires to schema.yml
Then do ./symfony doctrine:build-all (or :build --all for symfony 1.3/1.4)
As richsage says, you shouldn't be editing the base classes, so this operation is totally non destructive.
Doctrine also has functionality for migrations so that you an update the database schema easily as you deploy the new code into production:
http://www.doctrine-project.org/documentation/cookbook/1_0/en/symfony-and-doctrine-migrations
Newer versions of doctrine (1.1 +, symfony 1.3+) include the generate-migrations-diff task, which can create migrations for you. This is covered very well here:
Extra changeColumns in Doctrine generate-migrations-diff
[edit: the author of the question above has copy/pasted it below as well now]
The generate-migrations-diff doesn't diff two different yaml files. It actually compares your models and your yaml file and then generates a migration based on the differences. If you start from a db that is in sync with your yaml and classes, your workflow to make schema changes should be:
Change yaml file
Run generate-migrations-diff to diff your current (changed) yaml with your (unchanged) models. This will generate a migrations file in your doctrine/migrations directory (or whatever migrations_path is set to in your doctrine config).
Run migrate to run the migration created in step 2 and modify your database
Run generate-models-yaml to generate new classes based on your yaml file. These go where you've specified your generated models go (models_path in your doctrine config).
Run generate-sql to generate a SQL file. This will go where your doctrine sql_path config is set to.