Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1760 from ec-europa/ISAICP-5491
Browse files Browse the repository at this point in the history
ISAICP-5491: Refix the publication dates.
  • Loading branch information
claudiu-cristea authored Aug 22, 2019
2 parents e1cff24 + d41213b commit b24a0db
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
"Do not override the default user entity view. @see https://www.drupal.org/node/2650192": "https://www.drupal.org/files/issues/properly_place_fields-2650192-4-D8.patch"
},
"drupal/publication_date": {
"Published on empty for existing nodes @see https://www.drupal.org/project/publication_date/issues/2983348": "https://www.drupal.org/files/issues/2019-08-07/2983348-32.patch",
"Published on empty for existing nodes @see https://www.drupal.org/project/publication_date/issues/2983348": "https://www.drupal.org/files/issues/2019-08-16/2983348-39.patch",
"Provide computed properties that return the publication date or the created/updated date @see https://www.drupal.org/project/publication_date/issues/3066317": "https://www.drupal.org/files/issues/2019-07-07/3066317-3.patch"
},
"drupal/rdf_entity": {
Expand Down
36 changes: 22 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 78 additions & 2 deletions web/modules/custom/joinup_core/joinup_core.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,85 @@ function joinup_core_post_update_0_fix_publication_dates() {
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$connection = \Drupal::database();
$connection->update($node_storage->getDataTable())
->expression('published_at', PUBLICATION_DATE_DEFAULT)
->fields(['published_at' => NULL])
->execute();
$connection->update($node_storage->getRevisionDataTable())
->expression('published_at', PUBLICATION_DATE_DEFAULT)
->fields(['published_at' => NULL])
->execute();
}

/**
* Reset the publication dates again.
*/
function joinup_core_post_update_refix_publication_dates() {
$connection = Database::getConnection();

// Clean up the values from the database and start anew.
joinup_core_post_update_0_fix_publication_dates();

// The upstream update path is using the `changed` timestamp in order to
// update the publication timestamp. However, we already have a way of
// accurately tracking it, the `created` property. We already update the
// `created` property during the initial publication so by reproducing the
// upstream queries but having the `created` property used instead, we
// properly set the publication date.
$queries = [
// Update nodes with multiple revisions that have at least one published
// revision so the publication date is set to the created timestamp of the
// first published revision.
[
'query' => <<<SQL
UPDATE {node_field_revision} r, (
SELECT
nid,
MIN(vid) as vid,
MIN(created) as created
FROM {node_field_revision}
WHERE status = 1
GROUP BY nid
ORDER BY vid
) s
SET r.published_at = s.created
WHERE r.nid = s.nid AND r.vid >= s.vid;
SQL
,
'arguments' => [],
],

// Set the remainder of the publication dates in the revisions table to the
// default timestamp. This applies to all revisions that were created before
// the node was first published.
[
'query' => <<<SQL
UPDATE {node_field_revision} r
SET r.published_at = :default_timestamp
WHERE r.published_at IS NULL;
SQL
,
'arguments' => [':default_timestamp' => PUBLICATION_DATE_DEFAULT],
],

// Copy the publication date from the revisions table to the node table.
[
'query' => <<<SQL
UPDATE {node_field_data} d, {node_field_revision} r
SET d.published_at = r.published_at
WHERE d.vid = r.vid;
SQL
,
'arguments' => [],
],
];

// Perform the operations in a single atomic transaction.
$transaction = $connection->startTransaction();
try {
foreach ($queries as $query_data) {
\Drupal::database()->query($query_data['query'], $query_data['arguments']);
}
}
catch (Exception $e) {
$transaction->rollBack();
throw new Exception('Database error', 0, $e);
}
}

0 comments on commit b24a0db

Please sign in to comment.