Skip to content

Instantly share code, notes, and snippets.

@wizonesolutions
Forked from anonymous/mymodule.install.php
Last active December 10, 2015 04:28
Show Gist options
  • Save wizonesolutions/4381625 to your computer and use it in GitHub Desktop.
Save wizonesolutions/4381625 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.

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.

Change the update numbers as is appropriate in your custom module. This should go in your .install file.

Update 7006 adds the archived field.

Update 7007 only applies if you used a version of the patch that made the primary key on the fields item_id instead of value like it used to be. It migrates data from field_data_(field name)item_id and field_revision(field name)_item_id ('item_id') to corresponding _value ('value') columns. You have to update your code to access Field Collections using 'value' instead of 'item_id' again. If you previously fixed it in reverse, you're basically undoing that.

Update 7008 drops the item_id column from the field tables, and it adds the item_id index to field_collection_item_revision that wasn't in all of the patches.

The site I ran this on was using the following Field Collection patch: http://drupal.org/node/1031010#comment-6170908

/**
* Fix up the field collection tables in anticipation of field_collection_update_7002().
*/
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