You are here

function casetracker_update_16 in Case Tracker 5

File

./casetracker.install, line 461

Code

function casetracker_update_16() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("DELETE FROM {sequences} WHERE name = 'case_id'");
      $ret[] = update_sql("DELETE FROM {sequences} WHERE name = 'project_no'");
      db_query('LOCK TABLES {casetracker_project} WRITE');
      $project_number = db_result(db_query_range("SELECT project_no FROM {casetracker_project} ORDER BY project_no DESC", 0, 1));
      $project_number = $project_number ? $project_number : 100;

      // if there are no projects, we always start at 100.
      variable_set('casetracker_current_project_number', $project_number);
      db_query('LOCK TABLES {casetracker_case} WRITE');

      // case numbers were supposed to be namespaced to the project, but that
      // never actually happened, so we'll loop through all the projects and
      // just start proper namespacing at whatever the last case number was.
      $results = db_query("SELECT p_id, MAX(case_id) AS last_case_id FROM casetracker_case GROUP BY p_id");
      $project_case_numbers = array();
      while ($result = db_fetch_object($results)) {
        $project_case_numbers[$result->p_id] = $result->last_case_id ? $result->last_case_id : 1;
      }

      // i know, i know, serializing an array, but it's my cross to bare. cases always start at 1.
      variable_set('casetracker_current_case_numbers', $project_case_numbers);
      db_query('UNLOCK TABLES');
      break;
  }
  return $ret;
}