Skip to content

Commit 06060d0

Browse files
committed
fix(Migration): bug lors du renommage d'un champ d'une table
1 parent 09f19f5 commit 06060d0

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/Creator/BaseCreator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,32 @@ public function modifyColumn(string $table, array|string $field): bool
806806
return true;
807807
}
808808

809+
/**
810+
* Renomme une colonne dans la table spécifiée.
811+
*
812+
* Cette fonction renomme une colonne de la table donnée de son nom actuel à un nouveau nom.
813+
* Elle récupère les propriétés de la colonne existante et les utilise pour modifier la colonne avec le nouveau nom.
814+
*
815+
* @throws RuntimeException Si la colonne spécifiée n'existe pas dans la table.
816+
*/
817+
public function renameColumn(string $table, string $from, string $to): bool
818+
{
819+
$field = array_filter($this->db->getFieldData($table), fn($field) => $field->name === $from);
820+
$field = array_shift($field);
821+
822+
if (null === $field) {
823+
throw new RuntimeException(sprintf("La colonne %s n'existe pas dans la table %s", $from, $table));
824+
}
825+
826+
return $this->modifyColumn($table, [$from => [
827+
'name' => $to,
828+
'type' => strtoupper($field->type),
829+
'constraint' => $field->max_length,
830+
'null' => $field->nullable,
831+
'default' => $field->default,
832+
]]);
833+
}
834+
809835
/**
810836
* @return false|string|string[]
811837
*/

src/Migration/Transformer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ public function modifyTable(Structure $structure, array $commands = []): void
142142
if ($command->name === 'dropColumn') {
143143
$this->creator->dropColumn($table, $command->columns);
144144
} elseif ($command->name === 'renameColumn') {
145-
$this->creator->modifyColumn($table, [
146-
$command->from => array_merge(['name' => $command->to], $this->makeColumn($command)),
147-
]);
145+
$this->creator->renameColumn($table, $command->from, $command->to);
148146
} elseif ($command->name === 'dropIndex') {
149147
$this->creator->dropKey($table, $command->columns);
150148
} elseif ($command->name === 'dropUnique') {

0 commit comments

Comments
 (0)