Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

217 lines
7.9KB

  1. <?php
  2. namespace App\Util;
  3. use App\Models\ColumnName;
  4. use App\Models\Feature\IModelFeature;
  5. use Closure;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. use Illuminate\Support\Str;
  10. class MigrationHelper
  11. {
  12. public static function createTable(string $tableName, Closure $schema, string $tableComment = "")
  13. {
  14. if (Schema::hasTable($tableName)) {
  15. Schema::drop($tableName);
  16. }
  17. Schema::create($tableName, function (Blueprint $table) use ($tableName, $schema, $tableComment) {
  18. if ($tableComment) {
  19. $table->comment($tableComment);
  20. } else {
  21. $modelClassName = Str::singular('App\\Models\\' . Str::studly($tableName));
  22. if (class_exists($modelClassName)) {
  23. /**
  24. * @var IModelFeature
  25. */
  26. $model = new $modelClassName();
  27. $table->comment($model->getModelName());
  28. }
  29. }
  30. $forHistory = false;
  31. if (Str::endsWith($tableName, "histories")) {
  32. $forHistory = true;
  33. }
  34. $helper = new MigrationHelper($table, $forHistory);
  35. $schema($table, $helper);
  36. });
  37. }
  38. public static function alterTable(string $tableName, Closure $schema)
  39. {
  40. Schema::table($tableName, function (Blueprint $table) use ($tableName, $schema) {
  41. $forHistory = false;
  42. if (Str::endsWith($tableName, "histories")) {
  43. $forHistory = true;
  44. }
  45. $helper = new MigrationHelper($table, $forHistory);
  46. $schema($table, $helper);
  47. });
  48. }
  49. public static function addUnique(string $tableName, string $number, array $columns)
  50. {
  51. $statement = sprintf(
  52. 'CREATE UNIQUE INDEX %s ON %s (%s) WHERE (%s IS NULL)',
  53. sprintf("%s_uq_%02d", $tableName, $number),
  54. $tableName,
  55. implode(",", $columns),
  56. ColumnName::DELETED_AT
  57. );
  58. DB::statement($statement);
  59. }
  60. private Blueprint $table;
  61. public bool $forHistory = false;
  62. public function __construct(Blueprint $table, bool $forHistory = false)
  63. {
  64. $this->table = $table;
  65. $this->forHistory = $forHistory;
  66. }
  67. public function baseColumn()
  68. {
  69. if ($this->forHistory) {
  70. $this->table->id('history_id')->comment("履歴ID");
  71. $this->table->uuid('id')->comment("ID");
  72. } else {
  73. $this->table->uuid('id')->primary()->comment("ID");
  74. }
  75. $this->table->uuid(ColumnName::CREATED_BY)->nullable()->comment("作成者ID");
  76. $this->table->uuid(ColumnName::UPDATED_BY)->nullable()->comment("更新者ID");
  77. $this->table->timestamp(ColumnName::CREATED_AT)->nullable()->comment("作成日時");
  78. $this->table->timestamp(ColumnName::UPDATED_AT)->nullable()->comment("更新日時");
  79. $this->table->timestamp(ColumnName::DELETED_AT)->nullable()->comment("論理削除日時");
  80. $this->table->index([ColumnName::CREATED_AT], sprintf("%s_idx_CREATED_AT", $this->table->getTable()));
  81. $this->table->index([ColumnName::UPDATED_AT], sprintf("%s_idx_UPDATED_AT", $this->table->getTable()));
  82. return $this;
  83. }
  84. public function index(int $number, array $columns)
  85. {
  86. $indexName = $this->getIndexName($number);
  87. if ($this->forHistory) {
  88. $this->table->index($columns, $indexName);
  89. } else {
  90. $this->table->index([...$columns, ColumnName::DELETED_AT], $indexName);
  91. }
  92. return $this;
  93. }
  94. public function dropIndex(int $number)
  95. {
  96. $indexName = $this->getIndexName($number);
  97. $this->table->dropIndex($indexName);
  98. return $this;
  99. }
  100. public function dropUnique(int $number)
  101. {
  102. $uniqueName = $this->getUniqueName($number);
  103. $this->table->dropUnique($uniqueName);
  104. return $this;
  105. }
  106. private function getIndexName(int $number)
  107. {
  108. return sprintf("%s_idx_%02d", $this->table->getTable(), $number);
  109. }
  110. private function getUniqueName(int $number)
  111. {
  112. return sprintf("%s_uq_%02d", $this->table->getTable(), $number);
  113. }
  114. public function userId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  115. {
  116. $columnName = $columnName ?? ColumnName::USER_ID;
  117. $comment = $comment ?? "ユーザーID";
  118. $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
  119. return $this;
  120. }
  121. public function shopId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  122. {
  123. $columnName = $columnName ?? ColumnName::SHOP_ID;
  124. $comment = $comment ?? "店舗ID";
  125. $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
  126. return $this;
  127. }
  128. public function shopNo(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  129. {
  130. $columnName = $columnName ?? ColumnName::SHOP_NO;
  131. $comment = $comment ?? "店舗番号";
  132. $this->table->unsignedTinyInteger($columnName)->comment($comment)->nullable($nullable);
  133. return $this;
  134. }
  135. public function parkinManagementCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  136. {
  137. $columnName = $columnName ?? ColumnName::PARKING_MANAGEMENT_CODE;
  138. $comment = $comment ?? "駐車場管理コード";
  139. $this->table->string($columnName)->comment($comment)->nullable($nullable);
  140. return $this;
  141. }
  142. public function discountTicketParkingCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  143. {
  144. $columnName = $columnName ?? ColumnName::DISCOUNT_TICKET_PARKING_CODE;
  145. $comment = $comment ?? "サービス券駐車場コード";
  146. $this->table->string($columnName)->comment($comment)->nullable($nullable);
  147. return $this;
  148. }
  149. public function discountTicketCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  150. {
  151. $columnName = $columnName ?? ColumnName::DISCOUNT_TICKET_CODE;
  152. $comment = $comment ?? "サービス券コード";
  153. $this->table->unsignedTinyInteger($columnName)->comment($comment)->nullable($nullable);
  154. return $this;
  155. }
  156. public function qrServiceParkingGroupId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  157. {
  158. $columnName = $columnName ?? ColumnName::QR_SERVICE_PARKING_GROUP_ID;
  159. $comment = $comment ?? "QRサービス券駐車場グループID";
  160. $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
  161. return $this;
  162. }
  163. public function publishingTerminalCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  164. {
  165. $columnName = $columnName ?? ColumnName::PUBLISHING_TERMINAL_CODE;
  166. $comment = $comment ?? "発行端末番号";
  167. $this->table->string($columnName)->comment($comment)->nullable($nullable);
  168. return $this;
  169. }
  170. public function publishingDate(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  171. {
  172. $columnName = $columnName ?? ColumnName::PUBLISHING_DATE;
  173. $comment = $comment ?? "QRサービス券発行日";
  174. $this->table->date($columnName)->comment($comment)->nullable($nullable);
  175. return $this;
  176. }
  177. public function publishingNo(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  178. {
  179. $columnName = $columnName ?? ColumnName::PUBLISHING_NO;
  180. $comment = $comment ?? "発行連番";
  181. $this->table->unsignedInteger($columnName)->comment($comment)->nullable($nullable);
  182. return $this;
  183. }
  184. public function emailId(bool $nullable = false)
  185. {
  186. $this->table->uuid(ColumnName::EMAIL_ID)->comment("EメールID")->nullable($nullable);
  187. return $this;
  188. }
  189. }