From f6e08b783efb1cfbd3f73e7b9a52b20128908bde Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Fri, 18 Oct 2024 21:21:11 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9E=E3=82=A4=E3=82=B0=E3=83=AC=E3=83=BC?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ƒ³ãƒ—レートマイグレーション.php | 92 +++++++++++++++++++ .../全顧客マイグレーション.php | 6 -- .../HtpmsCustomerConnectionSwitch.php | 15 +++ 3 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 app/Console/Commands/Migration/テンプレートマイグレーション.php diff --git a/app/Console/Commands/Migration/テンプレートマイグレーション.php b/app/Console/Commands/Migration/テンプレートマイグレーション.php new file mode 100644 index 0000000..8ecd525 --- /dev/null +++ b/app/Console/Commands/Migration/テンプレートマイグレーション.php @@ -0,0 +1,92 @@ +outputInfoForBase = false; + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return int + */ + public function service(): int + { + + HtpmsCustomerConnectionSwitch::switchToTemplate(); + + $this->printMigrationStatus(); + + try { + $this->outputInfo("start htpms_template migration"); + $this->call("migrate", ["--force" => true]); // 対話OFF + $this->outputInfo("end htpms_template migration"); + } 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/app/Console/Commands/Migration/全顧客マイグレーション.php b/app/Console/Commands/Migration/全顧客マイグレーション.php index 8addf74..2a89601 100644 --- a/app/Console/Commands/Migration/全顧客マイグレーション.php +++ b/app/Console/Commands/Migration/全顧客マイグレーション.php @@ -81,12 +81,6 @@ class 全顧客マイグレーション extends BaseCommand } } - - // ## 暫定的ã«å¯¾è±¡ã‚’絞り中 テスト中 - if (!in_array($customer->id, [1, 131, 520], true)) { - return; - } - if (!HtpmsCustomerConnectionSwitch::isEnable($customer->id)) { $this->outputWarn(sprintf("存在ã—ãªã„スキーマã®ãŸã‚スキップ htpms_%d (%s:%s)", $customer->id, $customer->customer_name, $customer->customer_id)); return; diff --git a/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php b/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php index 6b8e135..ac5bb9c 100644 --- a/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php +++ b/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php @@ -28,6 +28,21 @@ class HtpmsCustomerConnectionSwitch } } + public static function switchToTemplate(): void + { + $connectionsRoot = "database.connections"; + $connectionHtmsCustomer = "htpms_customer"; + + $databaseNameKey = "{$connectionsRoot}.{$connectionHtmsCustomer}.database"; + $currentDatabaseName = config($databaseNameKey); + + $afterDatabaseName = "htpms_template"; + + if ($currentDatabaseName !== $afterDatabaseName) { + Config::set($databaseNameKey, $afterDatabaseName); + } + } + public static function isEnable(int $customerId): bool { $dsn = sprintf(