Skip to content

Commit

Permalink
Add the ability to define size for binary
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Nov 28, 2023
1 parent 9cad826 commit ef49ac2
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Atomizer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions src/Operation/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion tests/Migrations/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
31 changes: 31 additions & 0 deletions tests/Migrations/MySQL/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
31 changes: 31 additions & 0 deletions tests/Migrations/Postgres/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
31 changes: 31 additions & 0 deletions tests/Migrations/SQLServer/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit ef49ac2

Please sign in to comment.