【发布时间】:2020-02-13 19:11:41
【问题描述】:
Laravel (6.0.3):在我的迁移任务中,我想手动(使用自定义 sql)创建表并执行恢复控制台命令,因此我使用 up() 函数创建了文件 database/migrations/today_date_create_my_custom_tables.php:
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。我的自定义工匠控制台命令 db:restore 有效。我的数据是二进制格式的,所以只有 pg_restore 可以放回去。
如果我在睡眠行(应该创建表之后)检查数据库(例如使用 pgAdmin),我已经看到这些表还不存在。似乎所有 DB 命令都在函数结束(或 db 连接?)后刷新,所以我只在迁移完成时看到表。
我想在迁移命令中连接其他东西,但如果数据没有恢复,我就不能。您是否知道如何立即刷新数据库命令或其他解决问题的方法?非常感谢!
【问题讨论】:
标签: php database laravel postgresql flush