summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-02-12 10:27:13 (GMT)
committerAlex Pott2018-02-12 10:27:13 (GMT)
commite96ab4e1470296b852cc9d95865e9a67f7299736 (patch)
treeb08917584b51392dd215ad51764dfb0f0404a541
parent4aa88100722dd78ac32dfb639cb4f047e5aa0f7d (diff)
Issue #2817833 by Jo Fitzgerald, chiranjeeb2410, ckaotik, vaplas, svendecabooter, aleevas, bhanuprakashnani, heddn, quietone, mikeryan, alexpott, catch: Delay sql map table creation
-rw-r--r--core/modules/migrate/src/Plugin/migrate/id_map/Sql.php62
-rw-r--r--core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php23
-rw-r--r--core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php10
-rw-r--r--core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php10
4 files changed, 69 insertions, 36 deletions
diff --git a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
index 5949b59..a69e31c 100644
--- a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
+++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
@@ -3,6 +3,7 @@
namespace Drupal\migrate\Plugin\migrate\id_map;
use Drupal\Component\Utility\Unicode;
+use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
@@ -161,6 +162,18 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
$this->migration = $migration;
$this->eventDispatcher = $event_dispatcher;
$this->message = new MigrateMessage();
+
+ if (!isset($this->database)) {
+ $this->database = \Drupal::database();
+ }
+
+ // Default generated table names, limited to 63 characters.
+ $machine_name = str_replace(':', '__', $this->migration->id());
+ $prefix_length = strlen($this->database->tablePrefix());
+ $this->mapTableName = 'migrate_map_' . Unicode::strtolower($machine_name);
+ $this->mapTableName = Unicode::substr($this->mapTableName, 0, 63 - $prefix_length);
+ $this->messageTableName = 'migrate_message_' . Unicode::strtolower($machine_name);
+ $this->messageTableName = Unicode::substr($this->messageTableName, 0, 63 - $prefix_length);
}
/**
@@ -246,7 +259,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* The map table name.
*/
public function mapTableName() {
- $this->init();
return $this->mapTableName;
}
@@ -257,7 +269,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* The message table name.
*/
public function messageTableName() {
- $this->init();
return $this->messageTableName;
}
@@ -278,9 +289,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* The database connection object.
*/
public function getDatabase() {
- if (!isset($this->database)) {
- $this->database = \Drupal::database();
- }
$this->init();
return $this->database;
}
@@ -291,13 +299,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
protected function init() {
if (!$this->initialized) {
$this->initialized = TRUE;
- // Default generated table names, limited to 63 characters.
- $machine_name = str_replace(':', '__', $this->migration->id());
- $prefix_length = strlen($this->getDatabase()->tablePrefix());
- $this->mapTableName = 'migrate_map_' . Unicode::strtolower($machine_name);
- $this->mapTableName = Unicode::substr($this->mapTableName, 0, 63 - $prefix_length);
- $this->messageTableName = 'migrate_message_' . Unicode::strtolower($machine_name);
- $this->messageTableName = Unicode::substr($this->messageTableName, 0, 63 - $prefix_length);
$this->ensureTables();
}
}
@@ -696,21 +697,17 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* {@inheritdoc}
*/
public function processedCount() {
- return (int) $this->getDatabase()->select($this->mapTableName())
- ->countQuery()
- ->execute()
- ->fetchField();
+ return $this->countHelper(NULL, $this->mapTableName());
}
/**
* {@inheritdoc}
*/
public function importedCount() {
- return (int) $this->getDatabase()->select($this->mapTableName())
- ->condition('source_row_status', [MigrateIdMapInterface::STATUS_IMPORTED, MigrateIdMapInterface::STATUS_NEEDS_UPDATE], 'IN')
- ->countQuery()
- ->execute()
- ->fetchField();
+ return $this->countHelper([
+ MigrateIdMapInterface::STATUS_IMPORTED,
+ MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
+ ]);
}
/**
@@ -737,20 +734,28 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
/**
* Counts records in a table.
*
- * @param int $status
- * An integer for the source_row_status column.
+ * @param int|array $status
+ * (optional) Status code(s) to filter the source_row_status column.
* @param string $table
* (optional) The table to work. Defaults to NULL.
*
* @return int
* The number of records.
*/
- protected function countHelper($status, $table = NULL) {
- $query = $this->getDatabase()->select($table ?: $this->mapTableName());
+ protected function countHelper($status = NULL, $table = NULL) {
+ // Use database directly to avoid creating tables.
+ $query = $this->database->select($table ?: $this->mapTableName());
if (isset($status)) {
- $query->condition('source_row_status', $status);
+ $query->condition('source_row_status', $status, is_array($status) ? 'IN' : '=');
+ }
+ try {
+ $count = (int) $query->countQuery()->execute()->fetchField();
+ }
+ catch (DatabaseException $e) {
+ // The table does not exist, therefore there are no records.
+ $count = 0;
}
- return (int) $query->countQuery()->execute()->fetchField();
+ return $count;
}
/**
@@ -976,8 +981,9 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
// Get the highest id from the list of map tables.
$ids = [0];
foreach ($map_tables as $map_table) {
+ // If the map_table does not exist then continue on to the next map_table.
if (!$this->getDatabase()->schema()->tableExists($map_table)) {
- break;
+ continue;
}
$query = $this->getDatabase()->select($map_table, 'map')
diff --git a/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php b/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
index 80c55ef..bc84991 100644
--- a/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
+++ b/core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
@@ -1010,4 +1010,27 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
return $contents;
}
+ /**
+ * Tests the delayed creation of the "map" and "message" migrate tables.
+ */
+ public function testMapTableCreation() {
+ $id_map = $this->getIdMap();
+ $map_table_name = $id_map->mapTableName();
+ $message_table_name = $id_map->messageTableName();
+
+ // Check that tables names do exist.
+ $this->assertEquals('migrate_map_sql_idmap_test', $map_table_name);
+ $this->assertEquals('migrate_message_sql_idmap_test', $message_table_name);
+
+ // Check that tables don't exist.
+ $this->assertFalse($this->database->schema()->tableExists($map_table_name));
+ $this->assertFalse($this->database->schema()->tableExists($message_table_name));
+
+ $id_map->getDatabase();
+
+ // Check that tables do exist.
+ $this->assertTrue($this->database->schema()->tableExists($map_table_name));
+ $this->assertTrue($this->database->schema()->tableExists($message_table_name));
+ }
+
}
diff --git a/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php b/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php
index 860f576..bee030e 100644
--- a/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php
+++ b/core/modules/migrate_drupal/tests/src/Kernel/d6/MigrateDrupal6AuditIdsTest.php
@@ -60,8 +60,9 @@ class MigrateDrupal6AuditIdsTest extends MigrateDrupal6TestBase {
// Insert data in the d6_node:page migration mappping table to simulate a
// previously migrated node.
- $table_name = $this->getMigration('d6_node:page')->getIdMap()->mapTableName();
- $this->container->get('database')->insert($table_name)
+ $id_map = $this->getMigration('d6_node:page')->getIdMap();
+ $table_name = $id_map->mapTableName();
+ $id_map->getDatabase()->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
@@ -157,8 +158,9 @@ class MigrateDrupal6AuditIdsTest extends MigrateDrupal6TestBase {
// Insert data in the d6_node_revision:page migration mappping table to
// simulate a previously migrated node revison.
- $table_name = $this->getMigration('d6_node_revision:page')->getIdMap()->mapTableName();
- $this->container->get('database')->insert($table_name)
+ $id_map = $this->getMigration('d6_node_revision:page')->getIdMap();
+ $table_name = $id_map->mapTableName();
+ $id_map->getDatabase()->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
diff --git a/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php b/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php
index 351ab43..d12445b 100644
--- a/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php
+++ b/core/modules/migrate_drupal/tests/src/Kernel/d7/MigrateDrupal7AuditIdsTest.php
@@ -60,8 +60,9 @@ class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
// Insert data in the d7_node:page migration mappping table to simulate a
// previously migrated node.
- $table_name = $this->getMigration('d7_node:page')->getIdMap()->mapTableName();
- $this->container->get('database')->insert($table_name)
+ $id_map = $this->getMigration('d7_node:page')->getIdMap();
+ $table_name = $id_map->mapTableName();
+ $id_map->getDatabase()->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
@@ -156,8 +157,9 @@ class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
// Insert data in the d7_node_revision:page migration mappping table to
// simulate a previously migrated node revison.
- $table_name = $this->getMigration('d7_node_revision:page')->getIdMap()->mapTableName();
- $this->container->get('database')->insert($table_name)
+ $id_map = $this->getMigration('d7_node_revision:page')->getIdMap();
+ $table_name = $id_map->mapTableName();
+ $id_map->getDatabase()->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,