Laravel Migrations - keep them forever? What is best practice? - laravel

I have been working with Laravel for about a couple of months now - loving it. Have a question about the workflow that is recommended for managing changes to the database.
I have a test database that I used to develop my app, and when I was reasonably happy, I created a production database by copying the structure in phpMyAdmin. I had built the initial database on phpMyAdmin, as I was not familiar with the Laravel Migrations.
Now I am at a point where I better understand the power of Laravel Migrations, and would like to use them going forward to update the test database and move those changes to the production.
What is the course of action for me? Can I delete everything in the app/database/migrations dir and start from scratch? Will there be issues down the line calling php artisan migrate in my test and production environment?
Thanks in advance for the responses!

Short answer: no problems. If you just add/alter/remove tables/columns in the up() and down() methods.
Longer answer: still no problem. As long as you don't drop a table that is not created in an up() method.
But let's say you get to work with another developer, that person also wants to develop on a local DB. But you don't have the migration for it - so the new dev needs to run the initial structure from test DB, and then apply the new migrations.
In that case I would start with 1 big migration where you create all the initial tables, and in the down() the drops of those tables, so the new dev can just run the migrate and start coding.
edit: and after that migration for the initial structure, just add new ones while developing ;)

Related

How can I create the migration and seed files for CircleCI with Laravel?

We are looking to implement Continuous Integration using Circle CI but we are not sure on how should we proceed with our test database. We have the following alternatives in mind:
Run the migrations from scratch (the problem is that we have a lot of migration files, our first migrations were moving everything from MySQL and PostgreSQL and using a legacy database, so, it's rather complex).
Recreate the current DB and have a .sql file that will create our current tables, and then, we create a seeder to fill the information that we need.
But we are not sure which is the best alternative or if we're missing something?
Thank you

Laravel 5 : is it need to create new migration everytime while changes any column structure?

I seen in laravel documentation that whenever i append new column in existing table at that time i need to create
php artisan make:migration append_tablename
it is fine this level. but next time i need to update any column structure of same table then i need to create new migration or i can add this below code in second migration file which i already used for append new column ?
$table->string('name', 50)->change();
any idea please share.
Migrations only run once, so adjusting an already-ran migration won't do you any good. During development, you can keep tweaking one and run php artisan migrate:rollback to undo it and re-run it repeatedly until you get it right, but once you a) push to production or b) push it somewhere other developers may run it you shouldn't touch the migration any more.
Every migration should be independent.
Unless you are explicitly refactoring for it to be more compact (If you know that you want to deploy a cleaner version in production and it won't mess with other developers work). In which case you should probably add it to your initial table creation.
It is your database history and versioning. Which allow's you to rollback to any previous state.

incomplete migrations- stuck in generating new migrations

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.

Laravel Migration Advantages

I am new to using Laravel, and I'm currently learning about Laravel's database migration and seeding features.
It's working with the command prompt, but I can migrate and seed in phpMyAdmin as well. What are the advantages and disadvantages of migrating and seeding within Laravel as opposed to phpMyAdmin?
From Laravel docs on Migrations & Seeding:
Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state.
A simple search for why database migration also gives me some pretty decent results. One of the easiest to understand is a page by FlywayDB (I have no idea who they are until I search this term up):
Database migrations are a great way to regain control of this mess. They allow you to:
Recreate a database from scratch
Make it clear at all times what state a database is in
Migrate in a deterministic way from your current version of the database to a newer one
The illustration they made perhaps explain it more clearly, so you may want to check it out.

What is migration in laravel

I'm learning laravel and in most of tutorials they are making migrations using artisan.
I skip that step and I'm working directly on a database.
I don't know for what can a migration be useful.
so the question is: what is exactly a migration?
If you are the lone developer, you may not get much use out of it.
But imagine you have multiple developers each with your app stored locally on their machine. What happens when you need to make an update to the database? You would have to make the update, send the query to each other developer, and they would have to run the query on their machines. Once you get a decent amount of developers making changes to the database, this process can quickly become overwhelming and it's only a matter of time before people start missing the changes.
With migrations, all of the changes are stored as files, so in order for a developer to catch up, all they need to do is fetch the code changes and run php artisan migrate and all the work is done for them. This means all your developers are consistently working with the same database structure.

Resources