From 9ec77ab9d3bc5b525796e223c7b8f16cb58068e8 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 25 Jul 2018 15:05:17 +1200 Subject: [PATCH] Check database has table before altering. Work around to prevent several issues with silverstripe-framework#4451 and cwp#0031726. Throws a LogicException if the table does not exist. --- src/ORM/Connect/MySQLSchemaManager.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ORM/Connect/MySQLSchemaManager.php b/src/ORM/Connect/MySQLSchemaManager.php index 81f661ae2..c75097c4a 100644 --- a/src/ORM/Connect/MySQLSchemaManager.php +++ b/src/ORM/Connect/MySQLSchemaManager.php @@ -4,6 +4,7 @@ namespace SilverStripe\ORM\Connect; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Convert; +use LogicException; /** * Represents schema management object for MySQL @@ -139,9 +140,21 @@ class MySQLSchemaManager extends DBSchemaManager return $info && strtoupper($info['Table_type']) == 'VIEW'; } + /** + * Renames a table + * + * @param string $oldTableName + * @param string $newTableName + * @throws LogicException + * @return Query + */ public function renameTable($oldTableName, $newTableName) { - $this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\""); + if (!$this->hasTable($oldTableName)) { + throw new LogicException('Table '. $oldTableName . ' does not exist.'); + } + + return $this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\""); } public function checkAndRepairTable($tableName)