Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rendering of column attributes #58

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/Atomizer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,23 @@ private function columnOptions(AbstractColumn $column): array
{
$options = [
'nullable' => $column->isNullable(),
'default' => $column->getDefaultValue(),
'defaultValue' => $column->getDefaultValue(),
];

if ($column->getAbstractType() === 'enum') {
$options['values'] = $column->getEnumValues();
}

if ($column->getAbstractType() === 'string' || $column->getSize() > 0) {
$options['size'] = $column->getSize();
foreach ($column->getAttributes() as $attribute => $value) {
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
if ($attribute === 'size' && $value === 0) {
continue;
}
$options[$attribute] = $value;
}

if ($column->getAbstractType() === 'decimal') {
$options['scale'] = $column->getScale();
$options['precision'] = $column->getPrecision();
}

$default = $options['default'];
$default = $options['defaultValue'];
if ($column::DATETIME_NOW === ($default instanceof \Stringable ? (string)$default : $default)) {
$options['default'] = AbstractColumn::DATETIME_NOW;
$options['defaultValue'] = AbstractColumn::DATETIME_NOW;
}

return $options;
Expand Down
4 changes: 2 additions & 2 deletions src/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getMigrations(): array
$migrations = [];

foreach ($this->getFilesIterator() as $f) {
if (! \class_exists($f['class'], false)) {
if (!\class_exists($f['class'], false)) {
//Attempting to load migration class (we can not relay on autoloading here)
require_once($f['filename']);
}
Expand All @@ -73,7 +73,7 @@ public function getMigrations(): array

public function registerMigration(string $name, string $class, string $body = null): string
{
if (empty($body) && ! \class_exists($class)) {
if (empty($body) && !\class_exists($class)) {
throw new RepositoryException(
"Unable to register migration '{$class}', representing class does not exists"
);
Expand Down
5 changes: 4 additions & 1 deletion src/Operation/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn
$column->nullable($this->getOption('nullable', false));
$column->setAttributes($this->options + $column->getAttributes());

if ($this->hasOption('default')) {
if ($this->hasOption('defaultValue')) {
$column->defaultValue($this->getOption('defaultValue', null));
// @deprecated since v4.3
} elseif ($this->hasOption('default')) {
$column->defaultValue($this->getOption('default', null));
}

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'));
}
}
Loading