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.
Related
I have a system made with Laravel. As we know, we can make database migrations in Laravel using php artisan.
So bacially, you create a migration file then edit it for the structure like tables and fields then run the migration command and it will create the database tables for you.
Question, is it possbile to the reverse? I have already an exisiting database with tables in it and already structured. Is there something I can do to to generate all the migration files needed for that without doing it manually for each one?
Yep, have a look at:
https://github.com/Xethron/migrations-generator
This works quite well, used it a few times on projects I've inherited from other developers.
Works with Laravel 5 too.
I'm about to start building an API for an existing app with the DB already in production. Functionality will slowly be ported to the API in the future and the app will become more "API-centric".
One of the main starting points is to adopt migrations and a build process. I have reservations on the best way to create migrations for an existing schema without breaking production when they are executed.
As we would like to move quickly with porting functionality over to the API, we ideally want to recreate our current schema as part of our build process and get some core unit tests in place - as opposed to just creating migrations for future changes.
This is where I become unsure as to the best place to start.
What is the best approach for a task like this?
Could the current schema be imported as our first migration?
Could this initial migration be wrapped in something like:
if ( App::environment() !== 'production' ) to ensure it isn't executed in a production environment?
Is it ok to exclude a migration for a particular environment or could this cause problems?
Is there maybe another approach or something stupidly simple I'm missing? :)
I created a tool not to long ago that will generate all the migrations for your current database schema. It also adds the newly created migrations to the migrations table, as the tables already exist. You can get it here: https://github.com/Xethron/migrations-generator
Secondly, I use the following line of code in my DatabaseSeeder, but you can add it to any function you wish to disable in production:
if (App::environment() === 'production') {
exit('Don\'t be stupid, this is a production server!!!');
}
It shouldn't be a problem if you stop execution by throwing an error or as I do above. If you don't, Laravel will believe the changes happened successfully and remove them from the migrations table, and cause errors when running your migrations. The only exception is if you exclude both the up and down code (But I can't see why you would want to do that)
Hope this helps.
I wouldn't suggest running migrations in production environment at all, but if you must then make a backup copy of the database first and also create a copy of the database locally and do the migration there then test to make sure it all works properly.
You could create your first migration and manually add all the schema layout to it. But I would actually do this in a few migrations to have each table as its own migration.
Since migrations are run from the CLI using artisan you can pass the environment and the database you want the migration to be run on:
artisan migrate --database="connectionName" --env="local"
There is no issue running migration on a particular environment besides what I stated above for production.
Remember to add all existing schema layouts from step 1 (the migration file name excluding the extension e.g. 2014_03_25_143340_AddCountriesTable) to the migrations table in the database, otherwise running the command in step 2 will throw errors about table already exist.
Hope this helps.
Here is the legacy documentation explaining what "generate-migrations-db" does:
http://symfony.com/legacy/doc/doctrine/1_2/en/07-Migrations
It says
Generate migration classes from existing database connections
(doctrine-generate-migrations-db, doctrine-gen-migrations-from-db)
Also:
Generating Migrations
Doctrine offers the ability to generate sets of
migration classes for existing databases or existing models as well as
generating blank migration classes for you to fill in with the code to
make your schema changes.
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
In other words: it takes the schema from the database and generates a migration that performs that schema creation. No entities, no classes, no mappings are used in this process. It just takes a DB and builds a migration class.
We do not have generate-migrations-db anymore. Do we have something that performs that task? I couldn't find. If it was replaced by some other command, please let me know. If it was just removed, please let know.
I'm not aware of a command in Doctrine or the Migrations Bundle that creates migration files for an existing database.
So here's how I did it instead:
Install DoctrineMigrationsBundle
Create a new blank database
Update your config or parameters to point to this blank database rather than to your "real" one
Run php app/console doctrine:migrations:diff. This will create a migrations file that creates your database tables etc from scratch
Change back your config/parameters
Hope this is helpful.
Take a look at the DoctrineMigrationsBundle, which can generate migration classes with sql statements for migration.
I asked this question on Reddit, but received no answer. I thought I would try here, instead.
I am new to Laravel, and have been going over the documentation and also watching Jeffrey Way's NetTuts videos. However I haven't gotten in too deep just yet, so if this is answered clearly somewhere else, please just point me in that direction.
I come from a CodeIgniter background, and for all projects I have done with it, I typically design my databases in MySQL Workbench. I also use this to make changes to the schema, and also as a visual map of the database. The MySQL Workbench file generally gets passed around with the other developers via Git.
Laravel seems to want you to create your tables using migrations, which seems a bit counter intuitive coming from the MySQL Workbench side. I understand that migrations act as a version control for the database, which seems pretty nice. However, other than that, I don't quite get the point just yet.
Can anyone explain to me why I should be building the tables out via the migrations feature of Laravel vs. the way I've been doing it?
Laravel is a PHP framework. Like other frameworks, such as Zend, your development to deployment timeframe will be significantly reduced, as well as developing within a framework that can be understood by other developers as your projects becomes larger and you need more developers involved in the future.
Migrations, as part of Laravel, are designed to quickly and easily setup a database scheme without the need to type any MySQL. It also presents the schema correctly. As your schema exists within a file, you can easily rollback and transport your schema with ease.
Well, this is a valid concern. While Laravel doesn't specifically force you to use it, I can think of a few reasons for its implementation:
Compatibility. It works with almost any database. If you ever decide to change from MySQL to PostgreSQL, there's very little to change.
Version control. As you mentioned yourself, it allows you to have control over what's changed in your database, and provides quick and easy way to update your already running database.
Easy of use. It's incredibly easy to call it through the command line. Meaning you won't have any problems creating your database on the server side, through a shell.
The main reason Laravel has migrations is because version control. When you create a migration the name has a date attached to it. I use Laravel myself and I absolutely love migrations. Teamed up with the Schema builder it makes my life so much easier not using ancient MySQL methods. Here's some examples of the Schema and migration
Simple Command To Create A New Migration File:
php artisan migrate:make create_users_table
Inside The Users Table Migration File:
Schema::create('users', function($table){
$table->increments('id'); [will create a increments field with name of ID]
$table->string('username'); [string field with name of username]
$table->string('password'); [password is a special field]
$table->text('body'); [string field with name of username]
});
At the bottom all you need to add is this:
Schema::drop('users');
Then you run the migrate command:
php artisan migrate
It will add all the columns to the table.
if you ever want to take back or add to the database all you need to do is run this
php artisan migrate:rollback
[you can add specific commands here to only rollback certain tables]
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();