Skip to content

Commit 238cc74

Browse files
author
Bizley
committed
table options
1 parent 27b311f commit 238cc74

File tree

5 files changed

+111
-23
lines changed

5 files changed

+111
-23
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
@@ -75,6 +75,18 @@ class Generator extends Component
7575
*/
7676
public $generalSchema = true;
7777

78+
/**
79+
* @var string|null
80+
* @since 2.3.4
81+
*/
82+
public $tableOptionsInit;
83+
84+
/**
85+
* @var string|null
86+
* @since 2.3.4
87+
*/
88+
public $tableOptions;
89+
7890
/**
7991
* Checks if DB connection is passed.
8092
* @throws InvalidConfigException
@@ -259,7 +271,9 @@ public function getTable()
259271
'primaryKey' => $this->getTablePrimaryKey(),
260272
'columns' => $this->getTableColumns($indexes),
261273
'foreignKeys' => $this->getTableForeignKeys(),
262-
'indexes' => $indexes
274+
'indexes' => $indexes,
275+
'tableOptionsInit' => $this->tableOptionsInit,
276+
'tableOptions' => $this->tableOptions,
263277
]);
264278
}
265279
return $this->_table;

src/controllers/MigrationController.php

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

3131
/**
3232
* @var string Default command action.
@@ -115,6 +115,27 @@ class MigrationController extends Controller
115115
*/
116116
public $skipMigrations = [];
117117

118+
/**
119+
* @var string|null String rendered in the create migration template to initialize table options.
120+
* By default it adds variable "$tableOptions" with optional collate configuration for MySQL DBMS to be used with
121+
* default $tableOptions.
122+
* Alias -O
123+
* @since 2.3.4
124+
*/
125+
public $tableOptionsInit = '$tableOptions = null;
126+
if ($this->db->driverName === \'mysql\') {
127+
$tableOptions = \'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB\';
128+
}';
129+
130+
/**
131+
* @var string|null String rendered in the create migration template for table options.
132+
* By default it renders "$tableOptions" to indicate that options should be taken from variable
133+
* set in $tableOptionsInit property.
134+
* Alias -o
135+
* @since 2.3.4
136+
*/
137+
public $tableOptions = '$tableOptions';
138+
118139
/**
119140
* @inheritdoc
120141
*/
@@ -126,7 +147,7 @@ public function options($actionID)
126147
switch ($actionID) {
127148
case 'create':
128149
case 'create-all':
129-
return array_merge($options, $createOptions);
150+
return array_merge($options, $createOptions, ['tableOptionsInit', 'tableOptions']);
130151
case 'update':
131152
case 'update-all':
132153
return array_merge($options, $createOptions, $updateOptions);
@@ -150,6 +171,8 @@ public function optionAliases()
150171
'P' => 'useTablePrefix',
151172
'h' => 'fixHistory',
152173
's' => 'showOnly',
174+
'O' => 'tableOptionsInit',
175+
'o' => 'tableOptions',
153176
]);
154177
}
155178

@@ -331,6 +354,8 @@ public function actionCreate($table)
331354
'className' => $className,
332355
'namespace' => $this->migrationNamespace,
333356
'generalSchema' => $this->generalSchema,
357+
'tableOptionsInit' => $this->tableOptionsInit,
358+
'tableOptions' => $this->tableOptions,
334359
]);
335360

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

src/table/TableStructure.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ class TableStructure extends Object
5454
*/
5555
public $dbPrefix;
5656
/**
57-
* @var string
57+
* @var string|null
58+
* @since 2.3.4
5859
*/
59-
public $tableOptionsInit = <<<'PHP'
60-
$tableOptions = null;
61-
if ($this->db->driverName === 'mysql') {
62-
$tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
63-
}
64-
PHP;
60+
public $tableOptionsInit;
61+
/**
62+
* @var string|null
63+
* @since 2.3.4
64+
*/
65+
public $tableOptions;
6566

6667
/**
6768
* Returns schema type.
@@ -137,23 +138,14 @@ public function renderTable()
137138
{
138139
$output = '';
139140

140-
$tableOptionsSet = false;
141-
if ($this->generalSchema || $this->schema === self::SCHEMA_MYSQL) {
142-
$output .= <<<'PHP'
143-
$tableOptions = null;
144-
if ($this->db->driverName === 'mysql') {
145-
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
146-
}
147-
148-
149-
PHP;
150-
$tableOptionsSet = true;
141+
if ($this->tableOptionsInit !== null) {
142+
$output .= " {$this->tableOptionsInit}\n\n";
151143
}
152144
$output .= " \$this->createTable('" . $this->renderName() . "', [";
153145
foreach ($this->columns as $column) {
154146
$output .= "\n" . $column->render($this);
155147
}
156-
$output .= "\n ]" . ($tableOptionsSet ? ', $tableOptions' : '') . ");\n";
148+
$output .= "\n ]" . ($this->tableOptions !== null ? ", {$this->tableOptions}" : '') . ");\n";
157149

158150
return $output;
159151
}

tests/table/TableStructureTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
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()
10+
{
11+
$table = new TableStructure([
12+
'name' => 'test',
13+
'usePrefix' => false,
14+
]);
15+
$this->assertEquals('test', $table->renderName());
16+
}
17+
18+
public function testRenderNameWithPrefix()
19+
{
20+
$table = new TableStructure([
21+
'name' => 'test',
22+
]);
23+
$this->assertEquals('{{%test}}', $table->renderName());
24+
}
25+
26+
public function testRenderNameWithPrefixAlreadyInName()
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()
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()
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)