Skip to content

Commit

Permalink
Merge pull request #23 from shoji-mochizuki/add-force-drop-option
Browse files Browse the repository at this point in the history
generate関数に強制Dropオプション追加
  • Loading branch information
howyi authored Nov 2, 2018
2 parents 8394871 + 3d61ef5 commit e51cf49
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
8 changes: 6 additions & 2 deletions docs/en/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ Generate migration is Before `DatabaseStructure` and After `DatabaseStructure` p

2. Generate migration
```php
// Param: BeforeDBS, AfterDBS, Operator
// Param: BeforeDBS, AfterDBS, Operator, [bool(forceDrop)]
$alter = MigrationGenerator::generate(
$actualDbs,
$schemaDbs,
new \Conv\Operator($this->getHelper('question'), $input, $output)
new \Conv\Operator($this->getHelper('question'), $input, $output),
false
);
```

Expand All @@ -79,6 +80,9 @@ Migration have `::getUp()` と `::getDown()`.
- getUp to convert after database query.
- getDown to back before database query.

Forced Drop option
- true: Select Droped without interactive selection when changing column name.
- false: Choose Droped or rename in a interactive selection.

```
foreach ($alter->getMigrationList() as $migration) {
Expand Down
9 changes: 7 additions & 2 deletions docs/ja/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ default:

2. マイグレーションを生成
```php
// 引数は 変更前, 変更後, Operator の順とする。
// 引数は 変更前, 変更後, Operator, [強制Dropオプション]の順とする。
$alter = MigrationGenerator::generate(
$actualDbs,
$schemaDbs,
new \Conv\Operator($this->getHelper('question'), $input, $output)
new \Conv\Operator($this->getHelper('question'), $input, $output),
false
);
```

Expand All @@ -79,6 +80,10 @@ $alter = MigrationGenerator::generate(
- getDownで変更をもとに戻すクエリ
を取得できる。

強制Dropオプション
- true: カラム名変更時に会話形式選択をせずにDropedを選択する
- false: Dropedかrenameを会話形式で選択する

```
foreach ($alter->getMigrationList() as $migration) {
var_dump($migration->getUp());
Expand Down
9 changes: 8 additions & 1 deletion src/Conv/Generator/TableAlterMigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class TableAlterMigrationGenerator
* @param TableStructure $beforeTable
* @param TableStructure $afterTable
* @param Operator $operator
* @param bool $forceDrop
* @return TableMigration
*/
public static function generate(
TableStructure $beforeTable,
TableStructure $afterTable,
Operator $operator
Operator $operator,
bool $forceDrop = false
): TableAlterMigration {
// DROP-INDEX → DROP → MODIFY → ADD → ADD-INDEX の順でマイグレーションを生成する

Expand All @@ -57,6 +59,11 @@ public static function generate(
continue;
}

if ($forceDrop) {
$droppedFieldList[] = $missingField;
continue;
}

$answer = $operator->choiceQuestion(
sprintf('Column %s.%s is missing. Choose an action.', $beforeTable->tableName, $missingField),
['dropped', sprintf('renamed (%s)', implode(', ', $addedFieldList))]
Expand Down
20 changes: 17 additions & 3 deletions src/Conv/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class MigrationGenerator
* @param DatabaseStructure $beforeDatabase
* @param DatabaseStructure $afterDatabase
* @param Operator $operator
* @param bool $forceDrop
* @return Migration
*/
public static function generate(
DatabaseStructure $beforeDatabase,
DatabaseStructure $afterDatabase,
Operator $operator
Operator $operator,
bool $forceDrop = false
): Migration {
// DROP → MODIFY → ADD の順でマイグレーションを生成する

Expand All @@ -52,6 +54,11 @@ public static function generate(
continue;
}

if ($forceDrop) {
$droppedTableNameList[] = $missingTableName;
continue;
}

$answer = $operator->choiceQuestion(
sprintf('Table %s is missing. Choose an action.', $missingTableName),
['dropped', sprintf('renamed (%s)', implode(', ', $addedTableNameList))]
Expand Down Expand Up @@ -92,6 +99,11 @@ public static function generate(
continue;
}

if ($forceDrop) {
$droppedViewNameList[] = $missingViewName;
continue;
}

$answer = $operator->choiceQuestion(
sprintf('View %s is missing. Choose an action.', $missingViewName),
['dropped', sprintf('renamed (%s)', implode(', ', $addedViewNameList))]
Expand Down Expand Up @@ -135,7 +147,8 @@ public static function generate(
$tableAlterMigration = TableAlterMigrationGenerator::generate(
$beforeTable,
$afterDatabase->getTableList()[$tableName],
$operator
$operator,
$forceDrop
);
$allRenamedNameList = array_merge($allRenamedNameList, $tableAlterMigration->renamedNameList());
if (!$tableAlterMigration->isAltered()) {
Expand All @@ -148,7 +161,8 @@ public static function generate(
$tableAlterMigration = TableAlterMigrationGenerator::generate(
$beforeDatabase->getTableList()[$beforeTableName],
$afterDatabase->getTableList()[$afterTableName],
$operator
$operator,
$forceDrop
);
$allRenamedNameList = array_merge($allRenamedNameList, $tableAlterMigration->renamedNameList());
$migration->add($tableAlterMigration);
Expand Down

0 comments on commit e51cf49

Please sign in to comment.