Skip to content

Commit 13c9054

Browse files
author
Paweł Brzozowski
committed
2.4
1 parent 72de70d commit 13c9054

File tree

85 files changed

+2428
-860
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2428
-860
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ env:
88

99
services:
1010
- mysql
11+
- postgresql
1112

1213
cache:
1314
directories:
1415
- vendor
1516
- $HOME/.composer/cache
1617

1718
addons:
19+
postgresql: "9.6"
1820
apt:
1921
sources:
2022
- mysql-5.7-trusty
@@ -39,8 +41,10 @@ before_script:
3941
- sudo mysql_upgrade
4042
- travis_retry mysql -e 'CREATE DATABASE `migrationtest`;'
4143
- mysql -e "SET GLOBAL sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"
44+
- mysql -e "SET GLOBAL explicit_defaults_for_timestamp = 'ON';"
4245
- mysql -e "CREATE USER 'migration'@'localhost' IDENTIFIED BY 'migration';"
4346
- mysql -e "GRANT ALL PRIVILEGES ON migrationtest.* TO 'migration'@'localhost';"
47+
- psql -U postgres -c 'CREATE DATABASE migrationtest;';
4448

4549
script:
4650
- vendor/bin/phpunit --verbose $PHPUNIT_FLAGS

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ Add the package to your composer.json:
1515

1616
{
1717
"require": {
18-
"bizley/migration": "^3.0"
18+
"bizley/migration": "^3.1"
1919
}
2020
}
2121

22-
and run `composer update` or alternatively run `composer require bizley/migration:^3.0`
22+
and run `composer update` or alternatively run `composer require bizley/migration:^3.1`
2323

2424
## Installation for PHP < 7.1
2525

2626
Add the package to your composer.json:
2727

2828
{
2929
"require": {
30-
"bizley/migration": "^2.3"
30+
"bizley/migration": "^2.4"
3131
}
3232
}
3333

34-
and run `composer update` or alternatively run `composer require bizley/migration:^2.3`
34+
and run `composer update` or alternatively run `composer require bizley/migration:^2.4`
3535

3636
## Configuration
3737

@@ -141,8 +141,6 @@ This extension should work with all database types supported in Yii 2 core:
141141
- PostgreSQL (9.x and above)
142142
- SQLite (2/3)
143143

144-
Let me know if something is wrong with databases other than MySQL (and in case of MySQL let me know as well).
145-
146144
Yii 2 limitations:
147145
- version 2.0.13 is required to track non-unique indexes,
148146
- version 2.0.14 is required to handle TINYINT and JSON type columns.
@@ -154,5 +152,5 @@ etc.) are not tracked.
154152

155153
## Tests
156154

157-
Currently only MySQL tests are provided. Database configuration is stored in `tests/config.php` (you can override it by
155+
Tests for MySQL, PostgreSQL, and SQLite. Database configuration is stored in `tests/config.php` (you can override it by
158156
creating `config.local.php` file there).

src/Generator.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function getTableSchema()
120120
protected function getTablePrimaryKey()
121121
{
122122
$data = [];
123+
123124
if (method_exists($this->db->schema, 'getTablePrimaryKey')) { // requires Yii 2.0.13
124125
/* @var $constraint \yii\db\Constraint */
125126
$constraint = $this->db->schema->getTablePrimaryKey($this->tableName, true);
@@ -128,24 +129,32 @@ protected function getTablePrimaryKey()
128129
'columns' => $constraint->columnNames,
129130
'name' => $constraint->name,
130131
];
132+
} elseif ($this->db->schema instanceof \yii\db\sqlite\Schema) {
133+
// SQLite bug-case fixed in Yii 2.0.16 https://github.com/yiisoft/yii2/issues/16897
134+
135+
if ($this->tableSchema !== null && $this->tableSchema->primaryKey) {
136+
$data = [
137+
'columns' => $this->tableSchema->primaryKey,
138+
];
139+
}
131140
}
132-
} elseif ($this->tableSchema instanceof TableSchema) {
133-
if ($this->tableSchema->primaryKey) {
134-
$data = [
135-
'columns' => $this->tableSchema->primaryKey,
136-
];
137-
}
141+
} elseif ($this->tableSchema !== null && $this->tableSchema->primaryKey) {
142+
$data = [
143+
'columns' => $this->tableSchema->primaryKey,
144+
];
138145
}
146+
139147
return new TablePrimaryKey($data);
140148
}
141149

142150
/**
143151
* Returns columns structure.
144152
* @param TableIndex[] $indexes
153+
* @param string $schema
145154
* @return TableColumn[]
146155
* @throws InvalidConfigException
147156
*/
148-
protected function getTableColumns($indexes = [])
157+
protected function getTableColumns($indexes = [], $schema = null)
149158
{
150159
$columns = [];
151160
if ($this->tableSchema instanceof TableSchema) {
@@ -159,6 +168,7 @@ protected function getTableColumns($indexes = [])
159168
}
160169
}
161170
$columns[$column->name] = TableColumnFactory::build([
171+
'schema' => $schema,
162172
'name' => $column->name,
163173
'type' => $column->type,
164174
'size' => $column->size,
@@ -167,11 +177,11 @@ protected function getTableColumns($indexes = [])
167177
'isNotNull' => $column->allowNull ? null : true,
168178
'isUnique' => $isUnique,
169179
'check' => null,
170-
'default' => $column->defaultValue,
180+
'default' => $column->defaultValue ?: null,
171181
'isPrimaryKey' => $column->isPrimaryKey,
172182
'autoIncrement' => $column->autoIncrement,
173183
'isUnsigned' => $column->unsigned,
174-
'comment' => $column->comment,
184+
'comment' => $column->comment ?: null,
175185
]);
176186
}
177187
}
@@ -269,13 +279,14 @@ public function getTable()
269279
'usePrefix' => $this->useTablePrefix,
270280
'dbPrefix' => $this->db->tablePrefix,
271281
'primaryKey' => $this->getTablePrimaryKey(),
272-
'columns' => $this->getTableColumns($indexes),
273282
'foreignKeys' => $this->getTableForeignKeys(),
274283
'indexes' => $indexes,
275284
'tableOptionsInit' => $this->tableOptionsInit,
276285
'tableOptions' => $this->tableOptions,
277286
]);
287+
$this->_table->columns = $this->getTableColumns($indexes, $this->_table->schema);
278288
}
289+
279290
return $this->_table;
280291
}
281292

0 commit comments

Comments
 (0)