You are here

function flag_update_7303 in Flag 7.3

Rename database columns on the {flagging} table.

File

./flag.install, line 493
Flag module install/schema/update hooks.

Code

function flag_update_7303() {

  // Drop affected keys and indexes.
  db_drop_unique_key('flagging', 'fid_content_id_uid_sid');
  db_drop_index('flagging', 'content_type_uid_sid');
  db_drop_index('flagging', 'content_type_content_id_uid_sid');
  db_drop_index('flagging', 'content_id_fid');

  // Change field 'content_type' to 'entity_type'.
  db_change_field('flagging', 'content_type', 'entity_type', array(
    'description' => 'The flag type, eg "node", "comment", "user".',
    'type' => 'varchar',
    'length' => '32',
    'not null' => TRUE,
    'default' => '',
  ), array(
    'indexes' => array(
      'entity_type_uid_sid' => array(
        'entity_type',
        'uid',
        'sid',
      ),
    ),
  ));

  // Change field 'content_id' to 'entity_id'.
  db_change_field('flagging', 'content_id', 'entity_id', array(
    'description' => 'The unique ID of the content, such as either the {cid}, {uid}, or {nid}.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_add_unique_key('flagging', 'fid_entity_id_uid_sid', array(
    'fid',
    'entity_id',
    'uid',
    'sid',
  ));
  db_add_index('flagging', 'entity_type_entity_id_uid_sid', array(
    'entity_type',
    'entity_id',
    'uid',
    'sid',
  ));
  db_add_index('flagging', 'entity_id_fid', array(
    'entity_id',
    'fid',
  ));

  // A serial field must be defined as a key, so make a temporary index on
  // 'fcid' so we can safely drop the primary key.
  // @see http://drupal.org/node/190027
  db_add_index('flagging', 'temp', array(
    'fcid',
  ));

  // Drop the primary key so we can rename the field.
  db_drop_primary_key('flagging');

  // Change field 'fcid' to 'flagging_id'.
  db_change_field('flagging', 'fcid', 'flagging_id', array(
    'description' => 'The unique ID for this particular tag.',
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ), array(
    'primary key' => array(
      'flagging_id',
    ),
  ));

  // Drop our temporary index.
  db_drop_index('flagging', 'temp');
  cache_clear_all();
}