function blockreference_update_7200 in Block reference 7.2
Convert bid storage to module/delta storage.
File
- ./
blockreference.install, line 11 - Block Reference install & updates.
Code
function blockreference_update_7200() {
$old_schema = drupal_get_schema(NULL, TRUE);
// Find fields & tables.
$all_fields = field_info_field_map();
$alter_tables = array();
foreach ($all_fields as $field_name => $field) {
if ($field['type'] == 'blockreference') {
$table_name = 'field_data_' . $field_name;
$alter_tables[$table_name] = $field_name;
$revision_table_name = 'field_revision_' . $field_name;
if (isset($old_schema[$revision_table_name])) {
$alter_tables[$revision_table_name] = $field_name;
}
}
}
$col_spec = array(
'type' => 'varchar',
'length' => 129,
// 4 x 32 + 1 should be enough
'not null' => TRUE,
'default' => '',
);
$invalid_bids = array();
foreach ($alter_tables as $table_name => $field_name) {
$column_bid = $field_name . '_bid';
$column_moddelta = $field_name . '_moddelta';
// Alter table columns.
db_add_field($table_name, $column_moddelta, $col_spec);
// Convert bids to moddeltas.
db_query("\n UPDATE {{$table_name}}\n SET\n {$column_moddelta} = (SELECT CONCAT(module, ':', delta) FROM {block} WHERE bid = {{$table_name}}.{$column_bid})\n WHERE\n {$column_bid} IN (SELECT bid FROM {block})\n ")
->execute();
$invalid_bids = array_merge($invalid_bids, array_values(db_query("\n SELECT {$column_bid}\n FROM {{$table_name}}\n WHERE {$column_moddelta} = ''\n ")
->fetchCol(0, 0)));
// Alter table columns.
db_drop_field($table_name, $column_bid);
// Alter table columns.
db_add_index($table_name, 'moddelta', array(
$column_moddelta,
));
}
if ($invalid_bids) {
$invalid_bids = array_unique($invalid_bids);
drupal_set_message(format_string("@num block ids could not be converted, because they don't exist (anymore).", array(
'@num' => count($invalid_bids),
)), 'warning');
}
}