Upgrade path clean-up: Comment module.

From: Damien Tournoud <damien@commerceguys.com>


---
 comment/comment.install                            |  171 +++++++++-----------
 field/field.install                                |  102 ++++++++++++
 simpletest/simpletest.info                         |    2 
 .../tests/upgrade/drupal-6.comments.database.php   |   40 +++++
 simpletest/tests/upgrade/upgrade.comment.test      |   35 ++++
 simpletest/tests/upgrade/upgrade.poll.test         |    6 -
 simpletest/tests/upgrade/upgrade.test              |   16 +-
 7 files changed, 271 insertions(+), 101 deletions(-)
 create mode 100644 simpletest/tests/upgrade/drupal-6.comments.database.php
 create mode 100644 simpletest/tests/upgrade/upgrade.comment.test

diff --git modules/comment/comment.install modules/comment/comment.install
index 8702921..2b9e7aa 100644
--- modules/comment/comment.install
+++ modules/comment/comment.install
@@ -106,14 +106,33 @@ function comment_update_dependencies() {
  */
 
 /**
- * Remove comment settings for page ordering.
+ * Rename comment display setting variables.
  */
 function comment_update_7000() {
-  $types = node_type_get_types();
+  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
   foreach ($types as $type => $object) {
     variable_del('comment_default_order' . $type);
+
+    $setting = variable_get('comment_default_mode_' . $type, 4);
+    if ($setting == 3 || $setting == 4) {
+      variable_set('comment_default_mode_' . $type, 1);
+    }
+    else {
+      variable_set('comment_default_mode_' . $type, 0);
+    }
+
+    // There were only two comment modes in the past:
+    // - 1 was 'required' previously, convert into DRUPAL_REQUIRED (2).
+    // - 0 was 'optional' previously, convert into DRUPAL_OPTIONAL (1).
+    $original_preview = variable_get('comment_preview_' . $type, 1);
+    if ($original_preview) {
+      $preview = DRUPAL_REQUIRED;
+    }
+    else {
+      $preview = DRUPAL_OPTIONAL;
+    }
+    variable_set('comment_preview_' . $type, $preview);
   }
-  return t('Comment order settings removed.');
 }
 
 /**
@@ -138,49 +157,29 @@ function comment_update_7001() {
 }
 
 /**
- * Rename {comments} table to {comment}.
+ * Rename {comments} table to {comment} and upgrade it.
  */
 function comment_update_7002() {
   db_rename_table('comments', 'comment');
-}
-
-/**
- * Rename comment display setting variables.
- */
-function comment_update_7004() {
-  $types = node_type_get_types();
-  foreach ($types as $type => $object) {
-    $setting = variable_get('comment_default_mode_' . $type, 4);
-    if ($setting == 3 || $setting == 4) {
-      variable_set('comment_default_mode_' . $type, 1);
-    }
-    else {
-      variable_set('comment_default_mode_' . $type, 0);
-    }
-  }
-}
-
-/**
- * Create comment Field API bundles.
- */
-function comment_update_7005() {
-  foreach (node_type_get_types() as $info) {
-    field_attach_create_bundle('comment', 'comment_node_' . $info->type);
-  }
-}
 
-/**
- * Create user related indexes.
- */
-function comment_update_7006() {
+  // Add user-related indexes.
   db_add_index('comment', 'comment_uid', array('uid'));
   db_add_index('node_comment_statistics', 'last_comment_uid', array('last_comment_uid'));
+
+  // Create a language column.
+  db_add_field('comment', 'language', array(
+    'type' => 'varchar',
+    'length' => 12,
+    'not null' => TRUE,
+    'default' => '',
+  ));
+  db_add_index('comment', 'comment_nid_language', array('nid', 'language'));
 }
 
 /**
  * Split {comment}.timestamp into 'created' and 'changed', improve indexing on {comment}.
  */
-function comment_update_7007() {
+function comment_update_7003() {
   // Drop the old indexes.
   db_drop_index('comment', 'status');
   db_drop_index('comment', 'pid');
@@ -211,45 +210,9 @@ function comment_update_7007() {
 }
 
 /**
- * Add language column to the {comment} table.
- */
-function comment_update_7008() {
-  // Create a language column.
-  db_add_field('comment', 'language', array(
-    'type' => 'varchar',
-    'length' => 12,
-    'not null' => TRUE,
-    'default' => '',
-  ));
-
-  // Create the index.
-  db_add_index('comment', 'comment_nid_language', array('nid', 'language'));
-}
-
-/**
- * Update preview setting variable to use new constants
- */
-function comment_update_7009() {
-  foreach (node_type_get_types() as $type => $object) {
-    // There were only two comment modes in the past:
-    // - 1 was 'required' previously, convert into DRUPAL_REQUIRED (2).
-    // - 0 was 'optional' previously, convert into DRUPAL_OPTIONAL (1).
-    $original_preview = variable_get('comment_preview_' . $type, 1);
-    if ($original_preview) {
-      $preview = DRUPAL_REQUIRED;
-    }
-    else {
-      $preview = DRUPAL_OPTIONAL;
-    }
-    variable_set('comment_preview_' . $type, $preview);
-  }
-  return array();
-}
-
-/**
- * Add {node_comment_statistics}.cid column.
+ * Upgrade the {node_comment_statistics} table.
  */
-function comment_update_7010() {
+function comment_update_7004() {
   db_add_field('node_comment_statistics', 'cid', array(
     'type' => 'int',
     'not null' => TRUE,
@@ -257,59 +220,80 @@ function comment_update_7010() {
     'description' => 'The {comment}.cid of the last comment.',
   ));
   db_add_index('node_comment_statistics', 'cid', array('cid'));
-}
 
-/**
- * Add an index to node_comment_statistics on comment_count.
- */
-function comment_update_7011() {
+  // Add an index on the comment_count.
   db_add_index('node_comment_statistics', 'comment_count', array('comment_count'));
 }
 
 /**
  * Create the comment_body field.
  */
-function comment_update_7012() {
+function comment_update_7005() {
   // Create comment body field.
   $field = array(
     'field_name' => 'comment_body',
     'type' => 'text_long',
-    'entity_types' => array('comment'),
+    'module' => 'text',
+    'data' => array(
+      'entity_types' => array(
+        'comment',
+      ),
+      'settings' => array(),
+    ),
+    'cardinality' => 1,
   );
-  field_create_field($field);
+  _update_field_create_field($field);
 
   // Add the field to comments for all existing bundles.
-  $body_instance = array(
-    'field_name' => 'comment_body',
-    'label' => 'Comment',
+  $generic_instance = array(
     'entity_type' => 'comment',
-    'settings' => array('text_processing' => 1),
+    'label' => t('Comment'),
+    'settings' => array(
+      'text_processing' => 1,
+    ),
     'required' => TRUE,
     'display' => array(
       'default' => array(
         'label' => 'hidden',
         'type' => 'text_default',
         'weight' => 0,
+        'settings' => array(),
+        'module' => 'text',
+      ),
+    ),
+    'widget' => array(
+      'type' => 'text_textarea',
+      'settings' => array(
+        'rows' => 5,
       ),
+      'weight' => 0,
+      'module' => 'text',
     ),
+    'description' => '',
   );
-  foreach (node_type_get_types() as $info) {
-    $body_instance['bundle'] = 'comment_node_' . $info->type;
-    field_create_instance($body_instance);
+
+  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
+  foreach ($types as $type) {
+    $instance = $generic_instance;
+    $instance['bundle'] = 'comment_node_' . $type;
+    _update_field_create_instance($field, $instance);
   }
+
+  // Clear caches
+  field_cache_clear(TRUE);
 }
 
 /**
  * Migrate data from the comment field to field storage.
  */
-function comment_update_7013(&$sandbox) {
+function comment_update_7006(&$sandbox) {
   // This is a multipass update. First set up some comment variables.
   if (empty($sandbox['total'])) {
     $comments = (bool) db_query_range('SELECT 1 FROM {comment}', 0, 1)->fetchField();
     $sandbox['types'] = array();
     if ($comments) {
       $sandbox['etid'] = _field_sql_storage_etid('comment');
-      $sandbox['types'] = node_type_get_types();
+      $sandbox['types'] = db_query('SELECT type FROM {node_type}')->fetchCol();
     }
     $sandbox['total'] = count($sandbox['types']);
   }
@@ -318,9 +302,9 @@ function comment_update_7013(&$sandbox) {
     $type = array_shift($sandbox['types']);
 
     $query = db_select('comment', 'c');
-    $query->innerJoin('node', 'n', 'c.nid = n.nid AND n.type = :type', array(':type' => $type->type));
+    $query->innerJoin('node', 'n', 'c.nid = n.nid AND n.type = :type', array(':type' => $type));
     $query->addField('c', 'cid', 'entity_id');
-    $query->addExpression("'comment_node_$type->type'", 'bundle');
+    $query->addExpression("'comment_node_$type'", 'bundle');
     $query->addExpression($sandbox['etid'], 'etid');
     $query->addExpression('0', 'deleted');
     $query->addExpression("'" . LANGUAGE_NONE . "'", 'language');
@@ -328,8 +312,7 @@ function comment_update_7013(&$sandbox) {
     $query->addField('c', 'comment', 'comment_body_value');
     $query->addField('c', 'format', 'comment_body_format');
 
-    $comment_body = field_info_field('comment_body');
-    $comment_body_table = _field_sql_storage_tablename($comment_body);
+    $comment_body_table = 'field_data_comment_body';
 
     db_insert($comment_body_table)
       ->from($query)
diff --git modules/field/field.install modules/field/field.install
index 23cff05..e14f7e5 100644
--- modules/field/field.install
+++ modules/field/field.install
@@ -167,3 +167,105 @@ function field_schema() {
 
   return $schema;
 }
+
+
+/**
+ * Utility function: create a field directly to the database.
+ */
+function _update_field_create_field(&$field) {
+  // Merge in default values.`
+  $field += array(
+    'entity_types' => array(),
+    'cardinality' => 1,
+    'translatable' => FALSE,
+    'locked' => FALSE,
+    'settings' => array(),
+    'storage' => array(),
+    'indexes' => array(),
+    'deleted' => 0,
+    'active' => 1,
+  );
+  $field['data'] += array(
+    'entity_types' => array(),
+    'translatable' => FALSE,
+    'settings' => array(),
+    'cardinality' => 1,
+    'translatable' => FALSE,
+    'deleted' => 0,
+  );
+
+  // Set storage.
+  $field['storage'] = array(
+    'type' => 'field_sql_storage',
+    'settings' => array(),
+    'module' => 'field_sql_storage',
+    'active' => 1,
+  );
+
+  // The serialized 'data' column contains everything from $field that does not
+  // have its own column and is not automatically populated when the field is
+  // read.
+  $data = $field;
+  unset($data['columns'], $data['field_name'], $data['type'], $data['active'], $data['module'], $data['storage_type'], $data['storage_active'], $data['storage_module'], $data['locked'], $data['cardinality'], $data['deleted']);
+  // Additionally, do not save the 'bundles' property populated by
+  // field_info_field().
+  unset($data['bundles']);
+
+  // Write the field to the database.
+  $record = array(
+    'field_name' => $field['field_name'],
+    'type' => $field['type'],
+    'module' => $field['module'],
+    'active' => $field['active'],
+    'storage_type' => $field['storage']['type'],
+    'storage_module' => $field['storage']['module'],
+    'storage_active' => $field['storage']['active'],
+    'locked' => $field['locked'],
+    'data' => $data,
+    'cardinality' => $field['cardinality'],
+    'translatable' => $field['translatable'],
+    'deleted' => $field['deleted'],
+  );
+  drupal_write_record('field_config', $record);
+  $field['id'] = $record['id'];
+
+  // Create storage for this field.
+  $schema = (array) module_invoke($field['module'], 'field_schema', $field);
+  $schema += array('columns' => array(), 'indexes' => array());
+  // 'columns' are hardcoded in the field type.
+  $field['columns'] = $schema['columns'];
+  // 'indexes' can be both hardcoded in the field type, and specified in the
+  // incoming $field definition.
+  $field['indexes'] += $schema['indexes'];
+
+  module_invoke($field['storage']['module'], 'field_storage_create_field', $field);
+}
+
+/**
+ * Utility function: create a field instance directly to the database.
+ */
+function _update_field_create_instance($field, &$instance) {
+  // Merge in defaults.
+  $instance += array(
+    'field_id' => $field['id'],
+    'field_name' => $field['field_name'],
+    'deleted' => 0,
+  );
+
+  // The serialized 'data' column contains everything from $instance that does
+  // not have its own column and is not automatically populated when the
+  // instance is read.
+  $data = $instance;
+  unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);
+
+  $record = array(
+    'field_id' => $instance['field_id'],
+    'field_name' => $instance['field_name'],
+    'entity_type' => $instance['entity_type'],
+    'bundle' => $instance['bundle'],
+    'data' => $data,
+    'deleted' => $instance['deleted'],
+  );
+  drupal_write_record('field_config_instance', $record);
+  $instance['id'] = $record['id'];
+}
diff --git modules/simpletest/simpletest.info modules/simpletest/simpletest.info
index 63f61e6..79b7bf7 100644
--- modules/simpletest/simpletest.info
+++ modules/simpletest/simpletest.info
@@ -38,5 +38,7 @@ files[] = tests/theme.test
 files[] = tests/unicode.test
 files[] = tests/update.test
 files[] = tests/xmlrpc.test
+
 files[] = tests/upgrade/upgrade.test
 files[] = tests/upgrade/upgrade.poll.test
+files[] = tests/upgrade/upgrade.comment.test
diff --git modules/simpletest/tests/upgrade/drupal-6.comments.database.php modules/simpletest/tests/upgrade/drupal-6.comments.database.php
new file mode 100644
index 0000000..7da2504
--- /dev/null
+++ modules/simpletest/tests/upgrade/drupal-6.comments.database.php
@@ -0,0 +1,40 @@
+<?php
+db_update('node')->fields(array(
+    'comment' => 2
+  ))
+  ->condition('nid', 1)
+  ->execute();
+
+db_insert('comments')->fields(array(
+  'cid',
+  'pid',
+  'nid',
+  'uid',
+  'subject',
+  'comment',
+  'hostname',
+  'timestamp',
+  'status',
+  'format',
+  'thread',
+  'name',
+  'mail',
+  'homepage',
+))
+->values(array(
+  'cid' => 1,
+  'pid' => 0,
+  'nid' => 1,
+  'uid' => 3,
+  'subject' => 'Comment title 1',
+  'comment' => 'Comment body 1 - Comment body 1 - Comment body 1 - Comment body 1 - Comment body 1 - Comment body 1 - Comment body 1 - Comment body 1',
+  'hostname' => '127.0.0.1',
+  'timestamp' => 1008617630,
+  'status' => 0,
+  'format' => 1,
+  'thread' => '01/',
+  'name' => NULL,
+  'mail' => NULL,
+  'homepage' => '',
+))
+->execute();
diff --git modules/simpletest/tests/upgrade/upgrade.comment.test modules/simpletest/tests/upgrade/upgrade.comment.test
new file mode 100644
index 0000000..89fed62
--- /dev/null
+++ modules/simpletest/tests/upgrade/upgrade.comment.test
@@ -0,0 +1,35 @@
+<?php
+// $Id$
+
+/**
+ * Upgrade test for comment.module.
+ */
+class CommentUpgradePathTestCase extends UpgradePathTestCase {
+  public static function getInfo() {
+    return array(
+      'name'  => 'Comment upgrade path',
+      'description'  => 'Comment upgrade path tests.',
+      'group' => 'Upgrade path',
+    );
+  }
+
+  public function setUp() {
+    // Path to the database dump files.
+    $this->databaseDumpFiles = array(
+      drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php',
+      drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.comments.database.php',
+    );
+    parent::setUp();
+
+    $this->uninstallModulesExcept(array('comment'));
+  }
+
+  /**
+   * Test a successful upgrade.
+   */
+  public function testCommentUpgrade() {
+    $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
+
+    $this->drupalGet('node/1');
+  }
+}
diff --git modules/simpletest/tests/upgrade/upgrade.poll.test modules/simpletest/tests/upgrade/upgrade.poll.test
index 5804710..9b0054f 100644
--- modules/simpletest/tests/upgrade/upgrade.poll.test
+++ modules/simpletest/tests/upgrade/upgrade.poll.test
@@ -20,8 +20,10 @@ class PollUpgradePathTestCase extends UpgradePathTestCase {
   }
 
   public function setUp() {
-    // Path to the database dump.
-    $this->databaseDumpFile = drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php';
+    // Path to the database dump files.
+    $this->databaseDumpFiles = array(
+      drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php',
+    );
     parent::setUp();
 
     $this->uninstallModulesExcept(array('poll'));
diff --git modules/simpletest/tests/upgrade/upgrade.test modules/simpletest/tests/upgrade/upgrade.test
index aa487e6..4ef35b3 100644
--- modules/simpletest/tests/upgrade/upgrade.test
+++ modules/simpletest/tests/upgrade/upgrade.test
@@ -7,9 +7,11 @@
 abstract class UpgradePathTestCase extends DrupalWebTestCase {
 
   /**
-   * The file path to the dumped database to load into the child site.
+   * The file path(s) to the dumped database(s) to load into the child site.
+   *
+   * @var array
    */
-  var $databaseDumpFile = NULL;
+  var $databaseDumpFiles = array();
 
   /**
    * Flag that indicates whether the child site has been upgraded.
@@ -88,7 +90,9 @@ abstract class UpgradePathTestCase extends DrupalWebTestCase {
     $conf = array();
 
     // Load the database from the portable PHP dump.
-    require $this->databaseDumpFile;
+    foreach ($this->databaseDumpFiles as $file) {
+      require $file;
+    }
 
     // Set path variables.
     $this->variable_set('file_public_path', $public_files_directory);
@@ -328,8 +332,10 @@ class BasicUpgradePath extends UpgradePathTestCase {
   }
 
   public function setUp() {
-    // Path to the database dump.
-    $this->databaseDumpFile = drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.bare.database.php';
+    // Path to the database dump files.
+    $this->databaseDumpFiles = array(
+      drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.bare.database.php',
+    );
     parent::setUp();
   }
 
