diff --git a/app/Console/Commands/BaseCommand.php b/app/Console/Commands/BaseCommand.php index 5b571fe..bd04c50 100644 --- a/app/Console/Commands/BaseCommand.php +++ b/app/Console/Commands/BaseCommand.php @@ -42,7 +42,13 @@ abstract class BaseCommand extends Command try { $ret = $this->service(); } catch (Exception $e) { - $message = sprintf("例外発生:%s:%s:%d", $e->getMessage(), $e->getFile(), $e->getLine()); + $message = sprintf( + "例外発生:%s:%s:%s:%d", + get_class($e), + $e->getMessage(), + $e->getFile(), + $e->getLine() + ); $this->outputError($message, $e->getTrace()); $ret = self::RESULTCODE_FAILED; } diff --git a/app/Console/Commands/Migration/全顧客マイグレーション.php b/app/Console/Commands/Migration/全顧客マイグレーション.php new file mode 100644 index 0000000..f1e997a --- /dev/null +++ b/app/Console/Commands/Migration/全顧客マイグレーション.php @@ -0,0 +1,108 @@ +environment([EnvironmentName::PRODUCTOIN->value])) { + $this->outputWarn("本番環境でないため、520以外はスキップします"); + } + $customers = MstCustomer::query() + ->orderBy(MstCustomer::COL_NAME_ID) + ->get(); + foreach ($customers as $customer) { + $this->handleCustomer($customer); + } + + + return self::RESULTCODE_SUCCESS; + } + + private function handleCustomer(MstCustomer $customer) + { + + // 本番環境のみ全顧客をマイグレーションする + if (!app()->environment([EnvironmentName::PRODUCTOIN->value])) { + if ( + $customer->id !== 520 + ) { + return; + } + } + + + // ## 暫定的に対象を絞り中 テスト中 + if (!in_array($customer->id, [1, 520], true)) { + return; + } + + if (!HtpmsCustomerConnectionSwitch::isEnable($customer->id)) { + $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)); + } catch (Exception $e) { + print_r(DB::getConfig()); + throw $e; + } + } +} diff --git a/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php b/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php index 995bc09..6b8e135 100644 --- a/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php +++ b/app/Models/HtpmsCustomer/HtpmsCustomerConnectionSwitch.php @@ -2,12 +2,19 @@ namespace App\Models\HtpmsCustomer; -use Illuminate\Support\Facades\DB; +use Exception; +use Illuminate\Support\Facades\Config; +use PDO; class HtpmsCustomerConnectionSwitch { public static function switch(int $customerId): void { + + if (!self::isEnable($customerId)) { + throw new Exception("顧客切り替え不正"); + } + $connectionsRoot = "database.connections"; $connectionHtmsCustomer = "htpms_customer"; @@ -17,9 +24,26 @@ class HtpmsCustomerConnectionSwitch $afterDatabaseName = "htpms_{$customerId}"; if ($currentDatabaseName !== $afterDatabaseName) { - $conf = [$databaseNameKey => $afterDatabaseName]; - config($conf); - DB::reconnect($connectionHtmsCustomer); + Config::set($databaseNameKey, $afterDatabaseName); + } + } + + public static function isEnable(int $customerId): bool + { + $dsn = sprintf( + 'pgsql:dbname=htpms_%d host=%s port=%d', + $customerId, + config("database.connections.htpms_customer.host"), + config("database.connections.htpms_customer.port"), + ); + $user = config("database.connections.htpms_customer.username"); + $password = config("database.connections.htpms_customer.password"); + try { + new PDO($dsn, $user, $password); + } catch (Exception $e) { + return false; } + + return true; } } diff --git a/app/Util/MigrationEx.php b/app/Util/MigrationEx.php new file mode 100644 index 0000000..5131775 --- /dev/null +++ b/app/Util/MigrationEx.php @@ -0,0 +1,55 @@ +connection) { + throw new Exception("connection 未定義"); + } + + // 共通スキーマの変更は社内テスト(520)の場合のみ行う + if ($this->connection === "htpms") { + if (config("database.connections.htpms_customer.database") !== "htpms_520") { + printf("htpms スキップ\n"); + return; + } + } + + $this->runUp(); + } + + public function down(): void + { + + if (!$this->connection) { + throw new Exception("connection 未定義"); + } + + // 共通スキーマの変更は社内テスト(520)の場合のみ行う + if ($this->connection === "htpms") { + if (config("database.connections.htpms_customer.database") !== "htpms_520") { + printf("htpms スキップ\n"); + return; + } + } + + $this->runDown(); + } + + protected function runUp(): void + { + throw new NotImplementedException(); + } + protected function runDown(): void + { + throw new NotImplementedException(); + } +}; diff --git a/database/migrations/2014_10_12_000000_create_tbl3_mst_users.php b/database/migrations/2014_10_12_000000_create_tbl3_mst_users.php index df6a90d..0ab6807 100644 --- a/database/migrations/2014_10_12_000000_create_tbl3_mst_users.php +++ b/database/migrations/2014_10_12_000000_create_tbl3_mst_users.php @@ -1,18 +1,18 @@ baseColumn() @@ -40,7 +40,7 @@ return new class extends Migration /** * Reverse the migrations. */ - public function down(): void + public function runDown(): void { Schema::dropIfExists('tbl3_mst_users'); Schema::dropIfExists('tbl3_mst_user_histories'); diff --git a/database/migrations/2019_08_19_000000_create_tbl3_failed_jobs.php b/database/migrations/2019_08_19_000000_create_tbl3_failed_jobs.php index f07fb19..7b44f83 100644 --- a/database/migrations/2019_08_19_000000_create_tbl3_failed_jobs.php +++ b/database/migrations/2019_08_19_000000_create_tbl3_failed_jobs.php @@ -1,17 +1,17 @@ id()->comment("失敗したジョブの"); @@ -29,7 +29,7 @@ return new class extends Migration /** * Reverse the migrations. */ - public function down(): void + protected function runDown(): void { Schema::dropIfExists('tbl3_failed_jobs'); } diff --git a/database/migrations/2024_03_13_172509_create_tbl3_jobs.php b/database/migrations/2024_03_13_172509_create_tbl3_jobs.php index b8d8929..5cbdaee 100644 --- a/database/migrations/2024_03_13_172509_create_tbl3_jobs.php +++ b/database/migrations/2024_03_13_172509_create_tbl3_jobs.php @@ -1,18 +1,18 @@ getTable(), "transfer_reason")) { diff --git a/database/migrations/2024_04_04_085800_add_column_tbl3_mst_shop_no_relations.php b/database/migrations/2024_04_04_085800_add_column_tbl3_mst_shop_no_relations.php index aa66e29..e070591 100644 --- a/database/migrations/2024_04_04_085800_add_column_tbl3_mst_shop_no_relations.php +++ b/database/migrations/2024_04_04_085800_add_column_tbl3_mst_shop_no_relations.php @@ -2,20 +2,20 @@ use App\Codes\Model\QRServiceUsage; use App\Models\ColumnName; +use App\Util\MigrationEx; use App\Util\MigrationHelper; -use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; -return new class extends Migration +return new class extends MigrationEx { protected $connection = "htpms_customer"; /** * Run the migrations. */ - public function up(): void + protected function runUp(): void { // バックアップ $backup = DB::connection($this->connection)->table("tbl3_mst_shop_no_relations")->get(); @@ -67,7 +67,7 @@ return new class extends Migration /** * Reverse the migrations. */ - public function down(): void + protected function runDown(): void { } }; diff --git a/database/migrations/2024_04_04_154100_create_tbl3_qrs_print_tickets.php b/database/migrations/2024_04_04_154100_create_tbl3_qrs_print_tickets.php index 829f90d..5fc593d 100644 --- a/database/migrations/2024_04_04_154100_create_tbl3_qrs_print_tickets.php +++ b/database/migrations/2024_04_04_154100_create_tbl3_qrs_print_tickets.php @@ -1,20 +1,19 @@