Laravel在刷新时不会在`migrations`表中创建记录

时间:2018-05-23 07:15:54

标签: laravel eloquent laravel-migrations

我有一个尴尬的问题,每次我运行php artisan migrate:refresh --seedmigrations表都是空的,尽管它应该在迁移后添加初始迁移记录:2018_05_02_114819_add_initial_migration

这是我的迁移代码(它是旧数据库架构的导入):

<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddInitialMigration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(!Schema::hasTable('users')){
            DB::unprepared(File::get(database_path('sam.sql')));
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0');

        foreach (DB::select('SHOW TABLES') as $table) {

            $table_array = get_object_vars($table);
            if($table_array[key($table_array)] !== 'migrations'){
                DB::statement('DROP TABLE ' . $table_array[key($table_array)]);
            }

        }

        DB::statement('SET FOREIGN_KEY_CHECKS=1');
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了。似乎在.sql文件中有一行说:

SET AUTOCOMMIT = 0;

在文件末尾未将其设置回1。