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
```php
yii migrate/create drop_post
yii migrate/create drop_post --fields=title:string(12):notNull:unique,body:text
```
generates
@ -290,6 +290,9 @@ class m150811_220037_drop_post extends Migration
public function down()
{
$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.
* `generatorTemplateFiles`: array (defaults to `[
'create' => '@yii/views/createMigration.php',
'drop' => '@yii/views/dropMigration.php',
'add' => '@yii/views/addMigration.php',
'remove' => '@yii/views/removeMigration.php',
'create_table' => '@yii/views/createTableMigration.php',
'drop_table' => '@yii/views/dropTableMigration.php',
'add_column' => '@yii/views/addColumnMigration.php',
'drop_column' => '@yii/views/dropColumnMigration.php',
'create_junction' => '@yii/views/createJunctionMigration.php'
]`), specifies template files for generating migration code. See "[Generating Migrations](#generating-migrations)"
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 #7566: Improved `\yii\validators\CompareValidator` default messages (slinstj)
- 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 #8649: Added total applied migrations to final report (vernik91)
- 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.
*/
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.
* @param string $code the verification code
* @return string image contents
* @throws InvalidConfigException if imageLibrary is not supported
*/
protected function renderImage($code)
{
if (Captcha::checkRequirements() === 'gd') {
return $this->renderImageByGD($code);
if (isset($this->imageLibrary)) {
$imageLibrary = $this->imageLibrary;
} else {
$imageLibrary = Captcha::checkRequirements();
}
if ($imageLibrary === 'gd') {
return $this->renderImageByGD($code);
} elseif ($imageLibrary === 'imagick') {
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,
]);
} 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,
'table' => mb_strtolower($matches[2], Yii::$app->charset),
'fields' => $this->fields
]);
} 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,
'table' => mb_strtolower($matches[2], Yii::$app->charset),
'fields' => $this->fields
]);
} elseif (preg_match('/^create_(.+)$/', $name, $matches)) {
$this->addDefaultPrimaryKey();
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create']), [
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create_table']), [
'className' => $className,
'table' => mb_strtolower($matches[1], Yii::$app->charset),
'fields' => $this->fields
]);
} 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,
'table' => mb_strtolower($matches[1], Yii::$app->charset),
'fields' => $this->fields

8
framework/console/controllers/MigrateController.php

@ -67,10 +67,10 @@ class MigrateController extends BaseMigrateController
* @inheritdoc
*/
public $generatorTemplateFiles = [
'create' => '@yii/views/createMigration.php',
'drop' => '@yii/views/dropMigration.php',
'add' => '@yii/views/addColumnMigration.php',
'remove' => '@yii/views/dropColumnMigration.php',
'create_table' => '@yii/views/createTableMigration.php',
'drop_table' => '@yii/views/dropTableMigration.php',
'add_column' => '@yii/views/addColumnMigration.php',
'drop_column' => '@yii/views/dropColumnMigration.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.
* @param array $expectedMigrations migration names in expected order
* @param string $message failure message
@ -154,7 +166,7 @@ CODE;
$migrationName = 'DefaultTest';
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [$migrationName]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$newLine = '\n';
$code = <<<CODE
@ -189,7 +201,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
}
public function testGenerateCreateMigration()
@ -203,7 +215,7 @@ CODE;
'body:text:notNull'
]
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -228,7 +240,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [
@ -239,7 +251,7 @@ CODE;
],
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -262,7 +274,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [
@ -270,7 +282,7 @@ CODE;
'fields' => [
],
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -292,7 +304,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
}
public function testGenerateDropMigration()
@ -302,8 +314,40 @@ CODE;
$this->runMigrateControllerAction('create', [
$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
<?php
@ -319,12 +363,14 @@ class {$class} extends Migration
public function down()
{
\$this->createTable('test', [
'id' => \$this->primaryKey(),
'body' => \$this->text()->notNull()
]);
}
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [
@ -335,7 +381,7 @@ CODE;
],
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -358,7 +404,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
}
public function testGenerateAddColumnMigration()
@ -373,7 +419,7 @@ CODE;
'created_at:dateTime'
]
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -398,7 +444,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
}
public function testGenerateDropColumnMigration()
@ -413,7 +459,7 @@ CODE;
'created_at:dateTime'
]
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -438,7 +484,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
$class = 'm' . gmdate('ymd_His') . '_' . $migrationName;
$this->runMigrateControllerAction('create', [
@ -449,7 +495,7 @@ CODE;
'created_at:dateTime'
]
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -474,7 +520,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
}
public function testGenerateCreateJunctionMigration()
@ -484,7 +530,7 @@ CODE;
$this->runMigrateControllerAction('create', [
$migrationName,
]);
$files = FileHelper::findFiles($this->migrationPath);
$file = $this->parseNameClassMigration($class);
$code = <<<CODE
<?php
@ -515,7 +561,7 @@ class {$class} extends Migration
}
CODE;
$this->assertEqualsWithoutLE($code, file_get_contents($files[0]));
$this->assertEqualsWithoutLE($code, $file);
}
public function testUp()

Loading…
Cancel
Save