You are here

function flag_update_6209 in Flag 6.2

Clean up orphaned flags due to node and comment deletion.

File

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

Code

function flag_update_6209() {
  $ret = array();

  // Clean-up for nodes.
  db_query("DELETE FROM {flag_content} WHERE content_type = 'node' AND NOT EXISTS (SELECT 1 FROM {node} n WHERE content_id = n.nid)");
  db_query("DELETE FROM {flag_counts} WHERE content_type = 'node' AND NOT EXISTS (SELECT 1 FROM {node} n WHERE content_id = n.nid)");

  // Clean-up for comments. Do not use module_exists() because comment module
  // could be disabled.
  if (db_table_exists('comment')) {
    db_query("DELETE FROM {flag_content} WHERE content_type = 'comment' AND NOT EXISTS (SELECT 1 FROM {comment} c WHERE content_id = c.cid)");
    db_query("DELETE FROM {flag_counts} WHERE content_type = 'comment' AND NOT EXISTS (SELECT 1 FROM {comment} c WHERE content_id = c.cid)");
  }
  return $ret;
}