Skip to content

Commit

Permalink
Add rendering of column attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Jan 23, 2024
1 parent 9392f57 commit 04cc037
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Atomizer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class Renderer implements RendererInterface
*/
public const NEW_STATE = 0;
public const ORIGINAL_STATE = 1;
private const ALLOWED_ATTRIBUTES = ['scale', 'precision', 'unsigned', 'zerofill'];

public function createTable(Method $method, AbstractTable $table): void
{
Expand Down Expand Up @@ -255,9 +256,10 @@ private function columnOptions(AbstractColumn $column): array
$options['size'] = $column->getSize();
}

if ($column->getAbstractType() === 'decimal') {
$options['scale'] = $column->getScale();
$options['precision'] = $column->getPrecision();
foreach ($column->getAttributes() as $attribute => $value) {
if (\in_array($attribute, self::ALLOWED_ATTRIBUTES, true)) {
$options[$attribute] = $value;
}
}

$default = $options['default'];
Expand Down
96 changes: 96 additions & 0 deletions tests/Migrations/MySQL/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,100 @@ public function testChangeBinaryColumnSize(): void
$this->migrator->rollback();
$this->assertFalse($this->db->hasTable('sample'));
}

public function testUnsigned(): void
{
$this->migrator->configure();

$schema = $this->schema('sample');
$schema->primary('id');
$schema->integer('int');
$schema->integer('int_unsigned', unsigned: true);
$schema->tinyInteger('tiny_integer_unsigned', unsigned: true);
$schema->smallInteger('small_integer_unsigned', unsigned: true);
$schema->smallInteger('big_integer_unsigned', unsigned: true);
$this->atomize('migration1', [$schema]);

$this->migrator->run();
$this->assertFalse($this->schema('sample')->column('int')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('int_unsigned')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('tiny_integer_unsigned')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('small_integer_unsigned')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('big_integer_unsigned')->isUnsigned());

$schema = $this->schema('sample');
$schema->integer('int', unsigned: true);
$schema->integer('int_unsigned', unsigned: false);
$schema->tinyInteger('tiny_integer_unsigned', unsigned: false);
$schema->smallInteger('small_integer_unsigned', unsigned: false);
$schema->smallInteger('big_integer_unsigned', unsigned: false);
$this->atomize('migration2', [$schema]);

$this->migrator->run();

$this->assertTrue($this->schema('sample')->column('int')->isUnsigned());
$this->assertFalse($this->schema('sample')->column('int_unsigned')->isUnsigned());
$this->assertFalse($this->schema('sample')->column('tiny_integer_unsigned')->isUnsigned());
$this->assertFalse($this->schema('sample')->column('small_integer_unsigned')->isUnsigned());
$this->assertFalse($this->schema('sample')->column('big_integer_unsigned')->isUnsigned());

$this->migrator->rollback();

$this->assertFalse($this->schema('sample')->column('int')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('int_unsigned')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('tiny_integer_unsigned')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('small_integer_unsigned')->isUnsigned());
$this->assertTrue($this->schema('sample')->column('big_integer_unsigned')->isUnsigned());

$this->migrator->rollback();
$this->assertFalse($this->db->hasTable('sample'));
}

public function testZerofill(): void
{
$this->migrator->configure();

$schema = $this->schema('sample');
$schema->primary('id');
$schema->integer('int');
$schema->integer('int_zerofill', zerofill: true);
$schema->tinyInteger('tiny_integer_zerofill', zerofill: true);
$schema->smallInteger('small_integer_zerofill', zerofill: true);
$schema->smallInteger('big_integer_zerofill', zerofill: true);
$this->atomize('migration1', [$schema]);

$this->migrator->run();
$this->assertFalse($this->schema('sample')->column('int')->isZerofill());
$this->assertTrue($this->schema('sample')->column('int_zerofill')->isZerofill());
$this->assertTrue($this->schema('sample')->column('tiny_integer_zerofill')->isZerofill());
$this->assertTrue($this->schema('sample')->column('small_integer_zerofill')->isZerofill());
$this->assertTrue($this->schema('sample')->column('big_integer_zerofill')->isZerofill());

$schema = $this->schema('sample');
$schema->integer('int', zerofill: true);
$schema->integer('int_zerofill', zerofill: false);
$schema->tinyInteger('tiny_integer_zerofill', zerofill: false);
$schema->smallInteger('small_integer_zerofill', zerofill: false);
$schema->smallInteger('big_integer_zerofill', zerofill: false);
$this->atomize('migration2', [$schema]);

$this->migrator->run();

$this->assertTrue($this->schema('sample')->column('int')->isZerofill());
$this->assertFalse($this->schema('sample')->column('int_zerofill')->isZerofill());
$this->assertFalse($this->schema('sample')->column('tiny_integer_zerofill')->isZerofill());
$this->assertFalse($this->schema('sample')->column('small_integer_zerofill')->isZerofill());
$this->assertFalse($this->schema('sample')->column('big_integer_zerofill')->isZerofill());

$this->migrator->rollback();

$this->assertFalse($this->schema('sample')->column('int')->isZerofill());
$this->assertTrue($this->schema('sample')->column('int_zerofill')->isZerofill());
$this->assertTrue($this->schema('sample')->column('tiny_integer_zerofill')->isZerofill());
$this->assertTrue($this->schema('sample')->column('small_integer_zerofill')->isZerofill());
$this->assertTrue($this->schema('sample')->column('big_integer_zerofill')->isZerofill());

$this->migrator->rollback();
$this->assertFalse($this->db->hasTable('sample'));
}
}

0 comments on commit 04cc037

Please sign in to comment.