octicon-rss(16/)
You've already forked yii2
mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 23:50:38 +08:00
Fixes #5627: Added yii cache/flush-schema console command to flush DB schema cache of a given database connection
This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
octicon-diff(16/tw-mr-1) 3 changed files with 88 additions and 0 deletions
@@ -20,6 +20,8 @@ class CacheControllerTest extends TestCase
|
||||
*/
|
||||
private $_cacheController;
|
||||
|
||||
private $driverName = 'mysql';
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
@@ -28,14 +30,41 @@ class CacheControllerTest extends TestCase
|
||||
'class' => 'yiiunit\framework\console\controllers\CacheConsoledController',
|
||||
'interactive' => false,
|
||||
],[null, null]); //id and module are null
|
||||
|
||||
$databases = self::getParam('databases');
|
||||
$config = $databases[$this->driverName];
|
||||
$pdoDriver = 'pdo_' . $this->driverName;
|
||||
|
||||
if (!extension_loaded('pdo') || !extension_loaded($pdoDriver)) {
|
||||
$this->markTestSkipped('pdo and ' . $pdoDriver . ' extensions are required.');
|
||||
}
|
||||
|
||||
|
||||
$this->mockApplication([
|
||||
'components' => [
|
||||
'firstCache' => 'yii\caching\ArrayCache',
|
||||
'secondCache' => 'yii\caching\ArrayCache',
|
||||
'session' => 'yii\web\CacheSession', // should be ignored at `actionFlushAll()`
|
||||
'db' => [
|
||||
'class' => isset($config['class']) ? $config['class'] : 'yii\db\Connection',
|
||||
'dsn' => $config['dsn'],
|
||||
'username' => isset($config['username']) ? $config['username'] : null,
|
||||
'password' => isset($config['password']) ? $config['password'] : null,
|
||||
'enableSchemaCache' => true,
|
||||
'schemaCache' => 'firstCache',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
if(isset($config['fixture'])) {
|
||||
Yii::$app->db->open();
|
||||
$lines = explode(';', file_get_contents($config['fixture']));
|
||||
foreach ($lines as $line) {
|
||||
if (trim($line) !== '') {
|
||||
Yii::$app->db->pdo->exec($line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testFlushOne()
|
||||
@@ -50,6 +79,25 @@ class CacheControllerTest extends TestCase
|
||||
$this->assertFalse(Yii::$app->firstCache->get('secondKey'),'first cache data should be flushed');
|
||||
$this->assertEquals('thirdValue', Yii::$app->secondCache->get('thirdKey'), 'second cache data should not be flushed');
|
||||
}
|
||||
|
||||
public function testClearSchema()
|
||||
{
|
||||
$schema = Yii::$app->db->schema;
|
||||
Yii::$app->db->createCommand()->createTable('test_schema_cache', ['id' => 'pk'])->execute();
|
||||
$noCacheSchemas = $schema->getTableSchemas('', true);
|
||||
$cacheSchema = $schema->getTableSchemas('', false);
|
||||
|
||||
$this->assertEquals($noCacheSchemas, $cacheSchema, 'Schema should not be modified.');
|
||||
|
||||
Yii::$app->db->createCommand()->dropTable('test_schema_cache')->execute();
|
||||
$noCacheSchemas = $schema->getTableSchemas('', true);
|
||||
$this->assertNotEquals($noCacheSchemas, $cacheSchema, 'Schemas should be different.');
|
||||
|
||||
$this->_cacheController->actionFlushSchema('db');
|
||||
$cacheSchema = $schema->getTableSchemas('', false);
|
||||
$this->assertEquals($noCacheSchemas, $cacheSchema, 'Schema cache should be flushed.');
|
||||
|
||||
}
|
||||
|
||||
public function testFlushBoth()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user