如何在codeigniter中回滚迁移?

ev7lccsx  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(103)

我可以在Codeigniter中创建和运行迁移,但无法回滚迁移。有人能帮助我如何在Codeigniter中回滚迁移吗?
使用Illuminate\数据库\胶囊\管理器作为胶囊;
类迁移创建语言扩展配置项迁移{

public function up() {
    Capsule::schema()->create('languages', function($table){
        $table->increments('id');
        $table->string('name', 120);
        $table->string('country', 120);
        $table->string('country_code', 20);
        $table->enum('status', array('0','1'))->default('0');
        $table->timestamps();
    });
}

/*
* Sample function for rolling back the above action
*/
public function down()
{
    Capsule::schema()->drop('languages');
}

}

puruo6ea

puruo6ea1#

你能发布你的移民代码吗?
在每个迁移中都有一个名为down的方法,这就是向下迁移时执行的方法。确保你的sql语句处于down状态,例如,销毁一个表。然后,加载你想要的迁移版本。

kkbh8khc

kkbh8khc2#

对于Codeigniter 4,正确答案是:

namespace App\Controllers;

use CodeIgniter\Controller;
use Throwable;

class Migrate extends Controller {
    
    public function index(){
        $migrate = \Config\Services::migrations();

        try {
            //$migrate->latest();
            $migrate->regress(1);

            echo 'Migration complete';
        } catch (Throwable $e) {
            print'<pre>';print_r($e);print'</pre>';
        }
    }
}

传递给regress()的参数是一个批处理ID,可以在迁移表中找到。这也是要回退的版本-即,如果最新批处理为3,则将丢失批处理2和3。

相关问题