diff --git a/core/lib/Drupal/Core/Command/DbDump.php b/core/lib/Drupal/Core/Command/DbDump.php index 967fd17..a7c78af 100644 --- a/core/lib/Drupal/Core/Command/DbDump.php +++ b/core/lib/Drupal/Core/Command/DbDump.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Command; use Drupal\Core\Database\Connection; +use Drupal\Core\Extension\ModuleHandler; use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputInterface; @@ -21,12 +22,18 @@ class DbDump extends Application { */ protected $connection; + /** + * @var \Drupal\Core\Extension\ModuleHandler + */ + protected $moduleHandler; /** * @param \Drupal\Core\Database\Connection $connection + * @param \Drupal\Core\Extension\ModuleHandler $module_handler */ - function __construct(Connection $connection) { + function __construct(Connection $connection, ModuleHandler $module_handler) { $this->connection = $connection; + $this->moduleHandler = $module_handler; parent::__construct(); } @@ -43,7 +50,7 @@ protected function getCommandName(InputInterface $input) { protected function getDefaultCommands() { // Even though this is a single command, keep the HelpCommand (--help). $default_commands = parent::getDefaultCommands(); - $default_commands[] = new DbDumpCommand($this->connection); + $default_commands[] = new DbDumpCommand($this->connection, $this->moduleHandler); return $default_commands; } diff --git a/core/lib/Drupal/Core/Command/DbDumpCommand.php b/core/lib/Drupal/Core/Command/DbDumpCommand.php index a667554..d991150 100644 --- a/core/lib/Drupal/Core/Command/DbDumpCommand.php +++ b/core/lib/Drupal/Core/Command/DbDumpCommand.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\Variable; use Drupal\Core\Database\Connection; +use Drupal\Core\Extension\ModuleHandler; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -26,6 +27,11 @@ class DbDumpCommand extends Command { protected $connection; /** + * @var \Drupal\Core\Extension\ModuleHandler + */ + protected $moduleHandler; + + /** * An array of table patterns to exclude completely. * * This excludes any lingering simpletest tables generated during test runs. @@ -43,9 +49,11 @@ class DbDumpCommand extends Command { /** * @param \Drupal\Core\Database\Connection $connection + * @param \Drupal\Core\Extension\ModuleHandler $module_handler */ - function __construct(Connection $connection) { + function __construct(Connection $connection, ModuleHandler $module_handler) { $this->connection = $connection; + $this->moduleHandler = $module_handler; parent::__construct(); } @@ -92,8 +100,13 @@ protected function generateScript() { * An array of table names. */ protected function getTables() { - $tables = array_values($this->connection->schema()->findTables('%')); + $pattern = $this->connection->tablePrefix() . '%'; + $tables = array_values($this->connection->schema()->findTables($pattern)); foreach ($tables as $key => $table) { + // The prefix is removed for the resultant script. + $table = $tables[$key] = str_replace($this->connection->tablePrefix(), '', $table); + + // Remove any explicitly excluded tables. foreach ($this->excludeTables as $pattern) { if (preg_match('/^' . $pattern . '$/', $table)) { unset($tables[$key]); @@ -218,7 +231,7 @@ protected function getFieldOrder($table) { ORDER BY COLUMN_NAME"; $results = $this->connection->query($query); while (($row = $results->fetchAssoc()) !== FALSE) { - $order .= '{' . $row['COLUMN_NAME'] . '}, '; + $order .= $row['COLUMN_NAME'] . ', '; } if (!empty($order)) { $order = ' ORDER BY ' . rtrim($order, ', '); @@ -287,10 +300,12 @@ protected function getTableScript($table, array $schema, array $data) { */ protected function getModulesScript() { $output = ''; - foreach (\Drupal::moduleHandler()->getModuleList() as $module => $filename) { + $modules = $this->moduleHandler->getModuleList(); + ksort($modules); + foreach ($modules as $module => $filename) { $output .= " * - $module\n"; } - return $output; + return rtrim($output, "\n"); } } diff --git a/core/modules/system/src/Tests/Update/DbDumpTest.php b/core/modules/system/src/Tests/Update/DbDumpTest.php new file mode 100644 index 0000000..27b8a2c --- /dev/null +++ b/core/modules/system/src/Tests/Update/DbDumpTest.php @@ -0,0 +1,64 @@ +installSchema('system', ['sessions', 'url_alias']); + } + + /** + * Test the command directly. + */ + public function testDbDumpCommand() { + $application = new DbDump(Database::getConnection(), $this->container->get('module_handler')); + $command = $application->find('db-dump'); + $command_tester = new CommandTester($command); + $command_tester->execute([]); + + // The enabled modules should be present in the docblock. + $modules = static::$modules; + asort($modules); + $pattern = preg_quote(implode("\n * - ", $modules)); + $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.'); + + // Tables that are schema-only should not have data exported. + $pattern = preg_quote("\$connection->insert('sessions')"); + $this->assertFalse(preg_match('/' . $pattern . '/', $command_tester->getDisplay()), 'Tables defined as schema-only do not have data exported to the script.'); + + // Table data is exported. + $pattern = preg_quote("\$connection->insert('config')"); + $this->assertTrue(preg_match('/' . $pattern . '/', $command_tester->getDisplay()), 'Table data is properly exported to the script.'); + } + +} diff --git a/core/scripts/dump-database-d8.php b/core/scripts/dump-database-d8.php index 563c34f..4a83b92 100755 --- a/core/scripts/dump-database-d8.php +++ b/core/scripts/dump-database-d8.php @@ -19,5 +19,5 @@ $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot(); // Run the database dump command. -$application = new DbDump(Database::getConnection()); +$application = new DbDump(Database::getConnection(), \Drupal::moduleHandler()); $application->run();