You are here

function flag_views_bookmark_update_tables in Flag 5

Same name and namespace in other branches
  1. 6.2 includes/flag.views_bookmark.inc \flag_views_bookmark_update_tables()
  2. 6 includes/flag.views_bookmark.inc \flag_views_bookmark_update_tables()

Update Views Bookmark to the Flag schema.

1 call to flag_views_bookmark_update_tables()
flag_views_bookmark_update in includes/flag.views_bookmark.inc
Perform all pieces of the update.

File

includes/flag.views_bookmark.inc, line 64
flag.view_bookmark.inc

Code

function flag_views_bookmark_update_tables() {
  include_once './includes/install.inc';
  $ret = array();

  // Drop all keys.
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP PRIMARY KEY");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodes} DROP PRIMARY KEY");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_node_count} DROP PRIMARY KEY");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodetypes} DROP PRIMARY KEY");

  // Rename columns.
  $ret[] = update_sql("ALTER TABLE {views_bookmark_node_count} CHANGE COLUMN vbid fid smallint unsigned NOT NULL default '0'");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_node_count} CHANGE COLUMN nid content_id int unsigned NOT NULL default '0'");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodes} CHANGE COLUMN vbid fid smallint unsigned NOT NULL default '0'");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodes} CHANGE COLUMN nid content_id int unsigned NOT NULL default '0'");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodes} CHANGE COLUMN uid uid int unsigned NOT NULL default '0' AFTER content_id");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodetypes} CHANGE COLUMN vbid fid smallint unsigned NOT NULL default '0'");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodetypes} CHANGE COLUMN type type varchar(32) NOT NULL default ''");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} CHANGE COLUMN vbid fid smallint unsigned NOT NULL default '0'");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} CHANGE COLUMN global global tinyint default 0");

  // Add columns.
  $ret[] = update_sql("ALTER TABLE {views_bookmark_node_count} ADD content_type varchar(32) default '' AFTER fid");
  $ret[] = update_sql("ALTER TABLE {views_bookmark_nodes} ADD content_type varchar(32) default '' AFTER fid");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} ADD content_type varchar(32) default '' AFTER fid");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} ADD name varchar(32) default '' AFTER content_type");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} ADD options text default NULL");

  // Move options.
  $result = db_query("SELECT * FROM {views_bookmarks}");
  while ($bookmark = db_fetch_object($result)) {
    $options = array(
      'flag_short' => $bookmark->mark,
      'flag_long' => $bookmark->mark_long,
      'flag_message' => $bookmark->mark_message,
      'unflag_short' => $bookmark->unmark,
      'unflag_long' => $bookmark->unmark_long,
      'unflag_message' => $bookmark->unmark_message,
      'show_on_form' => $bookmark->show_on_form,
      'show_on_teaser' => $bookmark->teaser,
      'show_on_page' => 1,
    );
    db_query("UPDATE {views_bookmarks} SET options = '%s' WHERE fid = %d", serialize($options), $bookmark->fid);
  }

  // Delete columns.
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN mark");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN mark_long");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN mark_message");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN unmark");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN unmark_long");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN unmark_message");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN show_on_form");
  $ret[] = update_sql("ALTER TABLE {views_bookmarks} DROP COLUMN teaser");

  // Rename all tables.
  $ret[] = update_sql("RENAME TABLE {views_bookmarks} TO {flags}");
  $ret[] = update_sql("RENAME TABLE {views_bookmark_nodes} TO {flag_content}");
  $ret[] = update_sql("RENAME TABLE {views_bookmark_node_count} TO {flag_counts}");
  $ret[] = update_sql("RENAME TABLE {views_bookmark_nodetypes} TO {flag_types}");

  // Set defaults.
  $ret[] = update_sql("UPDATE {flag_content} SET content_type = 'node'");
  $ret[] = update_sql("UPDATE {flag_counts} SET content_type = 'node'");
  $ret[] = update_sql("UPDATE {flags} SET content_type = 'node'");

  // Create primary keys.
  $ret[] = update_sql("ALTER TABLE {flags} ADD PRIMARY KEY (fid)");
  $ret[] = update_sql("ALTER TABLE {flag_types} ADD INDEX (fid)");
  $ret[] = update_sql("ALTER TABLE {flag_counts} ADD PRIMARY KEY (fid, content_type, content_id)");
  $ret[] = update_sql("ALTER TABLE {flag_content} ADD fcid int unsigned NOT NULL auto_increment FIRST, ADD PRIMARY KEY (fcid)");
  $ret[] = update_sql("ALTER TABLE {flag_content} ADD UNIQUE INDEX fid_content_type_content_id_uid (fid, content_type, content_id, uid)");
  $ret[] = update_sql("ALTER TABLE {flag_content} ADD INDEX content_type_content_id (content_type, content_id)");
  $ret[] = update_sql("ALTER TABLE {flag_content} ADD INDEX content_type_uid (content_type, uid)");

  // Update sequences.
  $ret[] = update_sql("UPDATE {sequences} SET name = '{flags}_fid' WHERE name = '{views_bookmarks}_vbid'");

  // Give each flag a "name" attribute.
  $result = db_query("SELECT fid, title FROM {flags}");
  while ($row = db_fetch_object($result)) {
    $name = substr(preg_replace('/[^a-z_]/', '', str_replace(' ', '_', drupal_strtolower(trim($row->title)))), 0, 32);
    $ret[] = update_sql("UPDATE {flags} SET name = '" . $name . "' WHERE fid = " . $row->fid);
    drupal_set_message(t('The views bookmark %bookmark has been migrated to Flag. It was given the machine-name %name, which you may change on the <a href="!url">%bookmark configuration form</a>.', array(
      '%bookmark' => $row->title,
      '%name' => $name,
      '!url' => url('admin/build/flags/edit/' . $name),
    )));
  }

  // The key for flag name must be added after giving names.
  $ret[] = update_sql("ALTER TABLE {flags} ADD UNIQUE KEY (name)");
  return $ret;
}