Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<name>WebDAV</name>
<summary>WebDAV endpoint</summary>
<description>WebDAV endpoint</description>
<version>2.0.0-dev.0</version>
<version>2.0.0-dev.1</version>
<licence>agpl</licence>
<author>owncloud.org</author>
<namespace>DAV</namespace>
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@
'OCA\\DAV\\Migration\\Version1038Date20260302000000' => $baseDir . '/../lib/Migration/Version1038Date20260302000000.php',
'OCA\\DAV\\Migration\\Version1039Date20260408000000' => $baseDir . '/../lib/Migration/Version1039Date20260408000000.php',
'OCA\\DAV\\Migration\\Version1040Date20260805000000' => $baseDir . '/../lib/Migration/Version1040Date20260805000000.php',
'OCA\\DAV\\Migration\\Version2000Date20260703100001' => $baseDir . '/../lib/Migration/Version2000Date20260703100001.php',
'OCA\\DAV\\Migration\\Version2000Date20260703100002' => $baseDir . '/../lib/Migration/Version2000Date20260703100002.php',
'OCA\\DAV\\Model\\ExampleEvent' => $baseDir . '/../lib/Model/ExampleEvent.php',
'OCA\\DAV\\Paginate\\LimitedCopyIterator' => $baseDir . '/../lib/Paginate/LimitedCopyIterator.php',
'OCA\\DAV\\Paginate\\PaginateCache' => $baseDir . '/../lib/Paginate/PaginateCache.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Migration\\Version1038Date20260302000000' => __DIR__ . '/..' . '/../lib/Migration/Version1038Date20260302000000.php',
'OCA\\DAV\\Migration\\Version1039Date20260408000000' => __DIR__ . '/..' . '/../lib/Migration/Version1039Date20260408000000.php',
'OCA\\DAV\\Migration\\Version1040Date20260805000000' => __DIR__ . '/..' . '/../lib/Migration/Version1040Date20260805000000.php',
'OCA\\DAV\\Migration\\Version2000Date20260703100001' => __DIR__ . '/..' . '/../lib/Migration/Version2000Date20260703100001.php',
'OCA\\DAV\\Migration\\Version2000Date20260703100002' => __DIR__ . '/..' . '/../lib/Migration/Version2000Date20260703100002.php',
'OCA\\DAV\\Model\\ExampleEvent' => __DIR__ . '/..' . '/../lib/Model/ExampleEvent.php',
'OCA\\DAV\\Paginate\\LimitedCopyIterator' => __DIR__ . '/..' . '/../lib/Paginate/LimitedCopyIterator.php',
'OCA\\DAV\\Paginate\\PaginateCache' => __DIR__ . '/..' . '/../lib/Paginate/PaginateCache.php',
Expand Down
4 changes: 2 additions & 2 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
'{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'],
'{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}default-alarm-part-day' => ['default_alarm_pday', 'int'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}default-alarm-full-day' => ['default_alarm_fday', 'int'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}default-alarms-part-day' => ['default_alarms_pday', 'string'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}default-alarms-full-day' => ['default_alarms_fday', 'string'],
];

/**
Expand Down
90 changes: 90 additions & 0 deletions apps/dav/lib/Migration/Version2000Date20260703100001.php

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a second migration step to remove the old fields

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was implemented in a separate file - I'm not sure if that is the correct pattern or not.

Also, the migration process was tweaked a bit. I'm not super-familiar with SQL in PHP but I think this will execute as an UPDATE query DB-side which I would hope should perform fairly quickly even on a very large table.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAV\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\IDBConnection;
use OCP\Migration\Attributes\AddColumn;
use OCP\Migration\Attributes\ColumnType;
use OCP\Migration\Attributes\DataCleansing;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;

/**
* Add JSON default-alarm columns and copy legacy single-int defaults via set-based UPDATEs.
*/
#[DataCleansing(table: 'calendars', description: 'Migrate legacy default_alarm_* integers to default_alarms_* JSON')]
#[AddColumn(table: 'calendars', name: 'default_alarms_pday', type: ColumnType::TEXT)]
#[AddColumn(table: 'calendars', name: 'default_alarms_fday', type: ColumnType::TEXT)]
class Version2000Date20260703100001 extends SimpleMigrationStep {
public function __construct(
private IDBConnection $db,
) {
}

#[Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$calendarsTable = $schema->getTable('calendars');

if (!$calendarsTable->hasColumn('default_alarms_pday')) {
$calendarsTable->addColumn('default_alarms_pday', Types::TEXT, [
'notnull' => false,
'default' => null,
]);
}

if (!$calendarsTable->hasColumn('default_alarms_fday')) {
$calendarsTable->addColumn('default_alarms_fday', Types::TEXT, [
'notnull' => false,
'default' => null,
]);
}

return $schema;
}

#[Override]
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$calendarsTable = $schema->getTable('calendars');

if ($calendarsTable->hasColumn('default_alarm_pday') && $calendarsTable->hasColumn('default_alarms_pday')) {
$this->migrateLegacyIntColumn('default_alarm_pday', 'default_alarms_pday');
}

if ($calendarsTable->hasColumn('default_alarm_fday') && $calendarsTable->hasColumn('default_alarms_fday')) {
$this->migrateLegacyIntColumn('default_alarm_fday', 'default_alarms_fday');
}
}

/**
* Encode a single legacy trigger int as [{"trigger":N,"action":"DISPLAY"}]
* for all rows still missing the JSON column value.
*/
private function migrateLegacyIntColumn(string $legacyColumn, string $jsonColumn): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is definitely more efficient.

Normally, manually creating json like this would be a no-no but this is the most efficient for a one time migration on a multi-database platform

Works correctly

Image

$qb = $this->db->getQueryBuilder();
$qb->update('calendars')
->set($jsonColumn, $qb->func()->concat(
$qb->expr()->literal('[{"trigger":'),
$legacyColumn,
$qb->expr()->literal(',"action":"DISPLAY"}]'),
))
->where($qb->expr()->isNotNull($legacyColumn))
->andWhere($qb->expr()->isNull($jsonColumn));
Comment on lines +86 to +87

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. It will continue where it left of when interrupted

$qb->executeStatement();
}
}
42 changes: 42 additions & 0 deletions apps/dav/lib/Migration/Version2000Date20260703100002.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAV\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\Attributes\DropColumn;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;

/**
* Drop legacy single-int default alarm columns after JSON migration.
*/
#[DropColumn(table: 'calendars', name: 'default_alarm_pday', description: 'Replaced by default_alarms_pday JSON')]
#[DropColumn(table: 'calendars', name: 'default_alarm_fday', description: 'Replaced by default_alarms_fday JSON')]
class Version2000Date20260703100002 extends SimpleMigrationStep {
#[Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$calendarsTable = $schema->getTable('calendars');

if ($calendarsTable->hasColumn('default_alarm_pday')) {
$calendarsTable->dropColumn('default_alarm_pday');
}

if ($calendarsTable->hasColumn('default_alarm_fday')) {
$calendarsTable->dropColumn('default_alarm_fday');
}

return $schema;
}
}
54 changes: 36 additions & 18 deletions apps/dav/tests/unit/CalDAV/CalDavBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2081,46 +2081,64 @@ public function testUnshare(): void {

}

public function testDefaultAlarmProperties(): void {
public function testDefaultAlarmsProperties(): void {
$calendarId = $this->createTestCalendar();

// Test setting both default alarm properties
$partDayProperty = '{http://nextcloud.com/ns}default-alarms-part-day';
$fullDayProperty = '{http://nextcloud.com/ns}default-alarms-full-day';

$partDayJson = json_encode([
['trigger' => -86400, 'action' => 'EMAIL'],
['trigger' => -900, 'action' => 'DISPLAY'],
], JSON_THROW_ON_ERROR);
$fullDayJson = json_encode([
['trigger' => -3600, 'action' => 'EMAIL'],
], JSON_THROW_ON_ERROR);

// Set plural default-alarms properties
$patch = new PropPatch([
'{http://nextcloud.com/ns}default-alarm-part-day' => -900,
'{http://nextcloud.com/ns}default-alarm-full-day' => -3600,
$partDayProperty => $partDayJson,
$fullDayProperty => $fullDayJson,
]);
$this->backend->updateCalendar($calendarId, $patch);
$patch->commit();

// Verify the properties were set
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertCount(1, $calendars);
$this->assertEquals(-900, $calendars[0]['{http://nextcloud.com/ns}default-alarm-part-day']);
$this->assertEquals(-3600, $calendars[0]['{http://nextcloud.com/ns}default-alarm-full-day']);

// Test updating to different values
$this->assertEquals($partDayJson, $calendars[0][$partDayProperty]);
$this->assertEquals($fullDayJson, $calendars[0][$fullDayProperty]);
$this->assertArrayNotHasKey('{http://nextcloud.com/ns}default-alarm-part-day', $calendars[0]);
$this->assertArrayNotHasKey('{http://nextcloud.com/ns}default-alarm-full-day', $calendars[0]);

// Update to different values
$updatedPartDayJson = json_encode([
['trigger' => -1800, 'action' => 'DISPLAY'],
], JSON_THROW_ON_ERROR);
$updatedFullDayJson = json_encode([
['trigger' => -43200, 'action' => 'DISPLAY'],
], JSON_THROW_ON_ERROR);
$patch = new PropPatch([
'{http://nextcloud.com/ns}default-alarm-part-day' => -86400,
'{http://nextcloud.com/ns}default-alarm-full-day' => -43200,
$partDayProperty => $updatedPartDayJson,
$fullDayProperty => $updatedFullDayJson,
]);
$this->backend->updateCalendar($calendarId, $patch);
$patch->commit();

$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertEquals(-86400, $calendars[0]['{http://nextcloud.com/ns}default-alarm-part-day']);
$this->assertEquals(-43200, $calendars[0]['{http://nextcloud.com/ns}default-alarm-full-day']);
$this->assertEquals($updatedPartDayJson, $calendars[0][$partDayProperty]);
$this->assertEquals($updatedFullDayJson, $calendars[0][$fullDayProperty]);

// Test setting to null
// Clear properties
$patch = new PropPatch([
'{http://nextcloud.com/ns}default-alarm-part-day' => null,
'{http://nextcloud.com/ns}default-alarm-full-day' => null,
$partDayProperty => null,
$fullDayProperty => null,
]);
$this->backend->updateCalendar($calendarId, $patch);
$patch->commit();

$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertNull($calendars[0]['{http://nextcloud.com/ns}default-alarm-part-day']);
$this->assertNull($calendars[0]['{http://nextcloud.com/ns}default-alarm-full-day']);
$this->assertNull($calendars[0][$partDayProperty]);
$this->assertNull($calendars[0][$fullDayProperty]);

// Clean up
$this->backend->deleteCalendar($calendars[0]['id'], true);
Expand Down
Loading