Skip to content

Commit d19d1e2

Browse files
committed
refactor(systemtags): Port to snowflake ids
Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
1 parent 0fb67af commit d19d1e2

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

apps/systemtags/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => $baseDir . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
2222
'OCA\\SystemTags\\Migration\\Version31000Date20241018063111' => $baseDir . '/../lib/Migration/Version31000Date20241018063111.php',
2323
'OCA\\SystemTags\\Migration\\Version31000Date20241114171300' => $baseDir . '/../lib/Migration/Version31000Date20241114171300.php',
24+
'OCA\\SystemTags\\Migration\\Version33000Date20251104171300' => $baseDir . '/../lib/Migration/Version33000Date20251104171300.php',
2425
'OCA\\SystemTags\\Search\\TagSearchProvider' => $baseDir . '/../lib/Search/TagSearchProvider.php',
2526
'OCA\\SystemTags\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
2627
);

apps/systemtags/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ComposerStaticInitSystemTags
3636
'OCA\\SystemTags\\Listeners\\LoadAdditionalScriptsListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadAdditionalScriptsListener.php',
3737
'OCA\\SystemTags\\Migration\\Version31000Date20241018063111' => __DIR__ . '/..' . '/../lib/Migration/Version31000Date20241018063111.php',
3838
'OCA\\SystemTags\\Migration\\Version31000Date20241114171300' => __DIR__ . '/..' . '/../lib/Migration/Version31000Date20241114171300.php',
39+
'OCA\\SystemTags\\Migration\\Version33000Date20251104171300' => __DIR__ . '/..' . '/../lib/Migration/Version33000Date20251104171300.php',
3940
'OCA\\SystemTags\\Search\\TagSearchProvider' => __DIR__ . '/..' . '/../lib/Search/TagSearchProvider.php',
4041
'OCA\\SystemTags\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
4142
);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\SystemTags\Migration;
11+
12+
use Closure;
13+
use Doctrine\DBAL\Types\Types;
14+
use OCP\DB\ISchemaWrapper;
15+
use OCP\Migration\Attributes\AddColumn;
16+
use OCP\Migration\Attributes\ColumnType;
17+
use OCP\Migration\Attributes\ModifyColumn;
18+
use OCP\Migration\IOutput;
19+
use OCP\Migration\SimpleMigrationStep;
20+
21+
/**
22+
* Remove auto-increment to use snowflake ids
23+
*/
24+
#[ModifyColumn(table: 'systemtag', name: 'id', type: ColumnType::BIGINT, description: 'Remove auto-increment')]
25+
class Version33000Date20251104171300 extends SimpleMigrationStep {
26+
27+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
28+
/** @var ISchemaWrapper $schema */
29+
$schema = $schemaClosure();
30+
31+
if ($schema->hasTable('systemtag')) {
32+
$schema->dropAutoincrementColumn('systemtag', 'id');
33+
}
34+
35+
return $schema;
36+
}
37+
}

lib/private/SystemTag/SystemTagManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCP\IGroupManager;
1717
use OCP\IUser;
1818
use OCP\IUserSession;
19+
use OCP\Snowflake\IGenerator;
1920
use OCP\SystemTag\ISystemTag;
2021
use OCP\SystemTag\ISystemTagManager;
2122
use OCP\SystemTag\ManagerEvent;
@@ -42,6 +43,7 @@ public function __construct(
4243
protected IEventDispatcher $dispatcher,
4344
private IUserSession $userSession,
4445
private IAppConfig $appConfig,
46+
private IGenerator $generator,
4547
) {
4648
$query = $this->connection->getQueryBuilder();
4749
$this->selectTagQuery = $query->select('*')
@@ -167,8 +169,10 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab
167169
// Length of name column is 64
168170
$truncatedTagName = substr($tagName, 0, 64);
169171
$query = $this->connection->getQueryBuilder();
172+
$tagId = $this->generator->nextId();
170173
$query->insert(self::TAG_TABLE)
171174
->values([
175+
'id' => $query->createNamedParameter($tagId),
172176
'name' => $query->createNamedParameter($truncatedTagName),
173177
'visibility' => $query->createNamedParameter($userVisible ? 1 : 0),
174178
'editable' => $query->createNamedParameter($userAssignable ? 1 : 0),
@@ -188,10 +192,8 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab
188192
throw $e;
189193
}
190194

191-
$tagId = $query->getLastInsertId();
192-
193195
$tag = new SystemTag(
194-
(string)$tagId,
196+
$tagId,
195197
$truncatedTagName,
196198
$userVisible,
197199
$userAssignable

tests/lib/SystemTag/SystemTagManagerTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\IUser;
1818
use OCP\IUserSession;
1919
use OCP\Server;
20+
use OCP\Snowflake\IGenerator;
2021
use OCP\SystemTag\ISystemTag;
2122
use OCP\SystemTag\ISystemTagManager;
2223
use OCP\SystemTag\TagAlreadyExistsException;
@@ -36,6 +37,7 @@ class SystemTagManagerTest extends TestCase {
3637
private IUserSession $userSession;
3738
private IAppConfig $appConfig;
3839
private IEventDispatcher $dispatcher;
40+
private IGenerator $generator;
3941

4042
protected function setUp(): void {
4143
parent::setUp();
@@ -46,13 +48,15 @@ protected function setUp(): void {
4648
$this->groupManager = $this->createMock(IGroupManager::class);
4749
$this->userSession = $this->createMock(IUserSession::class);
4850
$this->appConfig = $this->createMock(IAppConfig::class);
51+
$this->generator = Server::get(IGenerator::class);
4952

5053
$this->tagManager = new SystemTagManager(
5154
$this->connection,
5255
$this->groupManager,
5356
$this->dispatcher,
5457
$this->userSession,
5558
$this->appConfig,
59+
$this->generator,
5660
);
5761
$this->pruneTagsTables();
5862
}
@@ -296,8 +300,8 @@ public static function updateTagProvider(): array {
296300
return [
297301
[
298302
// update name
299-
['one', true, true, '0082c9'],
300-
['two', true, true, '0082c9']
303+
['one', true, true],
304+
['two', true, true]
301305
],
302306
[
303307
// update one flag
@@ -323,7 +327,6 @@ public function testUpdateTag($tagCreate, $tagUpdated): void {
323327
$tagCreate[0],
324328
$tagCreate[1],
325329
$tagCreate[2],
326-
$tagCreate[3],
327330
);
328331
$this->tagManager->updateTag(
329332
$tag1->getId(),
@@ -336,7 +339,6 @@ public function testUpdateTag($tagCreate, $tagUpdated): void {
336339
$tagUpdated[0],
337340
$tagUpdated[1],
338341
$tagUpdated[2],
339-
$tagUpdated[3],
340342
);
341343

342344
$this->assertEquals($tag2->getId(), $tag1->getId());

0 commit comments

Comments
 (0)