taxonomy_update_7005
- Versions
- 7
taxonomy_update_7005(&$sandbox)
Migrate {taxonomy_term_node} table to field storage.
Code
modules/taxonomy/taxonomy.install, line 356
<?php
function taxonomy_update_7005(&$sandbox) {
// 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);
// This is a multi-pass update. On the first call we need to initialize some
// variables.
if (!isset($sandbox['total'])) {
$sandbox['last'] = 0;
$sandbox['count'] = 0;
$query = db_select('taxonomy_term_node', 't');
$sandbox['total'] = $query->countQuery()->execute()->fetchField();
$found = (bool) $sandbox['total'];
}
else {
// We do each pass in batches of 1000, this should result in a
// maximum of 2000 insert queries each operation.
$batch = 1000 + $sandbox['last'];
// Query and save data for the current revision.
$result = db_query_range('SELECT td.tid, tn.nid, td.weight, tn.vid, n2.type, n2.created, n2.sticky FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid INNER JOIN {node} n2 ON tn.nid = n2.nid INNER JOIN {node} n ON tn.vid = n.vid AND td.vid = :vocabulary_id ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid), $sandbox['last'], $batch);
$deltas = array();
foreach ($result as $record) {
$found = TRUE;
$sandbox['count'] += 1;
// 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, $record->type, $deltas[$record->nid], $record->tid);
db_insert($data_table)->fields($columns)->values($values)->execute();
// Update the {taxonomy_index} table.
db_insert('taxonomy_index')
->fields(array('nid', 'tid', 'sticky', 'created',))
->values(array($record->nid, $record->tid, $record->sticky, $record->created))
->execute();
}
// Query and save data for all revisions.
$result = db_query('SELECT td.tid, tn.nid, td.weight, tn.vid, n.type FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid AND td.vid = :vocabulary_id INNER JOIN {node} n ON tn.nid = n.nid ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid), $sandbox['last'][$batch]);
$deltas = array();
foreach ($result as $record) {
$found = TRUE;
$sandbox['count'] += 1;
// 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, $record->type, $deltas[$record->vid], $record->tid);
db_insert($revision_table)->fields($columns)->values($values)->execute();
}
$sandbox['last'] = $batch;
}
if (!$found) {
db_drop_table('taxonomy_term_node');
}
}
?>Login or register to post comments 