diff --git a/core/lib/Drupal/Core/Command/DbDumpCommand.php b/core/lib/Drupal/Core/Command/DbDumpCommand.php index 60cd4b6..cfaa460 100644 --- a/core/lib/Drupal/Core/Command/DbDumpCommand.php +++ b/core/lib/Drupal/Core/Command/DbDumpCommand.php @@ -168,10 +168,6 @@ protected function getTableSchema($table) { // If this is an auto increment, then the type is 'serial'. $type = 'serial'; } - // Add primary key information. - if ($row['Key'] === 'PRI') { - $definition['primary key'][] = $name; - } $definition['fields'][$name] = [ 'type' => $type, 'not null' => $row['Null'] === 'NO', @@ -216,35 +212,46 @@ protected function getTableSchema($table) { } } - // Attach any additional indexes. - $indexes = $this->getTableIndexes($table); - if (!empty($indexes)) { - $definition['indexes'] = $indexes; - } + // Set primary key, unique keys, and indexes. + $this->getTableIndexes($table, $definition); + return $definition; } /** - * Returns indexes for a given table. + * Adds primary key, unique keys, and index information to the schema. * * @param string $table * The table to find indexes for. - * - * @return array - * The 'indexes' portion of the Drupal table schema. + * @param array &$definition + * The schema definition to modify. */ - protected function getTableIndexes($table) { + protected function getTableIndexes($table, &$definition) { + // Note, this query doesn't support ordering, so that is worked around + // below by keying the array on Seq_in_index. $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']; + $column = $row['Column_name']; + // Key the arrays by the index sequence for proper ordering (start at 0). + $order = $row['Seq_in_index'] - 1; + + // If specified, add length to the index. + if ($row['Sub_part']) { + $column = [$column, $row['Sub_part']]; + } + if ($index_name === 'PRIMARY') { - continue; + $definition['primary key'][$order] = $column; + } + elseif ($row['Non_unique'] == 0) { + $definition['unique keys'][$index_name][$order] = $column; + } + else { + $definition['indexes'][$index_name][$order] = $column; } - $indexes[$index_name][] = $row['Column_name']; } - return $indexes; } /** diff --git a/core/modules/system/src/Tests/Update/DbDumpTest.php b/core/modules/system/src/Tests/Update/DbDumpTest.php index bc6c243..b857bdd 100644 --- a/core/modules/system/src/Tests/Update/DbDumpTest.php +++ b/core/modules/system/src/Tests/Update/DbDumpTest.php @@ -26,7 +26,7 @@ class DbDumpTest extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['system', 'config', 'dblog']; + public static $modules = ['system', 'config', 'dblog', 'menu_link_content', 'link', 'block_content']; /** * Test data to write into config. @@ -52,7 +52,7 @@ class DbDumpTest extends KernelTestBase { protected $originalTableSchemas = []; /** - * An array of original table indexes. + * An array of original table indexes (including primary and unique keys). * * @var array */ @@ -87,11 +87,20 @@ public function setUp() { $this->skipTests = Database::getConnection()->databaseType() !== 'mysql'; // Create some schemas so our export contains tables. - $this->installSchema('system', ['sessions', 'url_alias', 'key_value_expire']); + $this->installSchema('system', [ + 'sessions', + 'url_alias', + 'key_value_expire', + ]); $this->installSchema('dblog', ['watchdog']); + $this->installEntitySchema('block_content'); + $this->installEntitySchema('menu_link_content'); // Place some sample config to test for in the export. - $this->data = ['foo' => $this->randomMachineName(), 'bar' => $this->randomMachineName()]; + $this->data = [ + 'foo' => $this->randomMachineName(), + 'bar' => $this->randomMachineName() + ]; $storage = new DatabaseStorage(Database::getConnection(), 'config'); $storage->write('test_config', $this->data); @@ -99,7 +108,23 @@ 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', 'key_value_expire', 'watchdog']; + $this->tables = [ + 'block_content', + 'block_content_field_data', + 'block_content_field_revision', + 'block_content_revision', + 'cachetags', + 'config', + 'cache_discovery', + 'cache_bootstrap', + 'cache_default', + 'key_value_expire', + 'menu_link_content', + 'menu_link_content_data', + 'sessions', + 'url_alias', + 'watchdog', + ]; } /** @@ -123,8 +148,8 @@ public function testDbDumpCommand() { $this->assertTrue(preg_match('/' . $pattern . '/', $command_tester->getDisplay()), 'Module list is contained in the docblock of the script.'); // A module that is not enabled should not be listed. - $pattern = preg_quote(" * - block_content"); - $this->assertFalse(preg_match('/'. $pattern . '/', $command_tester->getDisplay()), 'Disabled modules do not appear in the docblock of the script.'); + $pattern = preg_quote(" * - telephone"); + $this->assertFalse(preg_match('/' . $pattern . '/', $command_tester->getDisplay()), 'Disabled modules do not appear in the docblock of the script.'); // Tables that are schema-only should not have data exported. $pattern = preg_quote("\$connection->insert('sessions')"); @@ -169,7 +194,9 @@ public function testScriptLoad() { // The tables should now exist and the schemas should match the originals. foreach ($this->tables as $table) { - $this->assertTrue(Database::getConnection()->schema()->tableExists($table), SafeMarkup::format('Table @table created by the database script.', ['@table' => $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])); } @@ -179,7 +206,8 @@ public function testScriptLoad() { $this->assertIdentical($config, $this->data, 'Script has properly restored the config table data.'); // Ensure the cache data was not exported. - $this->assertFalse(\Drupal::cache('discovery')->get('test'), 'Cache data was not exported to the script.'); + $this->assertFalse(\Drupal::cache('discovery') + ->get('test'), 'Cache data was not exported to the script.'); } /** @@ -208,20 +236,34 @@ protected function getTableSchema($table) { * The table to find indexes for. * * @return array - * The 'indexes' portion of the Drupal table schema. + * The 'primary key', 'unique keys', and 'indexes' portion of the Drupal + * table schema. */ protected function getTableIndexes($table) { $query = db_query("SHOW INDEX FROM {" . $table . "}"); - $indexes = []; + $definition = []; while ($row = $query->fetchAssoc()) { - // Primary keys are handled elsewhere, so don't include them here. $index_name = $row['Key_name']; + $column = $row['Column_name']; + // Key the arrays by the index sequence for proper ordering (start at 0). + $order = $row['Seq_in_index'] - 1; + + // If specified, add length to the index. + if ($row['Sub_part']) { + $column = [$column, $row['Sub_part']]; + } + if ($index_name === 'PRIMARY') { - continue; + $definition['primary key'][$order] = $column; + } + elseif ($row['Non_unique'] == 0) { + $definition['unique keys'][$index_name][$order] = $column; + } + else { + $definition['indexes'][$index_name][$order] = $column; } - $indexes[$index_name][] = $row['Column_name']; } - return $indexes; + return $definition; } }