Repository navigation
feat(rows): add persistent UUIDv7 identifiers to rows #3229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
foolfitz
wants to merge
6
commits into
nextcloud:main
Choose a base branch
from
foolfitz:feat/3093-row-uuid-squashed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e38e28
feat(rows): store immutable UUIDv7 identities on row sleeves
foolfitz cf1587d
feat(rows): backfill UUIDs for existing rows in background batches
foolfitz 525445c
feat(rows): expose stored UUIDs in row responses
foolfitz 8ab689f
build: bump the app version to 2.5.0-dev.1
foolfitz 8af07e7
Merge branch 'main' into feat/3093-row-uuid-squashed
foolfitz 01a3567
Merge branch 'main' into feat/3093-row-uuid-squashed
foolfitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\BackgroundJob; | ||
|
|
||
| use OCA\Tables\AppInfo\Application; | ||
| use OCA\Tables\Vendor\Symfony\Component\Uid\Uuid; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\IJobList; | ||
| use OCP\BackgroundJob\TimedJob; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IAppConfig; | ||
| use OCP\IDBConnection; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class BackfillRowUuids extends TimedJob { | ||
|
|
||
| private const BATCH_SIZE = 500; | ||
| private const TIME_BUDGET = 5; | ||
| private const SQLITE_BATCH_PAUSE_MICROSECONDS = 10_000; | ||
| private const CURSOR_KEY = 'row_uuid_backfill_cursor'; | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $time, | ||
| private readonly IDBConnection $connection, | ||
| private readonly IAppConfig $config, | ||
| private readonly IJobList $jobList, | ||
| private readonly LoggerInterface $logger, | ||
| ) { | ||
| parent::__construct($time); | ||
| $this->setInterval(60); | ||
| $this->setAllowParallelRuns(false); | ||
| } | ||
|
|
||
| public function run(mixed $argument): void { | ||
| $deadline = $this->time->getTime() + self::TIME_BUDGET; | ||
| $cursor = $this->config->getValueInt(Application::APP_ID, self::CURSOR_KEY, 0, true); | ||
| $select = $this->connection->getQueryBuilder(); | ||
| $select->select('id')->from('tables_row_sleeves') | ||
| ->where($select->expr()->isNull('uuid')) | ||
| ->andWhere($select->expr()->gt('id', $select->createParameter('cursor'))) | ||
| ->orderBy('id', 'ASC')->setMaxResults(self::BATCH_SIZE); | ||
| $update = $this->connection->getQueryBuilder(); | ||
| $update->update('tables_row_sleeves')->set('uuid', $update->createParameter('uuid')) | ||
| ->where($update->expr()->eq('id', $update->createParameter('id'))) | ||
| ->andWhere($update->expr()->isNull('uuid')); | ||
|
|
||
| try { | ||
| do { | ||
| $select->setParameter('cursor', $cursor, IQueryBuilder::PARAM_INT); | ||
| $result = $select->executeQuery(); | ||
| $ids = array_map(intval(...), $result->fetchFirstColumn()); | ||
| $result->closeCursor(); | ||
| if ($ids === []) { | ||
| if ($cursor === 0) { | ||
| $this->jobList->remove(self::class); | ||
| $this->config->deleteKey(Application::APP_ID, self::CURSOR_KEY); | ||
| return; | ||
| } | ||
| $cursor = 0; | ||
| continue; | ||
| } | ||
|
|
||
| $cursor = $this->processBatch($ids, $update, $deadline); | ||
| if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE && $this->time->getTime() < $deadline) { | ||
| // Yield SQLite's single writer between committed batches. | ||
| usleep(self::SQLITE_BATCH_PAUSE_MICROSECONDS); | ||
| } | ||
| } while ($this->time->getTime() < $deadline); | ||
| // A lagging cursor only repeats a NULL scan, so one write per run spares the shared config cache. | ||
| $this->config->setValueInt(Application::APP_ID, self::CURSOR_KEY, $cursor, true); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->error('Row UUID backfill will retry on its next run.', ['exception' => $e]); | ||
| } | ||
| } | ||
|
|
||
| /** @param non-empty-list<int> $ids */ | ||
| private function processBatch(array $ids, IQueryBuilder $update, int $deadline): int { | ||
| $this->connection->beginTransaction(); | ||
| try { | ||
| foreach ($ids as $id) { | ||
| $update->setParameter('id', $id, IQueryBuilder::PARAM_INT); | ||
| $update->setParameter('uuid', Uuid::v7()->toRfc4122()); | ||
| $update->executeStatement(); | ||
| if ($this->time->getTime() >= $deadline) { | ||
| break; | ||
| } | ||
| } | ||
| $this->connection->commit(); | ||
| return $id; | ||
| } catch (\Throwable $e) { | ||
| $this->connection->rollBack(); | ||
| throw $e; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\Migration; | ||
|
|
||
| use Closure; | ||
| use OCA\Tables\BackgroundJob\BackfillRowUuids; | ||
| use OCP\BackgroundJob\IJobList; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
|
|
||
| class Version2500Date20261120000000 extends SimpleMigrationStep { | ||
| public function __construct( | ||
| private readonly IJobList $jobList, | ||
| ) { | ||
| } | ||
|
|
||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
| $table = $schema->getTable('tables_row_sleeves'); | ||
| if (!$table->hasColumn('uuid')) { | ||
| $table->addColumn('uuid', Types::STRING, [ | ||
| 'notnull' => false, | ||
| 'default' => null, | ||
| 'length' => 36, | ||
| ]); | ||
| } | ||
| if (!$table->hasIndex('tables_row_uuid_uniq')) { | ||
| $table->addUniqueIndex(['uuid'], 'tables_row_uuid_uniq'); | ||
| } | ||
| return $schema; | ||
| } | ||
|
|
||
| public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
| $this->jobList->add(BackfillRowUuids::class); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why only v7 UUIDs accepted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can be sorted while still being random. Other versions cannot guarantee that.