1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-18 12:52:42 +01:00

Migrate Calendar away from stored-epoch fields

Summary:
Ref T10747. This deprecates "dateFrom", "dateTo", "allDayDateFrom", "allDayDateTo", and "recurrenceEndDate".

They are replaced with "utc*Epoch" fields (for querying) and CalendarDateTime objects (for start, end, until). These objects can represent the full range of dates and times expressible in ICS format, allowing us to import a wider range of ICS events.

Test Plan:
Ran migrations, viewed/edited Calendar, didn't catch anything catastrophcially broken.

This likely needs some followups, I'll keep it local for a bit until I'm confident I didn't break anything too catastrophically. I'm retaining the old data for now so we can likely fix things if it turns out there is some sort of issue.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T10747

Differential Revision: https://secure.phabricator.com/D16664
This commit is contained in:
epriestley 2016-10-04 12:03:20 -07:00
parent fae0ec9220
commit d3fc1800f8
6 changed files with 168 additions and 141 deletions

View file

@ -1,52 +1,3 @@
<?php <?php
$table = new PhabricatorCalendarEvent(); // This migration was replaced by "20161004.cal.01.noepoch.php".
$conn = $table->establishConnection('w');
// Previously, "All Day" events were stored with a start and end date set to
// the earliest possible start and end seconds for the corresponding days. We
// now store all day events with their "date" epochs as UTC, separate from
// individual event times.
$zone_min = new DateTimeZone('Pacific/Midway');
$zone_max = new DateTimeZone('Pacific/Kiritimati');
$zone_utc = new DateTimeZone('UTC');
foreach (new LiskMigrationIterator($table) as $event) {
// If this event has already migrated, skip it.
if ($event->getAllDayDateFrom()) {
continue;
}
$is_all_day = $event->getIsAllDay();
$epoch_min = $event->getDateFrom();
$epoch_max = $event->getDateTo();
$date_min = new DateTime('@'.$epoch_min);
$date_max = new DateTime('@'.$epoch_max);
if ($is_all_day) {
$date_min->setTimeZone($zone_min);
$date_min->modify('+2 days');
$date_max->setTimeZone($zone_max);
$date_max->modify('-2 days');
} else {
$date_min->setTimeZone($zone_utc);
$date_max->setTimeZone($zone_utc);
}
$string_min = $date_min->format('Y-m-d');
$string_max = $date_max->format('Y-m-d 23:59:00');
$allday_min = id(new DateTime($string_min, $zone_utc))->format('U');
$allday_max = id(new DateTime($string_max, $zone_utc))->format('U');
queryfx(
$conn,
'UPDATE %T SET allDayDateFrom = %d, allDayDateTo = %d
WHERE id = %d',
$table->getTableName(),
$allday_min,
$allday_max,
$event->getID());
}

View file

@ -0,0 +1,125 @@
<?php
$table = new PhabricatorCalendarEvent();
$conn = $table->establishConnection('w');
$table_name = 'calendar_event';
// Long ago, "All Day" events were stored with a start and end date set to
// the earliest possible start and end seconds for the corresponding days. We
// then moved to store all day events with their "date" epochs as UTC, separate
// from individual event times. Both systems were later replaced with use of
// CalendarDateTime.
$zone_min = new DateTimeZone('Pacific/Midway');
$zone_max = new DateTimeZone('Pacific/Kiritimati');
$zone_utc = new DateTimeZone('UTC');
foreach (new LiskRawMigrationIterator($conn, $table_name) as $row) {
$parameters = phutil_json_decode($row['parameters']);
if (isset($parameters['startDateTime'])) {
// This event has already been migrated.
continue;
}
$is_all_day = $row['isAllDay'];
if (empty($row['allDayDateFrom'])) {
// No "allDayDateFrom" means this is an old event which was never migrated
// by the earlier "20160715.event.03.allday.php" migration. The dateFrom
// and dateTo will be minimum and maximum earthly seconds for the event. We
// convert them to UTC if they were in extreme timezones.
$epoch_min = $row['dateFrom'];
$epoch_max = $row['dateTo'];
if ($is_all_day) {
$date_min = new DateTime('@'.$epoch_min);
$date_max = new DateTime('@'.$epoch_max);
$date_min->setTimeZone($zone_min);
$date_min->modify('+2 days');
$date_max->setTimeZone($zone_max);
$date_max->modify('-2 days');
$string_min = $date_min->format('Y-m-d');
$string_max = $date_max->format('Y-m-d 23:59:00');
$utc_min = id(new DateTime($string_min, $zone_utc))->format('U');
$utc_max = id(new DateTime($string_max, $zone_utc))->format('U');
} else {
$utc_min = $epoch_min;
$utc_max = $epoch_max;
}
} else {
// This is an event which was migrated already. We can pick the correct
// epoch timestamps based on the "isAllDay" flag.
if ($is_all_day) {
$utc_min = $row['allDayDateFrom'];
$utc_max = $row['allDayDateTo'];
} else {
$utc_min = $row['dateFrom'];
$utc_max = $row['dateTo'];
}
}
$utc_until = $row['recurrenceEndDate'];
$start_datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch($utc_min);
if ($is_all_day) {
$start_datetime->setIsAllDay(true);
}
$end_datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch($utc_max);
if ($is_all_day) {
$end_datetime->setIsAllDay(true);
}
if ($utc_until) {
$until_datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch($utc_until);
} else {
$until_datetime = null;
}
$parameters['startDateTime'] = $start_datetime->toDictionary();
$parameters['endDateTime'] = $end_datetime->toDictionary();
if ($until_datetime) {
$parameters['untilDateTime'] = $until_datetime->toDictionary();
}
queryfx(
$conn,
'UPDATE %T SET parameters = %s WHERE id = %d',
$table_name,
phutil_json_encode($parameters),
$row['id']);
}
// Generate UTC epochs for all events. We can't readily do this one at a
// time because instance UTC epochs rely on having the parent event.
$viewer = PhabricatorUser::getOmnipotentUser();
$all_events = id(new PhabricatorCalendarEventQuery())
->setViewer($viewer)
->execute();
foreach ($all_events as $event) {
if ($event->getUTCInitialEpoch()) {
// Already migrated.
continue;
}
try {
$event->updateUTCEpochs();
} catch (Exception $ex) {
continue;
}
queryfx(
$conn,
'UPDATE %T SET
utcInitialEpoch = %d,
utcUntilEpoch = %nd,
utcInstanceEpoch = %nd WHERE id = %d',
$table_name,
$event->getUTCInitialEpoch(),
$event->getUTCUntilEpoch(),
$event->getUTCInstanceEpoch(),
$event->getID());
}

View file

@ -352,14 +352,14 @@ final class PhabricatorCalendarEventQuery
if ($this->rangeBegin) { if ($this->rangeBegin) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'event.dateTo >= %d OR event.isRecurring = 1', '(event.utcUntilEpoch >= %d) OR (event.utcUntilEpoch IS NULL)',
$this->rangeBegin - phutil_units('16 hours in seconds')); $this->rangeBegin - phutil_units('16 hours in seconds'));
} }
if ($this->rangeEnd) { if ($this->rangeEnd) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'event.dateFrom <= %d', 'event.utcInitialEpoch <= %d',
$this->rangeEnd + phutil_units('16 hours in seconds')); $this->rangeEnd + phutil_units('16 hours in seconds'));
} }

View file

@ -17,10 +17,6 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
protected $name; protected $name;
protected $hostPHID; protected $hostPHID;
protected $dateFrom;
protected $dateTo;
protected $allDayDateFrom;
protected $allDayDateTo;
protected $description; protected $description;
protected $isCancelled; protected $isCancelled;
protected $isAllDay; protected $isAllDay;
@ -30,7 +26,6 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
protected $isRecurring = 0; protected $isRecurring = 0;
protected $recurrenceFrequency = array(); protected $recurrenceFrequency = array();
protected $recurrenceEndDate;
private $isGhostEvent = false; private $isGhostEvent = false;
protected $instanceOfEventPHID; protected $instanceOfEventPHID;
@ -51,6 +46,13 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
private $viewerTimezone; private $viewerTimezone;
// TODO: DEPRECATED. Remove once we're sure the migrations worked.
protected $allDayDateFrom;
protected $allDayDateTo;
protected $dateFrom;
protected $dateTo;
protected $recurrenceEndDate;
// Frequency Constants // Frequency Constants
const FREQUENCY_DAILY = 'daily'; const FREQUENCY_DAILY = 'daily';
const FREQUENCY_WEEKLY = 'weekly'; const FREQUENCY_WEEKLY = 'weekly';
@ -70,20 +72,6 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
$now = PhabricatorTime::getNow(); $now = PhabricatorTime::getNow();
$start = new DateTime('@'.$now);
$start->setTimeZone($actor->getTimeZone());
$start->setTime($start->format('H'), 0, 0);
$start->modify('+1 hour');
$end = id(clone $start)->modify('+1 hour');
$epoch_min = $start->format('U');
$epoch_max = $end->format('U');
$now_date = new DateTime('@'.$now);
$now_min = id(clone $now_date)->setTime(0, 0)->format('U');
$now_max = id(clone $now_date)->setTime(23, 59)->format('U');
$default_icon = 'fa-calendar'; $default_icon = 'fa-calendar';
$datetime_start = PhutilCalendarAbsoluteDateTime::newFromEpoch( $datetime_start = PhutilCalendarAbsoluteDateTime::newFromEpoch(
@ -106,10 +94,10 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
->setEditPolicy($edit_policy) ->setEditPolicy($edit_policy)
->setSpacePHID($actor->getDefaultSpacePHID()) ->setSpacePHID($actor->getDefaultSpacePHID())
->attachInvitees(array()) ->attachInvitees(array())
->setDateFrom($epoch_min) ->setDateFrom(0)
->setDateTo($epoch_max) ->setDateTo(0)
->setAllDayDateFrom($now_min) ->setAllDayDateFrom(0)
->setAllDayDateTo($now_max) ->setAllDayDateTo(0)
->setStartDateTime($datetime_start) ->setStartDateTime($datetime_start)
->setEndDateTime($datetime_end) ->setEndDateTime($datetime_end)
->applyViewerTimezone($actor); ->applyViewerTimezone($actor);
@ -130,7 +118,11 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
->setSequenceIndex($sequence) ->setSequenceIndex($sequence)
->setIsRecurring(true) ->setIsRecurring(true)
->setRecurrenceFrequency($this->getRecurrenceFrequency()) ->setRecurrenceFrequency($this->getRecurrenceFrequency())
->attachParentEvent($this); ->attachParentEvent($this)
->setAllDayDateFrom(0)
->setAllDayDateTo(0)
->setDateFrom(0)
->setDateTo(0);
return $child->copyFromParent($actor); return $child->copyFromParent($actor);
} }
@ -187,11 +179,16 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
$duration = $parent->getDuration(); $duration = $parent->getDuration();
$epochs = $parent->getSequenceIndexEpochs($actor, $sequence, $duration); $epochs = $parent->getSequenceIndexEpochs($actor, $sequence, $duration);
$start_datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch(
$epochs['dateFrom'],
$parent->newStartDateTime()->getTimezone());
$end_datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch(
$epochs['dateTo'],
$parent->newEndDateTime()->getTimezone());
$this $this
->setDateFrom($epochs['dateFrom']) ->setStartDateTime($start_datetime)
->setDateTo($epochs['dateTo']) ->setEndDateTime($end_datetime);
->setAllDayDateFrom($epochs['allDayDateFrom'])
->setAllDayDateTo($epochs['allDayDateTo']);
return $this; return $this;
} }
@ -213,12 +210,14 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
$frequency = $this->getFrequencyUnit(); $frequency = $this->getFrequencyUnit();
$modify_key = '+'.$sequence.' '.$frequency; $modify_key = '+'.$sequence.' '.$frequency;
$date = $this->getDateFrom(); $date_time = $this->newStartDateTime()
$date_time = PhabricatorTime::getDateTimeFromEpoch($date, $viewer); ->setViewerTimezone($viewer->getTimezoneIdentifier())
->newPHPDateTime();
$date_time->modify($modify_key); $date_time->modify($modify_key);
$date = $date_time->format('U'); $date = $date_time->format('U');
$end_date = $this->getRecurrenceEndDate(); $end_date = $this->getUntilDateTimeEpoch();
if ($end_date && $date > $end_date) { if ($end_date && $date > $end_date) {
throw new Exception( throw new Exception(
pht( pht(
@ -227,21 +226,9 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
$sequence)); $sequence));
} }
$utc = new DateTimeZone('UTC');
$allday_from = $this->getAllDayDateFrom();
$allday_date = new DateTime('@'.$allday_from, $utc);
$allday_date->setTimeZone($utc);
$allday_date->modify($modify_key);
$allday_min = $allday_date->format('U');
$allday_duration = ($this->getAllDayDateTo() - $allday_from);
return array( return array(
'dateFrom' => $date, 'dateFrom' => $date,
'dateTo' => $date + $duration, 'dateTo' => $date + $duration,
'allDayDateFrom' => $allday_min,
'allDayDateTo' => $allday_min + $allday_duration,
); );
} }
@ -280,24 +267,6 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
return ($this->getEndDateTimeEpoch() - $this->getStartDateTimeEpoch()); return ($this->getEndDateTimeEpoch() - $this->getStartDateTimeEpoch());
} }
public function getDateEpochForTimezone(
$epoch,
$src_zone,
$format,
$adjust,
$dst_zone) {
$src = new DateTime('@'.$epoch);
$src->setTimeZone($src_zone);
if (strlen($adjust)) {
$adjust = ' '.$adjust;
}
$dst = new DateTime($src->format($format).$adjust, $dst_zone);
return $dst->format('U');
}
public function updateUTCEpochs() { public function updateUTCEpochs() {
// The "intitial" epoch is the start time of the event, in UTC. // The "intitial" epoch is the start time of the event, in UTC.
$start_date = $this->newStartDateTime() $start_date = $this->newStartDateTime()
@ -314,9 +283,9 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
$until_epoch = $end_date->getEpoch(); $until_epoch = $end_date->getEpoch();
} else { } else {
$until_epoch = null; $until_epoch = null;
$until_date = $this->newUntilDateTime() $until_date = $this->newUntilDateTime();
->setViewerTimezone('UTC');
if ($until_date) { if ($until_date) {
$until_date->setViewerTimezone('UTC');
$duration = $this->newDuration(); $duration = $this->newDuration();
$until_epoch = id(new PhutilCalendarRelativeDateTime()) $until_epoch = id(new PhutilCalendarRelativeDateTime())
->setOrigin($until_date) ->setOrigin($until_date)
@ -377,23 +346,25 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
self::CONFIG_AUX_PHID => true, self::CONFIG_AUX_PHID => true,
self::CONFIG_COLUMN_SCHEMA => array( self::CONFIG_COLUMN_SCHEMA => array(
'name' => 'text', 'name' => 'text',
'dateFrom' => 'epoch',
'dateTo' => 'epoch',
'allDayDateFrom' => 'epoch',
'allDayDateTo' => 'epoch',
'description' => 'text', 'description' => 'text',
'isCancelled' => 'bool', 'isCancelled' => 'bool',
'isAllDay' => 'bool', 'isAllDay' => 'bool',
'icon' => 'text32', 'icon' => 'text32',
'mailKey' => 'bytes20', 'mailKey' => 'bytes20',
'isRecurring' => 'bool', 'isRecurring' => 'bool',
'recurrenceEndDate' => 'epoch?',
'instanceOfEventPHID' => 'phid?', 'instanceOfEventPHID' => 'phid?',
'sequenceIndex' => 'uint32?', 'sequenceIndex' => 'uint32?',
'isStub' => 'bool', 'isStub' => 'bool',
'utcInitialEpoch' => 'epoch', 'utcInitialEpoch' => 'epoch',
'utcUntilEpoch' => 'epoch?', 'utcUntilEpoch' => 'epoch?',
'utcInstanceEpoch' => 'epoch?', 'utcInstanceEpoch' => 'epoch?',
// TODO: DEPRECATED.
'allDayDateFrom' => 'epoch',
'allDayDateTo' => 'epoch',
'dateFrom' => 'epoch',
'dateTo' => 'epoch',
'recurrenceEndDate' => 'epoch?',
), ),
self::CONFIG_KEY_SCHEMA => array( self::CONFIG_KEY_SCHEMA => array(
'key_date' => array( 'key_date' => array(
@ -814,7 +785,7 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO
return null; return null;
} }
$epochs = $this->getParent()->getSequenceIndexEpochs( $epochs = $this->getParentEvent()->getSequenceIndexEpochs(
new PhabricatorUser(), new PhabricatorUser(),
$this->getSequenceIndex(), $this->getSequenceIndex(),
$this->getDuration()); $this->getDuration());

View file

@ -13,16 +13,6 @@ final class PhabricatorCalendarEventEndDateTransaction
public function applyInternalEffects($object, $value) { public function applyInternalEffects($object, $value) {
$actor = $this->getActor(); $actor = $this->getActor();
// TODO: DEPRECATED.
$object->setDateTo($value);
$object->setAllDayDateTo(
$object->getDateEpochForTimezone(
$value,
$actor->getTimeZone(),
'Y-m-d 23:59:00',
null,
new DateTimeZone('UTC')));
$datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch( $datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch(
$value, $value,
$actor->getTimezoneIdentifier()); $actor->getTimezoneIdentifier());

View file

@ -13,16 +13,6 @@ final class PhabricatorCalendarEventStartDateTransaction
public function applyInternalEffects($object, $value) { public function applyInternalEffects($object, $value) {
$actor = $this->getActor(); $actor = $this->getActor();
// TODO: DEPRECATED.
$object->setDateFrom($value);
$object->setAllDayDateFrom(
$object->getDateEpochForTimezone(
$value,
$actor->getTimeZone(),
'Y-m-d',
null,
new DateTimeZone('UTC')));
$datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch( $datetime = PhutilCalendarAbsoluteDateTime::newFromEpoch(
$value, $value,
$actor->getTimezoneIdentifier()); $actor->getTimezoneIdentifier());