Skip to content

Commit

Permalink
table options
Browse files Browse the repository at this point in the history
  • Loading branch information
Bizley committed Oct 29, 2018
1 parent 27b311f commit 238cc74
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ Starting with yii2-migration v2.0 it is possible to generate updating migration
| `generalSchema` | `g` | Whether to use general column schema instead of database specific (1). _default:_ `1`
| `fixHistory` | `h` | Whether to add migration history entry when migration is generated. _default:_ `0`
| `skipMigrations` | | List of migrations from the history table that should be skipped during the update process (2). _default:_ `[]`
| `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'; }`
| `tableOptions` | `o` | String rendered in the create migration template for table options. _default:_ `$tableOptions`

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

Expand Down
16 changes: 15 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ class Generator extends Component
*/
public $generalSchema = true;

/**
* @var string|null
* @since 2.3.4
*/
public $tableOptionsInit;

/**
* @var string|null
* @since 2.3.4
*/
public $tableOptions;

/**
* Checks if DB connection is passed.
* @throws InvalidConfigException
Expand Down Expand Up @@ -259,7 +271,9 @@ public function getTable()
'primaryKey' => $this->getTablePrimaryKey(),
'columns' => $this->getTableColumns($indexes),
'foreignKeys' => $this->getTableForeignKeys(),
'indexes' => $indexes
'indexes' => $indexes,
'tableOptionsInit' => $this->tableOptionsInit,
'tableOptions' => $this->tableOptions,
]);
}
return $this->_table;
Expand Down
31 changes: 28 additions & 3 deletions src/controllers/MigrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* Generates migration file based on the existing database table and previous migrations.
*
* @author Paweł Bizley Brzozowski
* @version 2.3.3
* @version 2.3.4
* @license Apache 2.0
* https://github.com/bizley/yii2-migration
*/
class MigrationController extends Controller
{
protected $version = '2.3.3';
protected $version = '2.3.4';

/**
* @var string Default command action.
Expand Down Expand Up @@ -115,6 +115,27 @@ class MigrationController extends Controller
*/
public $skipMigrations = [];

/**
* @var string|null String rendered in the create migration template to initialize table options.
* By default it adds variable "$tableOptions" with optional collate configuration for MySQL DBMS to be used with
* default $tableOptions.
* Alias -O
* @since 2.3.4
*/
public $tableOptionsInit = '$tableOptions = null;
if ($this->db->driverName === \'mysql\') {
$tableOptions = \'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB\';
}';

/**
* @var string|null String rendered in the create migration template for table options.
* By default it renders "$tableOptions" to indicate that options should be taken from variable
* set in $tableOptionsInit property.
* Alias -o
* @since 2.3.4
*/
public $tableOptions = '$tableOptions';

/**
* @inheritdoc
*/
Expand All @@ -126,7 +147,7 @@ public function options($actionID)
switch ($actionID) {
case 'create':
case 'create-all':
return array_merge($options, $createOptions);
return array_merge($options, $createOptions, ['tableOptionsInit', 'tableOptions']);
case 'update':
case 'update-all':
return array_merge($options, $createOptions, $updateOptions);
Expand All @@ -150,6 +171,8 @@ public function optionAliases()
'P' => 'useTablePrefix',
'h' => 'fixHistory',
's' => 'showOnly',
'O' => 'tableOptionsInit',
'o' => 'tableOptions',
]);
}

Expand Down Expand Up @@ -331,6 +354,8 @@ public function actionCreate($table)
'className' => $className,
'namespace' => $this->migrationNamespace,
'generalSchema' => $this->generalSchema,
'tableOptionsInit' => $this->tableOptionsInit,
'tableOptions' => $this->tableOptions,
]);

if ($generator->tableSchema === null) {
Expand Down
30 changes: 11 additions & 19 deletions src/table/TableStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ class TableStructure extends Object
*/
public $dbPrefix;
/**
* @var string
* @var string|null
* @since 2.3.4
*/
public $tableOptionsInit = <<<'PHP'
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
}
PHP;
public $tableOptionsInit;
/**
* @var string|null
* @since 2.3.4
*/
public $tableOptions;

/**
* Returns schema type.
Expand Down Expand Up @@ -137,23 +138,14 @@ public function renderTable()
{
$output = '';

$tableOptionsSet = false;
if ($this->generalSchema || $this->schema === self::SCHEMA_MYSQL) {
$output .= <<<'PHP'
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}


PHP;
$tableOptionsSet = true;
if ($this->tableOptionsInit !== null) {
$output .= " {$this->tableOptionsInit}\n\n";
}
$output .= " \$this->createTable('" . $this->renderName() . "', [";
foreach ($this->columns as $column) {
$output .= "\n" . $column->render($this);
}
$output .= "\n ]" . ($tableOptionsSet ? ', $tableOptions' : '') . ");\n";
$output .= "\n ]" . ($this->tableOptions !== null ? ", {$this->tableOptions}" : '') . ");\n";

return $output;
}
Expand Down
55 changes: 55 additions & 0 deletions tests/table/TableStructureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace bizley\tests\table;

use bizley\migration\table\TableStructure;

class TableStructureTest extends \PHPUnit\Framework\TestCase
{
public function testRenderNameNoPrefix()
{
$table = new TableStructure([
'name' => 'test',
'usePrefix' => false,
]);
$this->assertEquals('test', $table->renderName());
}

public function testRenderNameWithPrefix()
{
$table = new TableStructure([
'name' => 'test',
]);
$this->assertEquals('{{%test}}', $table->renderName());
}

public function testRenderNameWithPrefixAlreadyInName()
{
$table = new TableStructure([
'name' => 'prefix_test',
'dbPrefix' => 'prefix_',
]);
$this->assertEquals('{{%test}}', $table->renderName());
}

public function testRenderTableCreate()
{
$table = new TableStructure([
'name' => 'test',
]);
$this->assertEquals(" \$this->createTable('{{%test}}', [\n ]);\n", $table->renderTable());
}

public function testRenderTableCreateDefaultTableOptions()
{
$table = new TableStructure([
'name' => 'test',
'tableOptionsInit' => '$tableOptions = null;
if ($this->db->driverName === \'mysql\') {
$tableOptions = \'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB\';
}',
'tableOptions' => '$tableOptions',
]);
$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());
}
}

0 comments on commit 238cc74

Please sign in to comment.