diff --git a/composer.json b/composer.json index 71dfcab..b9357b6 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "php": ">=8.1", - "cycle/database": "^2.5", + "cycle/database": "^2.6.2", "spiral/core": "^3.0", "spiral/files": "^3.0", "spiral/tokenizer": "^3.0", diff --git a/src/Atomizer/Renderer.php b/src/Atomizer/Renderer.php index d313dbd..a200193 100644 --- a/src/Atomizer/Renderer.php +++ b/src/Atomizer/Renderer.php @@ -251,7 +251,7 @@ private function columnOptions(AbstractColumn $column): array $options['values'] = $column->getEnumValues(); } - if ($column->getAbstractType() === 'string') { + if ($column->getAbstractType() === 'string' || $column->getSize() > 0) { $options['size'] = $column->getSize(); } diff --git a/src/Operation/Column/Column.php b/src/Operation/Column/Column.php index dd9dd6f..8dbfe3f 100644 --- a/src/Operation/Column/Column.php +++ b/src/Operation/Column/Column.php @@ -69,6 +69,7 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn } $column->nullable($this->getOption('nullable', false)); + $column->setAttributes($this->options + $column->getAttributes()); if ($this->hasOption('default')) { $column->defaultValue($this->getOption('default', null)); diff --git a/tests/Migrations/AtomizerTest.php b/tests/Migrations/AtomizerTest.php index 438fdb3..7575aeb 100644 --- a/tests/Migrations/AtomizerTest.php +++ b/tests/Migrations/AtomizerTest.php @@ -313,7 +313,7 @@ public function testCreateDatetimeNowColumn(): void $this->migrator->configure(); $schema = $this->schema('sample'); - $column = $schema->datetime('value', size: 2, foo: 'bar'); + $column = $schema->datetime('value'); $column->defaultValue(new Fragment($column::DATETIME_NOW)); $this->atomize('migration1', [$schema]); diff --git a/tests/Migrations/MySQL/AtomizerTest.php b/tests/Migrations/MySQL/AtomizerTest.php index 82c8716..019a4e0 100644 --- a/tests/Migrations/MySQL/AtomizerTest.php +++ b/tests/Migrations/MySQL/AtomizerTest.php @@ -14,4 +14,35 @@ class AtomizerTest extends \Cycle\Migrations\Tests\AtomizerTest { public const DRIVER = 'mysql'; + + public function testChangeBinaryColumnSize(): void + { + $this->migrator->configure(); + + $schema = $this->schema('sample'); + $schema->primary('id'); + $schema->binary('value', size: 16); + $this->atomize('migration1', [$schema]); + + $this->migrator->run(); + $this->assertSame(16, $this->schema('sample')->column('value')->getSize()); + + $schema = $this->schema('sample'); + $schema->binary('value', size: 255); + $this->atomize('migration2', [$schema]); + + $this->migrator->run(); + + $this->assertSame(255, $this->schema('sample')->column('value')->getSize()); + + $this->assertTrue($this->db->hasTable('sample')); + + $this->migrator->rollback(); + $this->assertSame(16, $this->schema('sample')->column('value')->getSize()); + + $this->assertTrue($this->db->hasTable('sample')); + + $this->migrator->rollback(); + $this->assertFalse($this->db->hasTable('sample')); + } } diff --git a/tests/Migrations/Postgres/AtomizerTest.php b/tests/Migrations/Postgres/AtomizerTest.php index adaf8a5..10edacd 100644 --- a/tests/Migrations/Postgres/AtomizerTest.php +++ b/tests/Migrations/Postgres/AtomizerTest.php @@ -14,4 +14,35 @@ class AtomizerTest extends \Cycle\Migrations\Tests\AtomizerTest { public const DRIVER = 'postgres'; + + public function testChangeBinaryColumnSize(): void + { + $this->migrator->configure(); + + $schema = $this->schema('sample'); + $schema->primary('id'); + $schema->bit('value', size: 16); + $this->atomize('migration1', [$schema]); + + $this->migrator->run(); + $this->assertSame(16, $this->schema('sample')->column('value')->getSize()); + + $schema = $this->schema('sample'); + $schema->bit('value', size: 255); + $this->atomize('migration2', [$schema]); + + $this->migrator->run(); + + $this->assertSame(255, $this->schema('sample')->column('value')->getSize()); + + $this->assertTrue($this->db->hasTable('sample')); + + $this->migrator->rollback(); + $this->assertSame(16, $this->schema('sample')->column('value')->getSize()); + + $this->assertTrue($this->db->hasTable('sample')); + + $this->migrator->rollback(); + $this->assertFalse($this->db->hasTable('sample')); + } } diff --git a/tests/Migrations/SQLServer/AtomizerTest.php b/tests/Migrations/SQLServer/AtomizerTest.php index dee1789..8fe1f83 100644 --- a/tests/Migrations/SQLServer/AtomizerTest.php +++ b/tests/Migrations/SQLServer/AtomizerTest.php @@ -14,4 +14,35 @@ class AtomizerTest extends \Cycle\Migrations\Tests\AtomizerTest { public const DRIVER = 'sqlserver'; + + public function testChangeBinaryColumnSize(): void + { + $this->migrator->configure(); + + $schema = $this->schema('sample'); + $schema->primary('id'); + $schema->binary('value', size: 16); + $this->atomize('migration1', [$schema]); + + $this->migrator->run(); + $this->assertSame(16, $this->schema('sample')->column('value')->getSize()); + + $schema = $this->schema('sample'); + $schema->binary('value', size: 255); + $this->atomize('migration2', [$schema]); + + $this->migrator->run(); + + $this->assertSame(255, $this->schema('sample')->column('value')->getSize()); + + $this->assertTrue($this->db->hasTable('sample')); + + $this->migrator->rollback(); + $this->assertSame(16, $this->schema('sample')->column('value')->getSize()); + + $this->assertTrue($this->db->hasTable('sample')); + + $this->migrator->rollback(); + $this->assertFalse($this->db->hasTable('sample')); + } }