You are here

function event_update_3 in Event 5.2

Update 4.7 or 5.1 to 5.2

File

./event.install, line 140

Code

function event_update_3() {
  $ret = array();
  variable_del('event_range_prev');
  variable_del('event_range_next');
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("RENAME TABLE {event} TO {event_backup}");
      $ret[] = update_sql("CREATE TABLE {event_timezones} (\n                timezone int NOT NULL default '0',\n                name varchar(255) NOT NULL default '',\n                offset TIME NOT NULL default '0',\n                offset_dst TIME NOT NULL default '0',\n                dst_region int NOT NULL default '0',\n                PRIMARY KEY (timezone)\n                ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $zones = event_install_timezones();
      foreach ($zones as $zone => $value) {
        db_query("INSERT INTO {event_timezones} (timezone, name, offset, offset_dst, dst_region) VALUES (%d, '%s', '%s', '%s', %d)", $zone, $value['timezone'], $value['offset'], $value['offset_dst'], $value['dst_region']);
      }
      $ret[] = update_sql("CREATE TABLE {event} (\n                nid int(10) unsigned NOT NULL default '0',\n                event_start datetime NOT NULL,\n                event_end datetime NOT NULL,\n                timezone int NOT NULL default '0',\n                start_in_dst int NOT NULL default '0',\n                end_in_dst int NOT NULL default '0'\n                ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $ret[] = update_sql("SET time_zone = '+00:00'");
      $sql = "INSERT INTO {event} (nid, event_start, event_end, timezone, start_in_dst, end_in_dst) SELECT e.nid, IF(%cond_start, FROM_UNIXTIME(e.event_start) + INTERVAL ez.offset_dst HOUR_SECOND, FROM_UNIXTIME(e.event_start) + INTERVAL ez.offset HOUR_SECOND) AS event_start, IF(%cond_end, FROM_UNIXTIME(e.event_end) + INTERVAL ez.offset_dst HOUR_SECOND, FROM_UNIXTIME(e.event_end) + INTERVAL ez.offset HOUR_SECOND) AS event_end, e.timezone, IF(%cond_start, 1, 0), IF(%cond_end, 1, 0) FROM {event_backup} e INNER JOIN {event_timezones} ez ON e.timezone = ez.timezone WHERE ez.dst_region = %d";

      // Loop over the 21 supported DST regions.
      for ($i = 0; $i <= 20; $i++) {
        if ($i == 0) {
          $ret[] = update_sql("INSERT INTO {event} (nid, event_start, event_end, timezone, start_in_dst, end_in_dst) SELECT e.nid, FROM_UNIXTIME(e.event_start) + INTERVAL ez.offset HOUR_SECOND AS event_start, FROM_UNIXTIME(e.event_end) + INTERVAL ez.offset HOUR_SECOND AS event_end, e.timezone, 0, 0 FROM {event_backup} e INNER JOIN {event_timezones} ez ON e.timezone = ez.timezone WHERE ez.dst_region = 0");
        }
        else {
          $start = str_replace('%date', 'e.event_start', _event_install_get_dst_dates($i));
          $end = str_replace('%date', 'e.event_end', _event_install_get_dst_dates($i));
          $ret[] = update_sql(str_replace(array(
            '%cond_start',
            '%cond_end',
            '%d',
          ), array(
            $start,
            $end,
            $i,
          ), $sql));
        }
      }
      $ret[] = update_sql('ALTER TABLE {event} ADD PRIMARY KEY nid (nid)');
      $ret[] = update_sql('ALTER TABLE {event} ADD KEY timezone (timezone)');
      $ret[] = update_sql("ALTER TABLE {users} ADD timezone_id int NOT NULL default '0'");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {event} RENAME TO {event_backup}");
      $ret[] = update_sql("CREATE TABLE {event_timezones} (\n                timezone integer NOT NULL default '0',\n                name varchar(255) NOT NULL default '',\n                \"offset\" interval NOT NULL default '0 seconds',\n                offset_dst interval NOT NULL default '0 seconds',\n                dst_region integer NOT NULL default '0',\n                PRIMARY KEY (timezone)\n                ) ");
      $zones = event_install_timezones();
      foreach ($zones as $zone => $value) {
        db_query("INSERT INTO {event_timezones} (timezone, name, \"offset\", offset_dst, dst_region) VALUES (%d, '%s', '%s', '%s', %d)", $zone, $value['timezone'], $value['offset'], $value['offset_dst'], $value['dst_region']);
      }
      db_query("CREATE TABLE {event} (\n                nid integer NOT NULL default '0',\n                event_start timestamp NOT NULL,\n                event_end timestamp NOT NULL,\n                timezone integer NOT NULL default '0',\n                start_in_dst integer NOT NULL default '0',\n                end_in_dst integer NOT NULL default '0'\n                ) ");

      // needs work?
      $sql = "INSERT INTO {event} (nid, event_start, event_end, timezone, start_in_dst, end_in_dst) SELECT e.nid, IF(%cond_start, TIMESTAMP 'epoch' + event_start * INTERVAL '1 second' + ez.offset_dst, TIMESTAMP 'epoch' + event_start * INTERVAL '1 second' + ez.offset) AS event_start, IF(%cond_end, TIMESTAMP 'epoch' + event_end * INTERVAL '1 second' + ez.offset_dst, TIMESTAMP 'epoch' + event_end * INTERVAL '1 second' + ez.offset) AS event_end, e.timezone, IF(%cond_start, 1, 0), IF(%cond_end, 1, 0) FROM {event_backup} e INNER JOIN {event_timezones} ez ON e.timezone = ez.timezone WHERE ez.dst_region = %d";

      // Loop over the 21 supported DST regions.
      for ($i = 0; $i <= 20; $i++) {
        if ($i == 0) {

          // needs work?
          $ret[] = update_sql("INSERT INTO {event} (nid, event_start, event_end, timezone, start_in_dst, end_in_dst) SELECT e.nid, TIMESTAMP 'epoch' + event_start * INTERVAL '1 second' + ez.offset AS event_start, TIMESTAMP 'epoch' + event_end * INTERVAL '1 second' + ez.offset AS event_end, e.timezone, 0, 0 FROM {event_backup} e INNER JOIN {event_timezones} ez ON e.timezone = ez.timezone WHERE ez.dst_region = 0");
        }
        else {
          $start = str_replace('%date', 'e.event_start', _event_install_get_dst_dates($i));
          $end = str_replace('%date', 'e.event_end', _event_install_get_dst_dates($i));
          $ret[] = update_sql(str_replace(array(
            '%cond_start',
            '%cond_end',
            '%d',
          ), array(
            $start,
            $end,
            $i,
          ), $sql));
        }
      }
      $ret[] = update_sql('ALTER TABLE {event} ADD PRIMARY KEY (nid)');
      $ret[] = update_sql('CREATE INDEX {event}_timezone_idx ON {event} (timezone)');
      $ret[] = update_sql("ALTER TABLE {users} ADD timezone_id integer NOT NULL default '0'");
      break;
  }
  return $ret;
}