diff --git a/core/includes/database.inc b/core/includes/database.inc index 75f6a96..b974f2e 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -680,6 +680,19 @@ function db_rename_table($table, $new_name) { } /** + * Copies the structure of a table. + * + * @param $source + * The name of the table to be copied. + * @param $destination + * The name for the new table. + */ +function db_copy_table($source, $destination) { + return Database::getConnection()->schema()->copyTable($source, $destination); +} + + +/** * Drops a table. * * @param $table diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php index 5788bef..0c8116e 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php @@ -327,6 +327,17 @@ public function dropTable($table) { return TRUE; } + 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))); + } + $info = $this->getPrefixInfo($destination); + return $this->connection->query('CREATE TABLE `' . $info['table'] . '` LIKE {' . $source . '}'); + } + public function addField($table, $field, $spec, $keys_new = array()) { if (!$this->tableExists($table)) { throw new SchemaObjectDoesNotExistException(t("Cannot add field @table.@field: table doesn't exist.", array('@field' => $field, '@table' => $table))); diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php index e2798e1..7d7e67c 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php @@ -348,6 +348,17 @@ function renameTable($table, $new_name) { $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO ' . $prefixInfo['table']); } + 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))); + } + $info = $this->getPrefixInfo($destination); + return $this->connection->query('CREATE TABLE `' . $info['table'] . '` (LIKE {' . $source . '} INCLUDING ALL)'); + } + public function dropTable($table) { if (!$this->tableExists($table)) { return FALSE; diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php index f8050cd..6d34779 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php @@ -272,6 +272,16 @@ public function renameTable($table, $new_name) { } } + 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))); + } + $this->createTable($destination, $this->introspectSchema($source)); + } + public function dropTable($table) { if (!$this->tableExists($table)) { return FALSE;