diff --git a/core/lib/Drupal/Core/Command/DbDumpCommand.php b/core/lib/Drupal/Core/Command/DbDumpCommand.php index 924a8fc..ace6c4b 100644 --- a/core/lib/Drupal/Core/Command/DbDumpCommand.php +++ b/core/lib/Drupal/Core/Command/DbDumpCommand.php @@ -121,7 +121,7 @@ protected function generateScript() { } /** - * Returns a list of tables, not included those set to be excluded. + * Returns a list of tables, not including those set to be excluded. * * @return array * An array of table names. @@ -151,10 +151,11 @@ protected function getTables() { * * @return array * A schema array (as defined by hook_schema()). + * + * @todo This implementation is hard-coded for MySQL. */ protected function getTableSchema($table) { - // @todo this implementation is hard-coded for MySQL. - $query = $this->connection->query("SHOW COLUMNS FROM {" . $table . "}"); + $query = $this->connection->query("SHOW FULL COLUMNS FROM {" . $table . "}"); $definition = []; while (($row = $query->fetchAssoc()) !== FALSE) { $name = $row['Field']; @@ -200,11 +201,51 @@ protected function getTableSchema($table) { if (isset($matches[4])) { $definition['fields'][$name]['unsigned'] = TRUE; } + + // Check for the 'varchar_ascii' type that should be 'binary'. + if (isset($row['Collation']) && $row['Collation'] == 'ascii_bin') { + $definition['fields'][$name]['type'] = 'varchar_ascii'; + $definition['fields'][$name]['binary'] = TRUE; + } + + // Check for the non-binary 'varchar_ascii'. + if (isset($row['Collation']) && $row['Collation'] == 'ascii_general_ci') { + $definition['fields'][$name]['type'] = 'varchar_ascii'; + } + } + + // Attach any additional indexes. + $indexes = $this->getTableIndexes($table); + if (!empty($indexes)) { + $definition['indexes'] = $indexes; } return $definition; } /** + * Returns indexes for a given table. + * + * @param string $table + * The table to find indexes for. + * + * @return array + * The 'indexes' portion of the Drupal table schema. + */ + protected function getTableIndexes($table) { + $query = $this->connection->query("SHOW INDEX FROM {" . $table . "}"); + $indexes = []; + while (($row = $query->fetchAssoc()) !== FALSE) { + // Primary keys are handled elsewhere, so don't include them here. + $index_name = $row['Key_name']; + if ($index_name === 'PRIMARY') { + continue; + } + $indexes[$index_name][] = $row['Column_name']; + } + return $indexes; + } + + /** * Gets all data from a given table. * * If a table is set to be schema only, and empty array is returned. @@ -320,9 +361,9 @@ protected function getTemplate() { {{MODULES}} */ - use Drupal\Core\Database\Database; +use Drupal\Core\Database\Database; - $connection = Database::getConnection(); +$connection = Database::getConnection(); {{TABLES}} diff --git a/core/modules/system/src/Tests/Update/DbDumpTest.php b/core/modules/system/src/Tests/Update/DbDumpTest.php index d82a992..aeb012a 100644 --- a/core/modules/system/src/Tests/Update/DbDumpTest.php +++ b/core/modules/system/src/Tests/Update/DbDumpTest.php @@ -11,7 +11,6 @@ use Drupal\Core\Command\DbDumpApplication; use Drupal\Core\Config\DatabaseStorage; use Drupal\Core\Database\Database; -use Drupal\Core\Database\Driver\mysql\Connection; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\simpletest\KernelTestBase; use Symfony\Component\Console\Tester\CommandTester; @@ -53,6 +52,13 @@ class DbDumpTest extends KernelTestBase { protected $originalTableSchemas = []; /** + * An array of original table indexes. + * + * @var array + */ + protected $originalTableIndexes = []; + + /** * Tables that should be part of the exported script. * * @var array @@ -81,7 +87,7 @@ public function setUp() { $this->skipTests = Database::getConnection()->databaseType() !== 'mysql'; // Create some schemas so our export contains tables. - $this->installSchema('system', ['sessions', 'url_alias']); + $this->installSchema('system', ['sessions', 'url_alias', 'key_value_expire']); // Place some sample config to test for in the export. $this->data = ['foo' => $this->randomMachineName(), 'bar' => $this->randomMachineName()]; @@ -92,7 +98,7 @@ public function setUp() { \Drupal::cache('discovery')->set('test', $this->data); // These are all the tables that should now be in place. - $this->tables = ['sessions', 'url_alias', 'config', 'cache_discovery', 'cache_bootstrap', 'cache_default']; + $this->tables = ['sessions', 'url_alias', 'config', 'cache_discovery', 'cache_bootstrap', 'cache_default', 'key_value_expire']; } /** @@ -151,6 +157,7 @@ public function testScriptLoad() { // Store original schemas and drop tables to avoid errors. foreach ($this->tables as $table) { $this->originalTableSchemas[$table] = $this->getTableSchema($table); + $this->originalTableIndexes[$table] = $this->getTableIndexes($table); Database::getConnection()->schema()->dropTable($table); } @@ -163,6 +170,7 @@ public function testScriptLoad() { foreach ($this->tables as $table) { $this->assertTrue(Database::getConnection()->schema()->tableExists($table), SafeMarkup::format('Table @table created by the database script.', ['@table' => $table])); $this->assertIdentical($this->originalTableSchemas[$table], $this->getTableSchema($table), SafeMarkup::format('The schema for @table was properly restored.', ['@table' => $table])); + $this->assertIdentical($this->originalTableIndexes[$table], $this->getTableIndexes($table), SafeMarkup::format('The indexes for @table were properly restored.', ['@table' => $table])); } // Ensure the test config has been replaced. @@ -192,4 +200,27 @@ protected function getTableSchema($table) { return $definition; } + /** + * Returns indexes for a given table. + * + * @param string $table + * The table to find indexes for. + * + * @return array + * The 'indexes' portion of the Drupal table schema. + */ + protected function getTableIndexes($table) { + $query = db_query("SHOW INDEX FROM {" . $table . "}"); + $indexes = []; + while ($row = $query->fetchAssoc()) { + // Primary keys are handled elsewhere, so don't include them here. + $index_name = $row['Key_name']; + if ($index_name === 'PRIMARY') { + continue; + } + $indexes[$index_name][] = $row['Column_name']; + } + return $indexes; + } + }