Skip to content
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

[stable26] Migrate to the new file_metadata column layout #1736

Merged
merged 4 commits into from
Apr 12, 2023
Merged
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8",
"nextcloud/coding-standard": "1.0",
"nextcloud/ocp": "dev-master",
"nextcloud/ocp": "dev-stable26",
"phpunit/phpunit": "^9",
"sabre/dav": "^4.2.1",
"vimeo/psalm": "^4.22"
},
"require": {
"hexogen/kdtree": "^0.2.5"
}
}
}
64 changes: 7 additions & 57 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions lib/DB/Place/PlaceMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use OCP\IDBConnection;

class PlaceMapper {
public const METADATA_TYPE = 'photos_place';
public const METADATA_GROUP = 'photos_place';

public function __construct(
private IDBConnection $connection,
Expand All @@ -53,16 +53,16 @@ public function findPlacesForUser(string $userId): array {

$qb = $this->connection->getQueryBuilder();

$rows = $qb->selectDistinct('meta.metadata')
$rows = $qb->selectDistinct('meta.value')
->from('file_metadata', 'meta')
->join('meta', 'filecache', 'file', $qb->expr()->eq('file.fileid', 'meta.id', IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq('file.storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('file.mimepart', $qb->createNamedParameter($mimepart, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_GROUP)))
->executeQuery()
->fetchAll();

return array_map(fn ($row) => new PlaceInfo($userId, $row['metadata']), $rows);
return array_map(fn ($row) => new PlaceInfo($userId, $row['value']), $rows);
}

/** @return PlaceInfo */
Expand All @@ -76,21 +76,21 @@ public function findPlaceForUser(string $userId, string $place): PlaceInfo {

$qb = $this->connection->getQueryBuilder();

$rows = $qb->selectDistinct('meta.metadata')
$rows = $qb->selectDistinct('meta.value')
->from('file_metadata', 'meta')
->join('meta', 'filecache', 'file', $qb->expr()->eq('file.fileid', 'meta.id', IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq('file.storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('file.mimepart', $qb->createNamedParameter($mimepart, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('meta.metadata', $qb->createNamedParameter($place)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_GROUP)))
->andWhere($qb->expr()->eq('meta.value', $qb->createNamedParameter($place)))
->executeQuery()
->fetchAll();

if (count($rows) !== 1) {
throw new NotFoundException();
}

return new PlaceInfo($userId, $rows[0]['metadata']);
return new PlaceInfo($userId, $rows[0]['value']);
}

/** @return PlaceFile[] */
Expand All @@ -104,13 +104,13 @@ public function findFilesForUserAndPlace(string $userId, string $place) {

$qb = $this->connection->getQueryBuilder();

$rows = $qb->select('file.fileid', 'file.name', 'file.mimetype', 'file.size', 'file.mtime', 'file.etag', 'meta.metadata')
$rows = $qb->select('file.fileid', 'file.name', 'file.mimetype', 'file.size', 'file.mtime', 'file.etag', 'meta.value')
->from('file_metadata', 'meta')
->join('meta', 'filecache', 'file', $qb->expr()->eq('file.fileid', 'meta.id', IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq('file.storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('file.mimepart', $qb->createNamedParameter($mimepart, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('meta.metadata', $qb->createNamedParameter($place)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_GROUP)))
->andWhere($qb->expr()->eq('meta.value', $qb->createNamedParameter($place)))
->executeQuery()
->fetchAll();

Expand All @@ -122,7 +122,7 @@ public function findFilesForUserAndPlace(string $userId, string $place) {
(int)$row['size'],
(int)$row['mtime'],
$row['etag'],
$row['metadata']
$row['value']
),
$rows,
);
Expand All @@ -138,15 +138,15 @@ public function findFileForUserAndPlace(string $userId, string $place, string $f

$qb = $this->connection->getQueryBuilder();

$rows = $qb->select('file.fileid', 'file.name', 'file.mimetype', 'file.size', 'file.mtime', 'file.etag', 'meta.metadata')
$rows = $qb->select('file.fileid', 'file.name', 'file.mimetype', 'file.size', 'file.mtime', 'file.etag', 'meta.value')
->from('file_metadata', 'meta')
->join('meta', 'filecache', 'file', $qb->expr()->eq('file.fileid', 'meta.id', IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq('file.storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('file.mimepart', $qb->createNamedParameter($mimepart, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('file.fileid', $qb->createNamedParameter($fileId)))
->andWhere($qb->expr()->eq('file.name', $qb->createNamedParameter($fileName)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('meta.metadata', $qb->createNamedParameter($place)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_GROUP)))
->andWhere($qb->expr()->eq('meta.value', $qb->createNamedParameter($place)))
->executeQuery()
->fetchAll();

Expand All @@ -161,7 +161,7 @@ public function findFileForUserAndPlace(string $userId, string $place, string $f
(int)$rows[0]['size'],
(int)$rows[0]['mtime'],
$rows[0]['etag'],
$rows[0]['metadata']
$rows[0]['value']
);
}

Expand All @@ -171,8 +171,8 @@ public function setPlaceForFile(string $place, int $fileId): void {
$query->insert('file_metadata')
->values([
"id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
"group_name" => $query->createNamedParameter(self::METADATA_TYPE),
"metadata" => $query->createNamedParameter($place),
"group_name" => $query->createNamedParameter(self::METADATA_GROUP),
"value" => $query->createNamedParameter($place),
])
->executeStatement();
} catch (\Exception $ex) {
Expand All @@ -185,9 +185,9 @@ public function setPlaceForFile(string $place, int $fileId): void {
public function updatePlaceForFile(string $place, int $fileId): void {
$query = $this->connection->getQueryBuilder();
$query->update('file_metadata')
->set("metadata", $query->createNamedParameter($place))
->set("value", $query->createNamedParameter($place))
->where($query->expr()->eq('id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('group_name', $query->createNamedParameter(self::METADATA_TYPE)))
->andWhere($query->expr()->eq('group_name', $query->createNamedParameter(self::METADATA_GROUP)))
->executeStatement();
}
}
2 changes: 1 addition & 1 deletion lib/Sabre/PropFindPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function propFind(PropFind $propFind, INode $node): void {
$sizeMetadata = $this->metadataManager->fetchMetadataFor('size', [$node->getFile()->getFileId()])[$node->getFile()->getFileId()];
}

return json_encode((object)$sizeMetadata->getMetadata());
return $sizeMetadata->getValue();
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/MediaPlaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function updatePlaceForFile(int $fileId): void {

private function getPlaceForFile(int $fileId): ?string {
$gpsMetadata = $this->metadataManager->fetchMetadataFor('gps', [$fileId])[$fileId];
$metadata = $gpsMetadata->getMetadata();
$metadata = $gpsMetadata->getDecodedValue();

if (count($metadata) === 0) {
return null;
Expand Down
11 changes: 9 additions & 2 deletions tests/stub.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,18 @@ use OCP\DB\Types;
/**
* @method string getGroupName()
* @method void setGroupName(string $groupName)
* @method array getMetadata()
* @method void setMetadata(array $metadata)
* @method string getValue()
* @method void setValue(string $value)
* @see \OC\Core\Migrations\Version240000Date20220404230027
*/
class FileMetadata extends Entity {
public function getDecodedValue(): array {
return json_decode($this->getValue(), true) ?? [];
}

public function setArrayAsValue(array $value): void {
$this->setValue(json_encode($value, JSON_THROW_ON_ERROR));
}
}

}
Expand Down