I'm new using laravel and artisan migration command, and I want to create migrate files from an existing database, how can I do that ?
Laravel does not currently support this. You'll have to manually create all those files based off your schema already in your DB.
Related
I have a Laravel application that uses a DB. The DB is big and was produced by many migration files. Is there a way to cut down the number of migration files so that each table has one migration file? Also Would something results from having too many migration files? like performance issues?
if you have laravel 8 you can use squashing-migrations
by run php artisan schema:dump cmd
ref link https://laravel.com/docs/8.x/migrations#squashing-migrations
for older version you can try this
https://github.com/Cytracom/laravel-migration-squasher
or you can try this
Laravel 5.5 Consolidate migrations w/ production database
after laravel 8 you can run php artisan schema:dump
it change all migrations to single schema file
For more explanation check link https://advancedwebtuts.com/tutorial/what-is-the-use-of-laravel-squashing-migrations-schema-dump-command
I've DB in phpmyadmin with many tables in it.
I want to auto generate those tables into seperate laravel migration files.
Does anyone know of such a possibility?
You can install package for that https://github.com/Xethron/migrations-generator
This way your migrations table will be separated for each database.
Use the --database parameter with the migrate command and store the migrations for each database in separate directories.
You could have separate directories in app/database/migrations for each of your database (for example : db1 and db2) and store the appropriate migrations in each directory. Then you could run the migrations like this:
artisan migrate --database="db1" --path="app/database/migrations/db1"
artisan migrate --database="db2" --path="app/database/migrations/db2"
If you want to go the extra mile and automate the process you could create your custom command that will run all the migrations at once. You can create the command like this:
artisan command:make MigrateAllCommand --command=migrate:all
You can check the Laravel Command Docs for more info.
I created a MYSQL database using migrations, I added some data into it, but after that I recognized that I need to add a new column into my table.
I ran the command: 'php artisan migrate', but as it didn't work to synchronize
columns, it returns there is nothing to migrate.
So I ran the command 'php artisan migrate:reset', and then ran the command
'php artisan migrate' again, database schema updated correctly but for sure I
lost all my inserted data.
Now I'm just testing the application, but it would be very harmful if I found out that I should modify my database while it is runing with real data!!! What should I do in this case?
should I skip using migrations and create the Mysql database directly with
wamp? or use migration, but perform any later updates directly on database without updating the migration files? or there is another solution?
Answer
If you have already created a database table and realise you need to add more into it you can simply run: php artisan make:migration add_field_to_table_name --table=tableName
You then go into that file and create the new field. Once done simply run php artisan migrate and it'll add the new field into the desired table without causing any data loss.
Seeders
On another note, I would strongly suggest looking into seeders. This way when you're creating a project you can always refresh your migrations (wipe your database and re-migrate your tables) and then re-input the data using seeders.
you can create a migration to alter your tables. add new columns and more.
I am having a very bad situation here, working on a Laravel 5 project. previously developed by another developer. That developer at start created couple of tables using migration generators and added some columns using migrations. After that, he added table columns straight away using some sql GUI. I was given the sql dump which i imported and set it up on my local machine, now when i created a table using php artisan make:migration create_myTableName_table --create="myTableName" the table migration is created successfully, but, when i did php artisan migrate it's giving me SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'someTable' already exists I checked migrations folder and matched it with current version of someTable and i can see the columns are different, same with other tables aswell.
What should be the best case to handle this in this situation, i want to keep up with Laravel migrations generator so that in future if any other developer want to work on this project he just has to run migration command to migrate database or to create tables or create columns... Should i re-write all migrations ? pleas help. Thanks
Given your situation, I’d put the .sql export in your repository, clear out the old, broken migrations, and create a new one that initially imports the database dump. Then just create migrations as normal going forward.
Quick and dirty?
Delete current migration files. Clear your migrations table and export the whole DB. Put the SQL dump in the repo and instruct other devs to import it before they run php artisan migrate. With the dump imported and migrations table truncated, you can create new migrations with no further collisions with the legacy migrations.
Feeling ambitious?
Use a package like migrations-generator to generate migrations based on your current DB structure. Then, migrate away.
I have created the schema through MySQL Workbench and synchronized it with the database.
All the tables are there in database. Now when I use the
php artisan migrate
command, is there an option to take the fields director from the database instead of specifying the fields name.
Also could you please suggest any other tools on github.
Laravel Migrations Generator will help us generate migration source code from existed database in Laravel 4.
so if you already have your sql schema, created in mysql workbench, then you'd need to put that schema into laravel migrations using the schema builder.
there are tools that make this part easier:
http://www.laravelsd.com/ - kinda like mysql workbench, with an export possibility to laravel migration code
https://github.com/XCMer/larry-four-generator writes existing databases to laravel migration code
Another option is to use our tool Skipper (https://www.skipper18.com) We introduced Laravel support a month ago and now, in a beta phase, it is free for Laraver users.
Skipper allows you to import your MySQL Workbench project directly (or it is possible to import existing database too) and then export migrations and object files from Skipper to your Laravel project.
The benefit of this solution is that you can maintain your ORM schema directly in Skipper and continuously update model object files and create new migrations automatically from the application without the necessity to write any code manually.
If you want to try it, you can download it here https://www.skipper18.com/en/download, and in the initial application license screen select "Laravel Beta license".