Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2012 17:25
Show Gist options
  • Save anonymous/4381623 to your computer and use it in GitHub Desktop.
Save anonymous/4381623 to your computer and use it in GitHub Desktop.
Bridging code from Field Collection patched with earlier patches that provided revision support (ones that used item_id). Pick and choose the update logic that you need in your use case.
/**
* Fix up the field collection tables in anticipation of update 7001. If this
* doesn't work as an update, do it as a utility page.
*/
function mymodule_update_7006() {
// Add the archived column
$archived_spec = array(
'description' => 'Boolean indicating whether the field collection item is archived.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
);
db_add_field('field_collection_item', 'archived', $archived_spec);
return "Prep database for field_collection_update_7002().";
}
/**
* They went back to using value instead of item_id for the field schema.
*/
function mymodule_update_7007() {
$value_spec = array(
'type' => 'int',
'not null' => TRUE,
'description' => 'The field collection item id.',
// Set default to 0 temporarily.
'initial' => 0,
);
// Update the field_collection_field_schema columns for all tables.
foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
$table_prefixes = array('field_data', 'field_revision');
foreach ($table_prefixes as $table_prefix) {
$table = sprintf('%s_%s', $table_prefix, $field_name);
$item_id_column = sprintf('%s_item_id', $field_name);
$value_column = sprintf('%s_value', $field_name);
// Add a value column.
$value_spec['description'] = 'The field collection item id.';
db_add_field($table, $value_column, $value_spec);
// Initialize the value to be the same as the item_id.
db_update($table)
->expression($value_column, $item_id_column)
->execute();
}
}
return "Prep database for field_collection_update_7002().";
}
/**
* Drop the now-irrelevant item_id column to make debugging code that was using
* the old structure easier. Also drop field_name from
* field_collection_item_revision and add the index.
*/
function mymodule_update_7008() {
db_drop_field('field_collection_item_revision', 'field_name');
db_add_index('field_collection_item_revision', 'item_id', array('item_id'));
foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) {
$table_prefixes = array('field_data', 'field_revision');
foreach ($table_prefixes as $table_prefix) {
$table = sprintf('%s_%s', $table_prefix, $field_name);
$item_id_column = sprintf('%s_item_id', $field_name);
db_drop_field($table, $item_id_column);
}
}
return "Drop field_name from field_collection_item_revision, index item_id,
add foreign key, and drop item_id columns from Field Collection field
tables.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment