Browse Source

Merge remote-tracking branch 'upstream/master'

tags/2.0.7
Tom Worster 9 years ago
parent
commit
5da830d4ca
  1. 13
      docs/guide/db-migrations.md
  2. 1
      framework/CHANGELOG.md
  3. 18
      framework/captcha/CaptchaAction.php
  4. 9
      framework/console/controllers/BaseMigrateController.php
  5. 8
      framework/console/controllers/MigrateController.php
  6. 0
      framework/views/createTableMigration.php
  7. 0
      framework/views/dropTableMigration.php
  8. 86
      tests/framework/console/controllers/MigrateControllerTestTrait.php

13
docs/guide/db-migrations.md

@ -274,7 +274,7 @@ class m150811_220037_create_post extends Migration
### Drop Table ### Drop Table
```php ```php
yii migrate/create drop_post yii migrate/create drop_post --fields=title:string(12):notNull:unique,body:text
``` ```
generates generates
@ -290,6 +290,9 @@ class m150811_220037_drop_post extends Migration
public function down() public function down()
{ {
$this->createTable('post', [ $this->createTable('post', [
'id' => $this->primaryKey(),
'title' => $this->string(12)->notNull()->unique(),
'body' => $this->text()
]); ]);
} }
} }
@ -614,10 +617,10 @@ The migration command comes with a few command-line options that can be used to
named `$className` to get the migration class name. named `$className` to get the migration class name.
* `generatorTemplateFiles`: array (defaults to `[ * `generatorTemplateFiles`: array (defaults to `[
'create' => '@yii/views/createMigration.php', 'create_table' => '@yii/views/createTableMigration.php',
'drop' => '@yii/views/dropMigration.php', 'drop_table' => '@yii/views/dropTableMigration.php',
'add' => '@yii/views/addMigration.php', 'add_column' => '@yii/views/addColumnMigration.php',
'remove' => '@yii/views/removeMigration.php', 'drop_column' => '@yii/views/dropColumnMigration.php',
'create_junction' => '@yii/views/createJunctionMigration.php' 'create_junction' => '@yii/views/createJunctionMigration.php'
]`), specifies template files for generating migration code. See "[Generating Migrations](#generating-migrations)" ]`), specifies template files for generating migration code. See "[Generating Migrations](#generating-migrations)"
for more details. for more details.

1
framework/CHANGELOG.md

@ -44,6 +44,7 @@ Yii Framework 2 Change Log
- Enh #4104: Added `yii\filters\auth\AuthMetod::optional` for optional authentification in all child classes (SilverFire) - Enh #4104: Added `yii\filters\auth\AuthMetod::optional` for optional authentification in all child classes (SilverFire)
- Enh #7566: Improved `\yii\validators\CompareValidator` default messages (slinstj) - Enh #7566: Improved `\yii\validators\CompareValidator` default messages (slinstj)
- Enh #7581: Added ability to specify range using anonymous function in `RangeValidator` (RomeroMsk) - Enh #7581: Added ability to specify range using anonymous function in `RangeValidator` (RomeroMsk)
- Enh #8284: Added `\yii\captcha\CaptchaAction::$imageLibrary` property allowing to set image rendering library (AnatolyRugalev)
- Enh #8613: `yii\widgets\FragmentCache` will not store empty content anymore which fixes some problems related to `yii\filters\PageCache` (kidol) - Enh #8613: `yii\widgets\FragmentCache` will not store empty content anymore which fixes some problems related to `yii\filters\PageCache` (kidol)
- Enh #8649: Added total applied migrations to final report (vernik91) - Enh #8649: Added total applied migrations to final report (vernik91)
- Enh #9282: Improved JSON error handling to support PHP 5.5 error codes (freezy-sk) - Enh #9282: Improved JSON error handling to support PHP 5.5 error codes (freezy-sk)

18
framework/captcha/CaptchaAction.php

@ -98,6 +98,12 @@ class CaptchaAction extends Action
* If not set, it means the verification code will be randomly generated. * If not set, it means the verification code will be randomly generated.
*/ */
public $fixedVerifyCode; public $fixedVerifyCode;
/**
* @var string the rendering library to use. Currently supported only 'gd' and 'imagick'.
* If not set, library will be determined automatically.
* @since 2.0.7
*/
public $imageLibrary;
/** /**
@ -236,13 +242,21 @@ class CaptchaAction extends Action
* Renders the CAPTCHA image. * Renders the CAPTCHA image.
* @param string $code the verification code * @param string $code the verification code
* @return string image contents * @return string image contents
* @throws InvalidConfigException if imageLibrary is not supported
*/ */
protected function renderImage($code) protected function renderImage($code)
{ {
if (Captcha::checkRequirements() === 'gd') { if (isset($this->imageLibrary)) {
return $this->renderImageByGD($code); $imageLibrary = $this->imageLibrary;
} else { } else {
$imageLibrary = Captcha::checkRequirements();
}
if ($imageLibrary === 'gd') {
return $this->renderImageByGD($code);
} elseif ($imageLibrary === 'imagick') {
return $this->renderImageByImagick($code); return $this->renderImageByImagick($code);
} else {
throw new InvalidConfigException("Defined library '{$imageLibrary}' is not supported");
} }
} }

9
framework/console/controllers/BaseMigrateController.php

@ -511,26 +511,27 @@ abstract class BaseMigrateController extends Controller
'field_second' => $secondTable, 'field_second' => $secondTable,
]); ]);
} elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) { } elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) {
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['add']), [ $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['add_column']), [
'className' => $className, 'className' => $className,
'table' => mb_strtolower($matches[2], Yii::$app->charset), 'table' => mb_strtolower($matches[2], Yii::$app->charset),
'fields' => $this->fields 'fields' => $this->fields
]); ]);
} elseif (preg_match('/^drop_(.+)_from_(.+)$/', $name, $matches)) { } elseif (preg_match('/^drop_(.+)_from_(.+)$/', $name, $matches)) {
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['remove']), [ $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop_column']), [
'className' => $className, 'className' => $className,
'table' => mb_strtolower($matches[2], Yii::$app->charset), 'table' => mb_strtolower($matches[2], Yii::$app->charset),
'fields' => $this->fields 'fields' => $this->fields
]); ]);
} elseif (preg_match('/^create_(.+)$/', $name, $matches)) { } elseif (preg_match('/^create_(.+)$/', $name, $matches)) {
$this->addDefaultPrimaryKey(); $this->addDefaultPrimaryKey();
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create']), [ $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create_table']), [
'className' => $className, 'className' => $className,
'table' => mb_strtolower($matches[1], Yii::$app->charset), 'table' => mb_strtolower($matches[1], Yii::$app->charset),
'fields' => $this->fields 'fields' => $this->fields
]); ]);
} elseif (preg_match('/^drop_(.+)$/', $name, $matches)) { } elseif (preg_match('/^drop_(.+)$/', $name, $matches)) {
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop']), [ $this->addDefaultPrimaryKey();
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop_table']), [
'className' => $className, 'className' => $className,
'table' => mb_strtolower($matches[1], Yii::$app->charset), 'table' => mb_strtolower($matches[1], Yii::$app->charset),
'fields' => $this->fields 'fields' => $this->fields

8
framework/console/controllers/MigrateController.php

@ -67,10 +67,10 @@ class MigrateController extends BaseMigrateController
* @inheritdoc * @inheritdoc
*/ */
public $generatorTemplateFiles = [ public $generatorTemplateFiles = [
'create' => '@yii/views/createMigration.php', 'create_table' => '@yii/views/createTableMigration.php',
'drop' => '@yii/views/dropMigration.php', 'drop_table' => '@yii/views/dropTableMigration.php',
'add' => '@yii/views/addColumnMigration.php', 'add_column' => '@yii/views/addColumnMigration.php',
'remove' => '@yii/views/dropColumnMigration.php', 'drop_column' => '@yii/views/dropColumnMigration.php',
'create_junction' => '@yii/views/createJunctionMigration.php' 'create_junction' => '@yii/views/createJunctionMigration.php'
]; ];
/** /**

0
framework/views/createMigration.php → framework/views/createTableMigration.php

0
framework/views/dropMigration.php → framework/views/dropTableMigration.php

86
tests/framework/console/controllers/MigrateControllerTestTrait.php

@ -109,6 +109,18 @@ CODE;
} }
/** /**
* Change class name migration to $class
* @param string $class name class
* @return string content generated class migration
* @see https://github.com/yiisoft/yii2/pull/10213
*/
protected function parseNameClassMigration($class)
{
$files = FileHelper::findFiles($this->migrationPath);
return preg_replace('/class (m\d+_\d+) extents Migration {/', "class $class extends Migration {", file_get_contents($files[0]));
}
/**
* Checks if applied migration history matches expected one. * Checks if applied migration history matches expected one.
* @param array $expectedMigrations migration names in expected order * @param array $expectedMigrations migration names in expected order
* @param string $message failure message * @param string $message failure message
@ -154,7 +166,7 @@ CODE;
$migrationName = 'DefaultTest'; $migrationName = 'DefaultTest';
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [$migrationName]); $this->runMigrateControllerAction('create', [$migrationName]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$newLine = '\n'; $newLine = '\n';
$code = <<<CODE $code = <<<CODE
@ -189,7 +201,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
} }
public function testGenerateCreateMigration() public function testGenerateCreateMigration()
@ -203,7 +215,7 @@ CODE;
'body:text:notNull' 'body:text:notNull'
] ]
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -228,7 +240,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [ $this->runMigrateControllerAction('create', [
@ -239,7 +251,7 @@ CODE;
], ],
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -262,7 +274,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [ $this->runMigrateControllerAction('create', [
@ -270,7 +282,7 @@ CODE;
'fields' => [ 'fields' => [
], ],
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -292,7 +304,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
} }
public function testGenerateDropMigration() public function testGenerateDropMigration()
@ -302,8 +314,40 @@ CODE;
$this->runMigrateControllerAction('create', [ $this->runMigrateControllerAction('create', [
$migrationName $migrationName
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
use yii\db\Migration;
class {$class} extends Migration
{
public function up()
{
\$this->dropTable('test');
}
public function down()
{
\$this->createTable('test', [
'id' => \$this->primaryKey()
]);
}
}
CODE;
$this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [
$migrationName,
'fields' => [
'body:text:notNull'
],
]);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -319,12 +363,14 @@ class {$class} extends Migration
public function down() public function down()
{ {
\$this->createTable('test', [ \$this->createTable('test', [
'id' => \$this->primaryKey(),
'body' => \$this->text()->notNull()
]); ]);
} }
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [ $this->runMigrateControllerAction('create', [
@ -335,7 +381,7 @@ CODE;
], ],
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -358,7 +404,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
} }
public function testGenerateAddColumnMigration() public function testGenerateAddColumnMigration()
@ -373,7 +419,7 @@ CODE;
'created_at:dateTime' 'created_at:dateTime'
] ]
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -398,7 +444,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
} }
public function testGenerateDropColumnMigration() public function testGenerateDropColumnMigration()
@ -413,7 +459,7 @@ CODE;
'created_at:dateTime' 'created_at:dateTime'
] ]
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -438,7 +484,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName; $class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [ $this->runMigrateControllerAction('create', [
@ -449,7 +495,7 @@ CODE;
'created_at:dateTime' 'created_at:dateTime'
] ]
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -474,7 +520,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
} }
public function testGenerateCreateJunctionMigration() public function testGenerateCreateJunctionMigration()
@ -484,7 +530,7 @@ CODE;
$this->runMigrateControllerAction('create', [ $this->runMigrateControllerAction('create', [
$migrationName, $migrationName,
]); ]);
$files = FileHelper::findFiles($this->migrationPath); $file = $this->parseNameClassMigration($class);
$code = <<<CODE $code = <<<CODE
<?php <?php
@ -515,7 +561,7 @@ class {$class} extends Migration
} }
CODE; CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0])); $this->assertEqualsWithoutLE($code, $file);
} }
public function testUp() public function testUp()

Loading…
Cancel
Save