mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 06:48:59 +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.
|
* Collects the metadata of table columns.
|
||||||
* @param TableSchema $table the table metadata
|
* @param TableSchema $table the table metadata
|
||||||
* @return boolean whether the table exists in the database
|
* @return boolean whether the table exists in the database
|
||||||
|
* @throws \Exception if DB query fails
|
||||||
*/
|
*/
|
||||||
protected function findColumns($table)
|
protected function findColumns($table)
|
||||||
{
|
{
|
||||||
$rows = $this->db->createCommand("SHOW TABLES LIKE " . $this->quoteValue($table->name))->queryAll();
|
|
||||||
if (count($rows) === 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
|
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
|
||||||
$columns = $this->db->createCommand($sql)->queryAll();
|
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;
|
||||||
|
}
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
foreach ($columns as $info) {
|
foreach ($columns as $info) {
|
||||||
$column = $this->loadColumnSchema($info);
|
$column = $this->loadColumnSchema($info);
|
||||||
$table->columns[$column->name] = $column;
|
$table->columns[$column->name] = $column;
|
||||||
|
|||||||
Reference in New Issue
Block a user