Laravel (6.0.3) 不刷新 DB::statement 命令?

Laravel (6.0.3) doesn't flush DB::statement commands?

Laravel (6.0.3):在我的迁移任务中,我想手动(使用自定义 sql)创建表并执行恢复控制台命令,所以我创建了文件 database/migrations/today_date_create_my_custom_tables.php 使用 up() 函数:

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;

class CreateMyTables extends Migration
{
    public function up()
    {
        // ...
        try {
            \Log::info('create tables...');
            $result = DB::statement($a_lot_of_sql_commands_creating_tables_read_from_file);
            \Log::info('DB::statement executed... but...');
            \Log::info('also if I wait seconds...');
            sleep(3);
            \Log::info('try to call my working custom console command "pg_restore"...');
            $exitCode = Artisan::call('db:restore'); // it call pg_restore server command
            \Log::info('...give error: tables aren\'t created yet.');
            // here I need to do a lot of other stuff (create foreign keys, ecc..),
            // but data must be restored.
        }
        catch (QueryException $e) {
            //...
        }
    }
}

我使用 postgresql。我的自定义 artisan 控制台命令 db:restore 有效。我的数据是二进制格式,所以只有pg_restore可以放回去

如果我在睡眠行(应该创建表之后)检查数据库(例如使用 pgAdmin),我发现这些表还不存在。似乎所有的数据库命令都在函数结束(或数据库连接?)后被刷新,所以我只在迁移完成时才看到表。

我想在迁移命令中连接其他内容,但如果数据没有恢复,我不能。您是否知道如何立即刷新数据库命令或其他解决问题的方法?非常感谢!

好的,最后我改变了策略并解决了实现新的自定义数据库连接并在调用任何终端命令之前使用它的问题。

我用这样的代码创建了一个助手:

namespace App\Helpers;

class MyDB {
  /**
   * Custom DB connection
   */
  public static $dbConnection;

  /**
   * Create the connection resource to the DB
   */
  public static function connectDB () {
    $connectionString = 'user='.env('DB_DATABASE');
    $connectionString .= ' password='.env('DB_PASSWORD');
    $connectionString .= ' host='.env('DB_HOST');
    $connectionString .= ' port='.env('DB_PORT');
    $connectionString .= ' dbname='.env('DB_DATABASE');
    MyDB::$dbConnection = pg_pconnect($connectionString);
  }

  /**
   * Execute a Statement query with the custom DB connection.
   */
  public static function executeStatement ($query) {
    if (is_null(MyDB::$dbConnection)) {
      MyDB::connectDB();
    }
    $resource = pg_query(MyDB::$dbConnection, $query);
    if ($resource === false) {
      return false;
    }
    return true;
  }
}