From ef49ac2113c4b727448f55aa2851d225f4641a1a Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 28 Nov 2023 17:03:42 +0200 Subject: [PATCH 1/2] Add the ability to define size for binary --- composer.json | 2 +- src/Atomizer/Renderer.php | 2 +- src/Operation/Column/Column.php | 1 + tests/Migrations/AtomizerTest.php | 2 +- tests/Migrations/MySQL/AtomizerTest.php | 31 +++++++++++++++++++++ tests/Migrations/Postgres/AtomizerTest.php | 31 +++++++++++++++++++++ tests/Migrations/SQLServer/AtomizerTest.php | 31 +++++++++++++++++++++ 7 files changed, 97 insertions(+), 3 deletions(-) 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')); + } } From 5463cd3a8f9c085b53ecd2f353c11d57276b46a4 Mon Sep 17 00:00:00 2001 From: Aleksei Gagarin Date: Mon, 4 Dec 2023 20:44:39 +0400 Subject: [PATCH 2/2] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b9357b6..5a13d8c 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "php": ">=8.1", - "cycle/database": "^2.6.2", + "cycle/database": "^2.7.0", "spiral/core": "^3.0", "spiral/files": "^3.0", "spiral/tokenizer": "^3.0",