Revert "Issue #2817833 by Jo Fitzgerald, ckaotik, svendecabooter: Delay sql map table creation"

This reverts commit dc22273768.
merge-requests/1654/head
Alex Pott 2018-01-26 16:06:26 +00:00
parent 3d5592257d
commit 6f8bc7b1cf
2 changed files with 27 additions and 51 deletions

View File

@ -3,7 +3,6 @@
namespace Drupal\migrate\Plugin\migrate\id_map; namespace Drupal\migrate\Plugin\migrate\id_map;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Plugin\PluginBase;
@ -162,18 +161,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
$this->migration = $migration; $this->migration = $migration;
$this->eventDispatcher = $event_dispatcher; $this->eventDispatcher = $event_dispatcher;
$this->message = new MigrateMessage(); $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);
} }
/** /**
@ -259,6 +246,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* The map table name. * The map table name.
*/ */
public function mapTableName() { public function mapTableName() {
$this->init();
return $this->mapTableName; return $this->mapTableName;
} }
@ -269,6 +257,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* The message table name. * The message table name.
*/ */
public function messageTableName() { public function messageTableName() {
$this->init();
return $this->messageTableName; return $this->messageTableName;
} }
@ -289,6 +278,9 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* The database connection object. * The database connection object.
*/ */
public function getDatabase() { public function getDatabase() {
if (!isset($this->database)) {
$this->database = \Drupal::database();
}
$this->init(); $this->init();
return $this->database; return $this->database;
} }
@ -299,6 +291,13 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
protected function init() { protected function init() {
if (!$this->initialized) { if (!$this->initialized) {
$this->initialized = TRUE; $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(); $this->ensureTables();
} }
} }
@ -697,17 +696,21 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
* {@inheritdoc} * {@inheritdoc}
*/ */
public function processedCount() { public function processedCount() {
return $this->countHelper(NULL, $this->mapTableName()); return $this->getDatabase()->select($this->mapTableName())
->countQuery()
->execute()
->fetchField();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function importedCount() { public function importedCount() {
return $this->countHelper([ return $this->getDatabase()->select($this->mapTableName())
MigrateIdMapInterface::STATUS_IMPORTED, ->condition('source_row_status', [MigrateIdMapInterface::STATUS_IMPORTED, MigrateIdMapInterface::STATUS_NEEDS_UPDATE], 'IN')
MigrateIdMapInterface::STATUS_NEEDS_UPDATE, ->countQuery()
]); ->execute()
->fetchField();
} }
/** /**
@ -734,28 +737,20 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
/** /**
* Counts records in a table. * Counts records in a table.
* *
* @param int|array $status * @param int $status
* (optional) Status code(s) to filter the source_row_status column. * An integer for the source_row_status column.
* @param string $table * @param string $table
* (optional) The table to work. Defaults to NULL. * (optional) The table to work. Defaults to NULL.
* *
* @return int * @return int
* The number of records. * The number of records.
*/ */
protected function countHelper($status = NULL, $table = NULL) { protected function countHelper($status, $table = NULL) {
// Use database directly to avoid creating tables. $query = $this->getDatabase()->select($table ?: $this->mapTableName());
$query = $this->database->select($table ?: $this->mapTableName());
if (isset($status)) { if (isset($status)) {
$query->condition('source_row_status', $status, is_array($status) ? 'IN' : '='); $query->condition('source_row_status', $status);
} }
try { return $query->countQuery()->execute()->fetchField();
$count = $query->countQuery()->execute()->fetchField();
}
catch (DatabaseException $e) {
// The table does not exist, therefore there are no records.
$count = 0;
}
return $count;
} }
/** /**

View File

@ -1010,23 +1010,4 @@ class MigrateSqlIdMapTest extends MigrateTestCase {
return $contents; return $contents;
} }
public function testMapTableCreation() {
$id_map = $this->getIdMap();
$map_table_name = $id_map->mapTableName();
$message_table_name = $id_map->messageTableName();
$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));
}
} }