Using Laravel, after running migrate, refresh & rollback commands, I lost all my tables and content from my MySQL database. Is there any option to get all my tables and data back?
First I had run:
php artisan migrate: refresh
And I lost all my data. I thought if I used the rollback command I could get my data back, so I then ran
php artisan migrate: rollback.
This caused me to lose all my tables too!
Unlucky my friend, it's a lesson to be learned from!
Migrate | Refresh
php artisan migrate:refresh
Will revert all migrations for your application This will essentially drop all of the tables in your database before then re-running all of the up() portitions of your migrations.
Migrate | Rollback
php artisan migrate:rollback
This will look at the migrations table in your database and find all of the migrations run in the last batch (see the batch column on the migrations table). It will then run all of the down() methods to revert just those latest migrations.
Conclusion
Don't run php artisan migrate:refresh unless you have a database backup or don't care about losing the data (e.g. you are using seeders, etc)
Addendum
To re-import your database dump after a refresh, you can do something like this. However, this is not an ideal solution and I strongly suggest reading up on proper Database Seeding and Model Factories
https://laravel.com/docs/7.x/seeding#writing-seeders
<?php
use DB;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// Get the databse dump we wish to use
$sql = base_path('database/dumps/your_database.sql');
if ($sql) {
// Remove foreign keys for now
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
// Now we seed using our database dump of
DB::unprepared(file_get_contents($sql));
// Enable foreign keys
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
}
}
Now you can run the migration command with the --seed option which will run your database seeders
php artisan migrate:refresh --seed
Related
I create a seeder for insert default values in database.
If i run this seeder more than one time mysql return error for duplicate key,
So my question is that what is best approach to handle this error? And How can continue to run other seeds?
You shouldn't run db:seed command multiple times. A better way is to recreate all tables and seed the data with this command:
php artisan migrate:refresh --seed
Or just run db:seed once after running the php artisan migrate:refresh command.
https://laravel.com/docs/5.5/migrations#rolling-back-migrations
You can still use truncate method before seeding data, this will remove duplicate key errors because the table is already empty:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class EntitiesTableSeeder extends Seeder {
public function run() {
DB::table('table')->truncate();
//OR
\App\Model::truncate();
// then insert your data here
}
Separate your seeds to more files and in database/DatabaseSeeder.php just call them like so:
$this->call(AuthorSeeder::class);
$this->call(ContentSeeder::class);
But sure, you cannot duplicate keys, thats the issue you have to solve.
eighty8/laravel-seeder
This package solves the problem, its version controlling the seeding like laravel does for migration, has some other benefits
Allows you to seed databases in different environments with different values.
Allows you to "version" seeds the same way that Laravel currently handles
migrations. Running php artisan seed will only run seeds that haven't already been run.
Allows you to run multiple seeds of the same model/table
Prompts you if your database is in production
I have seeded my DB using php artisan db::seed. Is there a way to rollback what I have seeded into my DB?
I cannot seem to find any command like php artisan db::seed rollback.
use Undo Seeder for Laravel.
When you install UndoSeeder, the following artisan commands are made available:
db:seed-undo Undo seeds in the seeds directory.
db:seed-refresh Undo seeds run seeds again.
more Undo-Seeder
You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations. This command is useful for completely re-building your database:
php artisan migrate:refresh --seed
Running Seeders
If you want to wipe out certain table, then just TRUNCATE that table, and seed it again:
php artisan db:seed --class=UsersTableSeeder
No need for additional packages for such a simple task.
The easiest method is to go into your
database/seeders
folder and manually delete the files you want to remove then run php artisan migrate:fresh.
Open the database table with whichever platform you are using (phpMyAdmin / mySql-Workbench / a DB-editor plug-in etc.) and manually delete the seeded contents. Then you will be able to reseed the table using php artisan db:seed
I was looking for something else like i have ran php artisan db:seed and after that I wanted to change something in UserSeeder, like changing an email address.
So if you want to change something in Seeder class and you have already run db:seed command.
Then first of all you have to add truncate function before any other code like if you have UserSeeder class then add below code in run function before seeding all User model:
User::truncate();
Then all you have to do is re-run the command.
php artisan db:seed
It will seed again all the classes as per your change and delete already seeded Users in Database, You can use this method for any model you want to truncate the table and re insert the records.
I am new to laravel and while working I created a new migration. After running the command php artisan migrate, the migration was successful. I then went back to the previous created migration and added some new fields. When I run back the command php artisan migrate, I was receiving the message Nothing to migrate. I was then supposed to run the command php artisan migrate:refresh for the changes to be applied but I did not want to rollback some migrations so I went to those migrations and I modified the last method as following
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// The line below has been commented to prevent rollback
// Schema::drop('table-name');
}
Since then when I run php artisan migrate I get the following error:
[Symfony\Component\Debug\Exception\FatalErrorException] Class 'Carbon' not found
I have run composer update, composer dump-autoload, composer clear-cache, php artisan migrate:refresh, php artisan migrate:rollback, php artisan migrate:reset
Kindly help me solve this problem.
In your migration classes or scripts, you have used Carbon class, but forgot to import/include.
Add
use Carbon/Carbon;
in your migration scripts where you have used Carbon class and run migrations.
ref link: http://laravel.io/forum/03-12-2014-class-carbon-not-found
I would not recommend to comment some lines in the migration's down method.
Generally the down() method should always undo the operations made in the up() method.
If you want to add some fields to an existing table and don't want to loose some data by doing a refresh, then just create another migration and specify the table you want to modify. For example:
php artisan make:migration add_some_fields_to_users --table=users
You could also consider using Database Seeds so you can refresh your migrations and then seeding the database with data again. This is very powerful during development stage.
For you Carbon issue most likely the answer from #Sanrekula is what your are looking for.
I have several migration file .
I want run php artisan migrate:rollback by file name like this
2016_05_16_131054_create_contries_table.php
You can't specify migration to rollback. You can only rollback the last migration batch (look migrations table).
The best you can do is to make changes in your table with new migration. Or you can save data, rollback all migrations and and migrate again.
Say i got a.php, b.php, c.php and d.php migration classes files. How to rollback to a specific migration state, the state defined within b.php for example, with artisan command ?
I am afraid, you cannot do this directly.
You can:
1, Rollback The Last Migration Operation (all migrations ran in the last batch)
php artisan migrate:rollback
2, Rollback all migrations
php artisan migrate:reset
3, Rollback all migrations and run them all again
php artisan migrate:refresh
php artisan migrate:refresh --seed
In your situation, modify b.php and it's up() method, then execute artisan migrate:refresh command.
There's a way to hack it by manually editing the database. In the migrations table change the batch column by giving the last migration a different batch number. Be aware that they are in increasing order, so edit accordingly. This tracks which migrations were applied separately.
Then run artisan:rollback and it will undo the last "batch".
So if you want to separate them all, then start from the top and give each 1,2,3,4,5 and so on... You can see that it is easily scriptable, and you can make an artisan command if you wish to separate all your migrations.
Laravel 5.3
With Laravel 5.3 there is no need for heavy scripting. As it allows to rollback given number of migrations.
php artisan migrate:rollback --step=1
Here's the manual page for reference.
In my experience. I never do migrate:rollback. I would usually create another migration that does all the changes i need to "undo/rollback" the previous migrations.
This way you can be flexible if you want to rollback 2-x steps back, you can just create a new migration to effect the changes you want and then run the new migration by php artisan migrate.
In fact, there is not this feature (yet). surprisingly
The best idea, is create a new file backtob.php and make its up call the down of your other migrate files. To avoid copy and paste, you can do something like this:
class BacktoB {
public function up () {
// the database is in the after D state //
$migrateD = new D();
$migrateD->down();
// the database is in the after C state //
$migrateC = new C();
$migrateC->down();
// the database is in the before C state //
// before C = B //
}
public function down () {
// the database is in the B state //
$migrateC = new C();
$migrateC->up();
// the database is in the after C state //
$migrateD = new D();
$migrateD->up();
// the database is in the after D state //
}
}
As you can see, you can create the up and down calling the up and down of those migrations what you want to revert.
It is not the ideal, but it is what we can do.
If you really wanted to - you could write a custom function that queries the migrations table, looks for the file you are after, and works out how many times to roll back - then does a loop of 'migrate:rollback' until you reach the required migration...
There is an easy yet dirty way:
If you have migrations a.php, b.php and c.php and want to rollback c and b, you can simply modify a.php in such a way that there will be a syntax error... drop a semi-colon or something.
So, when you run php artisan migrate:rollback it will rollback both c and b and stop with an error in a. From then on, the rollback of c and b will be considered the last migration operation.
Don't forget to fix whatever error you made on purpose in a.php.
Since Laravel just provide php artisan migrate:rollback to rollback your migration script, the best way to rollback your selected migration script is to create a new migration script and put the script in your down method (on your selected migration script) to the newly created migration script.
Hope this help.
Use the php artisan migrate:rollback command.
php artisan migrate:rollback
To see what rollback will do, use the --pretend option.
php artisan migrate:rollback --pretend
You can also specify a database connection other than the default one.
php artisan migrate:rollback --pretend --database=other-one