运行php artisan迁移刷新&之后数据和表格的丢失回滚 [英] Loss of data and tables after running php artisan migrate refresh & rollback

查看:86
本文介绍了运行php artisan迁移刷新&之后数据和表格的丢失回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel,在运行迁移后,刷新&回滚命令,我丢失了我的MySQL数据库中的所有表和内容.是否可以选择将所有表和数据取回?

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?

首先我跑步:

php artisan migrate: refresh 

我丢失了所有数据.我以为如果使用rollback命令,我可以取回我的数据,然后我就跑了

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!

迁移|刷新

php artisan migrate:refresh

将还原应用程序的所有迁移.这实际上将删除数据库中的所有表,然后再重新运行迁移的所有 up()分区.

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.

迁移|回滚

php artisan migrate:rollback

这将查看数据库中的 migrations 表,并找到最后一批中运行的所有迁移(请参见 migrations 表).然后,它将运行所有 down()方法以仅还原那些最新的迁移.

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.

结论

除非您有数据库备份或不在乎丢失数据(例如,您正在使用播种机等),否则请不要运行 php artisan migration:refresh .

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)

附录

要在刷新后重新导入数据库转储,可以执行以下操作.但是, 这不是理想的解决方案 ,我强烈建议您阅读适当的数据库种子和模型工厂

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');
        }
    }
}

现在,您可以使用-seed 选项运行迁移命令,该命令将运行数据库播种器

Now you can run the migration command with the --seed option which will run your database seeders

php artisan migrate:refresh --seed

这篇关于运行php artisan迁移刷新&amp;之后数据和表格的丢失回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆