From 28a7f9ee42e39afb599f8a1f3b3ad83fedd10e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Brzozowski?= Date: Sun, 1 Jul 2018 16:26:29 +0200 Subject: [PATCH] size / precision and appended primary key following fixed --- README.md | 2 +- Updater.php | 23 +++++++++------ table/TableColumn.php | 1 + ...0701_160300_create_table_test_int_size.php | 25 +++++++++++++++++ ...80701_160900_create_table_test_char_pk.php | 25 +++++++++++++++++ tests/mysql/MysqlDbUpdaterTestCase.php | 28 +++++++++++++++++++ tests/mysql/UpdaterColumnsTest.php | 26 +++++++++++++++++ 7 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 tests/migrations/m180701_160300_create_table_test_int_size.php create mode 100644 tests/migrations/m180701_160900_create_table_test_char_pk.php diff --git a/README.md b/README.md index 81e6683..cd009a3 100644 --- a/README.md +++ b/README.md @@ -152,4 +152,4 @@ etc.) are not tracked. ## Tests Currently only MySQL tests are provided. Database configuration is stored in `tests/config.php` (you can override it by -creating `config.local.php` file there). +creating `config.local.php` file there). \ No newline at end of file diff --git a/Updater.php b/Updater.php index aa3aa0f..9f4e088 100644 --- a/Updater.php +++ b/Updater.php @@ -286,16 +286,21 @@ protected function compareStructures(): bool $different = true; continue; } - foreach (TableColumn::properties() as $property) { - if (!$this->generalSchema && $this->oldTable->columns[$name]->$property !== $column->$property) { - if ($this->showOnly) { - echo " - different '$name' column property: $property ("; - echo 'DB: ' . $this->displayValue($column->$property) . ' <> '; - echo 'MIG: ' . $this->displayValue($this->oldTable->columns[$name]->$property) . ")\n"; - } elseif (!isset($this->plan->alterColumn[$name])) { - $this->plan->alterColumn[$name] = $column; + if (!$this->generalSchema) { + foreach (TableColumn::properties() as $property) { + if ($property === 'append' && $column->append === null && !$this->table->primaryKey->isComposite() && $column->isColumnInPK($this->table->primaryKey)) { + $column->append = $column->prepareSchemaAppend($this->table, true, $column->autoIncrement); + } + if ($this->oldTable->columns[$name]->$property !== $column->$property) { + if ($this->showOnly) { + echo " - different '$name' column property: $property ("; + echo 'DB: ' . $this->displayValue($column->$property) . ' <> '; + echo 'MIG: ' . $this->displayValue($this->oldTable->columns[$name]->$property) . ")\n"; + } elseif (!isset($this->plan->alterColumn[$name])) { + $this->plan->alterColumn[$name] = $column; + } + $different = true; } - $different = true; } } } diff --git a/table/TableColumn.php b/table/TableColumn.php index 4e221f0..a1d1e62 100644 --- a/table/TableColumn.php +++ b/table/TableColumn.php @@ -95,6 +95,7 @@ public function getLength() public function setLength($value): void { $this->size = $value; + $this->precision = $value; } protected function buildSpecificDefinition($table): void {} diff --git a/tests/migrations/m180701_160300_create_table_test_int_size.php b/tests/migrations/m180701_160300_create_table_test_int_size.php new file mode 100644 index 0000000..4cbe695 --- /dev/null +++ b/tests/migrations/m180701_160300_create_table_test_int_size.php @@ -0,0 +1,25 @@ +db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%test_int_size}}', [ + 'col_int' => $this->integer(10), + ], $tableOptions); + } + + public function down() + { + $this->dropTable('{{%test_int_size}}'); + } +} diff --git a/tests/migrations/m180701_160900_create_table_test_char_pk.php b/tests/migrations/m180701_160900_create_table_test_char_pk.php new file mode 100644 index 0000000..bd6551b --- /dev/null +++ b/tests/migrations/m180701_160900_create_table_test_char_pk.php @@ -0,0 +1,25 @@ +db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%test_char_pk}}', [ + 'id' => $this->char(128)->notNull()->append('PRIMARY KEY'), + ], $tableOptions); + } + + public function down() + { + $this->dropTable('{{%test_char_pk}}'); + } +} diff --git a/tests/mysql/MysqlDbUpdaterTestCase.php b/tests/mysql/MysqlDbUpdaterTestCase.php index 94798bf..9b4c5a4 100644 --- a/tests/mysql/MysqlDbUpdaterTestCase.php +++ b/tests/mysql/MysqlDbUpdaterTestCase.php @@ -122,6 +122,22 @@ protected function dbUp($name): void static::addMigration('bizley\\migration\\tests\\migrations\\m180328_205900_drop_column_one_from_table_test_multiple'); } }, + 'test_int_size' => function () use ($tableOptions) { + if (!\in_array('test_int_size', Yii::$app->db->schema->tableNames, true)) { + Yii::$app->db->createCommand()->createTable('test_int_size', [ + 'col_int' => 'INT(10) NULL', + ], $tableOptions)->execute(); + static::addMigration('bizley\\migration\\tests\\migrations\\m180701_160300_create_table_test_int_size'); + } + }, + 'test_char_pk' => function () use ($tableOptions) { + if (!\in_array('test_char_pk', Yii::$app->db->schema->tableNames, true)) { + Yii::$app->db->createCommand()->createTable('test_char_pk', [ + 'id' => 'CHAR(128) NOT NULL PRIMARY KEY', + ], $tableOptions)->execute(); + static::addMigration('bizley\\migration\\tests\\migrations\\m180701_160900_create_table_test_char_pk'); + } + }, ]; \call_user_func($data[$name]); } @@ -130,6 +146,18 @@ protected function dbDown($name): void { // needs reverse order $data = [ + 'test_char_pk' => function () { + if (\in_array('test_char_pk', Yii::$app->db->schema->tableNames, true)) { + Yii::$app->db->createCommand()->dropTable('test_char_pk')->execute(); + static::deleteMigration('bizley\\migration\\tests\\migrations\\m180701_160900_create_table_test_char_pk'); + } + }, + 'test_int_size' => function () { + if (\in_array('test_int_size', Yii::$app->db->schema->tableNames, true)) { + Yii::$app->db->createCommand()->dropTable('test_int_size')->execute(); + static::deleteMigration('bizley\\migration\\tests\\migrations\\m180701_160300_create_table_test_int_size'); + } + }, 'test_multiple' => function () { if (\in_array('test_multiple', Yii::$app->db->schema->tableNames, true)) { Yii::$app->db->createCommand()->dropTable('test_multiple')->execute(); diff --git a/tests/mysql/UpdaterColumnsTest.php b/tests/mysql/UpdaterColumnsTest.php index c248640..31d536a 100644 --- a/tests/mysql/UpdaterColumnsTest.php +++ b/tests/mysql/UpdaterColumnsTest.php @@ -44,6 +44,32 @@ public function testChangeSizeSpecific(): void $this->assertEquals(9, $updater->plan->alterColumn['col_int']->precision); } + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + * https://github.com/bizley/yii2-migration/issues/30 + */ + public function testNoChangeSizeSpecific(): void + { + $this->dbUp('test_int_size'); + + $updater = $this->getUpdater('test_int_size', false); + $this->assertFalse($updater->isUpdateRequired()); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + * https://github.com/bizley/yii2-migration/issues/30 + */ + public function testNoChangePKSpecific(): void + { + $this->dbUp('test_char_pk'); + + $updater = $this->getUpdater('test_char_pk', false); + $this->assertFalse($updater->isUpdateRequired()); + } + /** * @runInSeparateProcess * @preserveGlobalState disabled