How to keep your old migration as reference? - php

I have 38 migration script that I wrote in Laravel 4.
I don't want to throw them away, but I also don't want to run them either. I just want to keep them as references.
If I place them in the migration folder in Laravel, it will run when I do
php artisan migrate and that will break some part of my database, as they have already been run.
I'm wondering if there is a way to mark them as already run, so Laravel will not trying to run them again.
I notice the migration table in my database - can I do something with it ?
What is the best way I should do to those 38 migrations ? Feel free to give me any suggestions.

Your question is a little confusing -- Laravel will only run each migration once. It keeps track of which migrations have run in the migrations table. The whole idea of migrations is a series of date sortable scripts that you can run at anytime start to finish and they rebuild your database, AND that you can add to without needing to rerun them all as they work (so your data is preserved)
If you're running
php artisan migrate
and Laravel's running a migration it has already run, something is very broken with your system.
(Speculation follows) It seems more likely the latest migration you're running may have halted half way though in a place MySQL couldn't rollback the changes, and Laravel's trying to rerun the latest one. Try a
php artisan migrate:rollback
Fix the error in the breaking migration, and you'll be all set.

Related

Why laravel migrate:fresh takes a long time for 422 tables?

I have a Laravel project and I'm using this command to migrate a database every time I want to create a new project
php artisan migrate:fresh
It was working fine, but after 3 years of working on it I've reached 422 tables and now when I want to migrate it takes almost 5 to 10 minutes at least to complete a migrate job.
I can wait without problems in localhost, but my biggest problem is when I use the LaravelInstaller library to add an installer feature to the project.
It takes a long time and eventually it returns the next error
internal server error 500
So in order to make this installation work with such long durations, I have to change Max time limit in php.ini to solve this issue.
Is there any way to speed up the migration?
Thanks
This is an answer to speed-up issue, not internal server error 500, because there's not any details related to issue that causing that error.
I'm assuming you're using Laravel 8 at least, and base on that you can try Schema Dump, which It's added to creates a dump of the current schema.
Here's the command :
php artisan schema:dump
# Below command is taken from below link, in case of more help, don't hesitate
# to read the docs
# Dump the current database schema and prune all existing migrations...
php artisan schema:dump --prune
You have this issue simply because you've a huge list of migration files that were created, and in this case, in general, doing a migration:fresh should not take so much time, even for 422 table, but you have this issues, because of repeating this process and make list bigger in years.
Here's the link to reference

Laravel 5.8 refresh database without losing data in tables already exist

I'm using Laravel 5.8 and each time I create a new migration I run php artisan migrate:refresh to update my database. I lose data stored in tables that already exist. I want to find a solution to add a new table without losing my data.
You can run php artisan migrate (without :refresh), this only runs migrations that have not been run yet.
The idea with migrations is that you do not edit them after you run them.
More info on migrations can be found in the migration documentation
You can use the seeding too if you need some base data when you make a migration:refresh, please see the link to know more about that.

Migrations Table Empty - Can't run php artisan migrate

So, I've copied a database off a server that I'm working on and I've just started working on it. However for some reason the migrations table is empty so when I try to run php artisan migrate it can't because it doesn't know that all the tables are already created. Is there a quick way of fixing this rather then adding in the name of every migration to the table manually as there are a lot of migrations in this project?
Thanks.
You can work around it the following way:
Create another empty database.
Switch your application to use the new database.
Run php artisan migrate
Copy migrations table from new database to the old one
Switch your application to use the old database again.
You could also delete existing migration files (bad idea) or move them to some other folder so that migrate command doesn't see them.
You could also fill the table yourself, but that seems pretty time consuming.
Why is that table empty in the first place? Is it empty on your server as well?

Laravel database migration methods not working after deleting a migration file

I am just beginning to learn laravel and so I am constantly dealing with migration methods right now. Previously, I have deleted a migration file ShippedViaToPurchaseOrders and after that I cannot perform php artisan migrate:reset/rollback anymore. What is the problem with this? Please help. Please take a look with the error below. Thank you so much.
These files are loaded using composer, it still thinks the file is there.
Try running the composer dump-autoload command before running migrations to recreate the autoload file and let composer know the file is no longer there.
Update:
The Laravel migration tool creates a table migrations in your database to know what migrations have been executed. In this table, remove the row corresponding to the removed migration.
Because the row is still there, Laravel will keep trying to run the rollback migration corresponding to that row.

Deleted a model manually, php artisan migrate gives and error

So I just started playing around with Laravel 5.1. I created a model with migration named "teachers". Then I manually deleted the model file which was Teachers.php which contained the Teachers class. Now when I try to roll back I get the following error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'CreateTeachersTable' not found
I tried composer dump-autoload and php artisan migrate:refresh and php artisan migrate:reset but it keeps on giving above error. I just want to start afresh. How to reset everything?
Ok. So I found the solution. So if you accidentally delete any model file or migrations file without using php artisan migrate:rollback the above command and any migrate command will cause problems. SO what you need to do is, delete all the tables manually from the database. I was using sqlite database and I deleted migrations, users, questions, teachers, password_resets all the tables that were generated by migrate command. Then I ran composer dumpautoload. Then I ran php artisan migrate command again after making changes to the files and everything was back to normal.
So basically deleting migrations table solves the issue.
When you refresh your migrations, Laravel looks at your migrations table and uses the values in there to load the migrations files to rollback.
Since you manually deleted the migration file, it was never removed from your migrations table. You'll have to manually delete the row in that table for the given migration file.
First, check if that class is still referenced somewhere by doing a full search within your project folder, and then if it is not, try running:
php artisan clear-compiled

Categories