function redhen_org_update_7005 in RedHen CRM 7
Add machine name field and index to {redhen_org}.
File
- modules/
redhen_org/ redhen_org.install, line 406 - Schema and installation hooks for redhen_org module.
Code
function redhen_org_update_7005(&$sandbox) {
// First time here.
if (!isset($sandbox['progress'])) {
// Add new fields and index.
$fields = array(
'name' => array(
'description' => 'The machine-readable name of this org.',
'type' => 'varchar',
'length' => 32,
'default' => '',
'not null' => TRUE,
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0x1,
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
),
'module' => array(
'description' => 'The name of the providing module if the entity has been defined in code.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
);
// Add new fields. Note, the check to see if this field exists has been
// added b/c of https://drupal.org/node/2041379. There was a database update
// issue that was causing this DB update to fail after creating these
// additional columns, but before marking the update as complete. This check
// should allow the 7005 to run successful after already failing.
if (!db_field_exists('redhen_org', 'name')) {
foreach ($fields as $name => $field) {
db_add_field('redhen_org', $name, $field);
}
// Add unique index for name.
db_add_index('redhen_org', 'name', array(
'name',
));
}
// Initialize sandbox variables for batch processing.
$sandbox['progress'] = 0;
$sandbox['current_org_id'] = 0;
$sandbox['max'] = db_query('SELECT COUNT(DISTINCT org_id) FROM {redhen_org}')
->fetchField();
}
// Grab the next 50 orgs to process.
$orgs = db_select('redhen_org', 'org')
->fields('org', array(
'org_id',
'label',
'name',
))
->condition('org_id', $sandbox['current_org_id'], '>')
->range(0, 50)
->orderBy('org_id', 'ASC')
->execute();
// Update any RedHen orgs created without machine-readable names.
foreach ($orgs as $org) {
if (empty($org->name)) {
$machine_name = redhen_org_machine_name($org->label);
db_update('redhen_org')
->fields(array(
'name' => $machine_name,
))
->condition('org_id', $org->org_id)
->execute();
}
$sandbox['progress']++;
$sandbox['current_org_id'] = $org->org_id;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
return t('RedHen organizations have been updated with a default machine name.');
}