How to undo last laravel db:seed? - laravel-5

I have run a db:seed and in Laravel 5.4. Now I found that it was not to be run . So is there anyway in Laravel that we can undo the last db:seed and insert the table with previous data. Thanks in advance

The only way to undo your seed(s) is by deleting them manually. With this sql command that should go pretty fast. As long as no data has been added after the seed you don't want to lose.
DELETE FROM {$table} ORDER BY created_at DESC LIMIT {$number_data_seeds}

Database: Seeding is not migration, you can not rollback the last seed.
You can rollback all migrations and re-run all of your migrations and run seed, but you need to understand that when rolling back migrations, you lose all the data.
php artisan migrate: refresh --seed
Alternatively, you can look in the database/seeds folder and understand what changes you need to rollback and manually make changes to the database.
Please see the documentation https://laravel.com/docs/5.4/seeding
can you find that useful.

Related

Laravel migrate rollback cancel

I tried to rollback a specific migration, I ran php artisan migrate:rollback, but I forgot to include --step=1, now all the migration files Ran? status is No, How do I cancel the rollback? Or is it possible at all to cancel the rollback? I do not want to re-migrate all of the migration files.
migrate:rollback will rollback the last batch of migrations, maybe it only rolled back a few depending on your database.
My suggestion is to backup your database, then run artisan:migrate to see how many migrations get generated.
https://laravel.com/docs/9.x/migrations#rolling-back-migrations
Well, I guess there's really nothing you can do about rollbacked migrations.
The best option I got not to lose existing records was to dump the database, run migration, then import the dump file.

Okay to amend migrations after migrate:reset?

I have a site in development so database structure is still in flux. If I run migrate:reset, this rollbacks all migrations. Am I, therefore, okay to amend the migrations - i.e. amend Schema closures and remove migration files etc - as opposed to adding more migrations to amend the DB structure? For example, client asked for certain functionality requiring a table, decides later he doesn't want it so I have a table in my migrations I will never use. Ideally I don't want this to appear in my migrations.
If you don't need a table anymore in your project of course you can delete it's migration file.
When you run php artisan migrate:reset Laravel rolls back all migrations. But if you delete your migration file without rolling it back, Laravel will try to find that migration file to roll it back and when it can't find that file; it will throw an exception.
In such cases you can use php artisan migrate:fresh
With migrate:fresh Laravel doesn't try to find and roll back migrations, it just drops all tables and starts a fresh migrations table and migrates every file from start.
So; if you have changes on your migration files, anything, and if you are on development enviroment and nothing will affected: you can do whatever you want with your migration files and run php artisan migrate:fresh to drop every table and migrate them again.
Please check here: https://laravel.com/docs/8.x/migrations#rolling-back-migrations

Remove specific migration in laravel

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

Rollback a specific migration in laravel5

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.

Rollback single table

How to rollback the single table in laravel 4.
I ran php artisan migrate:rollback to roll back all the migrations but how to rollback the single table
Advice much appreciated
for example, you want to create the user table again, just go to mysql and delete from migration's table the field that correspond to TIMESTAMP_create_users_table:
DELETE FROM MIGRATIONS WHERE MIGRATION ="2014_10_12_000000_create_users_table";
and drop the users table too:
DROP TABLE users;
after that, just run
php artisan migrate
and that's it!
If you wish to rollback only a specific table, you should make a migration that only includes that table. It's really that simple. Making large migrations covering several updates doesn't make any sense.
Create migrations based upon their purpose. That means; on a live project every little code change should have its own related migration.
Just run The command (Laravel 5):-
php artisan migrate:rollback --step=5
It will undo last 5 migrations (in fact it will execute the function "down" in each one)
php artisan migrate
It will add all migration table into database

Resources