Skip to content

Commit 5dce464

Browse files
author
Bizley
committed
table options
1 parent 1d34503 commit 5dce464

File tree

5 files changed

+113
-16
lines changed

5 files changed

+113
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Starting with yii2-migration v2.0 it is possible to generate updating migration
102102
| `generalSchema` | `g` | Whether to use general column schema instead of database specific (1). _default:_ `1`
103103
| `fixHistory` | `h` | Whether to add migration history entry when migration is generated. _default:_ `0`
104104
| `skipMigrations` | | List of migrations from the history table that should be skipped during the update process (2). _default:_ `[]`
105+
| `tableOptionsInit` | `O` | String rendered in the create migration template to initialize table options. _default:_ `$tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB'; }`
106+
| `tableOptions` | `o` | String rendered in the create migration template for table options. _default:_ `$tableOptions`
105107

106108
(1) Remember that with different database types general column schemas may be generated with different length.
107109

src/Generator.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ class Generator extends Component
7474
*/
7575
public $generalSchema = true;
7676

77+
/**
78+
* @var string|null
79+
* @since 3.0.4
80+
*/
81+
public $tableOptionsInit;
82+
83+
/**
84+
* @var string|null
85+
* @since 3.0.4
86+
*/
87+
public $tableOptions;
88+
7789
/**
7890
* Checks if DB connection is passed.
7991
* @throws InvalidConfigException
@@ -258,7 +270,9 @@ public function getTable(): TableStructure
258270
'primaryKey' => $this->getTablePrimaryKey(),
259271
'columns' => $this->getTableColumns($indexes),
260272
'foreignKeys' => $this->getTableForeignKeys(),
261-
'indexes' => $indexes
273+
'indexes' => $indexes,
274+
'tableOptionsInit' => $this->tableOptionsInit,
275+
'tableOptions' => $this->tableOptions,
262276
]);
263277
}
264278
return $this->_table;

src/controllers/MigrationController.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Generates migration file based on the existing database table and previous migrations.
2121
*
2222
* @author Paweł Bizley Brzozowski
23-
* @version 3.0.3
23+
* @version 3.0.4
2424
* @license Apache 2.0
2525
* https://github.com/bizley/yii2-migration
2626
*/
@@ -29,7 +29,7 @@ class MigrationController extends Controller
2929
/**
3030
* @var string
3131
*/
32-
protected $version = '3.0.3';
32+
protected $version = '3.0.4';
3333

3434
/**
3535
* @var string Default command action.
@@ -118,6 +118,27 @@ class MigrationController extends Controller
118118
*/
119119
public $skipMigrations = [];
120120

121+
/**
122+
* @var string|null String rendered in the create migration template to initialize table options.
123+
* By default it adds variable "$tableOptions" with optional collate configuration for MySQL DBMS to be used with
124+
* default $tableOptions.
125+
* Alias -O
126+
* @since 3.0.4
127+
*/
128+
public $tableOptionsInit = '$tableOptions = null;
129+
if ($this->db->driverName === \'mysql\') {
130+
$tableOptions = \'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB\';
131+
}';
132+
133+
/**
134+
* @var string|null String rendered in the create migration template for table options.
135+
* By default it renders "$tableOptions" to indicate that options should be taken from variable
136+
* set in $tableOptionsInit property.
137+
* Alias -o
138+
* @since 3.0.4
139+
*/
140+
public $tableOptions = '$tableOptions';
141+
121142
/**
122143
* @inheritdoc
123144
*/
@@ -129,7 +150,7 @@ public function options($actionID): array // BC declaration
129150
switch ($actionID) {
130151
case 'create':
131152
case 'create-all':
132-
return array_merge($options, $createOptions);
153+
return array_merge($options, $createOptions, ['tableOptionsInit', 'tableOptions']);
133154
case 'update':
134155
case 'update-all':
135156
return array_merge($options, $createOptions, $updateOptions);
@@ -153,6 +174,8 @@ public function optionAliases(): array
153174
'P' => 'useTablePrefix',
154175
'h' => 'fixHistory',
155176
's' => 'showOnly',
177+
'O' => 'tableOptionsInit',
178+
'o' => 'tableOptions',
156179
]);
157180
}
158181

@@ -332,6 +355,8 @@ public function actionCreate(string $table): int
332355
'className' => $className,
333356
'namespace' => $this->migrationNamespace,
334357
'generalSchema' => $this->generalSchema,
358+
'tableOptionsInit' => $this->tableOptionsInit,
359+
'tableOptions' => $this->tableOptions,
335360
]);
336361

337362
if ($generator->tableSchema === null) {

src/table/TableStructure.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class TableStructure extends BaseObject
5353
* @var string
5454
*/
5555
public $dbPrefix;
56+
/**
57+
* @var string|null
58+
* @since 3.0.4
59+
*/
60+
public $tableOptionsInit;
61+
/**
62+
* @var string|null
63+
* @since 3.0.4
64+
*/
65+
public $tableOptions;
5666

5767
protected $_schema;
5868

@@ -128,23 +138,14 @@ public function renderTable(): string
128138
{
129139
$output = '';
130140

131-
$tableOptionsSet = false;
132-
if ($this->generalSchema || $this->schema === self::SCHEMA_MYSQL) {
133-
$output .= <<<'PHP'
134-
$tableOptions = null;
135-
if ($this->db->driverName === 'mysql') {
136-
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
137-
}
138-
139-
140-
PHP;
141-
$tableOptionsSet = true;
141+
if ($this->tableOptionsInit !== null) {
142+
$output .= " {$this->tableOptionsInit}\n\n";
142143
}
143144
$output .= " \$this->createTable('" . $this->renderName() . "', [";
144145
foreach ($this->columns as $column) {
145146
$output .= "\n" . $column->render($this);
146147
}
147-
$output .= "\n ]" . ($tableOptionsSet ? ', $tableOptions' : '') . ");\n";
148+
$output .= "\n ]" . ($this->tableOptions !== null ? ", {$this->tableOptions}" : '') . ");\n";
148149

149150
return $output;
150151
}

tests/table/TableStructureTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace bizley\tests\table;
4+
5+
use bizley\migration\table\TableStructure;
6+
7+
class TableStructureTest extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testRenderNameNoPrefix(): void
10+
{
11+
$table = new TableStructure([
12+
'name' => 'test',
13+
'usePrefix' => false,
14+
]);
15+
$this->assertEquals('test', $table->renderName());
16+
}
17+
18+
public function testRenderNameWithPrefix(): void
19+
{
20+
$table = new TableStructure([
21+
'name' => 'test',
22+
]);
23+
$this->assertEquals('{{%test}}', $table->renderName());
24+
}
25+
26+
public function testRenderNameWithPrefixAlreadyInName(): void
27+
{
28+
$table = new TableStructure([
29+
'name' => 'prefix_test',
30+
'dbPrefix' => 'prefix_',
31+
]);
32+
$this->assertEquals('{{%test}}', $table->renderName());
33+
}
34+
35+
public function testRenderTableCreate(): void
36+
{
37+
$table = new TableStructure([
38+
'name' => 'test',
39+
]);
40+
$this->assertEquals(" \$this->createTable('{{%test}}', [\n ]);\n", $table->renderTable());
41+
}
42+
43+
public function testRenderTableCreateDefaultTableOptions(): void
44+
{
45+
$table = new TableStructure([
46+
'name' => 'test',
47+
'tableOptionsInit' => '$tableOptions = null;
48+
if ($this->db->driverName === \'mysql\') {
49+
$tableOptions = \'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB\';
50+
}',
51+
'tableOptions' => '$tableOptions',
52+
]);
53+
$this->assertEquals(" \$tableOptions = null;\n if (\$this->db->driverName === 'mysql') {\n \$tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';\n }\n\n \$this->createTable('{{%test}}', [\n ], \$tableOptions);\n", $table->renderTable());
54+
}
55+
}

0 commit comments

Comments
 (0)