mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 05:45:33 +08:00
Added tests for dropTable, truncateTable and renameTable
This commit is contained in:
committed by
Alexander Makarov
parent
4a464afe9b
commit
7c9b7c77a7
@@ -314,7 +314,10 @@ SQL;
|
||||
public function testCreateTable()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
$db->createCommand('DROP TABLE IF EXISTS {{testCreateTable}};')->execute();
|
||||
|
||||
if($db->getSchema()->getTableSchema('testCreateTable') !== null){
|
||||
$db->createCommand()->dropTable('testCreateTable')->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable('testCreateTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute();
|
||||
$db->createCommand()->insert('testCreateTable', ['bar' => 1])->execute();
|
||||
@@ -331,7 +334,10 @@ SQL;
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
$db->createCommand("DROP TABLE IF EXISTS {{testAlterTable}};")->execute();
|
||||
|
||||
if($db->getSchema()->getTableSchema('testAlterTable') !== null){
|
||||
$db->createCommand()->dropTable('testAlterTable')->execute();
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable('testAlterTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute();
|
||||
$db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute();
|
||||
@@ -347,6 +353,47 @@ SQL;
|
||||
}
|
||||
|
||||
|
||||
public function testDropTable()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
|
||||
$tableName = 'type';
|
||||
$this->assertNotNull($db->getSchema()->getTableSchema($tableName));
|
||||
$db->createCommand()->dropTable($tableName)->execute();
|
||||
$this->assertNull($db->getSchema()->getTableSchema($tableName));
|
||||
}
|
||||
|
||||
public function testTruncateTable()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
|
||||
$rows = $db->createCommand('SELECT * FROM {{animal}}')->queryAll();
|
||||
$this->assertEquals(2, count($rows));
|
||||
$db->createCommand()->truncateTable('animal')->execute();
|
||||
$rows = $db->createCommand('SELECT * FROM {{animal}}')->queryAll();
|
||||
$this->assertEquals(0, count($rows));
|
||||
}
|
||||
|
||||
public function testRenameTable()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
|
||||
$fromTableName = 'type';
|
||||
$toTableName = 'new_type';
|
||||
|
||||
if($db->getSchema()->getTableSchema($toTableName) !== null){
|
||||
$db->createCommand()->dropTable($toTableName)->execute();
|
||||
}
|
||||
|
||||
$this->assertNotNull($db->getSchema()->getTableSchema($fromTableName));
|
||||
$this->assertNull($db->getSchema()->getTableSchema($toTableName));
|
||||
|
||||
$db->createCommand()->renameTable($fromTableName, $toTableName)->execute();
|
||||
|
||||
$this->assertNull($db->getSchema()->getTableSchema($fromTableName, true));
|
||||
$this->assertNotNull($db->getSchema()->getTableSchema($toTableName, true));
|
||||
}
|
||||
|
||||
/*
|
||||
public function testUpdate()
|
||||
{
|
||||
@@ -356,18 +403,6 @@ SQL;
|
||||
{
|
||||
}
|
||||
|
||||
public function testRenameTable()
|
||||
{
|
||||
}
|
||||
|
||||
public function testDropTable()
|
||||
{
|
||||
}
|
||||
|
||||
public function testTruncateTable()
|
||||
{
|
||||
}
|
||||
|
||||
public function testAddColumn()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -82,49 +82,4 @@ class MssqlCommandTest extends CommandTest
|
||||
$command->bindValue(':name', 'user5');
|
||||
$this->assertEquals('user5@example.com', $command->queryScalar());
|
||||
}
|
||||
|
||||
public function testCreateTable()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
// on the first run the 'testCreateTable' table does not exist
|
||||
try {
|
||||
$db->createCommand()->dropTable('testCreateTable')->execute();
|
||||
} catch (\Exception $ex) {
|
||||
// 'testCreateTable' table does not exist
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable('testCreateTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute();
|
||||
$db->createCommand()->insert('testCreateTable', ['bar' => 1])->execute();
|
||||
$records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testCreateTable}};')->queryAll();
|
||||
$this->assertEquals([
|
||||
['id' => 1, 'bar' => 1],
|
||||
], $records);
|
||||
}
|
||||
|
||||
public function testAlterTable()
|
||||
{
|
||||
if ($this->driverName === 'sqlite'){
|
||||
$this->markTestSkipped('Sqlite does not support alterTable');
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
// on the first run the 'testAlterTable' table does not exist
|
||||
try {
|
||||
$db->createCommand()->dropTable('testAlterTable')->execute();
|
||||
} catch (\Exception $ex) {
|
||||
// 'testAlterTable' table does not exist
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable('testAlterTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute();
|
||||
$db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute();
|
||||
|
||||
$db->createCommand()->alterColumn('testAlterTable', 'bar', Schema::TYPE_STRING)->execute();
|
||||
|
||||
$db->createCommand()->insert('testAlterTable', ['bar' => 'hello'])->execute();
|
||||
$records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testAlterTable}};')->queryAll();
|
||||
$this->assertEquals([
|
||||
['id' => 1, 'bar' => 1],
|
||||
['id' => 2, 'bar' => 'hello'],
|
||||
], $records);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,49 +30,4 @@ class OracleCommandTest extends CommandTest
|
||||
$command->execute();
|
||||
$this->assertEquals(3, $db->getSchema()->getLastInsertID('profile_SEQ'));
|
||||
}
|
||||
|
||||
public function testCreateTable()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
// on the first run the 'testCreateTable' table does not exist
|
||||
try {
|
||||
$db->createCommand()->dropTable('testCreateTable')->execute();
|
||||
} catch (\Exception $ex) {
|
||||
// 'testCreateTable' table does not exist
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable('testCreateTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute();
|
||||
$db->createCommand()->insert('testCreateTable', ['bar' => 1])->execute();
|
||||
$records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testCreateTable}};')->queryAll();
|
||||
$this->assertEquals([
|
||||
['id' => 1, 'bar' => 1],
|
||||
], $records);
|
||||
}
|
||||
|
||||
public function testAlterTable()
|
||||
{
|
||||
if ($this->driverName === 'sqlite'){
|
||||
$this->markTestSkipped('Sqlite does not support alterTable');
|
||||
}
|
||||
|
||||
$db = $this->getConnection();
|
||||
// on the first run the 'testAlterTable' table does not exist
|
||||
try {
|
||||
$db->createCommand()->dropTable('testAlterTable')->execute();
|
||||
} catch (\Exception $ex) {
|
||||
// 'testAlterTable' table does not exist
|
||||
}
|
||||
|
||||
$db->createCommand()->createTable('testAlterTable', ['id' => Schema::TYPE_PK, 'bar' => Schema::TYPE_INTEGER])->execute();
|
||||
$db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute();
|
||||
|
||||
$db->createCommand()->alterColumn('testAlterTable', 'bar', Schema::TYPE_STRING)->execute();
|
||||
|
||||
$db->createCommand()->insert('testAlterTable', ['bar' => 'hello'])->execute();
|
||||
$records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testAlterTable}};')->queryAll();
|
||||
$this->assertEquals([
|
||||
['id' => 1, 'bar' => 1],
|
||||
['id' => 2, 'bar' => 'hello'],
|
||||
], $records);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user