diff --git a/core/lib/Drupal/Core/Command/DbDump.php b/core/lib/Drupal/Core/Command/DbDump.php index 8a5632c..48a1b97 100644 --- a/core/lib/Drupal/Core/Command/DbDump.php +++ b/core/lib/Drupal/Core/Command/DbDump.php @@ -41,7 +41,7 @@ function __construct(Connection $connection, ModuleHandlerInterface $module_hand * {@inheritdoc} */ protected function getCommandName(InputInterface $input) { - return 'db-dump'; + return 'dump-database-d8'; } /** diff --git a/core/lib/Drupal/Core/Command/DbDumpCommand.php b/core/lib/Drupal/Core/Command/DbDumpCommand.php index a050137..e182bbe 100644 --- a/core/lib/Drupal/Core/Command/DbDumpCommand.php +++ b/core/lib/Drupal/Core/Command/DbDumpCommand.php @@ -61,7 +61,7 @@ function __construct(Connection $connection, ModuleHandlerInterface $module_hand * {@inheritdoc} */ protected function configure() { - $this->setName('db-dump') + $this->setName('dump-database-d8') ->setDescription('Dump the current database to a generation script'); } @@ -216,8 +216,12 @@ protected function getTableData($table) { * type is returned. */ protected function fieldTypeMap($type) { - $map = array_flip($this->connection->schema()->getFieldTypeMap()); - return isset($map[$type]) ? $map[$type] : $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 size part. + return isset($map[$type]) ? explode(':', $map[$type])[0] : $type; } /** @@ -291,7 +295,7 @@ protected function getTableScript($table, array $schema, array $data) { $insert .= "->values(" . Variable::export($record) . ")\n"; } $output .= "\$connection->insert('" . $table . "')\n" - . "->fields(" . Variable::export($schema['fields']) . ")\n" + . "->fields(" . Variable::export(array_keys($schema['fields'])) . ")\n" . $insert . "->execute();\n\n"; } diff --git a/core/modules/system/src/Tests/Update/DbDumpTest.php b/core/modules/system/src/Tests/Update/DbDumpTest.php index 5da7258..8cb7d69 100644 --- a/core/modules/system/src/Tests/Update/DbDumpTest.php +++ b/core/modules/system/src/Tests/Update/DbDumpTest.php @@ -6,6 +6,7 @@ */ namespace Drupal\system\Tests\Update; +use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Command\DbDump; use Drupal\Core\Config\Config; use Drupal\Core\Config\DatabaseStorage; @@ -42,7 +43,7 @@ public function setUp() { $this->installSchema('system', ['sessions', 'url_alias']); // Place some sample config to test for in the export. - $this->data = ['foo' => $this->randomString(), 'bar' => $this->randomString()]; + $this->data = ['foo' => $this->randomMachineName(), 'bar' => $this->randomMachineName()]; $storage = new DatabaseStorage(Database::getConnection(), 'config'); $storage->write('test_config', $this->data); } @@ -52,7 +53,7 @@ public function setUp() { */ public function testDbDumpCommand() { $application = new DbDump(Database::getConnection(), $this->container->get('module_handler')); - $command = $application->find('db-dump'); + $command = $application->find('dump-database-d8'); $command_tester = new CommandTester($command); $command_tester->execute([]); @@ -79,4 +80,36 @@ public function testDbDumpCommand() { $this->assertTrue(preg_match('/' . $pattern . '/', $command_tester->getDisplay()), 'Generated data is found in the exported script.'); } + /** + * Test loading the script back into the database. + */ + public function testScriptLoad() { + // Generate the script. + $application = new DbDump(Database::getConnection(), $this->container->get('module_handler')); + $command = $application->find('dump-database-d8'); + $command_tester = new CommandTester($command); + $command_tester->execute([]); + $script = $command_tester->getDisplay(); + + // Drop tables to avoid errors. + $tables = ['sessions', 'url_alias', 'config']; + foreach ($tables as $table) { + Database::getConnection()->schema()->dropTable($table); + } + + // This will load the data. + $file = sys_get_temp_dir() . '/' . $this->randomMachineName(); + file_put_contents($file, $script); + require_once $file; + + // The tables should now exist. + foreach ($tables as $table) { + $this->assertTrue(Database::getConnection()->schema()->tableExists($table), SafeMarkup::format('Table @table created by the database script.', ['@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.'); + } + } diff --git a/core/scripts/dump-database-d8.php b/core/scripts/dump-database-d8 similarity index 100% rename from core/scripts/dump-database-d8.php rename to core/scripts/dump-database-d8