|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Phinx\Migration\AbstractMigration; |
| 6 | + |
| 7 | +/** |
| 8 | + * Adds the following columns to bxt_battle_tower_honor_roll, positioned after class_decode: |
| 9 | + * | class_decode | pokemon1 | pokemon1_decode | pokemon2 | pokemon2_decode | pokemon3 | pokemon3_decode | message_start | message_start_decode | |
| 10 | + * |
| 11 | + * Column definitions are copied from bxt_battle_tower_records when available. |
| 12 | + * If message_start/message_start_decode already exist, they are re-positioned to the right of pokemon3_decode. |
| 13 | + * |
| 14 | + * Phinx 0.16.x compatible (no AbstractMigration::hasColumn()). |
| 15 | + * Uses INFORMATION_SCHEMA checks; safe to run multiple times. |
| 16 | + */ |
| 17 | +final class BattleTowerAddPokemonColsToHonorRoll extends AbstractMigration |
| 18 | +{ |
| 19 | + private function tableExists(string $table): bool |
| 20 | + { |
| 21 | + $pdo = $this->getAdapter()->getConnection(); |
| 22 | + $row = $this->fetchRow( |
| 23 | + "SELECT 1 AS ok |
| 24 | + FROM INFORMATION_SCHEMA.TABLES |
| 25 | + WHERE TABLE_SCHEMA = DATABASE() |
| 26 | + AND TABLE_NAME = " . $pdo->quote($table) . " |
| 27 | + LIMIT 1" |
| 28 | + ); |
| 29 | + return (bool)$row; |
| 30 | + } |
| 31 | + |
| 32 | + private function columnExists(string $table, string $column): bool |
| 33 | + { |
| 34 | + $pdo = $this->getAdapter()->getConnection(); |
| 35 | + $row = $this->fetchRow( |
| 36 | + "SELECT 1 AS ok |
| 37 | + FROM INFORMATION_SCHEMA.COLUMNS |
| 38 | + WHERE TABLE_SCHEMA = DATABASE() |
| 39 | + AND TABLE_NAME = " . $pdo->quote($table) . " |
| 40 | + AND COLUMN_NAME = " . $pdo->quote($column) . " |
| 41 | + LIMIT 1" |
| 42 | + ); |
| 43 | + return (bool)$row; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return array<string, mixed>|null |
| 48 | + */ |
| 49 | + private function getColumnSpec(string $table, string $column): ?array |
| 50 | + { |
| 51 | + $pdo = $this->getAdapter()->getConnection(); |
| 52 | + $row = $this->fetchRow( |
| 53 | + "SELECT |
| 54 | + COLUMN_NAME, |
| 55 | + COLUMN_TYPE, |
| 56 | + DATA_TYPE, |
| 57 | + IS_NULLABLE, |
| 58 | + COLUMN_DEFAULT, |
| 59 | + EXTRA, |
| 60 | + COLUMN_COMMENT, |
| 61 | + CHARACTER_SET_NAME, |
| 62 | + COLLATION_NAME |
| 63 | + FROM INFORMATION_SCHEMA.COLUMNS |
| 64 | + WHERE TABLE_SCHEMA = DATABASE() |
| 65 | + AND TABLE_NAME = " . $pdo->quote($table) . " |
| 66 | + AND COLUMN_NAME = " . $pdo->quote($column) . " |
| 67 | + LIMIT 1" |
| 68 | + ); |
| 69 | + |
| 70 | + return $row ?: null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Builds a column definition fragment: |
| 75 | + * `col` <COLUMN_TYPE> [CHARSET/COLLATE] [NULL/NOT NULL] [DEFAULT ...] [EXTRA] [COMMENT ...] |
| 76 | + */ |
| 77 | + private function buildColumnDefinitionSql(array $spec, string $newName): string |
| 78 | + { |
| 79 | + $pdo = $this->getAdapter()->getConnection(); |
| 80 | + |
| 81 | + $type = (string)$spec['COLUMN_TYPE']; // includes length + unsigned etc. |
| 82 | + $dataType = strtolower((string)$spec['DATA_TYPE']); |
| 83 | + |
| 84 | + $sql = "`{$newName}` {$type}"; |
| 85 | + |
| 86 | + // Preserve charset/collation for character types when present. |
| 87 | + if (!empty($spec['CHARACTER_SET_NAME']) && !empty($spec['COLLATION_NAME'])) { |
| 88 | + $charset = (string)$spec['CHARACTER_SET_NAME']; |
| 89 | + $collation = (string)$spec['COLLATION_NAME']; |
| 90 | + $sql .= " CHARACTER SET {$charset} COLLATE {$collation}"; |
| 91 | + } |
| 92 | + |
| 93 | + $nullable = ((string)$spec['IS_NULLABLE'] === 'YES'); |
| 94 | + $sql .= $nullable ? " NULL" : " NOT NULL"; |
| 95 | + |
| 96 | + // DEFAULT handling |
| 97 | + $default = $spec['COLUMN_DEFAULT']; // can be null |
| 98 | + if ($default === null) { |
| 99 | + if ($nullable) { |
| 100 | + $sql .= " DEFAULT NULL"; |
| 101 | + } |
| 102 | + } else { |
| 103 | + $defaultStr = (string)$default; |
| 104 | + |
| 105 | + $upper = strtoupper($defaultStr); |
| 106 | + $isFuncDefault = in_array($upper, ['CURRENT_TIMESTAMP', 'CURRENT_TIMESTAMP()', 'NOW()', 'LOCALTIME', 'LOCALTIMESTAMP'], true); |
| 107 | + |
| 108 | + if ($isFuncDefault) { |
| 109 | + $sql .= " DEFAULT {$upper}"; |
| 110 | + } else { |
| 111 | + $numericTypes = [ |
| 112 | + 'tinyint','smallint','mediumint','int','integer','bigint', |
| 113 | + 'decimal','numeric','float','double','real', |
| 114 | + 'bit','bool','boolean', |
| 115 | + ]; |
| 116 | + |
| 117 | + if (in_array($dataType, $numericTypes, true)) { |
| 118 | + $sql .= " DEFAULT {$defaultStr}"; |
| 119 | + } else { |
| 120 | + $sql .= " DEFAULT " . $pdo->quote($defaultStr); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (!empty($spec['EXTRA'])) { |
| 126 | + $extra = trim((string)$spec['EXTRA']); |
| 127 | + if ($extra !== '' && stripos($extra, 'auto_increment') === false) { |
| 128 | + $sql .= " {$extra}"; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (isset($spec['COLUMN_COMMENT']) && (string)$spec['COLUMN_COMMENT'] !== '') { |
| 133 | + $sql .= " COMMENT " . $pdo->quote((string)$spec['COLUMN_COMMENT']); |
| 134 | + } |
| 135 | + |
| 136 | + return $sql; |
| 137 | + } |
| 138 | + |
| 139 | + private function addColumnLike(string $targetTable, string $newCol, ?array $sourceSpec, string $afterCol, string $fallbackSql): void |
| 140 | + { |
| 141 | + if ($this->columnExists($targetTable, $newCol)) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + $colSql = $sourceSpec |
| 146 | + ? $this->buildColumnDefinitionSql($sourceSpec, $newCol) |
| 147 | + : "`{$newCol}` {$fallbackSql}"; |
| 148 | + |
| 149 | + $this->execute("ALTER TABLE `{$targetTable}` ADD COLUMN {$colSql} AFTER `{$afterCol}`"); |
| 150 | + } |
| 151 | + |
| 152 | + private function modifyColumnPosition(string $targetTable, string $col, string $afterCol, ?array $spec, string $fallbackSql): void |
| 153 | + { |
| 154 | + if (!$this->columnExists($targetTable, $col)) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + $colSql = $spec |
| 159 | + ? $this->buildColumnDefinitionSql($spec, $col) |
| 160 | + : "`{$col}` {$fallbackSql}"; |
| 161 | + |
| 162 | + $this->execute("ALTER TABLE `{$targetTable}` MODIFY COLUMN {$colSql} AFTER `{$afterCol}`"); |
| 163 | + } |
| 164 | + |
| 165 | + public function up(): void |
| 166 | + { |
| 167 | + $target = 'bxt_battle_tower_honor_roll'; |
| 168 | + if (!$this->tableExists($target)) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + // Anchor after class_decode (or class if decode isn't present). |
| 173 | + $anchor = $this->columnExists($target, 'class_decode') ? 'class_decode' |
| 174 | + : ($this->columnExists($target, 'class') ? 'class' : null); |
| 175 | + |
| 176 | + if ($anchor === null) { |
| 177 | + // Unexpected schema; avoid making a wrong assumption. |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + // Source specs from records table (preferred). |
| 182 | + $source = 'bxt_battle_tower_records'; |
| 183 | + $srcTableOk = $this->tableExists($source); |
| 184 | + |
| 185 | + $specP1 = $srcTableOk ? $this->getColumnSpec($source, 'pokemon1') : null; |
| 186 | + $specP1d = $srcTableOk ? $this->getColumnSpec($source, 'pokemon1_decode') : null; |
| 187 | + $specP2 = $srcTableOk ? $this->getColumnSpec($source, 'pokemon2') : null; |
| 188 | + $specP2d = $srcTableOk ? $this->getColumnSpec($source, 'pokemon2_decode') : null; |
| 189 | + $specP3 = $srcTableOk ? $this->getColumnSpec($source, 'pokemon3') : null; |
| 190 | + $specP3d = $srcTableOk ? $this->getColumnSpec($source, 'pokemon3_decode') : null; |
| 191 | + |
| 192 | + // Reasonable fallbacks if records isn't available: |
| 193 | + // - pokemon# as BLOB nullable |
| 194 | + // - pokemon#_decode as varchar(50) nullable (mirrors typical *_decode pattern) |
| 195 | + $this->addColumnLike($target, 'pokemon1', $specP1, $anchor, "blob NULL"); |
| 196 | + $this->addColumnLike($target, 'pokemon1_decode', $specP1d, 'pokemon1', "varchar(50) NULL DEFAULT NULL"); |
| 197 | + $this->addColumnLike($target, 'pokemon2', $specP2, 'pokemon1_decode', "blob NULL"); |
| 198 | + $this->addColumnLike($target, 'pokemon2_decode', $specP2d, 'pokemon2', "varchar(50) NULL DEFAULT NULL"); |
| 199 | + $this->addColumnLike($target, 'pokemon3', $specP3, 'pokemon2_decode', "blob NULL"); |
| 200 | + $this->addColumnLike($target, 'pokemon3_decode', $specP3d, 'pokemon3', "varchar(50) NULL DEFAULT NULL"); |
| 201 | + |
| 202 | + // If message_start columns already exist (from the prior migration), ensure they sit AFTER pokemon3_decode. |
| 203 | + if ($this->columnExists($target, 'message_start')) { |
| 204 | + $specMs = $this->getColumnSpec($target, 'message_start'); |
| 205 | + $this->modifyColumnPosition($target, 'message_start', 'pokemon3_decode', $specMs, "int(10) unsigned NULL DEFAULT NULL"); |
| 206 | + } |
| 207 | + |
| 208 | + if ($this->columnExists($target, 'message_start_decode')) { |
| 209 | + $specMsd = $this->getColumnSpec($target, 'message_start_decode'); |
| 210 | + $after = $this->columnExists($target, 'message_start') ? 'message_start' : 'pokemon3_decode'; |
| 211 | + $fallback = "varchar(50) NULL DEFAULT NULL"; |
| 212 | + $this->modifyColumnPosition($target, 'message_start_decode', $after, $specMsd, $fallback); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + public function down(): void |
| 217 | + { |
| 218 | + $target = 'bxt_battle_tower_honor_roll'; |
| 219 | + if (!$this->tableExists($target)) { |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + $t = $this->table($target); |
| 224 | + |
| 225 | + foreach (['pokemon3_decode','pokemon3','pokemon2_decode','pokemon2','pokemon1_decode','pokemon1'] as $col) { |
| 226 | + if ($this->columnExists($target, $col)) { |
| 227 | + $t->removeColumn($col); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + $t->update(); |
| 232 | + |
| 233 | + // Optional: re-anchor message_start back after class_decode/class if it exists. |
| 234 | + $anchor = $this->columnExists($target, 'class_decode') ? 'class_decode' |
| 235 | + : ($this->columnExists($target, 'class') ? 'class' : null); |
| 236 | + |
| 237 | + if ($anchor !== null) { |
| 238 | + if ($this->columnExists($target, 'message_start')) { |
| 239 | + $specMs = $this->getColumnSpec($target, 'message_start'); |
| 240 | + $this->modifyColumnPosition($target, 'message_start', $anchor, $specMs, "int(10) unsigned NULL DEFAULT NULL"); |
| 241 | + } |
| 242 | + if ($this->columnExists($target, 'message_start_decode')) { |
| 243 | + $specMsd = $this->getColumnSpec($target, 'message_start_decode'); |
| 244 | + $after = $this->columnExists($target, 'message_start') ? 'message_start' : $anchor; |
| 245 | + $this->modifyColumnPosition($target, 'message_start_decode', $after, $specMsd, "varchar(50) NULL DEFAULT NULL"); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments