From 829fc92648f9c862bc0470b203b2cfc59b9d939d Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Mon, 8 Apr 2024 18:29:48 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E9=A1=A7=E5=AE=A2=E3=83=9E=E3=82=A4?= =?UTF-8?q?=E3=82=B0=E3=83=AC=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E6=A9=9F?= =?UTF-8?q?=E8=83=BD=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Codes/QueueName.php | 1 + .../全顧客マイグレーション.php | 16 +-- .../顧客マイグレーション.php | 97 +++++++++++++++++++ config/queue.php | 3 +- 4 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 app/Console/Commands/Migration/顧客マイグレーション.php diff --git a/app/Codes/QueueName.php b/app/Codes/QueueName.php index ba1c844..3216c50 100644 --- a/app/Codes/QueueName.php +++ b/app/Codes/QueueName.php @@ -6,4 +6,5 @@ enum QueueName: string { case EMAIL = 'email'; case JOB = 'job'; + case MIGRATE = 'migrate'; } diff --git a/app/Console/Commands/Migration/全顧客マイグレーション.php b/app/Console/Commands/Migration/全顧客マイグレーション.php index f1e997a..8addf74 100644 --- a/app/Console/Commands/Migration/全顧客マイグレーション.php +++ b/app/Console/Commands/Migration/全顧客マイグレーション.php @@ -3,12 +3,12 @@ namespace App\Console\Commands\Migration; use App\Codes\EnvironmentName; +use App\Codes\QueueName; use App\Console\Commands\BaseCommand; use App\Models\Htpms\MstCustomer; -use App\Models\HtpmsCustomer\Existing\Parking; use App\Models\HtpmsCustomer\HtpmsCustomerConnectionSwitch; use Exception; -use Illuminate\Database\QueryException; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; class 全顧客マイグレーション extends BaseCommand @@ -83,7 +83,7 @@ class 全顧客マイグレーション extends BaseCommand // ## 暫定的に対象を絞り中 テスト中 - if (!in_array($customer->id, [1, 520], true)) { + if (!in_array($customer->id, [1, 131, 520], true)) { return; } @@ -91,15 +91,9 @@ class 全顧客マイグレーション extends BaseCommand $this->outputWarn(sprintf("存在しないスキーマのためスキップ htpms_%d (%s:%s)", $customer->id, $customer->customer_name, $customer->customer_id)); return; } - - HtpmsCustomerConnectionSwitch::switch($customer->id); - try { - - - $this->outputInfo(sprintf("start htpms_%d migration (%s:%s) ", $customer->id, $customer->customer_name, $customer->customer_id)); - $this->call("migrate", ["--force" => true]); - $this->outputInfo(sprintf("end htpms_%d migration (%s:%s)", $customer->id, $customer->customer_name, $customer->customer_id)); + Artisan::queue("migrate:customer", ["customerId" => $customer->id]) + ->onQueue(QueueName::MIGRATE->value); } catch (Exception $e) { print_r(DB::getConfig()); throw $e; diff --git a/app/Console/Commands/Migration/顧客マイグレーション.php b/app/Console/Commands/Migration/顧客マイグレーション.php new file mode 100644 index 0000000..668b022 --- /dev/null +++ b/app/Console/Commands/Migration/顧客マイグレーション.php @@ -0,0 +1,97 @@ +outputInfoForBase = false; + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return int + */ + public function service(): int + { + $customerId = $this->argument("customerId"); + $customer = MstCustomer::query() + ->whereId($customerId) + ->firstOrFail(); + + HtpmsCustomerConnectionSwitch::switch($customerId); + + $this->printMigrationStatus(); + + try { + $this->outputInfo(sprintf("start htpms_%d migration (%s:%s) ", $customer->id, $customer->customer_name, $customer->customer_id)); + $this->call("migrate", ["--force" => true]); + $this->outputInfo(sprintf("end htpms_%d migration (%s:%s)", $customer->id, $customer->customer_name, $customer->customer_id)); + } catch (Exception $e) { + print_r(DB::getConfig()); + throw $e; + } + + return self::RESULTCODE_SUCCESS; + } + + private function printMigrationStatus() + { + + + $conn = DB::connection("htpms_customer"); + $config = $conn->getConfig(); + $database = data_get($config, "database", "htpms_XXX"); + if (!Schema::connection("htpms_customer")->hasTable("tbl3_migrations")) { + $this->outputInfo(sprintf("初回マイグレーション:%s", $database)); + return; + } + + + $mig = $conn->table("tbl3_migrations")->orderByDesc("id")->first(); + if ($mig) { + $this->outputInfo(sprintf("最新:%s:%s", $database, $mig->migration)); + } else { + $this->outputInfo(sprintf("最新:%s:なし", $database)); + } + } +} diff --git a/config/queue.php b/config/queue.php index 5f02403..cbc6f15 100644 --- a/config/queue.php +++ b/config/queue.php @@ -37,6 +37,7 @@ return [ 'database' => [ 'driver' => 'database', 'table' => 'tbl3_jobs', + 'connection' => 'htpms', 'queue' => 'default', 'retry_after' => 90, 'after_commit' => false, @@ -102,7 +103,7 @@ return [ 'failed' => [ 'driver' => env('QUEUE_FAILED_DRIVER', 'database-uuids'), - 'database' => env('DB_CONNECTION', 'mysql'), + 'database' => "htpms", 'table' => 'tbl3_failed_jobs', ],