選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

98 行
2.5KB

  1. <?php
  2. namespace App\Console\Commands\Migration;
  3. use App\Console\Commands\BaseCommand;
  4. use App\Models\Htpms\MstCustomer;
  5. use App\Models\HtpmsCustomer\HtpmsCustomerConnectionSwitch;
  6. use Exception;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. class 顧客マイグレーション extends BaseCommand
  10. {
  11. const COMMAND = "migrate:customer {customerId}";
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = self::COMMAND;
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '顧客毎テーブルをマイグレーションする';
  24. static public function getCommand()
  25. {
  26. return self::COMMAND;
  27. }
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. $this->outputInfoForBase = false;
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return int
  42. */
  43. public function service(): int
  44. {
  45. $customerId = $this->argument("customerId");
  46. $customer = MstCustomer::query()
  47. ->whereId($customerId)
  48. ->firstOrFail();
  49. HtpmsCustomerConnectionSwitch::switch($customerId);
  50. $this->printMigrationStatus();
  51. try {
  52. $this->outputInfo(sprintf("start htpms_%d migration (%s:%s) ", $customer->id, $customer->customer_name, $customer->customer_id));
  53. $this->call("migrate", ["--force" => true]);
  54. $this->outputInfo(sprintf("end htpms_%d migration (%s:%s)", $customer->id, $customer->customer_name, $customer->customer_id));
  55. } catch (Exception $e) {
  56. print_r(DB::getConfig());
  57. throw $e;
  58. }
  59. return self::RESULTCODE_SUCCESS;
  60. }
  61. private function printMigrationStatus()
  62. {
  63. $conn = DB::connection("htpms_customer");
  64. $config = $conn->getConfig();
  65. $database = data_get($config, "database", "htpms_XXX");
  66. if (!Schema::connection("htpms_customer")->hasTable("tbl3_migrations")) {
  67. $this->outputInfo(sprintf("初回マイグレーション:%s", $database));
  68. return;
  69. }
  70. $mig = $conn->table("tbl3_migrations")->orderByDesc("id")->first();
  71. if ($mig) {
  72. $this->outputInfo(sprintf("最新:%s:%s", $database, $mig->migration));
  73. } else {
  74. $this->outputInfo(sprintf("最新:%s:なし", $database));
  75. }
  76. }
  77. }