You are here

function user_relationships_api_update_5 in User Relationships 5.3

File

user_relationships_api/user_relationships_api.install, line 168

Code

function user_relationships_api_update_5() {
  return _user_relationships_api_installation_query(array(
    // Kill off the cache table
    'DROP TABLE {cache_user_relationships}',
    // drop rid as a primary key as it'll have duplicates
    'ALTER TABLE {user_relationships} DROP PRIMARY KEY',
    // create temporary created_at and updated_at fields and populate them with the current
    // created_at and updated_at values
    'ALTER TABLE {user_relationships} ADD `tmp_created_at` DATETIME',
    'ALTER TABLE {user_relationships} ADD `tmp_updated_at` DATETIME',
    'UPDATE {user_relationships} SET `tmp_created_at` = `created_at`, `tmp_updated_at` = `updated_at`',
    // alter the created_at and updated_at fields to be int(11) to hold unix timestamps
    // update the newly altered fields with the current data in unix timestamp format
    'ALTER TABLE {user_relationships} CHANGE  `created_at`  `created_at` INT(11) UNSIGNED NOT NULL',
    'ALTER TABLE {user_relationships} CHANGE  `updated_at`  `updated_at` INT(11) UNSIGNED NOT NULL',
    'UPDATE {user_relationships} SET `created_at` = UNIX_TIMESTAMP(`tmp_created_at`), `updated_at` = UNIX_TIMESTAMP(`tmp_updated_at`)',
    // remove the temporary fields
    'ALTER TABLE `user_relationships` DROP `tmp_created_at`',
    'ALTER TABLE `user_relationships` DROP `tmp_updated_at`',
    // add a uniqueness checker on the three main fields
    'ALTER IGNORE TABLE {user_relationships} ADD UNIQUE `relationship` (`requester_id`, `requestee_id`, `rtid`)',
    // migrate approved two-way relationships to be double entered
    'INSERT INTO {user_relationships} (`rid`, `requester_id`, `requestee_id`, `rtid`, `approved`, `created_at`, `updated_at`)
      SELECT `rid`, `requestee_id`, `requester_id`, ur.`rtid`, `approved`, `created_at`, `updated_at`
      FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt ON ur.`rtid` = urt.`rtid`
      WHERE urt.`is_oneway` = 0 AND ur.`approved` = 1',
  ), NULL, TRUE);
}