Skip to content

Commit

Permalink
fix: does not modify the data if the type does not change
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Jul 26, 2022
1 parent fc79e7b commit c2af335
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,18 @@ protected static function modify(
Field $field,
array &$tableData
): void {
$oldField = $table->getField($field->getName());

$table->addField($field);

/**
* Si la modification ne concerne pas le type, la mise à jour des données ne se fait pas.
* Exemple: rendre un champ nullable ne doit écraser les données présentent en table.
*/
if ($oldField::TYPE === $field::TYPE) {
return;
}

$increment = $field instanceof IncrementType
? 0
: null;
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/SchemaJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,15 @@ public function testAlterTableRename(): void
public function testAlterTableModify(): void
{
$this->bdd->alterTable('test_second', static function (TableAlter $table): void {
$table->string('value_s')->nullable()->modify();
$table->float('value_i')->valueDefault(1.0)->modify();
});

self::assertEquals(
[
'fields' => [
'value_i' => [ 'type' => 'float', 'default' => 1.0 ],
'value_s' => [ 'type' => 'string', 'length' => 255 ]
'value_s' => [ 'type' => 'string', 'length' => 255, 'nullable' => true ]
],
'increments' => null
],
Expand Down

0 comments on commit c2af335

Please sign in to comment.