function quotes_update_6102 in Quotes 6
Implementation of hook_update_N().
File
- ./
quotes.install, line 281 - Handles installation and updates for the quotes module.
Code
function quotes_update_6102() {
global $db_type;
$items = $create = array();
// Skip this if upgrading from 5.x later than this addition.
if (db_table_exists('quotes_authors')) {
return $items;
}
$schema['quotes_authors'] = array(
'module' => 'Quotes',
'description' => t('Quotes authors data.'),
'fields' => array(
'aid' => array(
'description' => t('Author identifier.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'disp-width' => '10',
),
'name' => array(
'description' => t('Author of the quote.'),
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'bio' => array(
'description' => t("Author's biography."),
'type' => 'text',
'not null' => FALSE,
),
),
'primary key' => array(
'aid',
),
'unique keys' => array(
'name' => array(
'name',
),
),
);
db_create_table($items, 'quotes_authors', $schema['quotes_authors']);
// Add the aid column after the vid column.
db_add_column($items, 'quotes', 'aid', 'INT UNSIGNED NOT NULL AFTER vid');
$add = $items[count($items) - 1]['success'];
if (!$add) {
drupal_set_message(t('Add column "aid" failed.'), 'error');
return;
}
// Add an index for the aid.
db_add_index($items, 'quotes', 'quotes_aid', array(
'aid',
));
$add = $items[count($items) - 1]['success'];
if (!$add) {
drupal_set_message(t('Add index for "aid" failed.'), 'error');
return;
}
// Get all the authors.
$result = db_query("SELECT DISTINCT(author) FROM {quotes} ORDER BY author");
while ($q = db_fetch_array($result)) {
$author = $q['author'];
// If the current author field has a mini-bio, split it off.
$paren = strpos($author, '(');
$comma = strpos($author, ',');
$sub = min($paren === FALSE ? drupal_strlen($author) : $paren, $comma === FALSE ? drupal_strlen($author) : $comma);
if ($sub === FALSE) {
$bio = NULL;
}
else {
$bio = trim(drupal_substr($author, $sub));
$author = trim(drupal_substr($author, 0, $sub));
}
// Add the row to the new table.
$items[] = update_sql("INSERT INTO {quotes_authors} (name, bio) VALUES ('" . db_escape_string($author) . "', '" . db_escape_string($bio) . "')");
$add = $items[count($items) - 1]['success'];
if ($add === FALSE) {
$aid = 'failed';
$upd = 'bypassed';
}
else {
$aid = db_result(db_query("SELECT LAST_INSERT_ID()"));
if ($aid < 1) {
drupal_set_message(t('Invalid aid returned:') . ' ' . $aid, 'error');
}
$query = 'UPDATE {quotes} SET aid=' . $aid . " WHERE author='" . db_escape_string($q['author']) . "'";
$items[] = update_sql($query);
}
}
db_drop_field($items, 'quotes', 'author');
$del = $items[count($items) - 1]['success'];
if (!$del) {
drupal_set_message(t('Drop column "author" failed.'), 'error');
}
drupal_set_message(t('Update 6102 for Quotes complete.'), 'status');
return $items;
}