自动 Laravel 迁移无法完成
Automated Laravel Migration unable to complete
我有一个 SAAS 应用程序,其中租户分离是按数据库进行的。当新客户注册时,将通过一系列自动自定义 Laravel 命令(使用任务 Scheduler/Cron 作业)配置帐户。其中之一是租户数据库迁移。在更新应用程序的过程中,我添加了新的迁移以创建额外的 table 并从其他 table 中添加和删除一些列。执行新租户迁移时,只有旧迁移 运行。只有当我 运行 在本地主机或生产环境中手动执行命令(没有 cron 作业)时,迁移才会完成。没有错误提示迁移未完成的原因。我错过了什么吗?下面是命令的代码片段。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Account;
use App\Traits\SwitchToAccountsManager;
use App\Traits\SwitchToTenantDatabase;
use DB;
class MigrateNewTenant extends Command
{
use SwitchToAccountsManager,SwitchToTenantDatabase;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:new';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs migration on all new tenants databases';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->SwitchToAccounts();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tenants=Account::where('db_created','Yes')->where('tables_created','No')->get();
$bar = $this->output->createProgressBar(count($tenants));
foreach ($tenants as $tenant) {
if(!empty($tenant->db_name)){
$account_id=$tenant->account_id;
//Set tenant database connection
$this->SwitchToTenantDB($tenant->db_name);
//Run migration
$this->info("Running migration for: ".$tenant->db_name);
$this->call('migrate',["--force" => true]);
//Switch back to tenants manager
$this->SwitchToAccounts();
//Mark tables as created/migrated
DB::table('accounts')
->where('account_id', $account_id)
->update(['tables_created' => 'Yes']);
}else{
$this->info("Skipping migration for: ".$tenant->name);
}
$bar->advance();
}
$bar->finish();
}
}
由于迁移不完整,客户遇到了错误。我必须手动重新运行 迁移。
我发现 cron 作业正在对暂存应用程序而非生产应用程序执行 php artisan chedule:run
命令。
我有一个 SAAS 应用程序,其中租户分离是按数据库进行的。当新客户注册时,将通过一系列自动自定义 Laravel 命令(使用任务 Scheduler/Cron 作业)配置帐户。其中之一是租户数据库迁移。在更新应用程序的过程中,我添加了新的迁移以创建额外的 table 并从其他 table 中添加和删除一些列。执行新租户迁移时,只有旧迁移 运行。只有当我 运行 在本地主机或生产环境中手动执行命令(没有 cron 作业)时,迁移才会完成。没有错误提示迁移未完成的原因。我错过了什么吗?下面是命令的代码片段。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Account;
use App\Traits\SwitchToAccountsManager;
use App\Traits\SwitchToTenantDatabase;
use DB;
class MigrateNewTenant extends Command
{
use SwitchToAccountsManager,SwitchToTenantDatabase;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:new';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs migration on all new tenants databases';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->SwitchToAccounts();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tenants=Account::where('db_created','Yes')->where('tables_created','No')->get();
$bar = $this->output->createProgressBar(count($tenants));
foreach ($tenants as $tenant) {
if(!empty($tenant->db_name)){
$account_id=$tenant->account_id;
//Set tenant database connection
$this->SwitchToTenantDB($tenant->db_name);
//Run migration
$this->info("Running migration for: ".$tenant->db_name);
$this->call('migrate',["--force" => true]);
//Switch back to tenants manager
$this->SwitchToAccounts();
//Mark tables as created/migrated
DB::table('accounts')
->where('account_id', $account_id)
->update(['tables_created' => 'Yes']);
}else{
$this->info("Skipping migration for: ".$tenant->name);
}
$bar->advance();
}
$bar->finish();
}
}
由于迁移不完整,客户遇到了错误。我必须手动重新运行 迁移。
我发现 cron 作业正在对暂存应用程序而非生产应用程序执行 php artisan chedule:run
命令。