|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Relaticle\CustomFields\Commands\Upgrade; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Database\Schema\Blueprint; |
| 7 | +use Illuminate\Support\Facades\Schema; |
| 8 | +use Relaticle\CustomFields\Commands\UpgradeCommand; |
| 9 | +use Relaticle\CustomFields\Support\Utils; |
| 10 | +use Illuminate\Console\Command; |
| 11 | + |
| 12 | +class UpdateDatabaseSchema |
| 13 | +{ |
| 14 | + public function handle(UpgradeCommand $command, Closure $next): mixed |
| 15 | + { |
| 16 | + $isDryRun = $command->isDryRun(); |
| 17 | + |
| 18 | + $command->info('--- Updating database schema...'); |
| 19 | + $command->newLine(); |
| 20 | + |
| 21 | + // Logic to update database schema |
| 22 | + $this->createCustomFieldSectionsTable($command, $isDryRun); |
| 23 | + $this->updateCustomFieldsTable($command, $isDryRun); |
| 24 | + $this->removeDeletedAtColumns($command, $isDryRun); |
| 25 | + |
| 26 | + $command->newLine(); |
| 27 | + $command->info('Database schema update step completed.'); |
| 28 | + $command->newLine(); |
| 29 | + |
| 30 | + return $next($command); |
| 31 | + } |
| 32 | + |
| 33 | + private function createCustomFieldSectionsTable(Command $command, bool $isDryRun): void |
| 34 | + { |
| 35 | + $sectionsTable = config('custom-fields.table_names.custom_field_sections', 'custom_field_sections'); |
| 36 | + |
| 37 | + if (!Schema::hasTable($sectionsTable)) { |
| 38 | + if ($isDryRun) { |
| 39 | + $command->line("Table `{$sectionsTable}` would be created."); |
| 40 | + } else { |
| 41 | + Schema::create($sectionsTable, function (Blueprint $table): void { |
| 42 | + $table->id(); |
| 43 | + if (Utils::isTenantEnabled()) { |
| 44 | + $table->foreignId(config('custom-fields.column_names.tenant_foreign_key'))->nullable()->index(); |
| 45 | + } |
| 46 | + $table->string('code'); |
| 47 | + $table->string('name'); |
| 48 | + $table->string('type'); |
| 49 | + $table->string('entity_type'); |
| 50 | + $table->unsignedBigInteger('sort_order')->nullable(); |
| 51 | + $table->string('description')->nullable(); |
| 52 | + $table->boolean('active')->default(true); |
| 53 | + $table->boolean('system_defined')->default(false); |
| 54 | + |
| 55 | + $uniqueColumns = ['entity_type', 'code']; |
| 56 | + if (Utils::isTenantEnabled()) { |
| 57 | + $uniqueColumns[] = config('custom-fields.column_names.tenant_foreign_key'); |
| 58 | + } |
| 59 | + $table->unique($uniqueColumns); |
| 60 | + |
| 61 | + $table->timestamps(); |
| 62 | + }); |
| 63 | + $command->info("Table `{$sectionsTable}` created successfully."); |
| 64 | + } |
| 65 | + } else { |
| 66 | + $command->line("Table `{$sectionsTable}` already exists. Skipping creation."); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private function updateCustomFieldsTable(Command $command, bool $isDryRun): void |
| 71 | + { |
| 72 | + $customFieldsTable = config('custom-fields.table_names.custom_fields'); |
| 73 | + |
| 74 | + $columnsToAdd = []; |
| 75 | + if (!Schema::hasColumn($customFieldsTable, 'custom_field_section_id')) { |
| 76 | + $columnsToAdd[] = 'custom_field_section_id'; |
| 77 | + } |
| 78 | + if (!Schema::hasColumn($customFieldsTable, 'width')) { |
| 79 | + $columnsToAdd[] = 'width'; |
| 80 | + } |
| 81 | + |
| 82 | + if (!empty($columnsToAdd)) { |
| 83 | + if ($isDryRun) { |
| 84 | + foreach ($columnsToAdd as $column) { |
| 85 | + $command->line("Column `{$column}` would be added to `{$customFieldsTable}` table."); |
| 86 | + } |
| 87 | + } else { |
| 88 | + Schema::table($customFieldsTable, function (Blueprint $table) use ($columnsToAdd): void { |
| 89 | + if (in_array('custom_field_section_id', $columnsToAdd)) { |
| 90 | + $table->unsignedBigInteger('custom_field_section_id')->nullable()->after('id'); |
| 91 | + } |
| 92 | + if (in_array('width', $columnsToAdd)) { |
| 93 | + $table->string('width')->nullable()->after('custom_field_section_id'); |
| 94 | + } |
| 95 | + }); |
| 96 | + foreach ($columnsToAdd as $column) { |
| 97 | + $command->info("Added `{$column}` column to `{$customFieldsTable}` table."); |
| 98 | + } |
| 99 | + } |
| 100 | + } else { |
| 101 | + $command->line("Columns `custom_field_section_id` and `width` already exist in `{$customFieldsTable}`. Skipping."); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private function removeDeletedAtColumns(Command $command, bool $isDryRun): void |
| 106 | + { |
| 107 | + $tablesWithDeletedAt = [ |
| 108 | + config('custom-fields.table_names.custom_fields'), |
| 109 | + config('custom-fields.table_names.custom_field_options'), |
| 110 | + config('custom-fields.table_names.custom_field_values'), |
| 111 | + ]; |
| 112 | + |
| 113 | + foreach ($tablesWithDeletedAt as $table) { |
| 114 | + if (Schema::hasColumn($table, 'deleted_at')) { |
| 115 | + if ($isDryRun) { |
| 116 | + $command->line("Column `deleted_at` would be removed from `{$table}` table."); |
| 117 | + } else { |
| 118 | + Schema::table($table, function (Blueprint $table): void { |
| 119 | + $table->dropSoftDeletes(); |
| 120 | + }); |
| 121 | + $command->info("Removed `deleted_at` column from `{$table}` table."); |
| 122 | + } |
| 123 | + } else { |
| 124 | + $command->line("Column `deleted_at` does not exist in `{$table}`. Skipping."); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments