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

[stable27] fix(mimetype): Fix aborted transaction on PostgreSQL when storing mimetype #40461

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 17 additions & 19 deletions lib/private/Files/Type/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,33 @@ public function reset() {
* @return int inserted ID
*/
protected function store($mimetype) {
$mimetypeId = $this->atomic(function () use ($mimetype) {
try {
try {
$mimetypeId = $this->atomic(function () use ($mimetype) {
$insert = $this->dbConnection->getQueryBuilder();
$insert->insert('mimetypes')
->values([
'mimetype' => $insert->createNamedParameter($mimetype)
])
->executeStatement();
return $insert->getLastInsertId();
} catch (DbalException $e) {
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}
$qb = $this->dbConnection->getQueryBuilder();
$qb->select('id')
->from('mimetypes')
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)));
$result = $qb->executeQuery();
$id = $result->fetchOne();
$result->closeCursor();
if ($id !== false) {
return (int) $id;
}
}, $this->dbConnection);
} catch (DbalException $e) {
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}

$qb = $this->dbConnection->getQueryBuilder();
$qb->select('id')
->from('mimetypes')
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)));
$result = $qb->executeQuery();
$id = $result->fetchOne();
$result->closeCursor();
if ($id === false) {
throw new \Exception("Database threw an unique constraint on inserting a new mimetype, but couldn't return the ID for this very mimetype");
}
}, $this->dbConnection);

if (!$mimetypeId) {
throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it");
$mimetypeId = (int) $id;
}

$this->mimetypes[$mimetypeId] = $mimetype;
Expand Down