mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 14:57:23 +08:00
new way of detecting if table exists.
This commit is contained in:
@@ -178,15 +178,21 @@ class Schema extends \yii\db\Schema
|
||||
* Collects the metadata of table columns.
|
||||
* @param TableSchema $table the table metadata
|
||||
* @return boolean whether the table exists in the database
|
||||
* @throws \Exception if DB query fails
|
||||
*/
|
||||
protected function findColumns($table)
|
||||
{
|
||||
$rows = $this->db->createCommand("SHOW TABLES LIKE " . $this->quoteValue($table->name))->queryAll();
|
||||
if (count($rows) === 0) {
|
||||
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
|
||||
try {
|
||||
$columns = $this->db->createCommand($sql)->queryAll();
|
||||
} catch (\Exception $e) {
|
||||
$previous = $e->getPrevious();
|
||||
if ($previous instanceof \PDOException && $previous->getCode() == '42S02') {
|
||||
// table does not exist
|
||||
return false;
|
||||
}
|
||||
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
|
||||
$columns = $this->db->createCommand($sql)->queryAll();
|
||||
throw $e;
|
||||
}
|
||||
foreach ($columns as $info) {
|
||||
$column = $this->loadColumnSchema($info);
|
||||
$table->columns[$column->name] = $column;
|
||||
|
||||
Reference in New Issue
Block a user