From 5132930c193024a6e5287198d12cf23efbff58d3 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Mon, 5 Oct 2015 09:21:52 -0700
Subject: [PATCH] Issue #2579399 by phenaproxima, mikeryan, neclimdul:
 db-tools.php import does not work

---
 .../Drupal/Core/Command/DbImportCommand.php   |  3 +++
 .../Kernel/Scripts/DbImportCommandTest.php    | 20 +++++++++++--------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/core/lib/Drupal/Core/Command/DbImportCommand.php b/core/lib/Drupal/Core/Command/DbImportCommand.php
index de41089d2b1f..a39f6cbde68e 100644
--- a/core/lib/Drupal/Core/Command/DbImportCommand.php
+++ b/core/lib/Drupal/Core/Command/DbImportCommand.php
@@ -58,6 +58,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
    *   Path to dump script.
    */
   protected function runScript(Connection $connection, $script) {
+    $old_key = Database::setActiveConnection($connection->getKey());
+
     if (substr($script, -3) == '.gz') {
       $script = "compress.zlib://$script";
     }
@@ -67,6 +69,7 @@ protected function runScript(Connection $connection, $script) {
     catch (SchemaObjectExistsException $e) {
       throw new \RuntimeException('An existing Drupal installation exists at this location. Try removing all tables or changing the database prefix in your settings.php file.');
     }
+    Database::setActiveConnection($old_key);
   }
 
 }
diff --git a/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php b/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php
index 9dc3407eb2d1..8cf23d5b7198 100644
--- a/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php
+++ b/core/modules/system/tests/src/Kernel/Scripts/DbImportCommandTest.php
@@ -8,7 +8,6 @@
 namespace Drupal\Tests\system\Kernel\Scripts;
 
 use Drupal\Core\Command\DbImportCommand;
-use Drupal\Core\Config\DatabaseStorage;
 use Drupal\Core\Database\Database;
 use Drupal\KernelTests\KernelTestBase;
 use Symfony\Component\Console\Tester\CommandTester;
@@ -54,20 +53,25 @@ class DbImportCommandTest extends KernelTestBase {
 
   /**
    * Test the command directly.
+   *
+   * @requires extension pdo_sqlite
    */
   public function testDbImportCommand() {
-    /** @var \Drupal\Core\Database\Connection $connection */
-    $connection = $this->container->get('database');
-    // Drop tables to avoid conflicts.
-    foreach ($this->tables as $table) {
-      $connection->schema()->dropTable($table);
-    }
+    $connection_info = array(
+      'driver' => 'sqlite',
+      'database' => ':memory:',
+    );
+    Database::addConnectionInfo($this->databasePrefix, 'default', $connection_info);
 
     $command = new DbImportCommand();
     $command_tester = new CommandTester($command);
-    $command_tester->execute(['script' => __DIR__ . '/../../../fixtures/update/drupal-8.bare.standard.php.gz']);
+    $command_tester->execute([
+      'script' => __DIR__ . '/../../../fixtures/update/drupal-8.bare.standard.php.gz',
+      '--database' => $this->databasePrefix,
+    ]);
 
     // The tables should now exist.
+    $connection = Database::getConnection('default', $this->databasePrefix);
     foreach ($this->tables as $table) {
       $this->assertTrue($connection
         ->schema()
-- 
GitLab