diff --git a/src/RevisionTracker.php b/src/RevisionTracker.php index 4e82fab..83c15c6 100644 --- a/src/RevisionTracker.php +++ b/src/RevisionTracker.php @@ -3,10 +3,7 @@ namespace Drupal\workbench_moderation; - use Drupal\Core\Database\Connection; -use Drupal\Core\Database\DatabaseExceptionWrapper; -use Drupal\Core\Database\SchemaObjectExistsException; /** * Tracks metadata about revisions across entities. @@ -14,13 +11,6 @@ use Drupal\Core\Database\SchemaObjectExistsException; class RevisionTracker implements RevisionTrackerInterface { /** - * The name of the SQL table we use for tracking. - * - * @var string - */ - protected $tableName; - - /** * Constructs a new RevisionTracker. * * @var \Drupal\Core\Database\Connection @@ -32,26 +22,16 @@ class RevisionTracker implements RevisionTrackerInterface { * * @param \Drupal\Core\Database\Connection $connection * The database connection. - * @param string $table - * The table that should be used for tracking. */ - public function __construct(Connection $connection, $table = 'workbench_revision_tracker') { + public function __construct(Connection $connection) { $this->connection = $connection; - $this->tableName = $table; } /** * {@inheritdoc} */ public function setLatestRevision($entity_type, $entity_id, $langcode, $revision_id) { - try { - $this->recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id); - } - catch (DatabaseExceptionWrapper $e) { - $this->ensureTableExists(); - $this->recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id); - } - + $this->recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id); return $this; } @@ -73,7 +53,7 @@ class RevisionTracker implements RevisionTrackerInterface { * One of the valid returns from a merge query's execute method. */ protected function recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id) { - return $this->connection->merge($this->tableName) + return $this->connection->merge('workbench_revision_tracker') ->keys([ 'entity_type' => $entity_type, 'entity_id' => $entity_id, @@ -85,70 +65,4 @@ class RevisionTracker implements RevisionTrackerInterface { ->execute(); } - /** - * Checks if the table exists and create it if not. - * - * @return bool - * TRUE if the table was created, FALSE otherwise. - */ - protected function ensureTableExists() { - try { - if (!$this->connection->schema()->tableExists($this->tableName)) { - $this->connection->schema()->createTable($this->tableName, $this->schemaDefinition()); - return TRUE; - } - } - catch (SchemaObjectExistsException $e) { - // If another process has already created the table, attempting to - // recreate it will throw an exception. In this case just catch the - // exception and do nothing. - return TRUE; - } - return FALSE; - } - - /** - * Defines the schema for the tracker table. - * - * @return array - * The schema API definition for the SQL storage table. - */ - protected function schemaDefinition() { - $schema = [ - 'description' => 'Tracks the latest revision for any entity', - 'fields' => [ - 'entity_type' => [ - 'description' => 'The entity type', - 'type' => 'varchar_ascii', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - ], - 'entity_id' => [ - 'description' => 'The entity ID', - 'type' => 'int', - 'length' => 255, - 'not null' => TRUE, - 'default' => 0, - ], - 'langcode' => [ - 'description' => 'The language of the entity revision', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ], - 'revision_id' => [ - 'description' => 'The latest revision ID for this entity', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ], - ], - 'primary key' => ['entity_type', 'entity_id', 'langcode'], - ]; - - return $schema; - } - } diff --git a/workbench_moderation.install b/workbench_moderation.install index 4bff255..647e610 100644 --- a/workbench_moderation.install +++ b/workbench_moderation.install @@ -6,6 +6,47 @@ */ /** + * Implements hook_schema(). + */ +function workbench_moderation_schema() { + $schema['workbench_revision_tracker'] = [ + 'description' => 'Tracks the latest revision for any entity', + 'fields' => [ + 'entity_type' => [ + 'description' => 'The entity type', + 'type' => 'varchar_ascii', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ], + 'entity_id' => [ + 'description' => 'The entity ID', + 'type' => 'int', + 'length' => 255, + 'not null' => TRUE, + 'default' => 0, + ], + 'langcode' => [ + 'description' => 'The language of the entity revision', + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ], + 'revision_id' => [ + 'description' => 'The latest revision ID for this entity', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ], + ], + 'primary key' => ['entity_type', 'entity_id', 'langcode'], + ]; + + return $schema; +} + +/** * Implements hook_install(). */ function workbench_moderation_install() { @@ -38,3 +79,12 @@ function workbench_moderation_install() { $entity_definition_update_manager->installFieldStorageDefinition('moderation_state', $type->id(), 'moderation_state', $workbench_moderation_definition); } } + +/** + * Creates the workbench_revision_tracker table if needed. + */ +function workbench_moderation_update_8001() { + if (!db_table_exists('workbench_revision_tracker')) { + drupal_install_schema('workbench_moderation'); + } +}