Skip to content

Commit ec30d17

Browse files
author
Bizley
authored
Merge pull request #75 from bizley/2.9.4
2.9.4
2 parents a9ed184 + 4e060c0 commit ec30d17

File tree

10 files changed

+181
-99
lines changed

10 files changed

+181
-99
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ before_script:
4545
- psql -U postgres -c 'CREATE DATABASE migrationtest;';
4646

4747
script:
48-
- vendor/bin/phpunit --verbose $PHPUNIT_FLAGS
48+
- vendor/bin/phpunit -v

src/Generator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use bizley\migration\table\TableIndex;
1010
use bizley\migration\table\TablePrimaryKey;
1111
use bizley\migration\table\TableStructure;
12+
use Exception;
13+
use PDO;
1214
use Yii;
1315
use yii\base\Component;
1416
use yii\base\InvalidConfigException;
@@ -178,6 +180,12 @@ protected function getTableColumns($indexes = [], $schema = null)
178180
if ($this->tableSchema instanceof TableSchema) {
179181
$indexData = !empty($indexes) ? $indexes : $this->getTableIndexes();
180182

183+
try {
184+
$version = $this->db->getSlavePdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
185+
} catch (Exception $exception) {
186+
$version = null;
187+
}
188+
181189
foreach ($this->tableSchema->columns as $column) {
182190
$isUnique = false;
183191

@@ -193,6 +201,7 @@ protected function getTableColumns($indexes = [], $schema = null)
193201
'name' => $column->name,
194202
'type' => $column->type,
195203
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$column->type],
204+
'engineVersion' => $version,
196205
'size' => $column->size,
197206
'precision' => $column->precision,
198207
'scale' => $column->scale,

src/controllers/MigrationController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* Generates migration file based on the existing database table and previous migrations.
2727
*
2828
* @author Paweł Bizley Brzozowski
29-
* @version 2.9.3
29+
* @version 2.9.4
3030
* @license Apache 2.0
3131
* https://github.com/bizley/yii2-migration
3232
*/
3333
class MigrationController extends Controller
3434
{
35-
protected $version = '2.9.3';
35+
protected $version = '2.9.4';
3636

3737
/**
3838
* @var string Default command action.

src/table/TableColumn.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,19 @@ class TableColumn extends Object
103103
* @since 2.9.0
104104
*/
105105
public $after;
106+
106107
/**
107108
* @var bool
108109
* @since 2.9.0
109110
*/
110111
public $isFirst = false;
111112

113+
/**
114+
* @var string
115+
* @since 2.9.4
116+
*/
117+
public $engineVersion;
118+
112119
/**
113120
* Sets length of the column.
114121
* @param string|int $value

src/table/TableColumnDateTime.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace bizley\migration\table;
44

5+
use yii\db\Expression;
6+
57
/**
68
* Class TableColumnDateTime
79
* @package bizley\migration\table
@@ -14,13 +16,23 @@ class TableColumnDateTime extends TableColumn
1416
*/
1517
public $lengthSchemas = [TableStructure::SCHEMA_PGSQL];
1618

19+
public function init()
20+
{
21+
parent::init();
22+
23+
if (is_string($this->default) && preg_match('/^current_timestamp\([0-9]*\)$/i', $this->default)) {
24+
// https://github.com/yiisoft/yii2/issues/17744
25+
$this->default = new Expression($this->default);
26+
}
27+
}
28+
1729
/**
1830
* Returns length of the column.
1931
* @return int|string
2032
*/
2133
public function getLength()
2234
{
23-
return in_array($this->schema, $this->lengthSchemas, true) ? $this->precision : null;
35+
return $this->isSchemaLengthSupporting() ? $this->precision : null;
2436
}
2537

2638
/**
@@ -29,7 +41,7 @@ public function getLength()
2941
*/
3042
public function setLength($value)
3143
{
32-
if (in_array($this->schema, $this->lengthSchemas, true)) {
44+
if ($this->isSchemaLengthSupporting()) {
3345
$this->precision = $value;
3446
}
3547
}
@@ -42,4 +54,17 @@ public function buildSpecificDefinition($table)
4254
{
4355
$this->definition[] = 'dateTime(' . $this->getRenderLength($table->generalSchema) . ')';
4456
}
57+
58+
private function isSchemaLengthSupporting()
59+
{
60+
if (
61+
$this->engineVersion
62+
&& $this->schema === TableStructure::SCHEMA_MYSQL
63+
&& version_compare($this->engineVersion, '5.6.4', '>=')
64+
) {
65+
return true;
66+
}
67+
68+
return in_array($this->schema, $this->lengthSchemas, true);
69+
}
4570
}

src/table/TableColumnTime.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class TableColumnTime extends TableColumn
2020
*/
2121
public function getLength()
2222
{
23-
return in_array($this->schema, $this->lengthSchemas, true) ? $this->precision : null;
23+
return $this->isSchemaLengthSupporting() ? $this->precision : null;
2424
}
2525

2626
/**
@@ -29,7 +29,7 @@ public function getLength()
2929
*/
3030
public function setLength($value)
3131
{
32-
if (in_array($this->schema, $this->lengthSchemas, true)) {
32+
if ($this->isSchemaLengthSupporting()) {
3333
$this->precision = $value;
3434
}
3535
}
@@ -42,4 +42,17 @@ public function buildSpecificDefinition($table)
4242
{
4343
$this->definition[] = 'time(' . $this->getRenderLength($table->generalSchema) . ')';
4444
}
45+
46+
private function isSchemaLengthSupporting()
47+
{
48+
if (
49+
$this->engineVersion
50+
&& $this->schema === TableStructure::SCHEMA_MYSQL
51+
&& version_compare($this->engineVersion, '5.6.4', '>=')
52+
) {
53+
return true;
54+
}
55+
56+
return in_array($this->schema, $this->lengthSchemas, true);
57+
}
4558
}

src/table/TableColumnTimestamp.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace bizley\migration\table;
44

5+
use yii\db\Expression;
6+
57
/**
68
* Class TableColumnTimestamp
79
* @package bizley\migration\table
@@ -14,13 +16,23 @@ class TableColumnTimestamp extends TableColumn
1416
*/
1517
public $lengthSchemas = [TableStructure::SCHEMA_PGSQL];
1618

19+
public function init()
20+
{
21+
parent::init();
22+
23+
if (is_string($this->default) && preg_match('/^current_timestamp\([0-9]*\)$/i', $this->default)) {
24+
// https://github.com/yiisoft/yii2/issues/17744
25+
$this->default = new Expression($this->default);
26+
}
27+
}
28+
1729
/**
1830
* Returns length of the column.
1931
* @return int|string
2032
*/
2133
public function getLength()
2234
{
23-
return in_array($this->schema, $this->lengthSchemas, true) ? $this->precision : null;
35+
return $this->isSchemaLengthSupporting() ? $this->precision : null;
2436
}
2537

2638
/**
@@ -29,7 +41,7 @@ public function getLength()
2941
*/
3042
public function setLength($value)
3143
{
32-
if (in_array($this->schema, $this->lengthSchemas, true)) {
44+
if ($this->isSchemaLengthSupporting()) {
3345
$this->precision = $value;
3446
}
3547
}
@@ -42,4 +54,17 @@ public function buildSpecificDefinition($table)
4254
{
4355
$this->definition[] = 'timestamp(' . $this->getRenderLength($table->generalSchema) . ')';
4456
}
57+
58+
private function isSchemaLengthSupporting()
59+
{
60+
if (
61+
$this->engineVersion
62+
&& $this->schema === TableStructure::SCHEMA_MYSQL
63+
&& version_compare($this->engineVersion, '5.6.4', '>=')
64+
) {
65+
return true;
66+
}
67+
68+
return in_array($this->schema, $this->lengthSchemas, true);
69+
}
4570
}

tests/table/TableColumnDateTimeTest.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,47 @@ public function testDefinitionNoSchema($column, $generalSchema, $result)
3333
public function withSchemaDataProvider()
3434
{
3535
return [
36-
[['precision' => 4], false, '$this->dateTime(4)'],
37-
[['precision' => 4], true, '$this->dateTime(4)'],
38-
[['precision' => 0], false, '$this->dateTime(0)'],
39-
[['precision' => 0], true, '$this->dateTime(0)'],
36+
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(4)'],
37+
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(4)'],
38+
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(0)'],
39+
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->dateTime(0)'],
40+
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime(4)'],
41+
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime(4)'],
42+
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime(0)'],
43+
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->dateTime()'],
44+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
45+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
46+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
47+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->dateTime()'],
48+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
49+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
50+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
51+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->dateTime()'],
52+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(4)'],
53+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(4)'],
54+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(0)'],
55+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->dateTime(0)'],
56+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime(4)'],
57+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime(4)'],
58+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime(0)'],
59+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->dateTime()'],
4060
];
4161
}
4262

4363
/**
4464
* @dataProvider withSchemaDataProvider
4565
* @param array $column
4666
* @param bool $generalSchema
67+
* @param string $schema
68+
* @param string $version
69+
* @param bool $mapping
4770
* @param string $result
4871
*/
49-
public function testDefinitionWithSchema($column, $generalSchema, $result)
72+
public function testDefinitionWithSchema($column, $generalSchema, $schema, $version, $mapping, $result)
5073
{
51-
$column['schema'] = TableStructure::SCHEMA_PGSQL;
52-
$column = new TableColumnDateTime($column);
53-
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
54-
}
55-
56-
public function withMappingAndSchemaDataProvider()
57-
{
58-
return [
59-
[['precision' => 4], false, '$this->dateTime(4)'],
60-
[['precision' => 4], true, '$this->dateTime(4)'],
61-
[['precision' => 0], false, '$this->dateTime(0)'],
62-
[['precision' => 0], true, '$this->dateTime()'],
63-
];
64-
}
65-
66-
/**
67-
* @dataProvider withMappingAndSchemaDataProvider
68-
* @param array $column
69-
* @param bool $generalSchema
70-
* @param string $result
71-
*/
72-
public function testDefinitionWithMappingAndSchema($column, $generalSchema, $result)
73-
{
74-
$column['schema'] = TableStructure::SCHEMA_PGSQL;
75-
$column['defaultMapping'] = 'timestamp(0)';
74+
$column['schema'] = $schema;
75+
$column['engineVersion'] = $version;
76+
$column['defaultMapping'] = $mapping ? 'datetime(0)' : null;
7677
$column = new TableColumnDateTime($column);
7778
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
7879
}

tests/table/TableColumnTimeTest.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,47 @@ public function testDefinitionNoSchema($column, $generalSchema, $result)
3333
public function withSchemaDataProvider()
3434
{
3535
return [
36-
[['precision' => 0], false, '$this->time(0)'],
37-
[['precision' => 4], false, '$this->time(4)'],
38-
[['precision' => 0], true, '$this->time(0)'],
39-
[['precision' => 4], true, '$this->time(4)'],
36+
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(0)'],
37+
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(4)'],
38+
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(0)'],
39+
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', false, '$this->time(4)'],
40+
[['precision' => 0], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->time(0)'],
41+
[['precision' => 4], false, TableStructure::SCHEMA_PGSQL, '', true, '$this->time(4)'],
42+
[['precision' => 0], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->time()'],
43+
[['precision' => 4], true, TableStructure::SCHEMA_PGSQL, '', true, '$this->time(4)'],
44+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
45+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
46+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
47+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', false, '$this->time()'],
48+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
49+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
50+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
51+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '', true, '$this->time()'],
52+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(0)'],
53+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(4)'],
54+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(0)'],
55+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', false, '$this->time(4)'],
56+
[['precision' => 0], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time(0)'],
57+
[['precision' => 4], false, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time(4)'],
58+
[['precision' => 0], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time()'],
59+
[['precision' => 4], true, TableStructure::SCHEMA_MYSQL, '5.6.4', true, '$this->time(4)'],
4060
];
4161
}
4262

4363
/**
4464
* @dataProvider withSchemaDataProvider
4565
* @param array $column
4666
* @param bool $generalSchema
67+
* @param string $schema
68+
* @param string $version
69+
* @param bool $mapping
4770
* @param string $result
4871
*/
49-
public function testDefinitionWithSchema($column, $generalSchema, $result)
72+
public function testDefinitionWithSchema($column, $generalSchema, $schema, $version, $mapping, $result)
5073
{
51-
$column['schema'] = TableStructure::SCHEMA_PGSQL;
52-
$column = new TableColumnTime($column);
53-
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
54-
}
55-
56-
public function withMappingAndSchemaDataProvider()
57-
{
58-
return [
59-
[['precision' => 0], false, '$this->time(0)'],
60-
[['precision' => 4], false, '$this->time(4)'],
61-
[['precision' => 0], true, '$this->time()'],
62-
[['precision' => 4], true, '$this->time(4)'],
63-
];
64-
}
65-
66-
/**
67-
* @dataProvider withMappingAndSchemaDataProvider
68-
* @param array $column
69-
* @param bool $generalSchema
70-
* @param string $result
71-
*/
72-
public function testDefinitionWithMappingAndSchema($column, $generalSchema, $result)
73-
{
74-
$column['schema'] = TableStructure::SCHEMA_PGSQL;
75-
$column['defaultMapping'] = 'time(0)';
74+
$column['schema'] = $schema;
75+
$column['engineVersion'] = $version;
76+
$column['defaultMapping'] = $mapping ? 'time(0)' : null;
7677
$column = new TableColumnTime($column);
7778
$this->assertEquals($result, $column->renderDefinition($this->getTable($generalSchema)));
7879
}

0 commit comments

Comments
 (0)