|
- <?php
-
- namespace App\Util;
-
- use App\Models\ColumnName;
- use App\Models\Feature\IModelFeature;
- use Closure;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Str;
-
- class MigrationHelper
- {
-
- public static function createTable(string $tableName, Closure $schema, string $tableComment = "")
- {
- if (Schema::hasTable($tableName)) {
- Schema::drop($tableName);
- }
- Schema::create($tableName, function (Blueprint $table) use ($tableName, $schema, $tableComment) {
-
- if ($tableComment) {
- $table->comment($tableComment);
- } else {
- $modelClassName = Str::singular('App\\Models\\' . Str::studly($tableName));
- if (class_exists($modelClassName)) {
-
- /**
- * @var IModelFeature
- */
- $model = new $modelClassName();
- $table->comment($model->getModelName());
- }
- }
-
- $forHistory = false;
- if (Str::endsWith($tableName, "histories")) {
- $forHistory = true;
- }
- $helper = new MigrationHelper($table, $forHistory);
- $schema($table, $helper);
- });
- }
-
- public static function alterTable(string $tableName, Closure $schema)
- {
- Schema::table($tableName, function (Blueprint $table) use ($tableName, $schema) {
- $forHistory = false;
- if (Str::endsWith($tableName, "histories")) {
- $forHistory = true;
- }
- $helper = new MigrationHelper($table, $forHistory);
- $schema($table, $helper);
- });
- }
-
- public static function addUnique(string $tableName, string $number, array $columns)
- {
- $statement = sprintf(
- 'CREATE UNIQUE INDEX %s ON %s (%s) WHERE (%s IS NULL)',
- sprintf("%s_uq_%02d", $tableName, $number),
- $tableName,
- implode(",", $columns),
- ColumnName::DELETED_AT
- );
- DB::statement($statement);
- }
-
-
-
- private Blueprint $table;
- public bool $forHistory = false;
-
- public function __construct(Blueprint $table, bool $forHistory = false)
- {
- $this->table = $table;
- $this->forHistory = $forHistory;
- }
-
-
- public function baseColumn()
- {
- if ($this->forHistory) {
- $this->table->id('history_id')->comment("履歴ID");
- $this->table->uuid('id')->comment("ID");
- } else {
- $this->table->uuid('id')->primary()->comment("ID");
- }
- $this->table->uuid(ColumnName::CREATED_BY)->nullable()->comment("作成者ID");
- $this->table->uuid(ColumnName::UPDATED_BY)->nullable()->comment("更新者ID");
- $this->table->timestamp(ColumnName::CREATED_AT)->nullable()->comment("作成日時");
- $this->table->timestamp(ColumnName::UPDATED_AT)->nullable()->comment("更新日時");
- $this->table->timestamp(ColumnName::DELETED_AT)->nullable()->comment("論理削除日時");
-
- $this->table->index([ColumnName::CREATED_AT], sprintf("%s_idx_CREATED_AT", $this->table->getTable()));
- $this->table->index([ColumnName::UPDATED_AT], sprintf("%s_idx_UPDATED_AT", $this->table->getTable()));
-
- return $this;
- }
-
- public function index(int $number, array $columns)
- {
- $indexName = $this->getIndexName($number);
- if ($this->forHistory) {
- $this->table->index($columns, $indexName);
- } else {
- $this->table->index([...$columns, ColumnName::DELETED_AT], $indexName);
- }
- return $this;
- }
-
- public function dropIndex(int $number)
- {
- $indexName = $this->getIndexName($number);
- $this->table->dropIndex($indexName);
- return $this;
- }
- public function dropUnique(int $number)
- {
- $uniqueName = $this->getUniqueName($number);
- $this->table->dropUnique($uniqueName);
- return $this;
- }
-
- private function getIndexName(int $number)
- {
- return sprintf("%s_idx_%02d", $this->table->getTable(), $number);
- }
-
- private function getUniqueName(int $number)
- {
- return sprintf("%s_uq_%02d", $this->table->getTable(), $number);
- }
-
- public function userId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
-
- $columnName = $columnName ?? ColumnName::USER_ID;
- $comment = $comment ?? "ユーザーID";
- $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
- public function shopId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::SHOP_ID;
- $comment = $comment ?? "店舗ID";
- $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
-
- public function shopNo(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::SHOP_NO;
- $comment = $comment ?? "店舗番号";
- $this->table->unsignedTinyInteger($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
-
- public function parkinManagementCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::PARKING_MANAGEMENT_CODE;
- $comment = $comment ?? "駐車場管理コード";
- $this->table->string($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
- public function discountTicketParkingCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::DISCOUNT_TICKET_PARKING_CODE;
- $comment = $comment ?? "サービス券駐車場コード";
- $this->table->string($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
-
- public function discountTicketCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::DISCOUNT_TICKET_CODE;
- $comment = $comment ?? "サービス券コード";
- $this->table->unsignedTinyInteger($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
- public function qrServiceParkingGroupId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::QR_SERVICE_PARKING_GROUP_ID;
- $comment = $comment ?? "QRサービス券駐車場グループID";
- $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
- public function publishingTerminalCode(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::PUBLISHING_TERMINAL_CODE;
- $comment = $comment ?? "発行端末番号";
- $this->table->string($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
- public function publishingDate(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::PUBLISHING_DATE;
- $comment = $comment ?? "QRサービス券発行日";
- $this->table->date($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
- public function publishingNo(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
- {
- $columnName = $columnName ?? ColumnName::PUBLISHING_NO;
- $comment = $comment ?? "発行連番";
- $this->table->unsignedInteger($columnName)->comment($comment)->nullable($nullable);
- return $this;
- }
-
- public function emailId(bool $nullable = false)
- {
- $this->table->uuid(ColumnName::EMAIL_ID)->comment("EメールID")->nullable($nullable);
- return $this;
- }
- }
|