Skip to content

Commit 519609a

Browse files
authored
Add rendering of column attributes (#58)
1 parent d12f436 commit 519609a

File tree

4 files changed

+110
-13
lines changed

4 files changed

+110
-13
lines changed

src/Atomizer/Renderer.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,23 @@ private function columnOptions(AbstractColumn $column): array
244244
{
245245
$options = [
246246
'nullable' => $column->isNullable(),
247-
'default' => $column->getDefaultValue(),
247+
'defaultValue' => $column->getDefaultValue(),
248248
];
249249

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

254-
if ($column->getAbstractType() === 'string' || $column->getSize() > 0) {
255-
$options['size'] = $column->getSize();
254+
foreach ($column->getAttributes() as $attribute => $value) {
255+
if ($attribute === 'size' && $value === 0) {
256+
continue;
257+
}
258+
$options[$attribute] = $value;
256259
}
257260

258-
if ($column->getAbstractType() === 'decimal') {
259-
$options['scale'] = $column->getScale();
260-
$options['precision'] = $column->getPrecision();
261-
}
262-
263-
$default = $options['default'];
261+
$default = $options['defaultValue'];
264262
if ($column::DATETIME_NOW === ($default instanceof \Stringable ? (string)$default : $default)) {
265-
$options['default'] = AbstractColumn::DATETIME_NOW;
263+
$options['defaultValue'] = AbstractColumn::DATETIME_NOW;
266264
}
267265

268266
return $options;

src/FileRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getMigrations(): array
5353
$migrations = [];
5454

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

7474
public function registerMigration(string $name, string $class, string $body = null): string
7575
{
76-
if (empty($body) && ! \class_exists($class)) {
76+
if (empty($body) && !\class_exists($class)) {
7777
throw new RepositoryException(
7878
"Unable to register migration '{$class}', representing class does not exists"
7979
);

src/Operation/Column/Column.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn
7171
$column->nullable($this->getOption('nullable', false));
7272
$column->setAttributes($this->options + $column->getAttributes());
7373

74-
if ($this->hasOption('default')) {
74+
if ($this->hasOption('defaultValue')) {
75+
$column->defaultValue($this->getOption('defaultValue', null));
76+
// @deprecated since v4.3
77+
} elseif ($this->hasOption('default')) {
7578
$column->defaultValue($this->getOption('default', null));
7679
}
7780

tests/Migrations/MySQL/AtomizerTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,100 @@ public function testChangeBinaryColumnSize(): void
4545
$this->migrator->rollback();
4646
$this->assertFalse($this->db->hasTable('sample'));
4747
}
48+
49+
public function testUnsigned(): void
50+
{
51+
$this->migrator->configure();
52+
53+
$schema = $this->schema('sample');
54+
$schema->primary('id');
55+
$schema->integer('int');
56+
$schema->integer('int_unsigned', unsigned: true);
57+
$schema->tinyInteger('tiny_integer_unsigned', unsigned: true);
58+
$schema->smallInteger('small_integer_unsigned', unsigned: true);
59+
$schema->smallInteger('big_integer_unsigned', unsigned: true);
60+
$this->atomize('migration1', [$schema]);
61+
62+
$this->migrator->run();
63+
$this->assertFalse($this->schema('sample')->column('int')->isUnsigned());
64+
$this->assertTrue($this->schema('sample')->column('int_unsigned')->isUnsigned());
65+
$this->assertTrue($this->schema('sample')->column('tiny_integer_unsigned')->isUnsigned());
66+
$this->assertTrue($this->schema('sample')->column('small_integer_unsigned')->isUnsigned());
67+
$this->assertTrue($this->schema('sample')->column('big_integer_unsigned')->isUnsigned());
68+
69+
$schema = $this->schema('sample');
70+
$schema->integer('int', unsigned: true);
71+
$schema->integer('int_unsigned', unsigned: false);
72+
$schema->tinyInteger('tiny_integer_unsigned', unsigned: false);
73+
$schema->smallInteger('small_integer_unsigned', unsigned: false);
74+
$schema->smallInteger('big_integer_unsigned', unsigned: false);
75+
$this->atomize('migration2', [$schema]);
76+
77+
$this->migrator->run();
78+
79+
$this->assertTrue($this->schema('sample')->column('int')->isUnsigned());
80+
$this->assertFalse($this->schema('sample')->column('int_unsigned')->isUnsigned());
81+
$this->assertFalse($this->schema('sample')->column('tiny_integer_unsigned')->isUnsigned());
82+
$this->assertFalse($this->schema('sample')->column('small_integer_unsigned')->isUnsigned());
83+
$this->assertFalse($this->schema('sample')->column('big_integer_unsigned')->isUnsigned());
84+
85+
$this->migrator->rollback();
86+
87+
$this->assertFalse($this->schema('sample')->column('int')->isUnsigned());
88+
$this->assertTrue($this->schema('sample')->column('int_unsigned')->isUnsigned());
89+
$this->assertTrue($this->schema('sample')->column('tiny_integer_unsigned')->isUnsigned());
90+
$this->assertTrue($this->schema('sample')->column('small_integer_unsigned')->isUnsigned());
91+
$this->assertTrue($this->schema('sample')->column('big_integer_unsigned')->isUnsigned());
92+
93+
$this->migrator->rollback();
94+
$this->assertFalse($this->db->hasTable('sample'));
95+
}
96+
97+
public function testZerofill(): void
98+
{
99+
$this->migrator->configure();
100+
101+
$schema = $this->schema('sample');
102+
$schema->primary('id');
103+
$schema->integer('int');
104+
$schema->integer('int_zerofill', zerofill: true);
105+
$schema->tinyInteger('tiny_integer_zerofill', zerofill: true);
106+
$schema->smallInteger('small_integer_zerofill', zerofill: true);
107+
$schema->smallInteger('big_integer_zerofill', zerofill: true);
108+
$this->atomize('migration1', [$schema]);
109+
110+
$this->migrator->run();
111+
$this->assertFalse($this->schema('sample')->column('int')->isZerofill());
112+
$this->assertTrue($this->schema('sample')->column('int_zerofill')->isZerofill());
113+
$this->assertTrue($this->schema('sample')->column('tiny_integer_zerofill')->isZerofill());
114+
$this->assertTrue($this->schema('sample')->column('small_integer_zerofill')->isZerofill());
115+
$this->assertTrue($this->schema('sample')->column('big_integer_zerofill')->isZerofill());
116+
117+
$schema = $this->schema('sample');
118+
$schema->integer('int', zerofill: true);
119+
$schema->integer('int_zerofill', zerofill: false);
120+
$schema->tinyInteger('tiny_integer_zerofill', zerofill: false);
121+
$schema->smallInteger('small_integer_zerofill', zerofill: false);
122+
$schema->smallInteger('big_integer_zerofill', zerofill: false);
123+
$this->atomize('migration2', [$schema]);
124+
125+
$this->migrator->run();
126+
127+
$this->assertTrue($this->schema('sample')->column('int')->isZerofill());
128+
$this->assertFalse($this->schema('sample')->column('int_zerofill')->isZerofill());
129+
$this->assertFalse($this->schema('sample')->column('tiny_integer_zerofill')->isZerofill());
130+
$this->assertFalse($this->schema('sample')->column('small_integer_zerofill')->isZerofill());
131+
$this->assertFalse($this->schema('sample')->column('big_integer_zerofill')->isZerofill());
132+
133+
$this->migrator->rollback();
134+
135+
$this->assertFalse($this->schema('sample')->column('int')->isZerofill());
136+
$this->assertTrue($this->schema('sample')->column('int_zerofill')->isZerofill());
137+
$this->assertTrue($this->schema('sample')->column('tiny_integer_zerofill')->isZerofill());
138+
$this->assertTrue($this->schema('sample')->column('small_integer_zerofill')->isZerofill());
139+
$this->assertTrue($this->schema('sample')->column('big_integer_zerofill')->isZerofill());
140+
141+
$this->migrator->rollback();
142+
$this->assertFalse($this->db->hasTable('sample'));
143+
}
48144
}

0 commit comments

Comments
 (0)