Created
May 25, 2012 04:29
-
-
Save nicksantamaria/2785770 to your computer and use it in GitHub Desktop.
Updating 4 field machine names in Drupal 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function er_migration_update_7003() { | |
// Changing the names of the porfolio field prefixes from modelp to portfolio | |
$fields = array( | |
'field_modelp_title' => array( | |
'data_col_suffix' => array( | |
'value' => 'LONGTEXT', | |
'format' => 'VARCHAR(255) DEFAULT NULL', | |
), | |
), | |
'field_modelp_desc' => array( | |
'data_col_suffix' => array( | |
'value' => 'LONGTEXT', | |
'format' => 'VARCHAR(255) DEFAULT NULL', | |
), | |
), | |
'field_modelp_type' => array( | |
'data_col_suffix' => array( | |
'value' => 'LONGTEXT', | |
'format' => 'VARCHAR(255) DEFAULT NULL', | |
), | |
), | |
'field_modelp_link' => array( | |
'data_col_suffix' => array( | |
'url' => "VARCHAR(2048) DEFAULT NULL", | |
'title' => "VARCHAR(255) DEFAULT NULL", | |
'attributes' => 'MEDIUMTEXT', | |
), | |
), | |
); | |
$replace = array( | |
'modelp' => 'portfolio', | |
); | |
foreach ($fields as $old_field_name => $field) { | |
$new_field_name = str_replace(array_keys($replace), $replace, $old_field_name); | |
$args[':new_field_name'] = $new_field_name; | |
$args[':field_name'] = $old_field_name; | |
$q[] = array( | |
'query' => "UPDATE {field_config} SET field_name = :new_field_name WHERE field_name = :field_name", | |
'args' => $args, | |
); | |
$q[] = array( | |
'query' => "UPDATE {field_config_instance} SET field_name = :new_field_name WHERE field_name = :field_name", | |
'args' => $args, | |
); | |
foreach (array('revision', 'data') as $type) { | |
$old_table = "field_". $type ."_". $old_field_name; | |
$new_table = "field_". $type ."_". $new_field_name; | |
$q[] = array( | |
'query' => "RENAME TABLE `". $old_table ."` TO `". $new_table ."`", | |
); | |
foreach ($field['data_col_suffix'] as $suffix => $extra) { | |
$old_col = $old_field_name .'_'. $suffix; | |
$new_col = $new_field_name .'_'. $suffix; | |
$q[] = array( | |
'query' => 'ALTER TABLE `'. $new_table .'` CHANGE `'. $old_col .'` `'. $new_col .'` '. $extra, | |
); | |
} | |
} | |
} | |
foreach ($q as $i) { | |
$args = (empty($i['args'])) ? array() : $i['args']; | |
db_query($i['query'], $args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment