From 312e5891fd898376b5c3bb5607f15d959d9e80be Mon Sep 17 00:00:00 2001 From: Dave Long <dave@longwaveconsulting.com> Date: Mon, 13 Mar 2023 09:46:19 +0000 Subject: [PATCH] Revert "Issue #3312733 by benjifisher, quietone, mikelutz, smustgrave: SQL migrations cannot be instantiated if database is not available and Node, Migrate Drupal modules are enabled" This reverts commit 3cad0ee92a3af4a885b98115061254d96299885c. --- .../src/Plugin/migrate/source/SqlBase.php | 25 ++++---- .../migrate_missing_database_test.info.yml | 6 -- .../migrations/missing_database.yml | 8 --- .../source/MigrateMissingDatabaseSource.php | 52 ----------------- .../src/Kernel/MigrateMissingDatabaseTest.php | 58 ------------------- .../migrate/tests/src/Kernel/SqlBaseTest.php | 23 +------- .../src/Kernel/MigrateMissingDatabaseTest.php | 57 ------------------ 7 files changed, 16 insertions(+), 213 deletions(-) delete mode 100644 core/modules/migrate/tests/modules/migrate_missing_database_test/migrate_missing_database_test.info.yml delete mode 100644 core/modules/migrate/tests/modules/migrate_missing_database_test/migrations/missing_database.yml delete mode 100644 core/modules/migrate/tests/modules/migrate_missing_database_test/src/Plugin/migrate/source/MigrateMissingDatabaseSource.php delete mode 100644 core/modules/migrate/tests/src/Kernel/MigrateMissingDatabaseTest.php delete mode 100644 core/modules/migrate_drupal/tests/src/Kernel/MigrateMissingDatabaseTest.php diff --git a/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php index 0ada082f0cc5..64db362eee7e 100644 --- a/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php +++ b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php @@ -182,10 +182,20 @@ public function getDatabase() { * Thrown if no source database connection is configured. */ protected function setUpDatabase(array $database_info) { - // If there is no explicit database configuration at all, fall back to a - // connection named 'migrate'. - $key = $database_info['key'] ?? 'migrate'; - $target = $database_info['target'] ?? 'default'; + if (isset($database_info['key'])) { + $key = $database_info['key']; + } + else { + // If there is no explicit database configuration at all, fall back to a + // connection named 'migrate'. + $key = 'migrate'; + } + if (isset($database_info['target'])) { + $target = $database_info['target']; + } + else { + $target = 'default'; + } if (isset($database_info['database'])) { Database::addConnectionInfo($key, $target, $database_info['database']); } @@ -210,12 +220,7 @@ protected function setUpDatabase(array $database_info) { */ public function checkRequirements() { if ($this->pluginDefinition['requirements_met'] === TRUE) { - try { - $this->getDatabase(); - } - catch (\PDOException $e) { - throw new RequirementsException("No database connection available for source plugin " . $this->pluginId, [], 0, $e); - } + $this->getDatabase(); } } diff --git a/core/modules/migrate/tests/modules/migrate_missing_database_test/migrate_missing_database_test.info.yml b/core/modules/migrate/tests/modules/migrate_missing_database_test/migrate_missing_database_test.info.yml deleted file mode 100644 index cd49b9401222..000000000000 --- a/core/modules/migrate/tests/modules/migrate_missing_database_test/migrate_missing_database_test.info.yml +++ /dev/null @@ -1,6 +0,0 @@ -name: 'Migration missing database test' -type: module -package: Testing -version: VERSION -dependencies: - - drupal:migrate diff --git a/core/modules/migrate/tests/modules/migrate_missing_database_test/migrations/missing_database.yml b/core/modules/migrate/tests/modules/migrate_missing_database_test/migrations/missing_database.yml deleted file mode 100644 index c194d1eea103..000000000000 --- a/core/modules/migrate/tests/modules/migrate_missing_database_test/migrations/missing_database.yml +++ /dev/null @@ -1,8 +0,0 @@ -id: missing_database -label: Migration using a SQL source -source: - plugin: migrate_missing_database_test -process: {} -destination: - plugin: 'null' -migration_dependencies: {} diff --git a/core/modules/migrate/tests/modules/migrate_missing_database_test/src/Plugin/migrate/source/MigrateMissingDatabaseSource.php b/core/modules/migrate/tests/modules/migrate_missing_database_test/src/Plugin/migrate/source/MigrateMissingDatabaseSource.php deleted file mode 100644 index 332ee4b15c5a..000000000000 --- a/core/modules/migrate/tests/modules/migrate_missing_database_test/src/Plugin/migrate/source/MigrateMissingDatabaseSource.php +++ /dev/null @@ -1,52 +0,0 @@ -<?php - -namespace Drupal\migrate_missing_database_test\Plugin\migrate\source; - -use Drupal\Core\Database\Query\SelectInterface; -use Drupal\migrate\Plugin\migrate\source\SqlBase; - -/** - * A simple migrate source for the missing database tests. - * - * @MigrateSource( - * id = "migrate_missing_database_test", - * source_module = "migrate_missing_database_test", - * requirements_met = true - * ) - */ -class MigrateMissingDatabaseSource extends SqlBase { - - /** - * {@inheritdoc} - */ - public function query(): SelectInterface { - $field_names = ['id']; - $query = $this - ->select('missing_database', 'm') - ->fields('m', $field_names); - return $query; - } - - /** - * {@inheritdoc} - */ - public function fields(): array { - $fields = [ - 'id' => $this->t('ID'), - ]; - - return $fields; - } - - /** - * {@inheritdoc} - */ - public function getIds(): array { - return [ - 'id' => [ - 'type' => 'integer', - ], - ]; - } - -} diff --git a/core/modules/migrate/tests/src/Kernel/MigrateMissingDatabaseTest.php b/core/modules/migrate/tests/src/Kernel/MigrateMissingDatabaseTest.php deleted file mode 100644 index 8c84bda3ec14..000000000000 --- a/core/modules/migrate/tests/src/Kernel/MigrateMissingDatabaseTest.php +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -namespace Drupal\Tests\migrate\Kernel; - -use Drupal\Core\Database\Database; -use Drupal\KernelTests\KernelTestBase; -use Drupal\migrate\Exception\RequirementsException; -use Drupal\migrate\Plugin\MigrateIdMapInterface; -use Drupal\migrate\Plugin\MigrationInterface; - -/** - * Tests that a SQL migration can be instantiated without a database connection. - * - * @group migrate - */ -class MigrateMissingDatabaseTest extends KernelTestBase { - - /** - * {@inheritdoc} - */ - protected static $modules = ['migrate', 'migrate_missing_database_test']; - - /** - * The migration plugin manager. - * - * @var \Drupal\migrate\Plugin\MigrationPluginManager - */ - protected $migrationPluginManager; - - /** - * {@inheritdoc} - */ - protected function setUp(): void { - parent::setUp(); - $this->migrationPluginManager = \Drupal::service('plugin.manager.migration'); - - // Set the 'migrate' database connection to use a missing host. - $info = Database::getConnectionInfo('default')['default']; - $info['host'] = 'does_not_exist'; - Database::addConnectionInfo('migrate', 'default', $info); - } - - /** - * Tests a SQL migration without the database connection. - * - * - The migration can be instantiated. - * - The checkRequirements() method throws a RequirementsException. - */ - public function testMissingDatabase(): void { - $migration = $this->migrationPluginManager->createInstance('missing_database'); - $this->assertInstanceOf(MigrationInterface::class, $migration); - $this->assertInstanceOf(MigrateIdMapInterface::class, $migration->getIdMap()); - $this->expectException(RequirementsException::class); - $this->expectExceptionMessage('No database connection available for source plugin migrate_missing_database_test'); - $migration->checkRequirements(); - } - -} diff --git a/core/modules/migrate/tests/src/Kernel/SqlBaseTest.php b/core/modules/migrate/tests/src/Kernel/SqlBaseTest.php index 38ac6c0d92cb..1661aece762f 100644 --- a/core/modules/migrate/tests/src/Kernel/SqlBaseTest.php +++ b/core/modules/migrate/tests/src/Kernel/SqlBaseTest.php @@ -128,27 +128,6 @@ public function testConnectionTypes() { $sql_base->getDatabase(); } - /** - * Tests the exception when a connection is defined but not available. - */ - public function testBrokenConnection(): void { - $sql_base = new TestSqlBase([], $this->migration); - $target = 'test_state_db_target2'; - $key = 'test_state_migrate_connection2'; - $database = Database::getConnectionInfo('default')['default']; - $database['host'] = 'no_such_host'; - $config = ['target' => $target, 'key' => $key, 'database' => $database]; - $database_state_key = 'migrate_sql_base_test2'; - \Drupal::state()->set($database_state_key, $config); - $sql_base->setConfiguration(['database_state_key' => $database_state_key]); - - // Call checkRequirements(): it will call getDatabase() and convert the - // exception to a RequirementsException. - $this->expectException(RequirementsException::class); - $this->expectExceptionMessage('No database connection available for source plugin sql_base'); - $sql_base->checkRequirements(); - } - /** * Tests that SqlBase respects high-water values. * @@ -222,7 +201,7 @@ class TestSqlBase extends SqlBase { * (optional) The migration being run. */ public function __construct(array $configuration = [], MigrationInterface $migration = NULL) { - parent::__construct($configuration, 'sql_base', ['requirements_met' => TRUE], $migration, \Drupal::state()); + parent::__construct($configuration, 'sql_base', [], $migration, \Drupal::state()); } /** diff --git a/core/modules/migrate_drupal/tests/src/Kernel/MigrateMissingDatabaseTest.php b/core/modules/migrate_drupal/tests/src/Kernel/MigrateMissingDatabaseTest.php deleted file mode 100644 index 463e17f9fefe..000000000000 --- a/core/modules/migrate_drupal/tests/src/Kernel/MigrateMissingDatabaseTest.php +++ /dev/null @@ -1,57 +0,0 @@ -<?php - -namespace Drupal\Tests\migrate_drupal\Kernel; - -use Drupal\Core\Database\Database; -use Drupal\KernelTests\KernelTestBase; -use Drupal\migrate\Plugin\MigrateIdMapInterface; -use Drupal\migrate\Plugin\MigrationInterface; - -/** - * Tests that a migration can be instantiated without a database connection. - * - * @group migrate_drupal - */ -class MigrateMissingDatabaseTest extends KernelTestBase { - - /** - * {@inheritdoc} - */ - protected static $modules = ['migrate', 'migrate_drupal', 'node']; - - /** - * The migration plugin manager. - * - * @var \Drupal\migrate\Plugin\MigrationPluginManager - */ - protected $migrationPluginManager; - - /** - * {@inheritdoc} - */ - protected function setUp(): void { - parent::setUp(); - $this->migrationPluginManager = \Drupal::service('plugin.manager.migration'); - - // Set the 'migrate' database connection to use a missing host. - $info = Database::getConnectionInfo('default')['default']; - $info['host'] = 'does_not_exist'; - Database::addConnectionInfo('migrate', 'default', $info); - } - - /** - * Tests that a migration can be instantiated with the node module enabled. - * - * When the migrate_drupal and node modules are enabled, the migration - * derivers call checkRequirements() whenever createInstance() is used. If the - * database connection is not available, then Migration::setUpDatabase() - * throws an exception. Check that the exception is caught and the migration - * can still be used to access its IdMap. - */ - public function testMissingDatabase(): void { - $migration = $this->migrationPluginManager->createInstance('d7_node_type'); - $this->assertInstanceOf(MigrationInterface::class, $migration); - $this->assertInstanceOf(MigrateIdMapInterface::class, $migration->getIdMap()); - } - -} -- GitLab