Skip to content

Commit

Permalink
add draft split view migration
Browse files Browse the repository at this point in the history
  • Loading branch information
howyi committed Nov 9, 2017
1 parent f2697b5 commit d2a4f65
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/Conv/Generator/TableAlterMigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static function generate(
): TableAlterMigration {
// DROP-INDEX → DROP → MODIFY → ADD → ADD-INDEX の順でマイグレーションを生成する

// カラムの変更後配列
$renamedNameList = [];

// 全ての消滅したカラム配列
$missingColumnList = $beforeTable->getDiffColumnList($afterTable);
// 全ての追加したカラム配列
Expand Down Expand Up @@ -70,6 +73,7 @@ public static function generate(
);
}
$renamedFieldList[$missingField] = $renamedField;
$renamedNameList[] = [$beforeTable->tableName, $renamedField];
$addedFieldList = array_diff($addedFieldList, [$renamedField]);
}
$droppedModifiedColumnList = $beforeTable->getModifiedColumnList($droppedFieldList);
Expand Down Expand Up @@ -162,7 +166,7 @@ public static function generate(
);
}

$tableAlterMigration = new TableAlterMigration($beforeTable->getTableName(), $afterTable->getTableName(), $migrationLineList);
$tableAlterMigration = new TableAlterMigration($beforeTable->getTableName(), $afterTable->getTableName(), $migrationLineList, $renamedNameList);

if ($tableAlterMigration->isAltered()) {
// Display
Expand Down
12 changes: 11 additions & 1 deletion src/Conv/Migration/Database/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Conv\Migration\Database;

use Conv\Migration\Table\TableMigrationInterface;
use Conv\Migration\Table\ViewAlterMigration;

class Migration
{
Expand All @@ -13,7 +14,16 @@ class Migration
*/
public function add(TableMigrationInterface $migration)
{
return $this->migrationList[] = $migration;
$this->migrationList[] = $migration;
}

/**
* @param ViewAlterMigration $migration
*/
public function addSplit(ViewAlterMigration $migration)
{
// array_unshift($this->migrationList, new ViewAlterOnlyDownMigration($migration));
// $this->migrationList[] = new ViewAlterOnlyUpMigration($migration);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Conv/Migration/Table/TableAlterMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
class TableAlterMigration extends AbstractTableMigration
{
private $isAltered = false;
private $renamedNameList;

/**
* @param string $beforeTableName
* @param string $afterTableName
* @param MigrationLineList $migrationLineList
* @param array $renamedNameList
*/
public function __construct(
string $beforeTableName,
string $afterTableName,
MigrationLineList $migrationLineList
MigrationLineList $migrationLineList,
array $renamedNameList
) {
$this->tableName = $beforeTableName;
$this->type = MigrationType::ALTER;
$this->renamedNameList = $renamedNameList;

$this->isAltered = $migrationLineList->isMigratable();

Expand All @@ -48,4 +52,12 @@ public function isAltered(): bool
{
return $this->isAltered;
}

/**
* @return array
*/
public function renamedNameList(): array
{
return $this->renamedNameList;
}
}
23 changes: 22 additions & 1 deletion src/Conv/Migration/Table/ViewAlterMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
class ViewAlterMigration extends AbstractTableMigration
{
private $isAltered = false;
private $isSplit = false;

/**
* @param ViewStructureInterface $beforeView
* @param ViewStructureInterface $afterView
* @param MigrationLineList $migrationLineList
* @param array $allRenamedNameList
*/
public function __construct(
ViewStructureInterface $beforeView,
ViewStructureInterface $afterView
ViewStructureInterface $afterView,
array $allRenamedNameList
) {
$this->tableName = $afterView->getViewName();
$this->type = MigrationType::CREATE_OR_REPLACE;
Expand All @@ -33,6 +36,16 @@ public function __construct(
$this->up = preg_replace('/CREATE/', 'CREATE OR REPLACE', $afterView->getCreateQuery());
$this->down = preg_replace('/CREATE/', 'CREATE OR REPLACE', $beforeView->getCreateQuery());

foreach ($allRenamedNameList as $renamedNameList) {
$count = 0;
foreach ($renamedNameList as $name) {
$count += (strpos($this->up, $name) === false) ? 0 : 1;
}
if ($count === count($renamedNameList)) {
$this->isSplit = true;
}
}

$beforeName = $beforeView->getViewName();
$afterName = $afterView->getViewName();

Expand All @@ -46,4 +59,12 @@ public function isAltered(): bool
{
return $this->isAltered;
}

/**
* @return bool
*/
public function isSplit(): bool
{
return $this->isSplit;
}
}
21 changes: 19 additions & 2 deletions src/Conv/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static function generate(
): Migration {
// DROP → MODIFY → ADD の順でマイグレーションを生成する

// テーブル、もしくはカラムの変更後配列
$allRenamedNameList = [];

// 全ての消滅したテーブル配列
$missingTableList = $beforeDatabase->getDiffTableList($afterDatabase);
// 全ての追加したテーブル配列
Expand Down Expand Up @@ -67,6 +70,7 @@ public static function generate(
);
}
$renamedTableNameList[$missingTableName] = $renamedTableName;
$allRenamedNameList[] = [$renamedTableName];
$addedTableNameList = array_diff($addedTableNameList, [$renamedTableName]);
}

Expand Down Expand Up @@ -133,6 +137,7 @@ public static function generate(
$afterDatabase->getTableList()[$tableName],
$operator
);
$allRenamedNameList = array_merge($allRenamedNameList, $tableAlterMigration->renamedNameList());
if (!$tableAlterMigration->isAltered()) {
continue;
}
Expand All @@ -145,6 +150,7 @@ public static function generate(
$afterDatabase->getTableList()[$afterTableName],
$operator
);
$allRenamedNameList = array_merge($allRenamedNameList, $tableAlterMigration->renamedNameList());
$migration->add($tableAlterMigration);
}

Expand All @@ -155,11 +161,17 @@ public static function generate(
}
$viewAlterMigration = new ViewAlterMigration(
$beforeView,
$afterDatabase->getTableList()[$viewName]
$afterDatabase->getTableList()[$viewName],
$allRenamedNameList
);
if (!$viewAlterMigration->isAltered()) {
continue;
}
// if ($viewAlterMigration->isSplit()) {
// $migration->addSplit($viewAlterMigration);
// } else {
// $migration->add($viewAlterMigration);
// }
$migration->add($viewAlterMigration);
}

Expand All @@ -168,10 +180,15 @@ public static function generate(
$afterView = $afterDatabase->getTableList()[$afterViewName];
$migration->add(new ViewRenameMigration($beforeView, $afterView));

$viewAlterMigration = new ViewAlterMigration($beforeView, $afterView);
$viewAlterMigration = new ViewAlterMigration($beforeView, $afterView, $allRenamedNameList);
if (!$viewAlterMigration->isAltered()) {
continue;
}
// if ($viewAlterMigration->isSplit()) {
// $migration->addSplit($viewAlterMigration);
// } else {
// $migration->add($viewAlterMigration);
// }
$migration->add($viewAlterMigration);
}

Expand Down
33 changes: 28 additions & 5 deletions tests/Conv/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use Conv\Migration\Table\TableAlterMigration;
use Conv\Migration\Table\TableCreateMigration;
use Conv\Migration\Table\TableDropMigration;
use Conv\Migration\Table\ViewDropMigration;
use Conv\Migration\Table\ViewAlterMigration;
use Conv\Migration\Table\ViewAlterOnlyDownMigration;
use Conv\Migration\Table\ViewAlterOnlyUpMigration;
use Conv\Migration\Table\ViewCreateMigration;
use Conv\Migration\Table\ViewDropMigration;
use Conv\Migration\Table\ViewRenameMigration;
use Conv\Structure\DatabaseStructure;
use Conv\Structure\TableStructure;
Expand All @@ -25,7 +27,12 @@ class MigrationGeneratorTest extends \PHPUnit\Framework\TestCase

protected function setup()
{
$this->pdo = new \PDO("mysql:host=localhost;dbname=conv_test;charset=utf8;", 'root', '');
$this->pdo = new \PDO(
"mysql:host=localhost;dbname=conv_test;charset=utf8;",
'root',
'',
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]
);
$this->prophet = new \Prophecy\Prophet;
}

Expand Down Expand Up @@ -58,9 +65,6 @@ public function testGenerate($dir, $calls, $expected)
$expectStructure,
$operator->reveal()
);
for ($i = 0; $i < count($alter->getMigrationList()); $i++) {
$this->assertInstanceOf($expected[$i], $alter->getMigrationList()[$i]);
}
foreach ($alter->getMigrationList() as $migration) {
$this->pdo->exec($migration->getUp());
}
Expand All @@ -70,6 +74,9 @@ public function testGenerate($dir, $calls, $expected)
foreach ($alter->getMigrationList() as $migration) {
$this->pdo->exec($migration->getUp());
}
for ($i = 0; $i < count($alter->getMigrationList()); $i++) {
$this->assertInstanceOf($expected[$i], $alter->getMigrationList()[$i]);
}
}

public function generateProvider()
Expand Down Expand Up @@ -162,6 +169,22 @@ public function generateProvider()
TableAlterMigration::class,
]
],
// [
// 'tests/Retort/test_schema/007',
// [
// [
// 'message' => 'Column tbl_user.name is missing. Choose an action.',
// 'return' => 'renamed (user_name)',
// ]
// ],
// [
// ViewAlterOnlyDownMigration::class,
// ViewAlterOnlyDownMigration::class,
// TableAlterMigration::class,
// ViewAlterOnlyUpMigration::class,
// ViewAlterOnlyUpMigration::class,
// ]
// ],
];
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Retort.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ test_schema/005/view_user3.yml: "type: view\nalgorithm: merge\nalias:\n tbl_use
test_schema/006/sushi_log.yml: "comment: 'Sushi log table'\ncolumn:\n log_id:\n type: int(11)\n comment: 'auto_increment ID'\n attribute:\n - auto_increment\n - unsigned\n user_id:\n type: int(11)\n comment: 'User ID'\n sushi_id:\n type: int(11)\n comment: 'Sushi ID'\n date:\n type: datetime\n comment: 'Eat date'\nprimary_key:\n - log_id\npartition:\n by: list\n value: log_id\n list:\n p0:\n in: '0'\n p1:\n in: '1'\n p2:\n in: '2'\n p3:\n in: '3'\n p4:\n in: '4,6'\n p5:\n in: '5'\n"
test_schema/006/tbl_country2.yml: "comment: 'Country table'\ncolumn:\n country_id:\n type: int(11)\n comment: 'Country id'\n country_name:\n type: varchar(255)\n comment: 'Country Name'\nprimary_key:\n - country_id\nengine: InnoDB\ndefault_charset: utf8mb4\ncollate: utf8mb4_bin\n"
test_schema/006/tbl_country3.yml: "comment: 'Country table'\ncolumn:\n country_id:\n type: int(11)\n comment: 'Country id'\n country_name:\n type: varchar(255)\n comment: 'Country Name'\nprimary_key:\n - country_id\nengine: InnoDB\ndefault_charset: utf8mb4\ncollate: utf8mb4_bin\n"
test_schema/006/tbl_music.yml: "comment: 'Music management table'\ncolumn:\n music_id:\n type: int(10)\n comment: 'Unique music ID'\n music_name:\n type: varchar(22)\n comment: 'Music name'\n description:\n type: varchar(22)\n comment: 'Music name'\n released_at:\n type: datetime\n comment: 'Music released date'\nprimary_key:\n - music_id\n - name(100)\nengine: memory\ndefault_charset: latin1\ncollate: utf8mb4_general_ci\n"
test_schema/006/tbl_music.yml: "comment: 'Music management table'\ncolumn:\n music_id:\n type: int(10)\n comment: 'Unique music ID'\n music_name:\n type: varchar(22)\n comment: 'Music name'\n description:\n type: varchar(22)\n comment: 'Music name'\n released_at:\n type: datetime\n comment: 'Music released date'\nprimary_key:\n - music_id\nengine: MyISAM\ndefault_charset: latin1\ncollate: latin1_general_ci\n"
test_schema/006/tbl_user.yml: "comment: 'User management table'\ncolumn:\n user_id:\n type: int(11)\n comment: 'User ID'\n name:\n type: varchar(255)\n comment: 'User name'\n attribute:\n - nullable\n age:\n type: int(11)\n comment: 'User age'\n attribute:\n - nullable\nprimary_key:\n - user_id\n"
test_schema/006/tbl_user_address.yml: "comment: 'User address management table'\ncolumn:\n user_id:\n type: int(11)\n comment: 'User ID'\n address_line:\n type: varchar(255)\n comment: 'User address'\n city:\n type: varchar(255)\n comment: 'User city'\n zip_code:\n type: int(11)\n comment: 'User zip code'\n country_id:\n type: int(11)\n comment: 'Country id'\nprimary_key:\n - user_id\n"
test_schema/006/unagi_log.yml: "comment: 'Unagi log table'\ncolumn:\n log_id:\n type: int(11)\n comment: 'auto_increment ID'\n attribute:\n - auto_increment\n - unsigned\n user_id:\n type: int(11)\n comment: 'User ID'\n sushi_id:\n type: int(11)\n comment: 'Unagi ID'\n date:\n type: datetime\n comment: 'Eat date'\nprimary_key:\n - log_id\npartition:\n by: key\n value: '`log_id`'\n num: 6\n"
test_schema/006/view_user.yml: "type: view\nalgorithm: merge\nalias:\n tbl_user: tu\n tbl_user_address: tua\n tbl_country: tc\ncolumn:\n user_id: tu.user_id\n name: tu.name\n address_line: tua.address_line\n zip_code: tua.zip_code\n country_name: tc.country_name\nfrom:\n reference: tu\n joins:\n - join:\n factor: tua\n on: tu.user_id = tua.user_id\n - left_join:\n factor: tc\n on: tua.country_id = tc.country_id\n"
test_schema/006/view_user3.yml: "type: view\nalgorithm: merge\nalias:\n tbl_user: tu\n tbl_user_address: tua\n tbl_country2: tc\ncolumn:\n user_id: tu.user_id\n name: tu.name\n address_line: tua.address_line\n zip_code: tua.zip_code\n country_name: tc.country_name\nfrom:\n reference: tu\n joins:\n - join:\n factor: tua\n on: tu.user_id = tua.user_id\n - left_join:\n factor: tc\n on: tua.country_id = tc.country_id\n"
test_schema/007/sushi_log.yml: "comment: 'Sushi log table'\ncolumn:\n log_id:\n type: int(11)\n comment: 'auto_increment ID'\n attribute:\n - auto_increment\n - unsigned\n user_id:\n type: int(11)\n comment: 'User ID'\n sushi_id:\n type: int(11)\n comment: 'Sushi ID'\n date:\n type: datetime\n comment: 'Eat date'\nprimary_key:\n - log_id\npartition:\n by: list\n value: log_id\n list:\n p0:\n in: '0'\n p1:\n in: '1'\n p2:\n in: '2'\n p3:\n in: '3'\n p4:\n in: '4,6'\n p5:\n in: '5'\n"
test_schema/007/tbl_country2.yml: "comment: 'Country table'\ncolumn:\n country_id:\n type: int(11)\n comment: 'Country id'\n country_name:\n type: varchar(255)\n comment: 'Country Name'\nprimary_key:\n - country_id\nengine: InnoDB\ndefault_charset: utf8mb4\ncollate: utf8mb4_bin\n"
test_schema/007/tbl_country3.yml: "comment: 'Country table'\ncolumn:\n country_id:\n type: int(11)\n comment: 'Country id'\n country_name:\n type: varchar(255)\n comment: 'Country Name'\nprimary_key:\n - country_id\nengine: InnoDB\ndefault_charset: utf8mb4\ncollate: utf8mb4_bin\n"
test_schema/007/tbl_music.yml: "comment: 'Music management table'\ncolumn:\n music_id:\n type: int(10)\n comment: 'Unique music ID'\n music_name:\n type: varchar(22)\n comment: 'Music name'\n description:\n type: varchar(22)\n comment: 'Music name'\n released_at:\n type: datetime\n comment: 'Music released date'\nprimary_key:\n - music_id\nengine: MyISAM\ndefault_charset: latin1\ncollate: latin1_general_ci\n"
test_schema/007/tbl_user.yml: "comment: 'User management table'\ncolumn:\n user_id:\n type: int(11)\n comment: 'User ID'\n user_name:\n type: varchar(255)\n comment: 'User name'\n attribute:\n - nullable\n age:\n type: int(11)\n comment: 'User age'\n attribute:\n - nullable\nprimary_key:\n - user_id\n"
test_schema/007/tbl_user_address.yml: "comment: 'User address management table'\ncolumn:\n user_id:\n type: int(11)\n comment: 'User ID'\n address_line:\n type: varchar(255)\n comment: 'User address'\n city:\n type: varchar(255)\n comment: 'User city'\n zip_code:\n type: int(11)\n comment: 'User zip code'\n country_id:\n type: int(11)\n comment: 'Country id'\nprimary_key:\n - user_id\n"
test_schema/007/unagi_log.yml: "comment: 'Unagi log table'\ncolumn:\n log_id:\n type: int(11)\n comment: 'auto_increment ID'\n attribute:\n - auto_increment\n - unsigned\n user_id:\n type: int(11)\n comment: 'User ID'\n sushi_id:\n type: int(11)\n comment: 'Unagi ID'\n date:\n type: datetime\n comment: 'Eat date'\nprimary_key:\n - log_id\npartition:\n by: key\n value: '`log_id`'\n num: 6\n"
test_schema/007/view_user.yml: "type: view\nalgorithm: merge\nalias:\n tbl_user: tu\n tbl_user_address: tua\n tbl_country: tc\ncolumn:\n user_id: tu.user_id\n name: tu.user_name\n address_line: tua.address_line\n zip_code: tua.zip_code\n country_name: tc.country_name\nfrom:\n reference: tu\n joins:\n - join:\n factor: tua\n on: tu.user_id = tua.user_id\n - left_join:\n factor: tc\n on: tua.country_id = tc.country_id\n"
test_schema/007/view_user3.yml: "type: view\nalgorithm: merge\nalias:\n tbl_user: tu\n tbl_user_address: tua\n tbl_country2: tc\ncolumn:\n user_id: tu.user_id\n name: tu.user_name\n address_line: tua.address_line\n zip_code: tua.zip_code\n country_name: tc.country_name\nfrom:\n reference: tu\n joins:\n - join:\n factor: tua\n on: tu.user_id = tua.user_id\n - left_join:\n factor: tc\n on: tua.country_id = tc.country_id\n"
test_schema/draft/tbl_music.yml: "comment: 'Music management table'\ncolumn:\n music_id:\n type: int(10)\n comment: 'Unique music ID'\n name:\n type: varchar(255)\n comment: 'Music name'\n released_at:\n type: datetime\n comment: 'Music released date'\nprimary_key:\n - music_id\n"
test_schema/draft/tbl_user.yml: "comment: 'User management table'\ncolumn:\n user_id:\n type: int(11)\n comment: 'User ID'\n name:\n type: int(11)\n comment: 'User name'\n attribute: [nullable]\n age:\n type: int(11)\n comment: 'User age'\n attribute: [nullable]\nprimary_key:\n - user_id\nindex:\n name:\n is_unique: true\n column: [name]\n"

0 comments on commit d2a4f65

Please sign in to comment.