From 5d3ce3924d18f9906a68a1d209b2e60eec42258b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= <14975046+skjnldsv@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:57:55 +0100 Subject: [PATCH] fix(migration): remove the personal calendars 1.0.12 stored MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Up to 1.0.12, Absence copied approved leave into a calendar stored in each employee's calendar home under the uri `absence`. The built-in personal calendar is listed as `app-generated--absence--absence` and does not replace it, so after the upgrade people saw every leave twice, and the old copy never followed a cancellation. The upgrade now deletes those calendars for good when they only hold the events 1.0.12 wrote (`absence--<8 hex>.ics`, trashed ones included). A calendar with events of the person's own keeps them and only loses the leave. An empty one goes only under the name 1.0.12 gave it. The 1.0.12 upgrade check seeds two such calendars and verifies the result. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com> --- SPECIFICATION.md | 8 + .../Version1024Date20261126150000.php | 135 +++++++++++++++ .../StoredPersonalCalendarRemovalTest.php | 154 ++++++++++++++++++ tests/Upgrade/upgrade-check.php | 47 ++++++ 4 files changed, 344 insertions(+) create mode 100644 lib/Migration/Version1024Date20261126150000.php create mode 100644 tests/Integration/StoredPersonalCalendarRemovalTest.php diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 7ded4c9..3d2c570 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -1287,6 +1287,14 @@ refused. bin and its shares, clears `calendar_event_uri`, and removes `CalendarSyncJob` and its `calendar_sync_cursor`. It marks the calendar as in the trash bin first, so the dav app deletes it without an activity for every member of those groups. +- **The stored personal calendars of 1.0.12** lived in each employee's calendar + home under the uri `absence`, with one event per approval named + `absence--<8 hex>.ics`. The built-in personal calendar does not replace + them (the server lists it as `app-generated--absence--absence`), so they would show + every leave twice and never follow a cancellation. The `Version1024…` migration + deletes each of them for good when it holds only such events, those in the trash + bin included; a calendar with events of the person's own keeps them and loses only + the leave. An empty one goes only when it still has the name `Absence`. --- diff --git a/lib/Migration/Version1024Date20261126150000.php b/lib/Migration/Version1024Date20261126150000.php new file mode 100644 index 0000000..0b9cd18 --- /dev/null +++ b/lib/Migration/Version1024Date20261126150000.php @@ -0,0 +1,135 @@ +-<8 hex>.ics` (`-deleted.ics` in the + * trash bin). A calendar holding + * nothing else is deleted for good; one with events of the person's own keeps + * them and loses only the leave. An empty one is deleted only when it still + * has the name 1.0.12 gave it. + */ +class Version1024Date20261126150000 extends SimpleMigrationStep { + private const PRINCIPAL_PREFIX = 'principals/users/'; + private const CALENDAR_URI = 'absence'; + private const DISPLAY_NAME = 'Absence'; + private const EVENT_URI = '/^absence-\d+-[0-9a-f]{8}(-deleted)?\.ics$/'; + + public function __construct( + private IDBConnection $db, + private ContainerInterface $container, + private LoggerInterface $logger, + ) { + } + + #[\Override] + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + if (!$this->db->tableExists('calendars')) { + return; + } + $calendars = $this->storedCalendars(); + if ($calendars === []) { + return; + } + try { + $backend = $this->container->get(CalDavBackend::class); + } catch (\Throwable $e) { + $this->logger->warning('Absence: the stored personal calendars could not be removed, the dav app is not available', ['exception' => $e]); + return; + } + + foreach ($calendars as $calendar) { + $events = $this->eventUris($calendar['id']); + $leave = array_values(array_filter($events, static fn (string $uri): bool => preg_match(self::EVENT_URI, $uri) === 1)); + if ($events === [] && $calendar['displayname'] !== self::DISPLAY_NAME) { + continue; + } + if (count($leave) === count($events)) { + $this->moveToTrashBin($calendar['id']); + $backend->deleteCalendar($calendar['id'], true); + continue; + } + foreach ($leave as $uri) { + $backend->deleteCalendarObject($calendar['id'], $uri, CalDavBackend::CALENDAR_TYPE_CALENDAR, true); + } + $this->logger->info('Absence: kept the stored calendar of {principal}, it holds events of their own', ['principal' => $calendar['principaluri']]); + } + } + + /** + * @return list + */ + private function storedCalendars(): array { + $qb = $this->db->getQueryBuilder(); + $qb->select('id', 'principaluri', 'displayname') + ->from('calendars') + ->where($qb->expr()->eq('uri', $qb->createNamedParameter(self::CALENDAR_URI))) + ->andWhere($qb->expr()->like('principaluri', $qb->createNamedParameter($this->db->escapeLikeParameter(self::PRINCIPAL_PREFIX) . '%'))); + $result = $qb->executeQuery(); + $calendars = []; + while ($row = $result->fetch()) { + $calendars[] = [ + 'id' => (int)$row['id'], + 'principaluri' => (string)$row['principaluri'], + 'displayname' => $row['displayname'] === null ? null : (string)$row['displayname'], + ]; + } + $result->closeCursor(); + return $calendars; + } + + /** + * The calendar's event uris, those in the trash bin included + * + * @return list + */ + private function eventUris(int $calendarId): array { + $qb = $this->db->getQueryBuilder(); + $qb->select('uri') + ->from('calendarobjects') + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId, IQueryBuilder::PARAM_INT))) + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter(CalDavBackend::CALENDAR_TYPE_CALENDAR, IQueryBuilder::PARAM_INT))); + $result = $qb->executeQuery(); + $uris = []; + while (($uri = $result->fetchOne()) !== false) { + $uris[] = (string)$uri; + } + $result->closeCursor(); + return $uris; + } + + /** + * As for the team calendar (Version1019…): a calendar in the trash bin is + * deleted by the dav app without an activity for anybody it was shared with. + */ + private function moveToTrashBin(int $calendarId): void { + $qb = $this->db->getQueryBuilder(); + $qb->update('calendars') + ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($calendarId, IQueryBuilder::PARAM_INT))) + ->andWhere($qb->expr()->isNull('deleted_at')); + $qb->executeStatement(); + } +} diff --git a/tests/Integration/StoredPersonalCalendarRemovalTest.php b/tests/Integration/StoredPersonalCalendarRemovalTest.php new file mode 100644 index 0000000..6b22590 --- /dev/null +++ b/tests/Integration/StoredPersonalCalendarRemovalTest.php @@ -0,0 +1,154 @@ + */ + private array $calendars = []; + + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + Server::get(IUserManager::class)->createUser(self::USER, 'Itest-Password-personalcal-1'); + } + + public static function tearDownAfterClass(): void { + Server::get(IUserManager::class)->get(self::USER)?->delete(); + parent::tearDownAfterClass(); + } + + protected function setUp(): void { + parent::setUp(); + $this->backend = Server::get(CalDavBackend::class); + } + + protected function tearDown(): void { + foreach ($this->calendars as $id) { + if ($this->backend->getCalendarById($id) !== null) { + $this->backend->deleteCalendar($id, true); + } + } + parent::tearDown(); + } + + public function testTheCalendarOf1012IsGoneForGood(): void { + $id = $this->calendar('absence', 'Absence', ['absence-12-0a1b2c3d.ics', 'absence-13-0a1b2c3d.ics']); + $this->backend->deleteCalendarObject($id, 'absence-13-0a1b2c3d.ics'); + + $this->migrate(Server::get(ContainerInterface::class)); + + self::assertNull($this->backend->getCalendarById($id)); + self::assertSame([], $this->events($id)); + } + + public function testEventsOfTheirOwnStayWithTheCalendar(): void { + $id = $this->calendar('absence', 'Absence', ['absence-12-0a1b2c3d.ics', 'dentist.ics']); + $this->backend->deleteCalendarObject($id, 'absence-12-0a1b2c3d.ics'); + $this->addEvent($id, 'absence-14-0a1b2c3d.ics'); + + $this->migrate(Server::get(ContainerInterface::class)); + + self::assertNotNull($this->backend->getCalendarById($id)); + self::assertSame(['dentist.ics'], $this->events($id)); + } + + public function testAnEmptyCalendarGoesOnlyUnderTheNameOf1012(): void { + $stored = $this->calendar('absence', 'Absence', []); + $renamed = $this->calendar('absence', 'Holidays', [], self::PRINCIPAL . '-other'); + + $this->migrate(Server::get(ContainerInterface::class)); + + self::assertNull($this->backend->getCalendarById($stored)); + self::assertNotNull($this->backend->getCalendarById($renamed)); + } + + public function testOtherCalendarsAreLeftAlone(): void { + $personal = $this->calendar('personal', 'Personal', ['absence-12-0a1b2c3d.ics']); + $system = $this->calendar('absence', 'Absence', ['absence-12-0a1b2c3d.ics'], 'principals/system/itest-personalcal'); + + $this->migrate(Server::get(ContainerInterface::class)); + + self::assertSame(['absence-12-0a1b2c3d.ics'], $this->events($personal)); + self::assertSame(['absence-12-0a1b2c3d.ics'], $this->events($system)); + } + + public function testWithoutTheDavAppNothingChanges(): void { + $id = $this->calendar('absence', 'Absence', ['absence-12-0a1b2c3d.ics']); + $container = $this->createMock(ContainerInterface::class); + $container->method('get')->willThrowException(new \RuntimeException('no dav')); + + $this->migrate($container); + + self::assertSame(['absence-12-0a1b2c3d.ics'], $this->events($id)); + } + + /** + * @param list $events + */ + private function calendar(string $uri, string $displayName, array $events, string $principal = self::PRINCIPAL): int { + $id = (int)$this->backend->createCalendar($principal, $uri, ['{DAV:}displayname' => $displayName]); + $this->calendars[] = $id; + foreach ($events as $event) { + $this->addEvent($id, $event); + } + return $id; + } + + private function addEvent(int $calendarId, string $objectUri): void { + $this->backend->createCalendarObject($calendarId, $objectUri, "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:" . $objectUri . "\r\nSUMMARY:Vacation\r\nDTSTART;VALUE=DATE:20260706\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + } + + private function migrate(ContainerInterface $container): void { + $migration = new Version1024Date20261126150000( + Server::get(IDBConnection::class), + $container, + Server::get(LoggerInterface::class), + ); + $migration->postSchemaChange($this->createMock(IOutput::class), static fn () => null, []); + } + + /** + * The calendar's event uris, those in the trash bin included + * + * @return list + */ + private function events(int $calendarId): array { + $qb = Server::get(IDBConnection::class)->getQueryBuilder(); + $qb->select('uri') + ->from('calendarobjects') + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter(CalDavBackend::CALENDAR_TYPE_CALENDAR))) + ->orderBy('uri'); + $result = $qb->executeQuery(); + $uris = []; + while (($uri = $result->fetchOne()) !== false) { + $uris[] = (string)$uri; + } + $result->closeCursor(); + return $uris; + } +} diff --git a/tests/Upgrade/upgrade-check.php b/tests/Upgrade/upgrade-check.php index f4e12b4..4d1bdbf 100644 --- a/tests/Upgrade/upgrade-check.php +++ b/tests/Upgrade/upgrade-check.php @@ -181,6 +181,12 @@ function seed(IDBConnection $db): void { 'created_at' => [new DateTime('2026-03-04 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE], ]); attachmentFolder()->newFile((string)$attachment, $content); + + if ($before1013) { + // The personal calendars 1.0.12 wrote approved leave into, one with an event of the person's own + storedCalendar($db, 'upgrade-alice', ['absence-' . $ids[0] . '-0a1b2c3d.ics']); + storedCalendar($db, 'upgrade-bob', ['absence-' . $ids[1] . '-0a1b2c3d.ics', 'invented-dentist.ics']); + } echo 'seeded the data of ' . ($before1013 ? '1.0.12' : 'a beta') . "\n"; } @@ -196,6 +202,42 @@ function insert(IDBConnection $db, string $table, array $values): int { return $qb->getLastInsertId(); } +/** @param list $events */ +function storedCalendar(IDBConnection $db, string $uid, array $events): void { + $calendar = insert($db, 'calendars', [ + 'principaluri' => 'principals/users/' . $uid, 'uri' => 'absence', 'displayname' => 'Absence', + 'synctoken' => [1, IQueryBuilder::PARAM_INT], 'components' => 'VEVENT', + ]); + foreach ($events as $uri) { + $ics = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:$uri\r\nSUMMARY:Vacation\r\nDTSTART;VALUE=DATE:20260706\r\nDTEND;VALUE=DATE:20260707\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; + insert($db, 'calendarobjects', [ + 'calendarid' => [$calendar, IQueryBuilder::PARAM_INT], 'uri' => $uri, 'uid' => $uri, + 'calendardata' => [$ics, IQueryBuilder::PARAM_LOB], 'lastmodified' => [time(), IQueryBuilder::PARAM_INT], + 'etag' => md5($ics), 'size' => [strlen($ics), IQueryBuilder::PARAM_INT], 'componenttype' => 'VEVENT', + 'firstoccurence' => [1783296000, IQueryBuilder::PARAM_INT], 'lastoccurence' => [1783382400, IQueryBuilder::PARAM_INT], + ]); + } +} + +/** @return list " " of the events in stored calendars under the uri `absence` */ +function storedCalendarEvents(IDBConnection $db): array { + if (!$db->tableExists('calendars')) { + return []; + } + $qb = $db->getQueryBuilder(); + $qb->select('c.principaluri', 'o.uri') + ->from('calendars', 'c') + ->innerJoin('c', 'calendarobjects', 'o', $qb->expr()->eq('o.calendarid', 'c.id')) + ->where($qb->expr()->eq('c.uri', $qb->createNamedParameter('absence'))) + ->andWhere($qb->expr()->like('c.principaluri', $qb->createNamedParameter('principals/users/%'))) + ->andWhere($qb->expr()->eq('o.calendartype', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); + $result = $qb->executeQuery(); + $events = array_map(static fn (array $r): string => $r['principaluri'] . ' ' . $r['uri'], $result->fetchAll()); + $result->closeCursor(); + sort($events); + return $events; +} + /** @return array leave type key => id */ function typeIds(IDBConnection $db): array { $result = $db->getQueryBuilder()->select('id', 'key')->from('absence_leave_types')->executeQuery(); @@ -403,6 +445,7 @@ function before(IDBConnection $db): array { 'contracts' => $periods === null ? [] : contracts($db, $periods), 'hrGroup' => $appConfig->hasKey(APP, 'hr_group') ? $appConfig->getValueString(APP, 'hr_group') : null, 'hrGroupExists' => Server::get(IGroupManager::class)->groupExists('hr'), + 'calendarEvents' => storedCalendarEvents($db), ]; } @@ -458,6 +501,10 @@ function verify(IDBConnection $db, array $before): int { $hrGroup = $before['hrGroup'] ?? ($before['hrGroupExists'] ? 'hr' : ''); $check($appConfig->getValueString(APP, 'hr_group') === $hrGroup, "HR group: still \"$hrGroup\"", $hrGroup, $appConfig->getValueString(APP, 'hr_group')); + // The stored personal calendars of 1.0.12 lose the leave and keep the events of their own + $own = array_values(array_filter($before['calendarEvents'] ?? [], static fn (string $e): bool => preg_match('/ absence-\d+-[0-9a-f]{8}(-deleted)?\.ics$/', $e) !== 1)); + $check(storedCalendarEvents($db) === $own, 'stored personal calendars of 1.0.12: only the events of their own left', $own, storedCalendarEvents($db)); + $wrong = []; foreach (rows($db, 'absence_comments', ['id', 'request_id', 'confidential']) as $i => $row) { if (flag($row['confidential']) !== $after['comments'][$i]['onConfidentialAbsence']) {