diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php index 1c15ba1..f4ad9a7 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php @@ -331,12 +331,7 @@ public function dropTable($table) { * {@inheritdoc} */ public function copyTable($source, $destination) { - if (!$this->tableExists($source)) { - throw new SchemaObjectDoesNotExistException(t("Cannot copy @source to @destination: table @source doesn't exist.", array('@source' => $source, '@destination' => $destination))); - } - if ($this->tableExists($destination)) { - throw new SchemaObjectExistsException(t("Cannot copy @source to @destination: table @destination already exists.", array('@source' => $source, '@destination' => $destination))); - } + parent::copyTable($source, $destination); $info = $this->getPrefixInfo($destination); return $this->connection->query('CREATE TABLE `' . $info['table'] . '` LIKE {' . $source . '}'); } diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php index 135b502..e6a3ba8 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php @@ -352,12 +352,7 @@ function renameTable($table, $new_name) { * {@inheritdoc} */ public function copyTable($source, $destination) { - if (!$this->tableExists($source)) { - throw new SchemaObjectDoesNotExistException(t("Cannot copy @source to @destination: table @source doesn't exist.", array('@source' => $source, '@destination' => $destination))); - } - if ($this->tableExists($destination)) { - throw new SchemaObjectExistsException(t("Cannot copy @source to @destination: table @destination already exists.", array('@source' => $source, '@destination' => $destination))); - } + parent::copyTable($source, $destination); $info = $this->getPrefixInfo($destination); return $this->connection->query('CREATE TABLE `' . $info['table'] . '` (LIKE {' . $source . '} INCLUDING ALL)'); } diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php index f78cfe7..24dbe19 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php @@ -276,12 +276,7 @@ public function renameTable($table, $new_name) { * {@inheritdoc} */ public function copyTable($source, $destination) { - if (!$this->tableExists($source)) { - throw new SchemaObjectDoesNotExistException(t("Cannot copy @source to @destination: table @source doesn't exist.", array('@source' => $source, '@destination' => $destination))); - } - if ($this->tableExists($destination)) { - throw new SchemaObjectExistsException(t("Cannot copy @source to @destination: table @destination already exists.", array('@source' => $source, '@destination' => $destination))); - } + parent::copyTable($source, $destination); $this->createTable($destination, $this->introspectSchema($source)); } diff --git a/core/lib/Drupal/Core/Database/Schema.php b/core/lib/Drupal/Core/Database/Schema.php index 16f7ec9..2a24ced 100644 --- a/core/lib/Drupal/Core/Database/Schema.php +++ b/core/lib/Drupal/Core/Database/Schema.php @@ -408,7 +408,7 @@ public function fieldExists($table, $column) { abstract public function dropTable($table); /** - * Copies a table. + * Copies the structure of a table without copying the content. * * @param string $source * The name of the table to be used as source. @@ -417,8 +417,20 @@ public function fieldExists($table, $column) { * * @return \Drupal\Core\Database\StatementInterface * The result of the executed query. + * + * @throws \Drupal\Core\Database\SchemaObjectExistsException + * Thrown when the source table does not exist. + * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException + * Thrown when the destination table already exists. */ - abstract public function copyTable($source, $destination); + public function copyTable($source, $destination) { + if (!$this->tableExists($source)) { + throw new SchemaObjectDoesNotExistException(t("Cannot copy @source to @destination: table @source doesn't exist.", array('@source' => $source, '@destination' => $destination))); + } + if ($this->tableExists($destination)) { + throw new SchemaObjectExistsException(t("Cannot copy @source to @destination: table @destination already exists.", array('@source' => $source, '@destination' => $destination))); + } + } /** * Add a new field to a table. diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php index 4845751..4c0085b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php @@ -87,6 +87,19 @@ function testSchema() { $index_exists = Database::getConnection()->schema()->indexExists('test_table2', 'test_field'); $this->assertTrue($index_exists, 'Index was renamed.'); + // Copy the schema of the table. + db_copy_table('test_table2', 'test_table3'); + + // Index should be copied. + $index_exists = Database::getConnection()->schema()->indexExists('test_table3', 'test_field'); + $this->assertTrue($index_exists, 'Index was copied.'); + + // Data should still exist on the old table but not on the new one. + $count = db_select('test_table2')->countQuery()->execute()->fetchField(); + $this->assertEqual($count, 1, 'The old table still has its content.'); + $count = db_select('test_table3')->countQuery()->execute()->fetchField(); + $this->assertEqual($count, 0, 'The new table has no content.'); + // We need the default so that we can insert after the rename. db_field_set_default('test_table2', 'test_field', 0); $this->assertFalse($this->tryInsert(), 'Insert into the old table failed.'); @@ -154,9 +167,9 @@ function testSchema() { */ function tryInsert($table = 'test_table') { try { - db_insert($table) - ->fields(array('id' => mt_rand(10, 20))) - ->execute(); + db_insert($table) + ->fields(array('id' => mt_rand(10, 20))) + ->execute(); return TRUE; } catch (\Exception $e) { @@ -227,8 +240,8 @@ function testUnsignedColumns() { function tryUnsignedInsert($table_name, $column_name) { try { db_insert($table_name) - ->fields(array($column_name => -1)) - ->execute(); + ->fields(array($column_name => -1)) + ->execute(); return TRUE; } catch (\Exception $e) {