Skip to content

Commit

Permalink
Merge pull request #75 from bizley/2.9.4
Browse files Browse the repository at this point in the history
2.9.4
  • Loading branch information
Bizley authored Dec 18, 2019
2 parents a9ed184 + 4e060c0 commit ec30d17
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ before_script:
- psql -U postgres -c 'CREATE DATABASE migrationtest;';

script:
- vendor/bin/phpunit --verbose $PHPUNIT_FLAGS
- vendor/bin/phpunit -v
9 changes: 9 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use bizley\migration\table\TableIndex;
use bizley\migration\table\TablePrimaryKey;
use bizley\migration\table\TableStructure;
use Exception;
use PDO;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -178,6 +180,12 @@ protected function getTableColumns($indexes = [], $schema = null)
if ($this->tableSchema instanceof TableSchema) {
$indexData = !empty($indexes) ? $indexes : $this->getTableIndexes();

try {
$version = $this->db->getSlavePdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
} catch (Exception $exception) {
$version = null;
}

foreach ($this->tableSchema->columns as $column) {
$isUnique = false;

Expand All @@ -193,6 +201,7 @@ protected function getTableColumns($indexes = [], $schema = null)
'name' => $column->name,
'type' => $column->type,
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$column->type],
'engineVersion' => $version,
'size' => $column->size,
'precision' => $column->precision,
'scale' => $column->scale,
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/MigrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
* Generates migration file based on the existing database table and previous migrations.
*
* @author Paweł Bizley Brzozowski
* @version 2.9.3
* @version 2.9.4
* @license Apache 2.0
* https://github.com/bizley/yii2-migration
*/
class MigrationController extends Controller
{
protected $version = '2.9.3';
protected $version = '2.9.4';

/**
* @var string Default command action.
Expand Down
7 changes: 7 additions & 0 deletions src/table/TableColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,19 @@ class TableColumn extends Object
* @since 2.9.0
*/
public $after;

/**
* @var bool
* @since 2.9.0
*/
public $isFirst = false;

/**
* @var string
* @since 2.9.4
*/
public $engineVersion;

/**
* Sets length of the column.
* @param string|int $value
Expand Down
29 changes: 27 additions & 2 deletions src/table/TableColumnDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace bizley\migration\table;

use yii\db\Expression;

/**
* Class TableColumnDateTime
* @package bizley\migration\table
Expand All @@ -14,13 +16,23 @@ class TableColumnDateTime extends TableColumn
*/
public $lengthSchemas = [TableStructure::SCHEMA_PGSQL];

public function init()
{
parent::init();

if (is_string($this->default) && preg_match('/^current_timestamp\([0-9]*\)$/i', $this->default)) {
// https://github.com/yiisoft/yii2/issues/17744
$this->default = new Expression($this->default);
}
}

/**
* Returns length of the column.
* @return int|string
*/
public function getLength()
{
return in_array($this->schema, $this->lengthSchemas, true) ? $this->precision : null;
return $this->isSchemaLengthSupporting() ? $this->precision : null;
}

/**
Expand All @@ -29,7 +41,7 @@ public function getLength()
*/
public function setLength($value)
{
if (in_array($this->schema, $this->lengthSchemas, true)) {
if ($this->isSchemaLengthSupporting()) {
$this->precision = $value;
}
}
Expand All @@ -42,4 +54,17 @@ public function buildSpecificDefinition($table)
{
$this->definition[] = 'dateTime(' . $this->getRenderLength($table->generalSchema) . ')';
}

private function isSchemaLengthSupporting()
{
if (
$this->engineVersion
&& $this->schema === TableStructure::SCHEMA_MYSQL
&& version_compare($this->engineVersion, '5.6.4', '>=')
) {
return true;
}

return in_array($this->schema, $this->lengthSchemas, true);
}
}
17 changes: 15 additions & 2 deletions src/table/TableColumnTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TableColumnTime extends TableColumn
*/
public function getLength()
{
return in_array($this->schema, $this->lengthSchemas, true) ? $this->precision : null;
return $this->isSchemaLengthSupporting() ? $this->precision : null;
}

/**
Expand All @@ -29,7 +29,7 @@ public function getLength()
*/
public function setLength($value)
{
if (in_array($this->schema, $this->lengthSchemas, true)) {
if ($this->isSchemaLengthSupporting()) {
$this->precision = $value;
}
}
Expand All @@ -42,4 +42,17 @@ public function buildSpecificDefinition($table)
{
$this->definition[] = 'time(' . $this->getRenderLength($table->generalSchema) . ')';
}

private function isSchemaLengthSupporting()
{
if (
$this->engineVersion
&& $this->schema === TableStructure::SCHEMA_MYSQL
&& version_compare($this->engineVersion, '5.6.4', '>=')
) {
return true;
}

return in_array($this->schema, $this->lengthSchemas, true);
}
}
29 changes: 27 additions & 2 deletions src/table/TableColumnTimestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace bizley\migration\table;

use yii\db\Expression;

/**
* Class TableColumnTimestamp
* @package bizley\migration\table
Expand All @@ -14,13 +16,23 @@ class TableColumnTimestamp extends TableColumn
*/
public $lengthSchemas = [TableStructure::SCHEMA_PGSQL];

public function init()
{
parent::init();

if (is_string($this->default) && preg_match('/^current_timestamp\([0-9]*\)$/i', $this->default)) {
// https://github.com/yiisoft/yii2/issues/17744
$this->default = new Expression($this->default);
}
}

/**
* Returns length of the column.
* @return int|string
*/
public function getLength()
{
return in_array($this->schema, $this->lengthSchemas, true) ? $this->precision : null;
return $this->isSchemaLengthSupporting() ? $this->precision : null;
}

/**
Expand All @@ -29,7 +41,7 @@ public function getLength()
*/
public function setLength($value)
{
if (in_array($this->schema, $this->lengthSchemas, true)) {
if ($this->isSchemaLengthSupporting()) {
$this->precision = $value;
}
}
Expand All @@ -42,4 +54,17 @@ public function buildSpecificDefinition($table)
{
$this->definition[] = 'timestamp(' . $this->getRenderLength($table->generalSchema) . ')';
}

private function isSchemaLengthSupporting()
{
if (
$this->engineVersion
&& $this->schema === TableStructure::SCHEMA_MYSQL
&& version_compare($this->engineVersion, '5.6.4', '>=')
) {
return true;
}

return in_array($this->schema, $this->lengthSchemas, true);
}
}
61 changes: 31 additions & 30 deletions tests/table/TableColumnDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,47 @@ public function testDefinitionNoSchema($column, $generalSchema, $result)
public function withSchemaDataProvider()
{
return [
[['precision' => 4], false, '$this->dateTime(4)'],
[['precision' => 4], true, '$this->dateTime(4)'],
[['precision' => 0], false, '$this->dateTime(0)'],
[['precision' => 0], true, '$this->dateTime(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(4)'],
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(0)'],
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime(4)'],
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime(0)'],
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime()'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(4)'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(0)'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime(4)'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime(0)'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime()'],
];
}

/**
* @dataProvider withSchemaDataProvider
* @param array $column
* @param bool $generalSchema
* @param string $schema
* @param string $version
* @param bool $mapping
* @param string $result
*/
public function testDefinitionWithSchema($column, $generalSchema, $result)
public function testDefinitionWithSchema($column, $generalSchema, $schema, $version, $mapping, $result)
{
$column['schema'] = TableStructure::SCHEMA_PGSQL;
$column = new TableColumnDateTime($column);
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
}

public function withMappingAndSchemaDataProvider()
{
return [
[['precision' => 4], false, '$this->dateTime(4)'],
[['precision' => 4], true, '$this->dateTime(4)'],
[['precision' => 0], false, '$this->dateTime(0)'],
[['precision' => 0], true, '$this->dateTime()'],
];
}

/**
* @dataProvider withMappingAndSchemaDataProvider
* @param array $column
* @param bool $generalSchema
* @param string $result
*/
public function testDefinitionWithMappingAndSchema($column, $generalSchema, $result)
{
$column['schema'] = TableStructure::SCHEMA_PGSQL;
$column['defaultMapping'] = 'timestamp(0)';
$column['schema'] = $schema;
$column['engineVersion'] = $version;
$column['defaultMapping'] = $mapping ? 'datetime(0)' : null;
$column = new TableColumnDateTime($column);
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
}
Expand Down
61 changes: 31 additions & 30 deletions tests/table/TableColumnTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,47 @@ public function testDefinitionNoSchema($column, $generalSchema, $result)
public function withSchemaDataProvider()
{
return [
[['precision' => 0], false, '$this->time(0)'],
[['precision' => 4], false, '$this->time(4)'],
[['precision' => 0], true, '$this->time(0)'],
[['precision' => 4], true, '$this->time(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(4)'],
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(0)'],
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->time(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->time(4)'],
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->time()'],
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->time(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(4)'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(0)'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(4)'],
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time(0)'],
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time(4)'],
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time()'],
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time(4)'],
];
}

/**
* @dataProvider withSchemaDataProvider
* @param array $column
* @param bool $generalSchema
* @param string $schema
* @param string $version
* @param bool $mapping
* @param string $result
*/
public function testDefinitionWithSchema($column, $generalSchema, $result)
public function testDefinitionWithSchema($column, $generalSchema, $schema, $version, $mapping, $result)
{
$column['schema'] = TableStructure::SCHEMA_PGSQL;
$column = new TableColumnTime($column);
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
}

public function withMappingAndSchemaDataProvider()
{
return [
[['precision' => 0], false, '$this->time(0)'],
[['precision' => 4], false, '$this->time(4)'],
[['precision' => 0], true, '$this->time()'],
[['precision' => 4], true, '$this->time(4)'],
];
}

/**
* @dataProvider withMappingAndSchemaDataProvider
* @param array $column
* @param bool $generalSchema
* @param string $result
*/
public function testDefinitionWithMappingAndSchema($column, $generalSchema, $result)
{
$column['schema'] = TableStructure::SCHEMA_PGSQL;
$column['defaultMapping'] = 'time(0)';
$column['schema'] = $schema;
$column['engineVersion'] = $version;
$column['defaultMapping'] = $mapping ? 'time(0)' : null;
$column = new TableColumnTime($column);
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
}
Expand Down
Loading

0 comments on commit ec30d17

Please sign in to comment.