Do you have the SQL structure for the empty database ? If so you can dump it into MySQL then run the migration generator thereafter.
Laravel 5 - Migrations from existing database
Hi Everybody,
I would like to know how can I proceed for creating migrations from an empty existing database with Laravel 5. With L4, I usually used an artisan command from this package : https://github.com/Xethron/migrations-generator , it allows me to do it simply with "php artisan migrate:generate".
And more, I'm curious to know how do you proceed for creating generally database. In my case, I use MySQLWorkbench and create a diagram (MCD) with relations. What's the good practice with database migrations in this case ?
Yes sure, that's what I did with Laravel 4 but with Laravel 5, the migration generator doesn't work. I regret that this feature doesn't exist by default in migrate's commands.
Ah ok, didn't know it didn't work with L5 (though it shouldn't be too hard to port, the database part didn't change too much from L4).
After the question is, do you really need a full migration ? Maybe you can call 'mysqldump' and feed it the .sql you have in your first migration script, which will setup the base db for your app (in migrate::down, just drop the whole database). Then you can work your way up from there and make modifications in subsequent migration files.
Yeah that can be a solution but in dev mode, the slightest change will require to edit the sql file manually (In fact, I don't want to create more and more migrations files for any change at this point ;) )
So, I will take a look to port the generator in the new version
No it can work both in dev' and production.
1) artisan migrate:make import_sql : in that file you import the db.sql file that you can put eg in your app/database folder., calling mysqldump from laravel.
2) artisan migrate:make structure_change : dropColumn, create tables .... the classical way.
etc...
The Migrations Generator now works with Laravel 5. So you can use it just like before: https://github.com/Xethron/migrations-generator
To remove
The Migrations Generator It's not working for Laravel 5.1 I get this error: Failed to update git@github.com:jamisonvalenta/Laravel-4-Generators.git, package information from this repository may be outdated
I was also getting this error. Adding below in composer.json fixed the issue:
"repositories": [
{
"type": "git",
"url": "https://github.com/jamisonvalenta/Laravel-4-Generators.git"
}
],
This is what my composer.json "require-dev" and "repositories" look like now:
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1",
"xethron/migrations-generator": "dev-l5",
"way/generators": "dev-feature/laravel-five-stable"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/jamisonvalenta/Laravel-4-Generators.git"
}
],
I hope this helps someone.
Thanks bro! Helps indeed
I've created a MySQL Workbench plugin to handle this. You can take a SQL model and convert the entire thing to Laravel 5 migrations.
https://github.com/beckenrode/mysql-workbench-export-laravel-5-migrations
If you like it please star it.
@beckenrode dude! this is fantastic! Saved me a ton of work.
Your generetor worked fine but it gave a simple syntax error in the migrations "Expecting ']' got ')' "in one of my migration file ,I had to correct it manually
I'm glad you found it useful! Could you give me some details about where in the migration file you received the error? If possible, the entire migration but if not then just the relevant parts.
Thanks!
@beckenrode , I tried it on a schema having foreign keys constraints. It had the error in one of the migration file in the down function like, $table->dropForeign(['pid'); instead of $table->dropForeign(['pid']);
Also it doesn't set the indexing i.e naming the files in the right way when you export for MySQL workbench, this is a problem when tables reference other table. If the primary table is not the first one as per the naming convention it fails for the make:migration command. I understand this might not be the scope of your project but I thought it might be good to mention
@Snedden27 I just corrected the missing closing bracket and pushed the fix. I'll look into the proper naming conventions to see what I can do to correct it. Thanks for your feedback!
@beckenrode You Are The Man! Thanks a lot, let me know if I can buy you a beer.
@Xethron worked amazingly on a 5.1 today
Going to have to do some triggers manually, but spared a lot of time, after trying another solutions..
Thanks!!
I wrote this quick script to make migration files out of an existing database. https://github.com/aljorhythm/sql-to-laravel-migrations
@aljorhythm Great job! It works like a charm using PHP.
Thanks!!
I tested it on L5.7 it worked well
Please or to participate in this conversation.