diff --git a/.github/workflows/upgrade-from-older-versions.yml b/.github/workflows/upgrade-from-older-versions.yml
new file mode 100644
index 00000000..80ddef53
--- /dev/null
+++ b/.github/workflows/upgrade-from-older-versions.yml
@@ -0,0 +1,190 @@
+# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
+# SPDX-License-Identifier: AGPL-3.0-or-later
+#
+# Installs an older Absence, fills it with invented data, moves to the code of
+# the pull request and checks that everything entered is still there.
+#
+# From: 1.0.12 (commit 0d84f4d), which HR used before the betas, and 1.0.0-beta.1.
+# How: `occ upgrade` over the replaced folder, or app removed and enabled again.
+
+name: Upgrade from older versions
+
+on: pull_request
+
+permissions:
+ contents: read
+
+concurrency:
+ group: upgrade-from-older-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
+
+jobs:
+ changes-and-matrix:
+ runs-on: ubuntu-latest-low
+ permissions:
+ contents: read
+ pull-requests: read
+
+ outputs:
+ src: ${{ steps.changes.outputs.src }}
+ php-min: ${{ steps.versions.outputs.php-min }}
+ server-min: ${{ steps.versions.outputs.branches-min }}
+
+ steps:
+ - uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
+ id: changes
+ continue-on-error: true
+ with:
+ filters: |
+ src:
+ - '.github/workflows/upgrade-from-older-versions.yml'
+ - 'appinfo/**'
+ - 'lib/**'
+ - 'tests/Upgrade/**'
+
+ - name: Checkout app
+ if: steps.changes.outputs.src != 'false'
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+
+ - name: Get version matrix
+ if: steps.changes.outputs.src != 'false'
+ id: versions
+ uses: nextcloud-libraries/nextcloud-version-matrix@71959de73c244b11ecbf4503b87c41d65b0f64ae # v1.3.4
+
+ upgrade:
+ runs-on: ubuntu-latest
+
+ needs: changes-and-matrix
+ if: needs.changes-and-matrix.outputs.src != 'false'
+
+ strategy:
+ fail-fast: false
+ matrix:
+ database: ['sqlite', 'mysql', 'pgsql', 'oci']
+ from: ['1.0.12', '1.0.0-beta.1']
+ how: ['upgrade', 'reinstall']
+ include:
+ - from: '1.0.12'
+ ref: 0d84f4da14aea8564ba666d3cfd31f60123302bc
+ - from: '1.0.0-beta.1'
+ ref: v1.0.0-beta.1
+
+ name: ${{ matrix.from }} ${{ matrix.how }} on ${{ matrix.database }}
+
+ services:
+ mysql:
+ image: ghcr.io/nextcloud/continuous-integration-mysql-8.4:latest # zizmor: ignore[unpinned-images]
+ ports:
+ - 4444:3306/tcp
+ env:
+ MYSQL_ROOT_PASSWORD: rootpassword
+ options: --health-cmd="mysqladmin ping" --health-interval 5s --health-timeout 2s --health-retries 10
+ postgres:
+ image: ghcr.io/nextcloud/continuous-integration-postgres-16:latest # zizmor: ignore[unpinned-images]
+ ports:
+ - 4445:5432/tcp
+ env:
+ POSTGRES_USER: root
+ POSTGRES_PASSWORD: rootpassword
+ POSTGRES_DB: nextcloud
+ options: --health-cmd pg_isready --health-interval 5s --health-timeout 2s --health-retries 5
+
+ steps:
+ - name: Checkout server
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ submodules: true
+ repository: nextcloud/server
+ ref: ${{ needs.changes-and-matrix.outputs.server-min }}
+
+ - name: Add the viewer app where the server does not ship it
+ env:
+ SERVER_BRANCH: ${{ needs.changes-and-matrix.outputs.server-min }}
+ run: |
+ # `occ upgrade` refuses to run while an always-enabled app has no files
+ if [ ! -d apps/viewer ]; then
+ git clone --depth 1 --branch "$SERVER_BRANCH" https://github.com/nextcloud/viewer.git apps/viewer
+ fi
+
+ - name: Checkout Absence ${{ matrix.from }}
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ ref: ${{ matrix.ref }}
+ path: apps/absence
+
+ - name: Checkout the code to upgrade to
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ path: new-absence
+
+ - name: Set up php ${{ needs.changes-and-matrix.outputs.php-min }}
+ uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
+ with:
+ php-version: ${{ needs.changes-and-matrix.outputs.php-min }}
+ extensions: bz2, ctype, curl, dom, fileinfo, gd, iconv, intl, json, libxml, mbstring, openssl, pcntl, posix, session, simplexml, xmlreader, xmlwriter, zip, zlib, sqlite, pdo_sqlite, mysql, pdo_mysql, pgsql, pdo_pgsql${{ matrix.database == 'oci' && ', oci8' || '' }}
+ coverage: none
+ ini-file: development
+ ini-values: disable_functions=
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+ - name: Start Oracle
+ if: matrix.database == 'oci'
+ run: |
+ # Not a service: only these jobs need it, and it is slow to start
+ docker run -d --name oracle -p 1521:1521 -e ORACLE_PASSWORD=oracle ghcr.io/gvenzl/oracle-free:23 # zizmor: ignore[unpinned-images]
+ for _ in $(seq 1 60); do
+ docker exec oracle healthcheck.sh && exit 0
+ sleep 5
+ done
+ docker logs oracle | tail -20
+ exit 1
+
+ - name: Install Nextcloud with Absence ${{ matrix.from }}
+ env:
+ DATABASE: ${{ matrix.database }}
+ run: |
+ mkdir data
+ case "$DATABASE" in
+ sqlite) ./occ maintenance:install --verbose --database=sqlite --admin-user admin --admin-pass admin ;;
+ mysql) ./occ maintenance:install --verbose --database=mysql --database-name=nextcloud --database-host=127.0.0.1 --database-port=4444 --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin ;;
+ pgsql) ./occ maintenance:install --verbose --database=pgsql --database-name=nextcloud --database-host=127.0.0.1 --database-port=4445 --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin ;;
+ oci) ./occ maintenance:install --verbose --database=oci --database-name=FREE --database-host=127.0.0.1 --database-port=1521 --database-user=system --database-pass=oracle --admin-user admin --admin-pass admin ;;
+ esac
+ ./occ app:enable --force absence
+ ./occ config:app:get absence installed_version
+
+ - name: Enter invented data on ${{ matrix.from }}
+ run: |
+ php new-absence/tests/Upgrade/upgrade-check.php "$PWD" seed
+ php new-absence/tests/Upgrade/upgrade-check.php "$PWD" snapshot before.json
+
+ - name: Move to the new code
+ env:
+ HOW: ${{ matrix.how }}
+ run: |
+ installed=$(./occ config:app:get absence installed_version)
+ new=$(sed -n 's:.*\(.*\).*:\1:p' new-absence/appinfo/info.xml)
+ if [ "$installed" = "$new" ]; then
+ # Unreleased code keeps the version of the last release, and Nextcloud only
+ # runs migrations for a new version: bump it here like the release will.
+ sed -i "s:$new:$new.1:" new-absence/appinfo/info.xml
+ fi
+ if [ "$HOW" = reinstall ]; then
+ ./occ app:remove absence
+ mv new-absence apps/absence
+ ./occ app:enable --force absence
+ else
+ rm -rf apps/absence
+ mv new-absence apps/absence
+ ./occ upgrade
+ fi
+ ./occ config:app:get absence installed_version
+
+ - name: Check the data
+ run: php apps/absence/tests/Upgrade/upgrade-check.php "$PWD" verify before.json
diff --git a/SPECIFICATION.md b/SPECIFICATION.md
index 79775d16..d376ea09 100644
--- a/SPECIFICATION.md
+++ b/SPECIFICATION.md
@@ -873,6 +873,14 @@ and to the admin audit log; the region code people set themselves (§7.2) goes t
same history. The weekdays drive the working-days prefill (§7.1); the percentage is
not used for leave yet.
+Before HR kept contracts, people set their own working weekdays. The upgrade
+(`Version1018…`) turns a valid setting into a period for everyone who has none yet:
+`valid_from` is January 1 of the current year (server date), since the setting has
+no date and the year's leave is still being counted; the percentage stays empty. The
+history entry has no author and `"source": "own_setting"` in its detail. The setting
+is then deleted for everybody; an invalid one is dropped. HR corrects the date where
+it is wrong.
+
The annual allowance is **not** derived from the contract: HR sets each person's days
for the year (`absence_entitlements`, §5.5). The same percentage can mean shorter days
on all five weekdays, with the full allowance, or fewer weekdays with fewer days, and
@@ -2151,9 +2159,14 @@ Recorded during the build so the spec matches the code:
prefill reads the list.
- **Contract data and a profile page** (§6.4, §15.2): HR enters start and end dates and
the working pattern from a date on; the prefill reads those weekdays instead of the
- ones people set for themselves, which are dropped (`Version1010…`, app 1.2.0). The
+ ones people set for themselves, which become a contract period on the upgrade
+ (`Version1018…`, §6.4) and are then deleted. The
personal section on the Availability page is gone: the region code moved to the
profile page.
+- **Shorter table names** (`Version1013…`, `Version1014…`): three tables were renamed
+ for Oracle's 30-character limit. Each is copied in one transaction, and the old table
+ is only dropped when the new one holds at least as many rows; otherwise the upgrade
+ stops with the old table intact.
- **HR → Employees** (§13, §15.2): the *Balances* page became a list of every employee
with their region code, contract, manager and the year's days, flagging missing data,
from one HR-only endpoint (`GET /api/employees`) that costs a fixed number of
diff --git a/lib/Migration/Version1010Date20261023100000.php b/lib/Migration/Version1010Date20261023100000.php
index b761b0dc..4bb256c8 100644
--- a/lib/Migration/Version1010Date20261023100000.php
+++ b/lib/Migration/Version1010Date20261023100000.php
@@ -10,8 +10,6 @@
namespace OCA\Absence\Migration;
use Closure;
-use OCA\Absence\AppInfo\Application;
-use OCP\Config\IUserConfig;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
@@ -20,15 +18,10 @@
/**
* Contract data HR keeps per employee (§6.4): start and end date, the working
* weekdays and contract percentage from a date on, and the history of changes.
- * The weekdays people set for themselves for the prefill are dropped: HR owns
- * them now.
+ * The weekdays people set for themselves stay until `Version1018…` turns them
+ * into contract periods, once the tables have their final names.
*/
class Version1010Date20261023100000 extends SimpleMigrationStep {
- public function __construct(
- private IUserConfig $userConfig,
- ) {
- }
-
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
@@ -71,9 +64,4 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
}
return $changed ? $schema : null;
}
-
- #[\Override]
- public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
- $this->userConfig->deleteKey(Application::APP_ID, 'work_weekdays');
- }
}
diff --git a/lib/Migration/Version1012Date20261111120000.php b/lib/Migration/Version1012Date20261111120000.php
index b8eacef0..6b38cd25 100644
--- a/lib/Migration/Version1012Date20261111120000.php
+++ b/lib/Migration/Version1012Date20261111120000.php
@@ -13,6 +13,7 @@
use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
+use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
@@ -20,6 +21,10 @@
* Days are kept as `YYYY-MM-DD` text, like the out-of-office days of the server
* (`dav_absence`). A DATE column comes back with a time part on Oracle, and the
* app reads, compares and sends these values as text everywhere.
+ *
+ * Other databases convert the values with the column. Oracle only changes the
+ * type of a column without values, so there the days are first written as text
+ * to a new column that then takes the place of the old one.
*/
class Version1012Date20261111120000 extends SimpleMigrationStep {
private const DAYS = [
@@ -29,6 +34,49 @@ class Version1012Date20261111120000 extends SimpleMigrationStep {
'absence_contract_periods' => ['valid_from'],
];
+ /** The indexes over a day column: Oracle drops them with the column it replaces */
+ private const INDEXES = [
+ 'absence_requests' => ['absence_req_range_idx' => [['start_date', 'end_date'], false]],
+ 'absence_holidays' => ['absence_hol_date_idx' => [['date'], false]],
+ 'absence_contract_periods' => ['absence_period_uid_from_uniq' => [['employee_uid', 'valid_from'], true]],
+ ];
+
+ public function __construct(
+ private IDBConnection $db,
+ ) {
+ }
+
+ #[\Override]
+ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
+ if ($this->db->getDatabaseProvider() !== IDBConnection::PLATFORM_ORACLE) {
+ return;
+ }
+ /** @var ISchemaWrapper $schema */
+ $schema = $schemaClosure();
+ $qb = $this->db->getQueryBuilder();
+ foreach (self::DAYS as $name => $columns) {
+ if (!$schema->hasTable($name)) {
+ continue;
+ }
+ $table = $schema->getTable($name);
+ $quotedTable = $qb->getTableName($name);
+ foreach ($columns as $columnName) {
+ if (!$table->hasColumn($columnName) || self::isText($table->getColumn($columnName))) {
+ continue;
+ }
+ $column = $qb->getColumnName($columnName);
+ $text = $qb->getColumnName($columnName . '_txt');
+ $this->db->executeStatement("ALTER TABLE $quotedTable ADD ($text VARCHAR2(10))");
+ $this->db->executeStatement("UPDATE $quotedTable SET $text = TO_CHAR($column, 'YYYY-MM-DD')");
+ $this->db->executeStatement("ALTER TABLE $quotedTable DROP COLUMN $column");
+ $this->db->executeStatement("ALTER TABLE $quotedTable RENAME COLUMN $text TO $column");
+ if ($table->getColumn($columnName)->getNotnull()) {
+ $this->db->executeStatement("ALTER TABLE $quotedTable MODIFY ($column NOT NULL)");
+ }
+ }
+ }
+ }
+
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
@@ -45,20 +93,31 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
continue;
}
$column = $table->getColumn($columnName);
- // Nextcloud 35 and later hand out their own column, with an enum
- // as type; Nextcloud 34 hands out the Doctrine one.
- $type = $column->getType();
- $wrapped = $type instanceof \BackedEnum;
- /** @psalm-suppress UndefinedClass Doctrine is not part of the OCP stubs */
- $isText = ($wrapped ? $type->value : Type::lookupName($type)) === Types::STRING;
- if (!$isText) {
+ if (!self::isText($column)) {
/** @psalm-suppress UndefinedClass Doctrine is not part of the OCP stubs */
- $column->setType($wrapped ? Types::STRING : Type::getType(Types::STRING));
+ $column->setType($column->getType() instanceof \BackedEnum ? Types::STRING : Type::getType(Types::STRING));
$column->setLength(10);
$changed = true;
}
}
+ foreach (self::INDEXES[$name] ?? [] as $index => [$indexed, $unique]) {
+ if (!$table->hasIndex($index)) {
+ $unique ? $table->addUniqueIndex($indexed, $index) : $table->addIndex($indexed, $index);
+ $changed = true;
+ }
+ }
}
return $changed ? $schema : null;
}
+
+ /**
+ * Nextcloud 35 and later hand out their own column, with an enum as type;
+ * Nextcloud 34 hands out the Doctrine one.
+ */
+ private static function isText(object $column): bool {
+ /** @psalm-suppress MixedMethodCall the column class depends on the server version */
+ $type = $column->getType();
+ /** @psalm-suppress UndefinedClass Doctrine is not part of the OCP stubs */
+ return ($type instanceof \BackedEnum ? $type->value : Type::lookupName($type)) === Types::STRING;
+ }
}
diff --git a/lib/Migration/Version1013Date20261111130000.php b/lib/Migration/Version1013Date20261111130000.php
index 60c7066d..31af1198 100644
--- a/lib/Migration/Version1013Date20261111130000.php
+++ b/lib/Migration/Version1013Date20261111130000.php
@@ -23,7 +23,9 @@
* `_seq`, a table name over 23 characters gets a sequence the server cannot
* find again, so nothing could be inserted there.
*
- * The rows are copied here, and the next step drops the old tables.
+ * The rows are copied here, and the next step drops the old tables. Each table
+ * is copied in one transaction: a copy that fails leaves the new table empty, so
+ * running the upgrade again copies it from the start.
*/
class Version1013Date20261111130000 extends SimpleMigrationStep {
/** Old name, new name and the columns to copy (every one but the id) */
@@ -100,9 +102,18 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
foreach (self::RENAMED as [$old, $new, $columns]) {
- if ($schema->hasTable($old) && $this->isEmpty($new)) {
- $output->info(sprintf('Copied %d rows from %s to %s', $this->copy($old, $new, $columns), $old, $new));
+ if (!$schema->hasTable($old) || !$this->isEmpty($new)) {
+ continue;
}
+ $this->db->beginTransaction();
+ try {
+ $copied = $this->copy($old, $new, $columns);
+ $this->db->commit();
+ } catch (\Throwable $e) {
+ $this->db->rollBack();
+ throw $e;
+ }
+ $output->info(sprintf('Copied %d rows from %s to %s', $copied, $old, $new));
}
}
diff --git a/lib/Migration/Version1014Date20261111140000.php b/lib/Migration/Version1014Date20261111140000.php
index 0dc1f9ad..d199157e 100644
--- a/lib/Migration/Version1014Date20261111140000.php
+++ b/lib/Migration/Version1014Date20261111140000.php
@@ -11,25 +11,51 @@
use Closure;
use OCP\DB\ISchemaWrapper;
+use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
- * Drops the three tables the previous step copied to their shorter names.
+ * Drops the three tables the previous step copied to their shorter names. A
+ * table is only dropped when its new copy holds at least as many rows;
+ * otherwise the upgrade stops and the old table keeps its rows.
*/
class Version1014Date20261111140000 extends SimpleMigrationStep {
+ public function __construct(
+ private IDBConnection $db,
+ ) {
+ }
+
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$changed = false;
- foreach (Version1013Date20261111130000::RENAMED as [$old]) {
- if ($schema->hasTable($old)) {
- $schema->dropTable($old);
- $changed = true;
+ foreach (Version1013Date20261111130000::RENAMED as [$old, $new]) {
+ if (!$schema->hasTable($old)) {
+ continue;
+ }
+ // A new install reads every step before writing any table: nothing to count
+ $oldRows = $this->db->tableExists($old) ? $this->rows($old) : 0;
+ $newRows = $this->db->tableExists($new) ? $this->rows($new) : 0;
+ if ($newRows < $oldRows) {
+ throw new \RuntimeException(sprintf(
+ 'Table %s holds %d rows but its copy %s only %d: not all rows were copied, so it is not dropped.',
+ $old, $oldRows, $new, $newRows,
+ ));
}
+ $schema->dropTable($old);
+ $changed = true;
}
return $changed ? $schema : null;
}
+
+ private function rows(string $table): int {
+ $qb = $this->db->getQueryBuilder();
+ $result = $qb->select($qb->func()->count('*'))->from($table)->executeQuery();
+ $rows = (int)$result->fetchOne();
+ $result->closeCursor();
+ return $rows;
+ }
}
diff --git a/lib/Migration/Version1018Date20261117180000.php b/lib/Migration/Version1018Date20261117180000.php
new file mode 100644
index 00000000..8747d705
--- /dev/null
+++ b/lib/Migration/Version1018Date20261117180000.php
@@ -0,0 +1,164 @@
+ null, 'end' => null, 'periods' => []];
+
+ public function __construct(
+ private IDBConnection $db,
+ private IUserConfig $userConfig,
+ private ClockService $clock,
+ ) {
+ }
+
+ #[\Override]
+ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
+ $values = $this->ownWeekdays();
+ if ($values === []) {
+ return;
+ }
+
+ $from = $this->clock->serverYear() . '-01-01';
+ $converted = 0;
+ foreach (array_chunk($values, self::CHUNK, true) as $chunk) {
+ $weekdays = [];
+ foreach ($chunk as $uid => $value) {
+ $days = self::parse($value);
+ if ($days !== null) {
+ $weekdays[(string)$uid] = $days;
+ }
+ }
+ $weekdays = array_diff_key($weekdays, array_flip($this->withPeriods(array_map(strval(...), array_keys($weekdays)))));
+ $converted += $this->convert($weekdays, $from);
+ }
+ $this->userConfig->deleteKey(Application::APP_ID, self::KEY);
+ $output->info(sprintf('Turned the own working weekdays of %d people into contract periods', $converted));
+ }
+
+ /**
+ * Read from the table: the typed read of the server fails on an empty value,
+ * which Oracle stores as NULL.
+ *
+ * @return array uid => stored value
+ */
+ private function ownWeekdays(): array {
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('userid', 'configvalue')
+ ->from('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID)))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter(self::KEY)));
+ $result = $qb->executeQuery();
+ $values = [];
+ while ($row = $result->fetch()) {
+ $values[$row['userid']] = (string)($row['configvalue'] ?? '');
+ }
+ $result->closeCursor();
+ return $values;
+ }
+
+ /**
+ * Comma separated ISO weekdays, at least one and each once, sorted.
+ *
+ * @return list|null
+ */
+ private static function parse(string $value): ?array {
+ if (!preg_match('/^[1-7](,[1-7])*$/', $value)) {
+ return null;
+ }
+ $days = array_map(intval(...), explode(',', $value));
+ if (count(array_unique($days)) !== count($days)) {
+ return null;
+ }
+ sort($days);
+ return $days;
+ }
+
+ /**
+ * @param list $uids
+ * @return list the uids that already have a contract period
+ */
+ private function withPeriods(array $uids): array {
+ if ($uids === []) {
+ return [];
+ }
+ $qb = $this->db->getQueryBuilder();
+ $qb->selectDistinct('employee_uid')
+ ->from('absence_periods')
+ ->where($qb->expr()->in('employee_uid', $qb->createNamedParameter($uids, IQueryBuilder::PARAM_STR_ARRAY)));
+ $result = $qb->executeQuery();
+ $found = array_map(strval(...), $result->fetchFirstColumn());
+ $result->closeCursor();
+ return $found;
+ }
+
+ /**
+ * One period and one history entry per person, written together.
+ *
+ * @param array> $weekdays
+ */
+ private function convert(array $weekdays, string $from): int {
+ if ($weekdays === []) {
+ return 0;
+ }
+ $now = $this->clock->now();
+ $this->db->beginTransaction();
+ try {
+ foreach ($weekdays as $uid => $days) {
+ $uid = (string)$uid;
+ $period = $this->db->getQueryBuilder();
+ $period->insert('absence_periods')->values([
+ 'employee_uid' => $period->createNamedParameter($uid),
+ 'valid_from' => $period->createNamedParameter($from),
+ 'weekdays' => $period->createNamedParameter(implode(',', $days)),
+ 'percent' => $period->createNamedParameter(null, IQueryBuilder::PARAM_NULL),
+ ])->executeStatement();
+
+ $after = self::NO_CONTRACT;
+ $after['periods'] = [['from' => $from, 'weekdays' => $days, 'percent' => null]];
+ $history = $this->db->getQueryBuilder();
+ $history->insert('absence_emp_history')->values([
+ 'employee_uid' => $history->createNamedParameter($uid),
+ 'actor_uid' => $history->createNamedParameter(null, IQueryBuilder::PARAM_NULL),
+ 'field' => $history->createNamedParameter(EmployeeChange::CONTRACT),
+ 'detail' => $history->createNamedParameter(json_encode(['before' => self::NO_CONTRACT, 'after' => $after, 'source' => 'own_setting'], JSON_THROW_ON_ERROR)),
+ 'created_at' => $history->createNamedParameter($now, IQueryBuilder::PARAM_DATETIME_MUTABLE),
+ ])->executeStatement();
+ }
+ $this->db->commit();
+ } catch (\Throwable $e) {
+ $this->db->rollBack();
+ throw $e;
+ }
+ return count($weekdays);
+ }
+}
diff --git a/tests/Integration/UpgradeKeepsDataTest.php b/tests/Integration/UpgradeKeepsDataTest.php
new file mode 100644
index 00000000..dcafe24f
--- /dev/null
+++ b/tests/Integration/UpgradeKeepsDataTest.php
@@ -0,0 +1,316 @@
+db = Server::get(Connection::class);
+ $this->userConfig = Server::get(IUserConfig::class);
+ $this->cleanUp();
+ }
+
+ protected function tearDown(): void {
+ $this->cleanUp();
+ parent::tearDown();
+ }
+
+ public function testOwnWeekdaysBecomeAContract(): void {
+ $alice = self::PREFIX . 'alice';
+ $bob = self::PREFIX . 'bob';
+ $numeric = '90817';
+ $grace = self::PREFIX . 'grace';
+ $invalid = [
+ self::PREFIX . 'charlie' => '1,8',
+ self::PREFIX . 'dave' => '2,2,3',
+ self::PREFIX . 'erin' => '',
+ self::PREFIX . 'frank' => 'mon,tue',
+ ];
+ $prefs = [$alice => '1,2,3,4,5', $bob => '5,1,3', $numeric => '2,4', $grace => '3,4'] + $invalid;
+ foreach ($prefs as $uid => $value) {
+ $this->userConfig->setValueString((string)$uid, ConfigService::APP_ID, self::KEY, $value);
+ }
+ $this->insert('absence_periods', ['employee_uid' => $grace, 'valid_from' => '2020-03-01', 'weekdays' => '1,2', 'percent' => null]);
+
+ $this->runPostSchemaChange(Server::get(Version1018Date20261117180000::class));
+
+ $from = Server::get(ClockService::class)->serverYear() . '-01-01';
+ $periods = Server::get(ContractPeriodMapper::class);
+ $describe = static fn (string $uid): array => array_map(
+ static fn ($period): array => [$period->getValidFrom(), $period->getWeekdays(), $period->getPercent()],
+ $periods->findForEmployee($uid),
+ );
+ self::assertSame([[$from, '1,2,3,4,5', null]], $describe($alice));
+ self::assertSame([[$from, '1,3,5', null]], $describe($bob));
+ self::assertSame([[$from, '2,4', null]], $describe($numeric));
+ self::assertSame([['2020-03-01', '1,2', null]], $describe($grace));
+ foreach (array_keys($invalid) as $uid) {
+ self::assertSame([], $describe($uid), $uid);
+ }
+
+ $history = Server::get(EmployeeService::class)->history($bob);
+ self::assertCount(1, $history);
+ self::assertSame(['contract', null], [$history[0]['field'], $history[0]['actor']]);
+ self::assertSame(['start' => null, 'end' => null, 'periods' => []], $history[0]['before']);
+ self::assertSame(['start' => null, 'end' => null, 'periods' => [['from' => $from, 'weekdays' => [1, 3, 5], 'percent' => null]]], $history[0]['after']);
+ self::assertSame('own_setting', json_decode($this->historyDetails($bob)[0], true)['source']);
+ self::assertCount(1, $this->historyDetails($alice));
+ self::assertCount(1, $this->historyDetails($numeric));
+ self::assertSame([], $this->historyDetails($grace));
+ foreach (array_keys($invalid) as $uid) {
+ self::assertSame([], $this->historyDetails($uid), $uid);
+ }
+ self::assertSame([], $this->prefs());
+
+ $before = [$this->rows('absence_periods'), $this->rows('absence_emp_history')];
+ $this->runPostSchemaChange(Server::get(Version1018Date20261117180000::class));
+ self::assertSame($before, [$this->rows('absence_periods'), $this->rows('absence_emp_history')]);
+ }
+
+ public function testTheContractStepKeepsOwnWeekdays(): void {
+ $this->userConfig->setValueString(self::PREFIX . 'alice', ConfigService::APP_ID, self::KEY, '1,2,3');
+
+ $this->runPostSchemaChange(Server::get(Version1010Date20261023100000::class));
+
+ self::assertSame([self::PREFIX . 'alice' => '1,2,3'], $this->prefs());
+ }
+
+ public function testACopyThatFailsLeavesTheNewTableEmpty(): void {
+ self::assertSame(0, $this->rows(self::NEW_EVENTS), 'The test needs an empty ' . self::NEW_EVENTS);
+ // `field` may be null here, so the second row cannot go into the new table
+ $this->createOldEvents(fieldNullable: true);
+ $this->insertOldEvents(['base_days', null, 'manual_adjustment']);
+
+ $migration = Server::get(Version1013Date20261111130000::class);
+ $error = null;
+ try {
+ $this->runPostSchemaChange($migration);
+ } catch (\Throwable $e) {
+ $error = $e;
+ }
+ self::assertNotNull($error, 'The copy did not fail');
+ self::assertSame(0, $this->rows(self::NEW_EVENTS));
+
+ $qb = $this->db->getQueryBuilder();
+ $qb->update(self::OLD_EVENTS)
+ ->set('field', $qb->createNamedParameter('carry_over_days'))
+ ->where($qb->expr()->isNull('field'))
+ ->executeStatement();
+ $this->runPostSchemaChange($migration);
+ self::assertSame(3, $this->rows(self::NEW_EVENTS));
+ }
+
+ public function testOldTableStaysWhenTheNewOneHasFewerRows(): void {
+ self::assertSame(0, $this->rows(self::NEW_EVENTS), 'The test needs an empty ' . self::NEW_EVENTS);
+ $this->createOldEvents();
+ $this->insertOldEvents(['base_days', 'carry_over_days', 'manual_adjustment']);
+ $this->insertNewEvent();
+
+ $error = null;
+ try {
+ $this->runChangeSchema(Server::get(Version1014Date20261111140000::class));
+ } catch (\RuntimeException $e) {
+ $error = $e;
+ }
+ self::assertNotNull($error, 'The old table was dropped');
+ self::assertMatchesRegularExpression('/absence_entitlement_events\\D+3\\D+absence_ent_events\\D+1\\D/', $error->getMessage());
+ self::assertTrue($this->db->tableExists(self::OLD_EVENTS));
+ self::assertSame(3, $this->rows(self::OLD_EVENTS));
+ }
+
+ public function testOldTableIsDroppedOnceEveryRowIsCopied(): void {
+ self::assertSame(0, $this->rows(self::NEW_EVENTS), 'The test needs an empty ' . self::NEW_EVENTS);
+ $this->createOldEvents();
+ $this->insertOldEvents(['base_days']);
+ $this->insertNewEvent();
+
+ $this->runChangeSchema(Server::get(Version1014Date20261111140000::class));
+
+ self::assertFalse($this->db->tableExists(self::OLD_EVENTS));
+ }
+
+ public function testAFreshInstallDropsTheOldTablesWithoutCounting(): void {
+ // A new install reads every step's schema change before writing any table
+ $schema = new SchemaWrapper($this->db);
+ $schema->createTable(self::OLD_EVENTS)->addColumn('id', Types::BIGINT, ['autoincrement' => true, 'notnull' => true]);
+
+ $result = Server::get(Version1014Date20261111140000::class)
+ ->changeSchema($this->createMock(IOutput::class), static fn (): ISchemaWrapper => $schema, []);
+
+ self::assertFalse($result?->hasTable(self::OLD_EVENTS));
+ }
+
+ private function runPostSchemaChange(\OCP\Migration\IMigrationStep $migration): void {
+ $migration->postSchemaChange($this->createMock(IOutput::class), fn (): ISchemaWrapper => new SchemaWrapper($this->db), []);
+ }
+
+ private function runChangeSchema(\OCP\Migration\IMigrationStep $migration): void {
+ $schema = $migration->changeSchema($this->createMock(IOutput::class), fn (): ISchemaWrapper => new SchemaWrapper($this->db), []);
+ if ($schema instanceof SchemaWrapper) {
+ $this->db->migrateToSchema($schema->getWrappedSchema());
+ $schema->performDropTableCalls();
+ }
+ }
+
+ /**
+ * The entitlement history table as 1.0.12 created it.
+ */
+ private function createOldEvents(bool $fieldNullable = false): void {
+ $schema = new SchemaWrapper($this->db);
+ $table = $schema->createTable(self::OLD_EVENTS);
+ $table->addColumn('id', Types::BIGINT, ['autoincrement' => true, 'notnull' => true]);
+ $table->addColumn('entitlement_id', Types::BIGINT, ['notnull' => true]);
+ $table->addColumn('employee_uid', Types::STRING, ['notnull' => true, 'length' => 64]);
+ $table->addColumn('actor_uid', Types::STRING, ['notnull' => true, 'length' => 64]);
+ $table->addColumn('field', Types::STRING, ['notnull' => !$fieldNullable, 'length' => 32]);
+ $table->addColumn('old_value', Types::FLOAT, ['notnull' => true, 'default' => 0]);
+ $table->addColumn('new_value', Types::FLOAT, ['notnull' => true, 'default' => 0]);
+ $table->addColumn('note', Types::TEXT, ['notnull' => false]);
+ $table->addColumn('created_at', Types::DATETIME, ['notnull' => true]);
+ $table->setPrimaryKey(['id']);
+ $table->addIndex(['entitlement_id'], 'absence_entev_ent');
+ $table->addIndex(['employee_uid'], 'absence_entev_emp');
+ $this->db->migrateToSchema($schema->getWrappedSchema());
+ }
+
+ /**
+ * @param list $fields
+ */
+ private function insertOldEvents(array $fields): void {
+ foreach ($fields as $i => $field) {
+ $this->insert(self::OLD_EVENTS, [
+ 'entitlement_id' => 990001,
+ 'employee_uid' => self::PREFIX . 'alice',
+ 'actor_uid' => self::PREFIX . 'hr',
+ 'field' => $field,
+ 'old_value' => (string)$i,
+ 'new_value' => (string)($i + 1),
+ 'note' => null,
+ 'created_at' => '2026-08-20 10:00:00',
+ ]);
+ }
+ }
+
+ private function insertNewEvent(): void {
+ $this->insert(self::NEW_EVENTS, [
+ 'entitlement_id' => 990001,
+ 'employee_uid' => self::PREFIX . 'alice',
+ 'actor_uid' => self::PREFIX . 'hr',
+ 'field' => 'base_days',
+ 'old_value' => '0',
+ 'new_value' => '1',
+ 'note' => null,
+ 'created_at' => '2026-08-20 10:00:00',
+ ]);
+ }
+
+ /**
+ * @param array $values
+ */
+ private function insert(string $table, array $values): void {
+ $qb = $this->db->getQueryBuilder();
+ $qb->insert($table);
+ foreach ($values as $column => $value) {
+ $qb->setValue($column, $value === null
+ ? $qb->createNamedParameter(null, IQueryBuilder::PARAM_NULL)
+ : $qb->createNamedParameter((string)$value));
+ }
+ $qb->executeStatement();
+ }
+
+ private function rows(string $table): int {
+ $qb = $this->db->getQueryBuilder();
+ $result = $qb->select($qb->func()->count('*'))->from($table)->executeQuery();
+ $count = (int)$result->fetchOne();
+ $result->closeCursor();
+ return $count;
+ }
+
+ /**
+ * @return list
+ */
+ private function historyDetails(string $uid): array {
+ $qb = $this->db->getQueryBuilder();
+ $result = $qb->select('detail')
+ ->from('absence_emp_history')
+ ->where($qb->expr()->eq('employee_uid', $qb->createNamedParameter($uid)))
+ ->executeQuery();
+ $details = $result->fetchFirstColumn();
+ $result->closeCursor();
+ return $details;
+ }
+
+ /**
+ * @return array
+ */
+ private function prefs(): array {
+ // From the table: the server's typed read fails on Oracle, where the
+ // empty value of one of these people is stored as NULL.
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('userid', 'configvalue')
+ ->from('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter(ConfigService::APP_ID)))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter(self::KEY)));
+ $result = $qb->executeQuery();
+ $prefs = [];
+ while ($row = $result->fetch()) {
+ $prefs[(string)$row['userid']] = (string)($row['configvalue'] ?? '');
+ }
+ $result->closeCursor();
+ return $prefs;
+ }
+
+ private function cleanUp(): void {
+ $this->userConfig->deleteKey(ConfigService::APP_ID, self::KEY);
+ foreach (['absence_periods', 'absence_emp_history', self::NEW_EVENTS] as $table) {
+ $qb = $this->db->getQueryBuilder();
+ $qb->delete($table)
+ ->where($qb->expr()->orX(
+ $qb->expr()->like('employee_uid', $qb->createNamedParameter(self::PREFIX . '%')),
+ $qb->expr()->eq('employee_uid', $qb->createNamedParameter('90817')),
+ ))
+ ->executeStatement();
+ }
+ if ($this->db->tableExists(self::OLD_EVENTS)) {
+ $this->db->dropTable(self::OLD_EVENTS);
+ }
+ }
+}
diff --git a/tests/Upgrade/upgrade-check.php b/tests/Upgrade/upgrade-check.php
new file mode 100644
index 00000000..900cb8de
--- /dev/null
+++ b/tests/Upgrade/upgrade-check.php
@@ -0,0 +1,484 @@
+ seed invented data, on the old version
+ * php upgrade-check.php snapshot write what is stored
+ * php upgrade-check.php verify after `occ upgrade`
+ *
+ * `snapshot` and `verify` read whatever the instance holds, so they also serve
+ * to rehearse the upgrade on a copy of a real database. A snapshot of real
+ * data is personal data: keep it out of the repository.
+ *
+ * `seed` writes the rows the way the old version stored them: 1.0.12 (commit
+ * 0d84f4d), the version HR used before the betas, or 1.0.0-beta.1. Every
+ * person and value in it is invented.
+ *
+ * Only OCP and plain queries are used to read and write, so the same file runs
+ * against every version. The balances are asked from the app itself, to
+ * compare what it shows before and after.
+ */
+
+use OCP\App\IAppManager;
+use OCP\Config\IUserConfig;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\Files\AppData\IAppDataFactory;
+use OCP\Files\NotFoundException;
+use OCP\IAppConfig;
+use OCP\IDBConnection;
+use OCP\IGroupManager;
+use OCP\IUserManager;
+use OCP\Server;
+
+if ($argc < 3) {
+ fwrite(STDERR, "usage: upgrade-check.php seed|snapshot |verify \n");
+ exit(2);
+}
+[, $serverRoot, $mode] = $argv;
+$file = $argv[3] ?? null;
+require rtrim($serverRoot, '/') . '/lib/base.php';
+
+const APP = 'absence';
+const USERS = ['upgrade-alice', 'upgrade-bob', 'upgrade-carol', 'upgrade-dave', 'upgrade-hr'];
+
+/** Weekdays people set for themselves on 1.0.12: sorted, short, unsorted and not weekdays at all */
+const WEEKDAYS = [
+ 'upgrade-alice' => '1,2,3,4,5',
+ 'upgrade-bob' => '1,2,3,4',
+ 'upgrade-carol' => '5,1,3',
+ 'upgrade-dave' => 'every day',
+];
+
+$db = Server::get(IDBConnection::class);
+
+if ($mode === 'seed') {
+ seed($db);
+} elseif ($mode === 'snapshot' && $file !== null) {
+ file_put_contents($file, json_encode(before($db), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
+} elseif ($mode === 'verify' && $file !== null) {
+ exit(verify($db, json_decode((string)file_get_contents($file), true, flags: JSON_THROW_ON_ERROR)));
+} else {
+ exit(2);
+}
+
+function seed(IDBConnection $db): void {
+ // 1.0.12 keeps the entitlement history under its long name and has no contracts yet
+ $before1013 = !$db->tableExists('absence_ent_events');
+
+ $users = Server::get(IUserManager::class);
+ foreach (USERS as $uid) {
+ $users->get($uid) ?? $users->createUser($uid, bin2hex(random_bytes(16)) . 'Aa1!');
+ }
+ $groups = Server::get(IGroupManager::class);
+ // The HR group of the old default; the setting itself is never stored
+ ($groups->get('hr') ?? $groups->createGroup('hr'))->addUser($users->get('upgrade-hr'));
+ foreach (['upgrade-alice', 'upgrade-bob', 'upgrade-carol'] as $uid) {
+ $users->get($uid)->setManagerUids(['upgrade-dave']);
+ }
+
+ $config = Server::get(IUserConfig::class);
+ if ($before1013) {
+ foreach (WEEKDAYS as $uid => $stored) {
+ $config->setValueString($uid, APP, 'work_weekdays', $stored);
+ }
+ } else {
+ // The betas keep contracts: HR entered one for somebody
+ insert($db, 'absence_employments', ['employee_uid' => 'upgrade-alice', 'start_date' => '2022-04-01', 'end_date' => null]);
+ insert($db, 'absence_periods', ['employee_uid' => 'upgrade-alice', 'valid_from' => '2022-04-01', 'weekdays' => '1,2,3,4', 'percent' => [80, IQueryBuilder::PARAM_INT]]);
+ }
+ $config->setValueString('upgrade-alice', APP, 'holiday_country', 'DE');
+ $config->setValueString('upgrade-alice', APP, 'holiday_region', 'BY');
+ $config->setValueString('upgrade-bob', APP, 'holiday_country', 'FR');
+
+ $appConfig = Server::get(IAppConfig::class);
+ $appConfig->setValueFloat(APP, 'default_entitlement', 28.0);
+ $appConfig->setValueInt(APP, 'max_concurrent_absences', 3);
+ $appConfig->setValueString(APP, 'carryover_expiry', '03-31', lazy: true);
+
+ $types = typeIds($db);
+ insert($db, 'absence_leave_types', [
+ 'key' => 'training', 'label' => 'Training', 'color' => '#4271a6', 'icon' => '📚',
+ 'counts_against_balance' => [false, IQueryBuilder::PARAM_BOOL], 'requires_approval' => [true, IQueryBuilder::PARAM_BOOL],
+ 'requires_note' => [true, IQueryBuilder::PARAM_BOOL], 'requires_replacement' => [false, IQueryBuilder::PARAM_BOOL],
+ 'employee_requestable' => [true, IQueryBuilder::PARAM_BOOL], 'enabled' => [true, IQueryBuilder::PARAM_BOOL],
+ 'hr_only' => [false, IQueryBuilder::PARAM_BOOL], 'sort_order' => [20, IQueryBuilder::PARAM_INT],
+ ]);
+ $update = $db->getQueryBuilder();
+ $update->update('absence_leave_types')->set('enabled', $update->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
+ ->where($update->expr()->eq('key', $update->createNamedParameter('special')))->executeStatement();
+
+ // A half day, a confidential absence, pending leave, leave across New Year and a cancelled request
+ $requests = [
+ ['upgrade-alice', 'annual', '2026-02-16', '2026-02-20', '5', 'APPROVED', 'Ski week', 'upgrade-bob', 'upgrade-dave', false],
+ ['upgrade-alice', 'annual', '2026-08-03', '2026-08-04', '1.5', 'APPROVED', null, 'upgrade-bob', 'upgrade-dave', false],
+ ['upgrade-bob', 'sick', '2026-03-02', '2026-03-03', '2', 'APPROVED', null, null, 'upgrade-hr', true],
+ ['upgrade-carol', 'maternity', '2026-04-01', '2026-06-30', '60', 'APPROVED', 'Invented confidential note', null, 'upgrade-hr', false],
+ ['upgrade-bob', 'annual', '2026-12-21', '2026-12-23', '3', 'PENDING', null, 'upgrade-alice', null, false],
+ ['upgrade-carol', 'annual', '2025-12-29', '2026-01-02', '4', 'APPROVED', null, 'upgrade-alice', 'upgrade-dave', false],
+ ['upgrade-alice', 'unpaid', '2026-05-04', '2026-05-04', '0.5', 'CANCELLED', 'Invented reason', null, null, false],
+ ];
+ $ids = [];
+ foreach ($requests as [$uid, $type, $from, $to, $days, $status, $reason, $replacement, $decidedBy, $disability]) {
+ $ids[] = insert($db, 'absence_requests', [
+ 'employee_uid' => $uid, 'manager_uid' => 'upgrade-dave', 'type_id' => [$types[$type], IQueryBuilder::PARAM_INT],
+ // 1.0.12 keeps days in date columns, the betas as text
+ 'start_date' => $before1013 ? [new DateTime($from), IQueryBuilder::PARAM_DATE] : $from,
+ 'end_date' => $before1013 ? [new DateTime($to), IQueryBuilder::PARAM_DATE] : $to,
+ 'working_days' => $days, 'status' => $status, 'reason' => $reason, 'replacement_uid' => $replacement,
+ 'decided_by' => $decidedBy, 'decided_at' => [$decidedBy === null ? null : new DateTime('2026-01-10 09:00:00'), $decidedBy === null ? IQueryBuilder::PARAM_NULL : IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ 'escalated' => [false, IQueryBuilder::PARAM_BOOL], 'disability' => [$disability, IQueryBuilder::PARAM_BOOL],
+ 'created_at' => [new DateTime('2026-01-05 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ 'updated_at' => [new DateTime('2026-01-10 09:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ ]);
+ }
+
+ foreach ([
+ [$ids[0], 'upgrade-dave', 'Enjoy the snow.'],
+ [$ids[3], 'upgrade-hr', 'Invented HR-only remark.'],
+ [$ids[4], 'upgrade-bob', 'Back on the 28th.'],
+ ] as [$request, $author, $body]) {
+ insert($db, 'absence_comments', ['request_id' => [$request, IQueryBuilder::PARAM_INT], 'author_uid' => $author, 'body' => $body,
+ 'created_at' => [new DateTime('2026-01-06 10:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE]]);
+ }
+ foreach ([[$ids[0], 'upgrade-alice', 'created'], [$ids[0], 'upgrade-dave', 'approved'], [$ids[3], 'upgrade-hr', 'created']] as [$request, $actor, $event]) {
+ insert($db, 'absence_request_events', ['request_id' => [$request, IQueryBuilder::PARAM_INT], 'actor_uid' => $actor, 'event_type' => $event,
+ 'detail' => null, 'created_at' => [new DateTime('2026-01-05 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE]]);
+ }
+
+ // A half-day allowance, a carry-over and an adjustment with its note
+ $entitlements = [
+ ['upgrade-alice', 2026, '28', '5', '-1.5', 'Invented correction'],
+ ['upgrade-bob', 2026, '27.5', '0', '0', null],
+ ['upgrade-carol', 2025, '28', '0', '0', null],
+ ['upgrade-carol', 2026, '30', '3', '0', null],
+ ];
+ foreach ($entitlements as [$uid, $year, $base, $carry, $adjustment, $note]) {
+ $id = insert($db, 'absence_entitlements', [
+ 'employee_uid' => $uid, 'year' => [$year, IQueryBuilder::PARAM_INT], 'type_id' => [$types['annual'], IQueryBuilder::PARAM_INT],
+ 'base_days' => $base, 'carry_over_days' => $carry, 'manual_adjustment' => $adjustment, 'adjustment_note' => $note,
+ 'created_at' => [new DateTime('2026-01-02 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ 'updated_at' => [new DateTime('2026-01-02 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ ]);
+ insert($db, $before1013 ? 'absence_entitlement_events' : 'absence_ent_events', [
+ 'entitlement_id' => [$id, IQueryBuilder::PARAM_INT], 'employee_uid' => $uid, 'actor_uid' => 'upgrade-hr',
+ 'field' => 'base_days', 'old_value' => '0', 'new_value' => $base, 'note' => $note,
+ 'created_at' => [new DateTime('2026-01-02 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ ]);
+ }
+
+ $content = "%PDF-1.4\ninvented attachment for the upgrade check\n";
+ $attachment = insert($db, 'absence_attachments', [
+ 'request_id' => [$ids[2], IQueryBuilder::PARAM_INT], 'uploader_uid' => 'upgrade-hr', 'name' => 'invented-note.pdf',
+ 'mime' => 'application/pdf', 'size' => [strlen($content), IQueryBuilder::PARAM_INT],
+ 'created_at' => [new DateTime('2026-03-04 08:00:00'), IQueryBuilder::PARAM_DATETIME_MUTABLE],
+ ]);
+ attachmentFolder()->newFile((string)$attachment, $content);
+ echo 'seeded the data of ' . ($before1013 ? '1.0.12' : 'a beta') . "\n";
+}
+
+/** @param array $values a value, or [value, parameter type] */
+function insert(IDBConnection $db, string $table, array $values): int {
+ $qb = $db->getQueryBuilder();
+ $qb->insert($table);
+ foreach ($values as $column => $value) {
+ [$value, $type] = is_array($value) ? $value : [$value, $value === null ? IQueryBuilder::PARAM_NULL : IQueryBuilder::PARAM_STR];
+ $qb->setValue($column, $qb->createNamedParameter($value, $type));
+ }
+ $qb->executeStatement();
+ return $qb->getLastInsertId();
+}
+
+/** @return array leave type key => id */
+function typeIds(IDBConnection $db): array {
+ $result = $db->getQueryBuilder()->select('id', 'key')->from('absence_leave_types')->executeQuery();
+ $ids = [];
+ while ($row = $result->fetch()) {
+ $ids[$row['key']] = (int)$row['id'];
+ }
+ $result->closeCursor();
+ return $ids;
+}
+
+function attachmentFolder(): \OCP\Files\SimpleFS\ISimpleFolder {
+ $appData = Server::get(IAppDataFactory::class)->get(APP);
+ try {
+ return $appData->getFolder('attachments');
+ } catch (NotFoundException) {
+ return $appData->newFolder('attachments');
+ }
+}
+
+/** @return list> */
+function rows(IDBConnection $db, string $table, array $columns, string $order = 'id'): array {
+ $qb = $db->getQueryBuilder();
+ $result = $qb->select(...$columns)->from($table)->orderBy($order)->executeQuery();
+ $rows = $result->fetchAll();
+ $result->closeCursor();
+ return $rows;
+}
+
+function flag(mixed $value): bool {
+ return in_array($value, [true, 1, '1', 't', 'true'], true);
+}
+
+function day(mixed $value): ?string {
+ return $value === null ? null : substr((string)$value, 0, 10);
+}
+
+function number(mixed $value): string {
+ return rtrim(rtrim(sprintf('%.2F', (float)$value), '0'), '.');
+}
+
+/** The weekdays of a personal setting in order, or null when it does not list weekdays */
+function weekdays(string $stored): ?string {
+ if (preg_match('/^[1-7](,[1-7])*$/', $stored) !== 1) {
+ return null;
+ }
+ $days = explode(',', $stored);
+ if (count(array_unique($days)) !== count($days)) {
+ return null;
+ }
+ sort($days);
+ return implode(',', $days);
+}
+
+/**
+ * Read from the table: the typed read of the server fails on an empty value,
+ * which Oracle stores as NULL.
+ *
+ * @return array uid => value, for everybody who stored one
+ */
+function personal(string $key): array {
+ $qb = Server::get(IDBConnection::class)->getQueryBuilder();
+ $qb->select('userid', 'configvalue')->from('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter(APP)))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
+ $result = $qb->executeQuery();
+ $values = [];
+ while ($row = $result->fetch()) {
+ $values[(string)$row['userid']] = (string)($row['configvalue'] ?? '');
+ }
+ $result->closeCursor();
+ ksort($values);
+ return $values;
+}
+
+/**
+ * What the app itself shows as balance, for every person and year with an
+ * entitlement or a request.
+ *
+ * @param list> $requests
+ * @param list> $entitlements
+ * @return array>> "uid year" => type => figures
+ */
+function balances(array $requests, array $entitlements): array {
+ Server::get(IAppManager::class)->loadApp(APP);
+ /** @psalm-suppress UndefinedClass the app is not part of this script */
+ $service = Server::get(\OCA\Absence\Service\BalanceService::class);
+ $wanted = [];
+ foreach ($requests as $request) {
+ $wanted[$request['employee'] . ' ' . substr((string)$request['from'], 0, 4)] = true;
+ }
+ foreach ($entitlements as $entitlement) {
+ $wanted[$entitlement['employee'] . ' ' . $entitlement['year']] = true;
+ }
+ ksort($wanted);
+ $balances = [];
+ foreach (array_keys($wanted) as $key) {
+ [$uid, $year] = explode(' ', $key);
+ foreach ($service->getBalance($uid, (int)$year)['balances'] as $row) {
+ if ((int)$row['year'] !== (int)$year) {
+ continue;
+ }
+ $balances[$key][$row['typeKey']] = [
+ 'entitlement' => number($row['entitlement']), 'used' => number($row['used']),
+ 'pending' => number($row['pending']), 'available' => number($row['available']),
+ ];
+ }
+ ksort($balances[$key]);
+ }
+ return $balances;
+}
+
+/**
+ * What people entered, in a form that reads the same before and after the
+ * upgrade: dates as `YYYY-MM-DD`, numbers without trailing zeros, types by key.
+ *
+ * @return array
+ */
+function snapshot(IDBConnection $db): array {
+ $types = array_flip(typeIds($db));
+ $requests = array_map(static fn (array $r): array => [
+ 'id' => (int)$r['id'], 'employee' => $r['employee_uid'], 'manager' => $r['manager_uid'], 'type' => $types[(int)$r['type_id']] ?? null,
+ 'from' => day($r['start_date']), 'to' => day($r['end_date']), 'days' => number($r['working_days']), 'status' => $r['status'],
+ 'reason' => $r['reason'], 'replacement' => $r['replacement_uid'], 'decidedBy' => $r['decided_by'], 'disability' => flag($r['disability']),
+ ], rows($db, 'absence_requests', ['id', 'employee_uid', 'manager_uid', 'type_id', 'start_date', 'end_date', 'working_days', 'status', 'reason', 'replacement_uid', 'decided_by', 'disability']));
+
+ $entitlements = array_map(static fn (array $r): array => [
+ 'id' => (int)$r['id'], 'employee' => $r['employee_uid'], 'year' => (int)$r['year'], 'type' => $types[(int)$r['type_id']] ?? null,
+ 'base' => number($r['base_days']), 'carryOver' => number($r['carry_over_days']), 'adjustment' => number($r['manual_adjustment']), 'note' => $r['adjustment_note'],
+ ], rows($db, 'absence_entitlements', ['id', 'employee_uid', 'year', 'type_id', 'base_days', 'carry_over_days', 'manual_adjustment', 'adjustment_note']));
+
+ $eventsTable = $db->tableExists('absence_ent_events') ? 'absence_ent_events' : 'absence_entitlement_events';
+ $entitlementEvents = array_map(static fn (array $r): array => [
+ 'entitlement' => (int)$r['entitlement_id'], 'employee' => $r['employee_uid'], 'actor' => $r['actor_uid'], 'field' => $r['field'],
+ 'old' => number($r['old_value']), 'new' => number($r['new_value']), 'note' => $r['note'], 'at' => substr((string)$r['created_at'], 0, 19),
+ ], rows($db, $eventsTable, ['entitlement_id', 'employee_uid', 'actor_uid', 'field', 'old_value', 'new_value', 'note', 'created_at']));
+
+ $hrOnly = [];
+ foreach (rows($db, 'absence_leave_types', ['id', 'hr_only']) as $type) {
+ $hrOnly[(int)$type['id']] = flag($type['hr_only']);
+ }
+ $confidentialRequests = [];
+ foreach (rows($db, 'absence_requests', ['id', 'type_id']) as $request) {
+ $confidentialRequests[(int)$request['id']] = $hrOnly[(int)$request['type_id']] ?? false;
+ }
+ $comments = array_map(static fn (array $r): array => [
+ 'request' => (int)$r['request_id'], 'author' => $r['author_uid'], 'body' => $r['body'],
+ 'onConfidentialAbsence' => $confidentialRequests[(int)$r['request_id']] ?? false,
+ ], rows($db, 'absence_comments', ['request_id', 'author_uid', 'body']));
+ $history = array_map(static fn (array $r): array => ['request' => (int)$r['request_id'], 'actor' => $r['actor_uid'], 'event' => $r['event_type']],
+ rows($db, 'absence_request_events', ['request_id', 'actor_uid', 'event_type']));
+
+ $folder = attachmentFolder();
+ $attachments = array_map(static function (array $r) use ($folder): array {
+ try {
+ $hash = sha1($folder->getFile((string)$r['id'])->getContent());
+ } catch (NotFoundException) {
+ $hash = null;
+ }
+ return ['request' => (int)$r['request_id'], 'name' => $r['name'], 'mime' => $r['mime'], 'size' => (int)$r['size'], 'sha1' => $hash];
+ }, rows($db, 'absence_attachments', ['id', 'request_id', 'name', 'mime', 'size']));
+
+ $leaveTypes = array_map(static fn (array $r): array => [
+ 'key' => $r['key'], 'label' => $r['label'], 'hrOnly' => flag($r['hr_only']), 'enabled' => flag($r['enabled']),
+ 'countsAgainstBalance' => flag($r['counts_against_balance']), 'requiresNote' => flag($r['requires_note']),
+ ], rows($db, 'absence_leave_types', ['key', 'label', 'hr_only', 'enabled', 'counts_against_balance', 'requires_note'], 'key'));
+
+ $appConfig = Server::get(IAppConfig::class);
+ $snapshot = [
+ 'requests' => $requests,
+ 'entitlements' => $entitlements,
+ 'entitlementEvents' => $entitlementEvents,
+ 'comments' => $comments,
+ 'requestHistory' => $history,
+ 'attachments' => $attachments,
+ 'leaveTypes' => $leaveTypes,
+ 'balances' => balances($requests, $entitlements),
+ 'regions' => ['country' => personal('holiday_country'), 'region' => personal('holiday_region')],
+ 'settings' => [
+ 'default_entitlement' => number($appConfig->getValueFloat(APP, 'default_entitlement', 28.0)),
+ 'max_concurrent_absences' => $appConfig->getValueInt(APP, 'max_concurrent_absences', 2),
+ 'carryover_expiry' => $appConfig->getValueString(APP, 'carryover_expiry', '', lazy: true),
+ ],
+ ];
+ // Contracts exist from the betas on
+ if ($db->tableExists('absence_employments')) {
+ $snapshot['employments'] = array_map(static fn (array $r): array => ['employee' => $r['employee_uid'], 'start' => day($r['start_date']), 'end' => day($r['end_date'])],
+ rows($db, 'absence_employments', ['employee_uid', 'start_date', 'end_date'], 'employee_uid'));
+ }
+ return $snapshot;
+}
+
+/**
+ * The parts of a snapshot that are compared one to one, and what is only
+ * needed to work out what the upgrade has to do.
+ *
+ * @return array{kept: array, weekdays: array, contracts: list>, hrGroup: ?string, hrGroupExists: bool}
+ */
+function before(IDBConnection $db): array {
+ $appConfig = Server::get(IAppConfig::class);
+ $periods = $db->tableExists('absence_periods') ? 'absence_periods' : ($db->tableExists('absence_contract_periods') ? 'absence_contract_periods' : null);
+ return [
+ 'kept' => snapshot($db),
+ 'weekdays' => personal('work_weekdays'),
+ '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'),
+ ];
+}
+
+/** @return list */
+function contracts(IDBConnection $db, string $table): array {
+ return array_map(static fn (array $r): array => [
+ 'employee' => $r['employee_uid'], 'from' => day($r['valid_from']), 'weekdays' => $r['weekdays'], 'percent' => $r['percent'] === null ? null : (int)$r['percent'],
+ ], rows($db, $table, ['employee_uid', 'valid_from', 'weekdays', 'percent']));
+}
+
+/** @param array $before */
+function verify(IDBConnection $db, array $before): int {
+ $after = snapshot($db);
+ $failures = 0;
+ $check = static function (bool $ok, string $what, mixed $expected = null, mixed $got = null) use (&$failures): void {
+ echo ($ok ? 'ok ' : 'FAIL ') . $what . "\n";
+ if (!$ok) {
+ echo ' expected: ' . json_encode($expected) . "\n got: " . json_encode($got) . "\n";
+ $failures++;
+ }
+ };
+
+ foreach ($before['kept'] as $section => $data) {
+ $check($data === ($after[$section] ?? null), "$section: the same before and after", $data, $after[$section] ?? null);
+ }
+
+ foreach (['absence_entitlement_events', 'absence_contract_periods', 'absence_employee_history'] as $old) {
+ $check(!$db->tableExists($old), "$old: gone, its rows are under the new name");
+ }
+
+ // A contract HR entered stays; somebody without one gets their own weekdays as contract
+ $contracts = contracts($db, 'absence_periods');
+ $expected = $before['contracts'];
+ $withContract = array_column($before['contracts'], 'employee');
+ foreach ($before['weekdays'] as $uid => $stored) {
+ $days = weekdays($stored);
+ if ($days !== null && !in_array($uid, $withContract, true)) {
+ $expected[] = ['employee' => $uid, 'weekdays' => $days, 'percent' => null];
+ }
+ }
+ $comparable = static function (array $rows): array {
+ $rows = array_map(static fn (array $r): string => $r['employee'] . ' ' . $r['weekdays'] . ' ' . json_encode($r['percent']) . (isset($r['from']) ? ' ' . $r['from'] : ''), $rows);
+ sort($rows);
+ return $rows;
+ };
+ // The date of a converted setting is the upgrade's choice, so it is left out of the comparison
+ $known = array_column($before['contracts'], 'employee');
+ $got = array_map(static fn (array $r): array => in_array($r['employee'], $known, true) ? $r : ['employee' => $r['employee'], 'weekdays' => $r['weekdays'], 'percent' => $r['percent']], $contracts);
+ $check($comparable($expected) === $comparable($got), 'contracts: the ones HR entered, plus one for each own weekdays setting', $comparable($expected), $comparable($got));
+ $check(personal('work_weekdays') === [], 'own weekdays settings: all taken over or dropped', [], personal('work_weekdays'));
+
+ $appConfig = Server::get(IAppConfig::class);
+ $hrGroup = $before['hrGroup'] ?? ($before['hrGroupExists'] ? 'hr' : '');
+ $check($appConfig->getValueString(APP, 'hr_group') === $hrGroup, "HR group: still \"$hrGroup\"", $hrGroup, $appConfig->getValueString(APP, 'hr_group'));
+
+ $wrong = [];
+ foreach (rows($db, 'absence_comments', ['id', 'request_id', 'confidential']) as $i => $row) {
+ if (flag($row['confidential']) !== $after['comments'][$i]['onConfidentialAbsence']) {
+ $wrong[] = (int)$row['id'];
+ }
+ }
+ $check($wrong === [], 'comments: marked confidential exactly on confidential absences', [], $wrong);
+
+ // Without a new version number Nextcloud runs no migration at all
+ $apps = Server::get(IAppManager::class);
+ $steps = array_map(static fn (string $file): string => substr(basename($file, '.php'), strlen('Version')), glob($apps->getAppPath(APP) . '/lib/Migration/Version*.php') ?: []);
+ $qb = $db->getQueryBuilder();
+ $result = $qb->select('version')->from('migrations')->where($qb->expr()->eq('app', $qb->createNamedParameter(APP)))->executeQuery();
+ $ran = $result->fetchFirstColumn();
+ $result->closeCursor();
+ $check(array_values(array_diff($steps, $ran)) === [], 'migrations: every step of the code ran', [], array_values(array_diff($steps, $ran)));
+
+ $installed = $appConfig->getValueString(APP, 'installed_version');
+ $code = $apps->getAppVersion(APP, false);
+ $check($installed === $code, "installed version: the code's ($code)", $code, $installed);
+
+ echo $failures === 0 ? "upgrade check passed\n" : "$failures check(s) failed\n";
+ return $failures === 0 ? 0 : 1;
+}