Index: modules/taxonomy/taxonomy.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.install,v
retrieving revision 1.21
diff -u -p -r1.21 taxonomy.install
--- modules/taxonomy/taxonomy.install	4 Aug 2009 06:50:07 -0000	1.21
+++ modules/taxonomy/taxonomy.install	18 Aug 2009 17:20:36 -0000
@@ -202,13 +202,6 @@ function taxonomy_schema() {
         'size' => 'big',
         'description' => 'Description of the vocabulary.',
       ),
-      'help' => array(
-        'type' => 'varchar',
-        'length' => 255,
-        'not null' => TRUE,
-        'default' => '',
-        'description' => 'Help text to display for the vocabulary.',
-      ),
       'relations' => array(
         'type' => 'int',
         'unsigned' => TRUE,
@@ -225,30 +218,6 @@ function taxonomy_schema() {
         'size' => 'tiny',
         'description' => 'The type of hierarchy allowed within the vocabulary. (0 = disabled, 1 = single, 2 = multiple)',
       ),
-      'multiple' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'size' => 'tiny',
-        'description' => 'Whether or not multiple terms from this vocabulary may be assigned to a node. (0 = disabled, 1 = enabled)',
-      ),
-      'required' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'size' => 'tiny',
-        'description' => 'Whether or not terms are required for nodes using this vocabulary. (0 = disabled, 1 = enabled)',
-      ),
-      'tags' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'size' => 'tiny',
-        'description' => 'Whether or not free tagging is enabled for the vocabulary. (0 = disabled, 1 = enabled)',
-      ),
       'module' => array(
         'type' => 'varchar',
         'length' => 255,
@@ -256,13 +225,6 @@ function taxonomy_schema() {
         'default' => '',
         'description' => 'The module which created the vocabulary.',
       ),
-      'weight' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'default' => 0,
-        'size' => 'tiny',
-        'description' => 'The weight of the vocabulary in relation to other vocabularies.',
-      ),
     ),
     'primary key' => array('vid'),
     'indexes' => array(
@@ -270,33 +232,6 @@ function taxonomy_schema() {
     ),
   );
 
-  $schema['taxonomy_vocabulary_node_type'] = array(
-    'description' => 'Stores which node types vocabularies may be used with.',
-    'fields' => array(
-      'vid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'description' => 'Primary Key: the {taxonomy_vocabulary}.vid of the vocabulary.',
-      ),
-      'type' => array(
-        'type' => 'varchar',
-        'length' => 32,
-        'not null' => TRUE,
-        'default' => '',
-        'description' => 'The {node}.type of the node type for which the vocabulary may be used.',
-      ),
-    ),
-    'primary key' => array('type', 'vid'),
-    'indexes' => array(
-      'vid' => array('vid'),
-    ),
-    'foreign keys' => array(
-      'vid' => array('taxonomy_vocabulary' => 'vid'),
-    ),
-  );
-
   return $schema;
 }
 
@@ -338,3 +273,79 @@ function taxonomy_update_7003() {
 
   return $ret;
 }
+
+/**
+ * Move taxonomy vocabulary associations for nodes to fields and field instances.
+ */
+function taxonomy_update_7005() {
+  $ret = array();
+  foreach (taxonomy_get_vocabularies() as $vocabulary) {
+    $field_name = 'taxonomy_vocabulary_' . $vocabulary->machine_name;
+    $field = array(
+      'field_name' => $field_name,
+      'type' => 'taxonomy_term',
+      'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
+      'settings' => array(
+        'required' => $vocabulary->required ? TRUE : FALSE,
+        'allowed_values' => array(
+          array(
+            'vid' => $vocabulary->vid,
+            'parent' => 0,
+          ),
+        ),
+      ),
+    );
+    field_create_field($field);
+
+    foreach ($vocabulary->nodes as $bundle) {
+      $instance = array(
+        'label' => $vocabulary->name,
+        'field_name' => $field_name,
+        'bundle' => $bundle,
+        'description' => $vocabulary->help,
+        'widget' => array(
+          'type' => $vocabulary->tags ? 'taxonomy_autocomplete' : 'select',
+        ),
+      );
+      field_create_instance($instance);
+    }
+    // Migrate {term_node} data to the new field tables.
+    // Since we are upgrading from Drupal 6, we know that only
+    // field_sql_storage.module will be enabled.
+    $field = field_info_field($field['field_name']);
+    $data_table = _field_sql_storage_tablename($field);
+    $revision_table = _field_sql_storage_revision_tablename($field);
+    $etid = _field_sql_storage_etid('node');
+    $value_column = $field['field_name'] . '_value';
+    $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'delta', $value_column);
+
+    // Query and save data for the current revision.
+    $result = db_query('SELECT td.tid, tn.nid, td.weight, tn.vid FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid INNER JOIN {node} n WHERE tn.vid = n.vid AND td.vid = :vocabulary_id ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid));
+    $deltas = array();
+    foreach ($result as $record) {
+      // Start deltas from 0, and increment by one for each
+      //  term attached to a node.
+      $deltas[$record->nid] = isset($deltas[$record->nid]) ? ++$deltas[$record->nid] : 0;
+      $values = array($etid, $record->nid, $record->vid, $bundle, $deltas[$record->nid], $record->tid);
+      db_insert($data_table)->fields($columns)->values($values)->execute();
+    }
+
+    // Query and save data for all revisions.
+    $result = db_query('SELECT td.tid, tn.nid, td.weight, tn.vid FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid AND td.vid = :vocabulary_id ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid));
+    $deltas = array();
+    foreach ($result as $record) {
+      // Start deltas at 0, and increment by one for each term attached to a revision.
+      $deltas[$record->vid] = isset($deltas[$record->vid]) ? ++$deltas[$record->vid] : 0;
+      $values = array($etid, $record->nid, $record->vid, $bundle, $deltas[$record->vid], $record->tid);
+      db_insert($revision_table)->fields($columns)->values($values)->execute();
+    }
+  }
+
+  db_drop_table($ret, 'taxonomy_vocabulary_node_type');
+  $fields = array('help', 'multiple', 'required', 'tags');
+  foreach ($fields as $field) {
+    db_drop_field($ret, 'taxonomy_vocabulary', $field);
+  }
+
+  return $ret;
+}
