From e7787c9cca63a416701b20b90793b4bc5f0def24 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Mon, 8 Oct 2012 09:39:59 -0700
Subject: [PATCH] Issue #1642526 by andypost, Berdir, dixon_: Add upgrade path
 from for generating UUIDs for all core entities.

---
 core/includes/update.inc                      | 28 +++++++++
 core/modules/comment/comment.install          | 18 +++++-
 core/modules/node/node.install                | 18 +++++-
 .../Tests/Upgrade/UuidUpgradePathTest.php     | 62 +++++++++++++++++++
 core/modules/system/system.install            | 18 +++++-
 core/modules/taxonomy/taxonomy.install        | 18 +++++-
 core/modules/user/user.install                | 19 +++++-
 7 files changed, 171 insertions(+), 10 deletions(-)
 create mode 100644 core/modules/system/lib/Drupal/system/Tests/Upgrade/UuidUpgradePathTest.php

diff --git a/core/includes/update.inc b/core/includes/update.inc
index 5d80188344db..13109985220b 100644
--- a/core/includes/update.inc
+++ b/core/includes/update.inc
@@ -11,6 +11,7 @@
 use Drupal\Component\Graph\Graph;
 use Drupal\Core\Config\FileStorage;
 use Drupal\Core\Config\ConfigException;
+use Drupal\Component\Uuid\Uuid;
 
 /**
  * Minimum schema version of Drupal 7 required for upgrade to Drupal 8.
@@ -1125,3 +1126,30 @@ function update_variables_to_config($config_name, array $variable_map) {
   // Delete the migrated variables.
   db_delete('variable')->condition('name', array_keys($variable_map), 'IN')->execute();
 }
+
+/**
+ * Helper function to update entities with uuid.
+ *
+ * @param array $sandbox
+ *   A sandbox where conversion happens.
+ * @param string $table
+ *   A table whose data should be updated.
+ * @param string $primary_key
+ *   A $table primary key column.
+ * @param array $values
+ *   A $primary_key values of rows to be updated.
+ */
+function update_add_uuids(&$sandbox, $table, $primary_key, $values) {
+  $uuid = new Uuid();
+  foreach ($values as $value) {
+    db_update($table)
+      ->fields(array(
+        'uuid' => $uuid->generate(),
+      ))
+      ->condition($primary_key, $value)
+      ->isNull('uuid')
+      ->execute();
+    $sandbox['progress']++;
+    $sandbox['last'] = $value;
+  }
+}
diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install
index 6692a954d4d4..f390f570ff08 100644
--- a/core/modules/comment/comment.install
+++ b/core/modules/comment/comment.install
@@ -295,8 +295,6 @@ function comment_update_8000() {
 
 /**
  * Create a UUID column for comments.
- *
- * @todo UUID upgrade path: http://drupal.org/node/1642526
  */
 function comment_update_8001() {
   $spec = array(
@@ -367,6 +365,22 @@ function comment_update_8002() {
   );
 }
 
+/**
+ * Generate an UUID for all comments.
+ */
+function comment_update_8003(&$sandbox) {
+  if (!isset($sandbox['progress'])) {
+    $sandbox['progress'] = 0;
+    $sandbox['last'] = 0;
+    $sandbox['max'] = db_query('SELECT COUNT(cid) FROM {comment} WHERE uuid IS NULL')->fetchField();
+  }
+
+  $cids = db_query_range('SELECT cid FROM {comment} WHERE cid > :cid AND uuid IS NULL ORDER BY cid ASC', 0, 10, array(':cid' => $sandbox['last']))->fetchCol();
+  update_add_uuids($sandbox, 'comment', 'cid', $cids);
+
+  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
+}
+
 /**
  * @} End of "addtogroup updates-7.x-to-8.x".
  * The next series of updates should start at 9000.
diff --git a/core/modules/node/node.install b/core/modules/node/node.install
index ac777f3f4429..6847fb456be3 100644
--- a/core/modules/node/node.install
+++ b/core/modules/node/node.install
@@ -582,8 +582,6 @@ function node_update_8003() {
 
 /**
  * Create a UUID column for nodes.
- *
- * @todo UUID upgrade path: http://drupal.org/node/1642526
  */
 function node_update_8004() {
   $spec = array(
@@ -663,6 +661,22 @@ function node_update_8005() {
   );
 }
 
+/**
+ * Generate an UUID for all nodes.
+ */
+function node_update_8006(&$sandbox) {
+  if (!isset($sandbox['progress'])) {
+    $sandbox['progress'] = 0;
+    $sandbox['last'] = 0;
+    $sandbox['max'] = db_query('SELECT COUNT(nid) FROM {node} WHERE uuid IS NULL')->fetchField();
+  }
+
+  $nids = db_query_range('SELECT nid FROM {node} WHERE nid > :nid AND uuid IS NULL ORDER BY nid ASC', 0, 10, array(':nid' => $sandbox['last']))->fetchCol();
+  update_add_uuids($sandbox, 'node', 'nid', $nids);
+
+  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
+}
+
 /**
  * @} End of "addtogroup updates-7.x-to-8.x"
  * The next series of updates should start at 9000.
diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UuidUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UuidUpgradePathTest.php
new file mode 100644
index 000000000000..5bf6436c941c
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UuidUpgradePathTest.php
@@ -0,0 +1,62 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\system\Tests\Upgrade\UuidUpgradePathTest.
+ */
+
+namespace Drupal\system\Tests\Upgrade;
+
+/**
+ * Performs major version release upgrade tests on a populated database.
+ *
+ * Loads an installation of Drupal 7.x and runs the upgrade process on it.
+ *
+ * The install contains the minimal profile modules (along with generated
+ * content) so that an update from of a site under this profile may be tested.
+ */
+class UuidUpgradePathTest extends UpgradePathTestBase {
+  public static function getInfo() {
+    return array(
+      'name'  => 'Uuid upgrade test',
+      'description'  => 'Upgrade tests for a node and user data.',
+      'group' => 'Upgrade path',
+    );
+  }
+
+  public function setUp() {
+    // Path to the database dump files.
+    $this->databaseDumpFiles = array(
+      drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.filled.standard_all.database.php.gz',
+      drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.language.database.php',
+    );
+    parent::setUp();
+  }
+
+  /**
+   * Tests a successful point release update.
+   */
+  public function testUuidUpgrade() {
+    $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
+
+    // Confirm that all {node} entries has uuid.
+    $result = db_query('SELECT COUNT(*) FROM {comment} WHERE uuid IS NULL')->fetchField();
+    $this->assertFalse($result, 'All comments has uuid assigned');
+
+    // Confirm that all {node} entries has uuid.
+    $result = db_query('SELECT COUNT(*) FROM {file_managed} WHERE uuid IS NULL')->fetchField();
+    $this->assertFalse($result, 'All files has uuid assigned');
+
+    // Confirm that all {node} entries has uuid.
+    $result = db_query('SELECT COUNT(*) FROM {node} WHERE uuid IS NULL')->fetchField();
+    $this->assertFalse($result, 'All nodes has uuid assigned');
+
+    // Confirm that all {node} entries has uuid.
+    $result = db_query('SELECT COUNT(*) FROM {taxonomy_term_data} WHERE uuid IS NULL')->fetchField();
+    $this->assertFalse($result, 'All taxonomy terms has uuid assigned');
+
+    // Confirm that all {user} entries has uuid.
+    $result = db_query('SELECT COUNT(*) FROM {users} WHERE uuid IS NULL')->fetchField();
+    $this->assertFalse($result, 'All users has uuid assigned');
+  }
+}
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index 28d95da17f5e..5d901614c61a 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -1937,8 +1937,6 @@ function system_update_8014() {
 
 /**
  * Create a UUID column for managed files.
- *
- * @todo UUID upgrade path: http://drupal.org/node/1642526
  */
 function system_update_8015() {
   $spec = array(
@@ -2184,6 +2182,22 @@ function system_update_8023() {
   db_create_table('key_value_expire', $table);
 }
 
+/**
+ * Generate an UUID for all files.
+ */
+function system_update_8024(&$sandbox) {
+  if (!isset($sandbox['progress'])) {
+    $sandbox['progress'] = 0;
+    $sandbox['last'] = 0;
+    $sandbox['max'] = db_query('SELECT COUNT(fid) FROM {file_managed} WHERE uuid IS NULL')->fetchField();
+  }
+
+  $fids = db_query_range('SELECT fid FROM {file_managed} WHERE fid > :fid AND uuid IS NULL ORDER BY fid ASC', 0, 10, array(':fid' => $sandbox['last']))->fetchCol();
+  update_add_uuids($sandbox, 'file_managed', 'fid', $fids);
+
+  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
+}
+
 /**
  * @} End of "defgroup updates-7.x-to-8.x".
  * The next series of updates should start at 9000.
diff --git a/core/modules/taxonomy/taxonomy.install b/core/modules/taxonomy/taxonomy.install
index 1a0b84993b23..bbd79f84a287 100644
--- a/core/modules/taxonomy/taxonomy.install
+++ b/core/modules/taxonomy/taxonomy.install
@@ -312,8 +312,6 @@ function taxonomy_update_8001() {
 
 /**
  * Create a UUID column for taxonomy terms.
- *
- * @todo UUID upgrade path: http://drupal.org/node/1642526
  */
 function taxonomy_update_8002() {
   $spec = array(
@@ -335,3 +333,19 @@ function taxonomy_update_8002() {
     db_add_field('taxonomy_term_data', 'uuid', $spec, $keys);
   }
 }
+
+/**
+ * Generate an UUID for all terms.
+ */
+function taxonomy_update_8003(&$sandbox) {
+  if (!isset($sandbox['progress'])) {
+    $sandbox['progress'] = 0;
+    $sandbox['last'] = 0;
+    $sandbox['max'] = db_query('SELECT COUNT(tid) FROM {taxonomy_term_data} WHERE uuid IS NULL')->fetchField();
+  }
+
+  $tids = db_query_range('SELECT tid FROM {taxonomy_term_data} WHERE tid > :tid AND uuid IS NULL ORDER BY tid ASC', 0, 10, array(':tid' => $sandbox['last']))->fetchCol();
+  update_add_uuids($sandbox, 'taxonomy_term_data', 'tid', $tids);
+
+  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
+}
diff --git a/core/modules/user/user.install b/core/modules/user/user.install
index 45d35b6b2fc8..a099f6bdf4e6 100644
--- a/core/modules/user/user.install
+++ b/core/modules/user/user.install
@@ -458,8 +458,6 @@ function user_update_8002() {
 
 /**
  * Create a UUID column for users.
- *
- * @todo UUID upgrade path: http://drupal.org/node/1642526
  */
 function user_update_8003() {
   $spec = array(
@@ -583,6 +581,23 @@ function user_update_8008() {
   );
 }
 
+/**
+ * Generate an UUID for all users.
+ */
+function user_update_8009(&$sandbox) {
+  if (!isset($sandbox['progress'])) {
+    $sandbox['progress'] = 0;
+    // The first user id is 0, so it needs to start with -1.
+    $sandbox['last'] = -1;
+    $sandbox['max'] = db_query('SELECT COUNT(uid) FROM {users} WHERE uuid IS NULL')->fetchField();
+  }
+
+  $uids = db_query_range('SELECT uid FROM {users} WHERE uid > :uid AND uuid IS NULL ORDER BY uid ASC', 0, 10, array(':uid' => $sandbox['last']))->fetchCol();
+  update_add_uuids($sandbox, 'users', 'uid', $uids);
+
+  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
+}
+
 /**
  * @} End of "addtogroup updates-7.x-to-8.x".
  */
-- 
GitLab