-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat: keep a classname mapping instead of always storing the full name for shares #62904
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
icewind1991
wants to merge
2
commits into
master
Choose a base branch
from
unified-sharing-classmap
base: master
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.
+537
−148
Open
Changes from all commits
Commits
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
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
105 changes: 105 additions & 0 deletions
105
apps/sharing/lib/Migration/Version1000Date20260731171922.php
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,105 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Sharing\Migration; | ||
|
|
||
| use Closure; | ||
| use Doctrine\DBAL\Schema\SchemaException; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\Attributes\AddColumn; | ||
| use OCP\Migration\Attributes\AddIndex; | ||
| use OCP\Migration\Attributes\CreateTable; | ||
| use OCP\Migration\Attributes\DropColumn; | ||
| use OCP\Migration\Attributes\DropIndex; | ||
| use OCP\Migration\Attributes\IndexType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
|
|
||
| #[CreateTable(table: 'sharing_classmap')] | ||
| #[DropColumn(table: 'sharing_share_sources', name: 'source_class')] | ||
| #[DropColumn(table: 'sharing_share_recipients', name: 'recipient_class')] | ||
| #[DropColumn(table: 'sharing_share_properties', name: 'property_class')] | ||
| #[DropColumn(table: 'sharing_share_permissions', name: 'permission_class')] | ||
| #[AddColumn(table: 'sharing_share_sources', name: 'source_class_id')] | ||
| #[AddColumn(table: 'sharing_share_recipients', name: 'recipient_class_id')] | ||
| #[AddColumn(table: 'sharing_share_properties', name: 'property_class_id')] | ||
| #[AddColumn(table: 'sharing_share_permissions', name: 'permission_class_id')] | ||
| #[DropIndex(table: 'sharing_share_sources', type: IndexType::PRIMARY)] | ||
| #[DropIndex(table: 'sharing_share_recipients', type: IndexType::PRIMARY)] | ||
| #[DropIndex(table: 'sharing_share_properties', type: IndexType::PRIMARY)] | ||
| #[DropIndex(table: 'sharing_share_permissions', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'sharing_share_sources', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'sharing_share_recipients', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'sharing_share_properties', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'sharing_share_permissions', type: IndexType::PRIMARY)] | ||
| final class Version1000Date20260731171922 extends SimpleMigrationStep { | ||
| /** | ||
| * @param Closure():ISchemaWrapper $schemaClosure | ||
| * @throws SchemaException | ||
| */ | ||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if (!$schema->hasTable('sharing_classmap')) { | ||
| $table = $schema->createTable('sharing_classmap'); | ||
| $table->addColumn('class_id', Types::INTEGER, [ | ||
| 'autoincrement' => true, | ||
| 'notnull' => true, | ||
| ]); | ||
| $table->addColumn('class_name', Types::STRING, ['length' => 64]); | ||
| $table->setPrimaryKey(['class_id']); | ||
| $table->addUniqueIndex(['class_name']); | ||
| } | ||
|
|
||
| $sourcesTable = $schema->getTable('sharing_share_sources'); | ||
| if ($sourcesTable->hasColumn('source_class')) { | ||
| $sourcesTable->dropColumn('source_class'); | ||
| $sourcesTable->addColumn('source_class_id', Types::INTEGER); | ||
| $sourcesTable->dropPrimaryKey(); | ||
| $sourcesTable->setPrimaryKey(['share_id', 'source_class_id', 'source_value']); | ||
| $sourcesTable->addForeignKeyConstraint('sharing_classmap', ['source_class_id'], ['class_id']); | ||
| } | ||
|
|
||
| $recipientsTable = $schema->getTable('sharing_share_recipients'); | ||
| if ($recipientsTable->hasColumn('recipient_class')) { | ||
| $recipientsTable->dropColumn('recipient_class'); | ||
| $recipientsTable->addColumn('recipient_class_id', Types::INTEGER); | ||
| $recipientsTable->dropPrimaryKey(); | ||
| $recipientsTable->setPrimaryKey(['share_id', 'recipient_class_id', 'recipient_value']); | ||
| $recipientsTable->addForeignKeyConstraint('sharing_classmap', ['recipient_class_id'], ['class_id']); | ||
| } | ||
|
|
||
| $propertiesTable = $schema->getTable('sharing_share_properties'); | ||
| if ($propertiesTable->hasColumn('property_class')) { | ||
| $propertiesTable->dropColumn('property_class'); | ||
| $propertiesTable->addColumn('property_class_id', Types::INTEGER); | ||
| $propertiesTable->dropPrimaryKey(); | ||
| $propertiesTable->setPrimaryKey(['share_id', 'property_class_id']); | ||
| $propertiesTable->addForeignKeyConstraint('sharing_classmap', ['property_class_id'], ['class_id']); | ||
| } | ||
|
|
||
| $permissionsTable = $schema->getTable('sharing_share_permissions'); | ||
| if ($permissionsTable->hasColumn('permission_class')) { | ||
| $permissionsTable->dropColumn('permission_class'); | ||
| $permissionsTable->addColumn('permission_class_id', Types::INTEGER); | ||
| $permissionsTable->dropPrimaryKey(); | ||
| $permissionsTable->setPrimaryKey(['share_id', 'permission_class_id']); | ||
| $permissionsTable->addForeignKeyConstraint('sharing_classmap', ['permission_class_id'], ['class_id']); | ||
| } | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
| } | ||
| } | ||
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,156 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Sharing; | ||
|
|
||
| use OCP\DB\Exception; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IDBConnection; | ||
|
|
||
| final class ClassMapper { | ||
| /** @var array<int, class-string> $map */ | ||
| private array $map = []; | ||
|
|
||
| /** @var array<class-string, int> */ | ||
| private array $reverseMap = []; | ||
|
|
||
| private bool $loaded = false; | ||
|
|
||
| public function __construct( | ||
| private readonly IDBConnection $connection, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array{class_id: int|string, class_name: class-string} $row | ||
| */ | ||
| private function insertRow(array $row): void { | ||
| $id = (int)$row['class_id']; | ||
| $class = $row['class_name']; | ||
| $this->map[$id] = $class; | ||
| $this->reverseMap[$class] = $id; | ||
| } | ||
|
|
||
| private function loadFromDb(): void { | ||
| if ($this->loaded) { | ||
| return; | ||
| } | ||
|
|
||
| $query = $this->connection->getTypedQueryBuilder(); | ||
| $query->selectColumns('class_id', 'class_name') | ||
| ->from('sharing_classmap'); | ||
| $rows = $query->executeQuery()->fetchAll(); | ||
|
|
||
| foreach ($rows as $row) { | ||
| /** @var array{class_id: int|string, class_name: class-string} $row */ | ||
| $this->insertRow($row); | ||
| } | ||
|
|
||
| $this->loaded = true; | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string $className | ||
| */ | ||
| private function loadFromDbByName(string $className): ?int { | ||
| $query = $this->connection->getTypedQueryBuilder(); | ||
| $query->selectColumns('class_id', 'class_name') | ||
| ->from('sharing_classmap') | ||
| ->where($query->expr()->eq('class_name', $query->createNamedParameter($className))); | ||
| $row = $query->executeQuery()->fetchAssociative(); | ||
|
|
||
| if ($row !== false) { | ||
| /** @var array{class_id: int|string, class_name: class-string} $row */ | ||
| $this->insertRow($row); | ||
| return (int)$row['class_id']; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @return class-string|null | ||
| */ | ||
| private function loadFromDbById(int $id): ?string { | ||
| $query = $this->connection->getTypedQueryBuilder(); | ||
| $query->selectColumns('class_id', 'class_name') | ||
| ->from('sharing_classmap') | ||
| ->where($query->expr()->eq('class_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); | ||
| $row = $query->executeQuery()->fetchAssociative(); | ||
|
|
||
| if ($row !== false) { | ||
| /** @var array{class_id: int|string, class_name: class-string} $row */ | ||
| $this->insertRow($row); | ||
| return $row['class_name']; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string $className | ||
| */ | ||
| private function insert(string $className): int { | ||
| $id = $this->loadFromDbByName($className); | ||
| if ($id !== null) { | ||
| return $id; | ||
| } | ||
|
|
||
| $query = $this->connection->getTypedQueryBuilder(); | ||
| $query->insert('sharing_classmap') | ||
| ->values([ | ||
| 'class_name' => $query->createNamedParameter($className) | ||
| ]); | ||
| try { | ||
| $query->executeStatement(); | ||
| $id = $query->getLastInsertId(); | ||
| $this->map[$id] = $className; | ||
| $this->reverseMap[$className] = $id; | ||
| return $id; | ||
| } catch (Exception $exception) { | ||
| // handle concurrent inserts | ||
| if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { | ||
| $id = $this->loadFromDbByName($className); | ||
| if ($id === null) { | ||
| throw new \Exception(sprintf("Failed to insert '%s' into sharing_classmap, duplicate on insert but can't fetch it either", $className), $exception->getCode(), $exception); | ||
| } | ||
|
|
||
| return $id; | ||
| } | ||
|
|
||
| throw $exception; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string $class | ||
| */ | ||
| public function getClassId(string $class): int { | ||
| $this->loadFromDb(); | ||
|
|
||
| return $this->reverseMap[$class] ?? $this->insert($class); | ||
| } | ||
|
|
||
| /** | ||
| * @return class-string | ||
| */ | ||
| public function getClassName(int $id): string { | ||
| $this->loadFromDb(); | ||
| if (isset($this->map[$id])) { | ||
| return $this->map[$id]; | ||
| } | ||
|
|
||
| $class = $this->loadFromDbById($id); | ||
| if ($class) { | ||
| return $class; | ||
| } | ||
|
|
||
| throw new \Exception(sprintf("Unknown mapped class '%d'", $id)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.