diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php index eb940766699..a909f337209 100644 --- a/lib/ddl/database_manager.php +++ b/lib/ddl/database_manager.php @@ -497,7 +497,7 @@ class database_manager { // Check new table doesn't exist if ($this->table_exists($check)) { - throw new ddl_exception('ddltablealreadyexists', $xmldb_table->getName(), 'can not rename table'); + throw new ddl_exception('ddltablealreadyexists', $check->getName(), 'can not rename table'); } if (!$sqlarr = $this->generator->getRenameTableSQL($xmldb_table, $newname)) { diff --git a/lib/ddl/tests/ddl_test.php b/lib/ddl/tests/ddl_test.php index 25ec8c3b51a..11e9929553e 100644 --- a/lib/ddl/tests/ddl_test.php +++ b/lib/ddl/tests/ddl_test.php @@ -563,6 +563,17 @@ class core_ddl_testcase extends database_driver_testcase { 'secondname' => 'not important', 'intro' => 'not important'); $this->assertSame($insertedrows+1, $DB->insert_record('test_table_cust1', $rec)); + + // Verify behavior when target table already exists. + $sourcetable = $this->create_deftable('test_table0'); + $targettable = $this->create_deftable('test_table1'); + try { + $dbman->rename_table($sourcetable, $targettable->getName()); + $this->fail('Exception expected'); + } catch (moodle_exception $e) { + $this->assertInstanceOf('ddl_exception', $e); + $this->assertEquals('Table "test_table1" already exists (can not rename table)', $e->getMessage()); + } } /**