Skip to content

Commit 580f43b

Browse files
author
Bizley
authored
bugfix/default-length-for-columns (#96)
1 parent c4eb98c commit 580f43b

21 files changed

+118
-45
lines changed

src/Comparator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ private function compareColumns(
151151
continue;
152152
}
153153

154+
if (
155+
$propertyFetch === 'getDefault'
156+
&& $schema === Schema::SQLITE
157+
&& $newProperty === 'NULL'
158+
&& $oldProperty === null
159+
) {
160+
continue;
161+
}
162+
154163
$blueprint->addDescription(
155164
"different '$name' column property: $propertyName ("
156165
. 'DB: ' . $this->stringifyValue($newProperty) . ' != '

src/TableMapper.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
use yii\db\Connection;
2121
use yii\db\Constraint;
2222
use yii\db\cubrid\Schema as CubridSchema;
23-
use yii\db\ForeignKeyConstraint;
24-
use yii\db\IndexConstraint;
2523
use yii\db\mssql\Schema as MssqlSchema;
2624
use yii\db\mysql\Schema as MysqlSchema;
2725
use yii\db\oci\Schema as OciSchema;
2826
use yii\db\pgsql\Schema as PgsqlSchema;
2927
use yii\db\sqlite\Schema as SqliteSchema;
3028
use yii\db\TableSchema;
3129

30+
use function count;
31+
use function in_array;
32+
3233
final class TableMapper implements TableMapperInterface
3334
{
3435
/** @var Connection */
@@ -86,7 +87,6 @@ private function getForeignKeys(string $table): array
8687
$schema = $this->db->getSchema();
8788
$tableForeignKeys = $schema->getTableForeignKeys($table, true);
8889

89-
/** @var ForeignKeyConstraint $foreignKey */
9090
foreach ($tableForeignKeys as $foreignKey) {
9191
$mappedForeignKey = new ForeignKey();
9292
$mappedForeignKey->setTableName($table);
@@ -116,7 +116,6 @@ private function getIndexes(string $table): array
116116
$schema = $this->db->getSchema();
117117
$tableIndexes = $schema->getTableIndexes($table, true);
118118

119-
/** @var IndexConstraint $index */
120119
foreach ($tableIndexes as $index) {
121120
if ($index->isPrimary === false) {
122121
$mappedIndex = new Index();
@@ -159,6 +158,7 @@ private function getPrimaryKey(string $table): ?PrimaryKeyInterface
159158
* @param string $table
160159
* @param array<IndexInterface> $indexes
161160
* @return array<string, ColumnInterface>
161+
* @throws NotSupportedException
162162
*/
163163
private function getColumns(string $table, array $indexes = []): array
164164
{
@@ -167,6 +167,8 @@ private function getColumns(string $table, array $indexes = []): array
167167
if ($tableSchema === null) {
168168
return [];
169169
}
170+
$schema = $this->getSchemaType();
171+
$engineVersion = $this->getEngineVersion();
170172

171173
foreach ($tableSchema->columns as $column) {
172174
$isUnique = false;
@@ -182,9 +184,17 @@ private function getColumns(string $table, array $indexes = []): array
182184

183185
$mappedColumn = ColumnFactory::build($column->type);
184186
$mappedColumn->setName($column->name);
185-
$mappedColumn->setSize($column->size);
186-
$mappedColumn->setPrecision($column->precision);
187-
$mappedColumn->setScale($column->scale);
187+
if ($column->size === null && $column->precision === null && $column->scale === null) {
188+
$mappedColumn->setLength(
189+
Schema::getDefaultLength($schema, $mappedColumn->getType(), $engineVersion),
190+
$schema,
191+
$engineVersion
192+
);
193+
} else {
194+
$mappedColumn->setSize($column->size);
195+
$mappedColumn->setPrecision($column->precision);
196+
$mappedColumn->setScale($column->scale);
197+
}
188198
$mappedColumn->setNotNull($column->allowNull ? null : true);
189199
$mappedColumn->setUnique($isUnique);
190200
$mappedColumn->setDefault($column->defaultValue);

src/controllers/MigrationController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
use yii\helpers\Console;
2323
use yii\helpers\FileHelper;
2424

25+
use function array_column;
2526
use function array_merge;
27+
use function array_unique;
2628
use function count;
2729
use function explode;
2830
use function file_put_contents;
@@ -34,25 +36,24 @@
3436
use function preg_match;
3537
use function sort;
3638
use function sprintf;
39+
use function str_replace;
3740
use function strlen;
3841
use function strpos;
3942
use function trim;
40-
use function array_column;
41-
use function str_replace;
4243

4344
/**
4445
* Migration creator and updater.
4546
* Generates migration files based on the existing database table and previous migrations.
4647
*
4748
* @author Paweł Bizley Brzozowski
48-
* @version 4.0.1
49+
* @version 4.0.2
4950
* @license Apache 2.0
5051
* https://github.com/bizley/yii2-migration
5152
*/
5253
class MigrationController extends BaseMigrationController
5354
{
5455
/** @var string */
55-
private $version = '4.0.1';
56+
private $version = '4.0.2';
5657

5758
/**
5859
* @var string|array<string> Directory storing the migration classes.
@@ -410,7 +411,7 @@ public function actionUpdate(string $inputTable): int
410411

411412
$this->stdout("\n");
412413
} else {
413-
$this->stdout("DONE!\n");
414+
$this->stdout("DONE!\n", Console::FG_GREEN);
414415
}
415416
} catch (NotSupportedException $exception) {
416417
$this->stdout(

src/table/Column.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ abstract class Column
2121
/** @var string */
2222
private $type;
2323

24-
/** @var bool|null */
25-
private $notNull;
24+
/** @var bool */
25+
private $notNull = false;
2626

2727
/** @var int|string|null */
2828
private $size;
@@ -111,7 +111,8 @@ public function isNotNull(): ?bool
111111
*/
112112
public function setNotNull(?bool $notNull): void
113113
{
114-
$this->notNull = $notNull;
114+
// this makes sure notNull is strictly `boolean` without introducing BC break for this method
115+
$this->notNull = $notNull ?? false;
115116
}
116117

117118
/**

tests/functional/DbLoaderTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected function addBase(): void
102102
'id' => $this->primaryKey(),
103103
'col' => $this->integer(),
104104
'col2' => $this->string(),
105+
'col3' => $this->timestamp()->defaultValue(null)
105106
]
106107
);
107108

tests/functional/UpdaterTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function shouldUpdateTableByAddingColumn(): void
9797
$this->assertStringContainsString(
9898
'public function up()
9999
{
100-
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->after(\'col2\'));
100+
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->after(\'col3\'));
101101
}
102102
103103
public function down()
@@ -237,4 +237,16 @@ public function down()
237237
MigrationControllerStub::$content
238238
);
239239
}
240+
241+
/**
242+
* @test
243+
* @throws ConsoleException
244+
* @throws InvalidRouteException
245+
* @throws Exception
246+
*/
247+
public function shouldNotUpdateTableWithTimestampColumnWhenItsNotChanged(): void
248+
{
249+
$this->assertEquals(ExitCode::OK, $this->controller->runAction('update', ['updater_base']));
250+
$this->assertSame('', MigrationControllerStub::$content);
251+
}
240252
}

tests/functional/mysql/GeneratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ public function shouldGenerateNonGeneralSchemaTable(): void
116116
\'col_bool\' => $this->tinyInteger(1),
117117
\'col_char\' => $this->char(1),
118118
\'col_date\' => $this->date(),
119-
\'col_date_time\' => $this->dateTime(),
119+
\'col_date_time\' => $this->dateTime(0),
120120
\'col_decimal\' => $this->decimal(10, 0),
121121
\'col_double\' => $this->double(),
122122
\'col_float\' => $this->float(),
123123
\'col_money\' => $this->decimal(19, 4),
124124
\'col_string\' => $this->string(255),
125125
\'col_text\' => $this->text(),
126-
\'col_time\' => $this->time(),
127-
\'col_timestamp\' => $this->timestamp()->notNull()->defaultExpression(\'CURRENT_TIMESTAMP\'),
126+
\'col_time\' => $this->time(0),
127+
\'col_timestamp\' => $this->timestamp(0)->notNull()->defaultExpression(\'CURRENT_TIMESTAMP\'),
128128
\'col_json\' => $this->json(),
129129
],
130130
$tableOptions

tests/functional/mysql/UpdaterPkShowTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function shouldShowUpdateTableByAddingPrimaryKey(): void
3232
$this->assertEquals(ExitCode::OK, $this->controller->runAction('update', ['no_pk']));
3333
$this->assertStringContainsString(
3434
' > Comparing current table \'no_pk\' with its migrations ...Showing differences:
35-
- different \'col\' column property: not null (DB: TRUE != MIG: NULL)
35+
- different \'col\' column property: not null (DB: TRUE != MIG: FALSE)
3636
- different primary key definition
3737
3838
No files generated.',
@@ -54,7 +54,7 @@ public function shouldShowUpdateTableByDroppingPrimaryKey(): void
5454
$this->assertEquals(ExitCode::OK, $this->controller->runAction('update', ['string_pk']));
5555
$this->assertStringContainsString(
5656
' > Comparing current table \'string_pk\' with its migrations ...Showing differences:
57-
- different \'col\' column property: not null (DB: TRUE != MIG: NULL)
57+
- different \'col\' column property: not null (DB: TRUE != MIG: FALSE)
5858
- different \'col\' column property: append (DB: NULL != MIG: "PRIMARY KEY")
5959
- different primary key definition
6060
@@ -77,8 +77,8 @@ public function shouldShowUpdateTableByAddingCompositePrimaryKey(): void
7777
$this->assertEquals(ExitCode::OK, $this->controller->runAction('update', ['no_pk']));
7878
$this->assertStringContainsString(
7979
' > Comparing current table \'no_pk\' with its migrations ...Showing differences:
80-
- different \'col\' column property: not null (DB: TRUE != MIG: NULL)
81-
- different \'col2\' column property: not null (DB: TRUE != MIG: NULL)
80+
- different \'col\' column property: not null (DB: TRUE != MIG: FALSE)
81+
- different \'col2\' column property: not null (DB: TRUE != MIG: FALSE)
8282
- different primary key definition
8383
8484
No files generated.',

tests/functional/mysql/UpdaterShowTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function shouldShowUpdateTableByAlteringColumnWithNotNull(): void
148148
$this->assertEquals(ExitCode::OK, $this->controller->runAction('update', ['updater_base']));
149149
$this->assertStringContainsString(
150150
' > Comparing current table \'updater_base\' with its migrations ...Showing differences:
151-
- different \'col\' column property: not null (DB: TRUE != MIG: NULL)
151+
- different \'col\' column property: not null (DB: TRUE != MIG: FALSE)
152152
153153
No files generated.',
154154
MigrationControllerStub::$stdout

tests/functional/mysql/UpdaterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function shouldUpdateTableByAddingColumnWithUnsigned(): void
187187
$this->assertStringContainsString(
188188
'public function up()
189189
{
190-
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->unsigned()->after(\'col2\'));
190+
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->unsigned()->after(\'col3\'));
191191
}
192192
193193
public function down()
@@ -212,7 +212,7 @@ public function shouldUpdateTableByAddingColumnWithNotNull(): void
212212
$this->assertStringContainsString(
213213
'public function up()
214214
{
215-
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->notNull()->after(\'col2\'));
215+
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->notNull()->after(\'col3\'));
216216
}
217217
218218
public function down()
@@ -241,7 +241,7 @@ public function shouldUpdateTableByAddingColumnWithComment(): void
241241
$this->assertStringContainsString(
242242
'public function up()
243243
{
244-
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->comment(\'test\')->after(\'col2\'));
244+
$this->addColumn(\'{{%updater_base}}\', \'added\', $this->integer()->comment(\'test\')->after(\'col3\'));
245245
}
246246
247247
public function down()

0 commit comments

Comments
 (0)