Skip to content

Commit

Permalink
size / precision and appended primary key following fixed [2.x]
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Brzozowski committed Jul 1, 2018
1 parent 957a1a7 commit 65f5581
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 12 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -8,7 +8,19 @@

Generates migration file based on the existing database table and previous migrations.

## Installation
## Installation for PHP >= 7.1 and Yii >= 2.0.15.1

Add the package to your composer.json:

{
"require": {
"bizley/migration": "~3.0.0"
}
}

and run `composer update` or alternatively run `composer require bizley/migration:~3.0.0`

## Installation for PHP < 7.1

Add the package to your composer.json:

Expand Down Expand Up @@ -140,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).
25 changes: 15 additions & 10 deletions Updater.php
Expand Up @@ -285,18 +285,23 @@ protected function compareStructures()
$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";
} else {
if (!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";
} else {
if (!isset($this->plan->alterColumn[$name])) {
$this->plan->alterColumn[$name] = $column;
}
}
$different = true;
}
$different = true;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions table/TableColumn.php
Expand Up @@ -95,6 +95,7 @@ public function getLength()
public function setLength($value)
{
$this->size = $value;
$this->precision = $value;
}

protected function buildSpecificDefinition($table) {}
Expand Down
25 changes: 25 additions & 0 deletions tests/migrations/m180701_160300_create_table_test_int_size.php
@@ -0,0 +1,25 @@
<?php

namespace bizley\migration\tests\migrations;

use yii\db\Migration;

class m180701_160300_create_table_test_int_size extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->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}}');
}
}
25 changes: 25 additions & 0 deletions tests/migrations/m180701_160900_create_table_test_char_pk.php
@@ -0,0 +1,25 @@
<?php

namespace bizley\migration\tests\migrations;

use yii\db\Migration;

class m180701_160900_create_table_test_char_pk extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->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}}');
}
}
28 changes: 28 additions & 0 deletions tests/mysql/MysqlDbUpdaterTestCase.php
Expand Up @@ -122,6 +122,22 @@ protected function dbUp($name)
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]);
}
Expand All @@ -130,6 +146,18 @@ protected function dbDown($name)
{
// 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();
Expand Down
26 changes: 26 additions & 0 deletions tests/mysql/UpdaterColumnsTest.php
Expand Up @@ -44,6 +44,32 @@ public function testChangeSizeSpecific()
$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
Expand Down

0 comments on commit 65f5581

Please sign in to comment.