You are here

function book_copy_update_6000 in Book Copy 6

File

./book_copy.install, line 75

Code

function book_copy_update_6000() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {book_copy_history} ADD uid int(11) NOT NULL");
      $result = db_query("SELECT distinct b.bid, u.uid FROM {book} b LEFT JOIN {node} n ON n.nid = b.nid LEFT JOIN {users} u ON n.uid = u.uid;");

      // set copy uid to user who owns the book now
      while ($row = db_fetch_array($result)) {
        db_query("UPDATE {book_copy_history} SET uid = %d WHERE bid = %d", $row['uid'], $row['bid']);
      }

      // fix any nodes that may have since been deleted (we will still have some orphans)
      $result = db_query("SELECT n.nid, u.uid FROM {book_copy_history} bch LEFT JOIN {node} n ON n.nid = bch.nid LEFT JOIN {users} u ON u.uid = n.uid WHERE bch.uid = 0");
      while ($row = db_fetch_array($result)) {
        db_query("UPDATE {book_copy_history} SET uid = %d WHERE nid = %d", $row['uid'], $row['nid']);
      }
      break;
  }
  return $ret;
}