I created few migrations and migrated them, after that I deleted them manually without rolling back.
and now I want to rollback other migrations but I'm getting an error that migration not found referring to the last migration I had deleted, even when I rollback a specefic migration with a --path I still get
migration not found: name_of_the_last_migration_I deleted_manually
with referring the last migration I deleted manually.
The migrations are stored to a table (called "migrations") in your database. When you run a migration, it is added to that table - using the rollback function looks at the latest migration in the table and runs the down() function for it.
If you reverse a migration manually (ie. deleting the table or columns that the migration created) but then try to use the rollback function, it will fail because the migration cannot remove columns / tables that have already been removed.
So the answer is to delete the migration(s) that you have reversed from the migrations table.
Related
Have been using Laravel for over a year now. My migrations folder has 39 files at the moment and i work (construct/destroy) with my database occasionally.
What happens is since ive created a "create_payments_table" migration, every migrate command that i execute builds all my tables normally but when login the migration history to my migrations table it stops at the 'create_payments_table' migration. So now when i create new migrations and run migrate again it tries to restart from my payments table which already exists giving me an error. Comparing my "create_payments_table" migration with the last logged migration file "create_pictures_table" it seems that everything is in order. Would like to know why my migrations create the tables but not all of them are been logged in migrations table?
My migrations table:
My migrations folder:
As u can see i added two new migrations and would like to run them without having to rollback my migrations table. But when doing so:
I get this error as artisan tryies to run de pauments migration again
You can migrate only a specific migration file :
php artisan migrate --path=/database/migrations/my_migration.php
Where my_migration will be your new migration file name like 2020_06_20_221554_create_additional_users_table
I am using Code First, and I want to migrate it to ORACLE database, after multiple migrations process, I am getting this error,
The best overloaded method match for Oracle.ManagedDataAccess.EntityFramework.OracleMigrationSqlGenerator.Generate(System.Data.Entity.Migrations.Model.CreateTableOperation) has some invalid arguments
I'm facing the same problem
finally I solve it by opening the database and delete the migration_history table manually
and run the command again.
see at EF5 Code First - Changing A Column Type With Migrations
in each migration you will save a record in __MigrationHistory, so in case there is no sync between the code and the migration do the following:
delete the migration history table in the database.
migrate it again by create new migration 'in package manager console' add-migration data.
update the migration to database update-database
As per laravel doc, To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files:
php artisan migrate:rollback
You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:
php artisan migrate:rollback --step=5
The migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
You can check here. But i need to remove the specific migration file. As per my project having 30-40 migration file. I want to remove one of the migration file and its model. Is there any way to do this or have to do it manually.
Don’t. Migrations are version control for your database. “Removing” a particular migration is like removing a random commit from your Git repository’s history: it can have terrible consequences.
Instead, if you no longer need a table, then create a new migration that drops that table in the up method, and recreates it in the down method so the migration can be rolled back.
Delete the migration file, remove the table from the database, and also remove that file name from migrations table in the database.
Sometimes, doing things manually is the best way.
Just do it manually and save yourself the stress of further issues
Delete the model first (if you don't) need the model any longer
Delete the migration from ...database/migrations folder
If you have already migrated i.e if you have already run php artisan migrate, log into your phpmyadmin or SQL(whichever the case is) and in your database, delete the table created by the migration
Still within your database, in the migrations folder, locate the row with that migration file name and delete the row.
Works for me, hope it helps!
If you simply remove (delete) the migration file and re-run the migrations (migrate:refresh), the database tables will be rebuilt (without the table that's defined in the migration file you deleted).
you can increment the batch number of that particular migration to make it latest batch and run rollback command.
Rollback one specific migration in Laravel
I need to rollback a specific table as I forgot to mention foreign key.
and I dont want my data loss from all tables.
My framework is Laravel 5.4
Thanks in Advance Everyone
I do it this way:
Deleting a table manually;
Going to "migrations" table and deleting corresponding row to your migration;
php artisan migrate;
done
For example, if you modified migration called "Settings", then you delete this table from DB and migrations table, then rerun artisan command.
You can do
php artisan migrate:rollback --path=/database/migrations/your_file.php
Migrations are meant to be applied and rolled back in a specific order. Therefore there is no "proper" way of reapplying an arbitrary migration.
In any case there are at least two options:
"Fail forward" - create a subsequent migration that creates a necessary FK and apply it. This is the only proper way if you are already in production.
If you're just in early stages of development and don't won't to bloat the migrations directory you can
dump the tables so that you preserve the data
rollback up to this particular migration
fix and test the migration
migrate
load the data from dumps
Unfortunately there isn't an option to rollback a migration per table basis. You can only rollback the latest migration or last few migrations using the step parameter.
But there is a hacky way to do it in case you really need to. You can set the batch value in the migrations table to a higher number than the most recent migration for only the migrations you want to rollback. With this when you call php artisan migrate:rollback, only that particular migration files with batch value altered would rollback.
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.