diff --git a/core/lib/Drupal/Core/Command/DbDumpCommand.php b/core/lib/Drupal/Core/Command/DbDumpCommand.php index 3c0d5c4..c6b6758 100644 --- a/core/lib/Drupal/Core/Command/DbDumpCommand.php +++ b/core/lib/Drupal/Core/Command/DbDumpCommand.php @@ -167,6 +167,9 @@ protected function getTableSchema($table) { 'type' => $type, 'not null' => $row['Null'] === 'NO', ]; + if ($size = $this->fieldSizeMap($matches[1])) { + $definition['fields'][$name]['size'] = $size; + } if (isset($matches[2]) && $type === 'numeric') { // Add precision and scale. $definition['fields'][$name]['precision'] = $matches[2]; @@ -178,7 +181,7 @@ protected function getTableSchema($table) { $definition['fields'][$name]['type'] = 'varchar'; $definition['fields'][$name]['length'] = '100'; } - else { + elseif (!isset($definition['fields'][$name]['size'])) { // Try use the provided length, if it doesn't exist default to 100. It's // not great but good enough for our dumps at this point. $definition['fields'][$name]['length'] = isset($matches[2]) ? $matches[2] : 100; @@ -242,6 +245,27 @@ protected function fieldTypeMap($type) { } /** + * Given a database field type, return a Drupal size. + * + * @param string $type + * The MySQL field type. + * + * @return string + * The Drupal schema field size. + */ + protected function fieldSizeMap($type) { + // Convert everything to lowercase. + $map = array_map('strtolower', $this->connection->schema()->getFieldTypeMap()); + $map = array_flip($map); + + // The MySql map contains type:size. Remove the type part. + $schema_type = explode(':', $map[$type])[0]; + if (in_array($schema_type, ['blob', 'int', 'text'])) { + return explode(':', $map[$type])[1]; + } + } + + /** * Gets field ordering for a given table. * * @param string $table diff --git a/core/modules/system/src/Tests/Update/DbDumpTest.php b/core/modules/system/src/Tests/Update/DbDumpTest.php index f07522a..678ac82 100644 --- a/core/modules/system/src/Tests/Update/DbDumpTest.php +++ b/core/modules/system/src/Tests/Update/DbDumpTest.php @@ -12,8 +12,10 @@ 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; +use Symfony\Component\DependencyInjection\Reference; /** * Tests for the database dump commands. @@ -44,11 +46,40 @@ class DbDumpTest extends KernelTestBase { protected $skipTests = FALSE; /** + * An array of original table schemas. + * + * @var array + */ + protected $originalTableSchemas = []; + + /** + * Tables that should be part of the exported script. + * + * @var array + */ + protected $tables; + + /** + * {@inheritdoc} + * + * Register a database cache backend rather than memory-based. + */ + public function containerBuild(ContainerBuilder $container) { + parent::containerBuild($container); + $container->register('cache_factory', 'Drupal\Core\Cache\DatabaseBackendFactory') + ->addArgument(new Reference('database')) + ->addArgument(new Reference('cache_tags.invalidator.checksum')); + } + + /** * {@inheritdoc} */ public function setUp() { parent::setUp(); + // Determine what database backend is running, and set the skip flag. + $this->skipTests = !(Database::getConnection() instanceof Connection); + // Create some schemas so our export contains tables. $this->installSchema('system', ['sessions', 'url_alias']); @@ -57,8 +88,11 @@ public function setUp() { $storage = new DatabaseStorage(Database::getConnection(), 'config'); $storage->write('test_config', $this->data); - // Determine what database backend is running, and set the skip flag. - $this->skipTests = !(Database::getConnection() instanceof Connection); + // Create a cache table (this will create 'cache_discovery'). + \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']; } /** @@ -114,9 +148,9 @@ public function testScriptLoad() { $command_tester->execute([]); $script = $command_tester->getDisplay(); - // Drop tables to avoid errors. - $tables = ['sessions', 'url_alias', 'config']; - foreach ($tables as $table) { + // Store original schemas and drop tables to avoid errors. + foreach ($this->tables as $table) { + $this->originalTableSchemas[$table] = $this->getTableSchema($table); Database::getConnection()->schema()->dropTable($table); } @@ -125,14 +159,37 @@ public function testScriptLoad() { file_put_contents($file, $script); require_once $file; - // The tables should now exist. - foreach ($tables as $table) { + // 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->assertIdentical($this->originalTableSchemas[$table], $this->getTableSchema($table), SafeMarkup::format('The schema for @table was properly restored.', ['@table' => $table])); } // Ensure the test config has been replaced. $config = unserialize(db_query("SELECT data FROM {config} WHERE name = 'test_config'")->fetchField()); $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.'); + } + + /** + * Helper function to get a simplified schema for a given table. + * + * @param string $table + * + * @return array + * Array keyed by field name, with the values being the field type. + */ + protected function getTableSchema($table) { + // Verify the field type on the data column in the cache table. + // @todo this is MySQL specific. + $query = db_query("SHOW COLUMNS FROM {" . $table . "}"); + $definition = []; + while ($row = $query->fetchAssoc()) { + $definition[$row['Field']] = $row['Type']; + } + return $definition; } }