Skip to content

Commit 91a7538

Browse files
authored
Merge pull request #40461 from nextcloud/backport/40203/stable27
[stable27] fix(mimetype): Fix aborted transaction on PostgreSQL when storing mimetype
2 parents 8045a6d + cfae699 commit 91a7538

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

lib/private/Files/Type/Loader.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,35 +116,33 @@ public function reset() {
116116
* @return int inserted ID
117117
*/
118118
protected function store($mimetype) {
119-
$mimetypeId = $this->atomic(function () use ($mimetype) {
120-
try {
119+
try {
120+
$mimetypeId = $this->atomic(function () use ($mimetype) {
121121
$insert = $this->dbConnection->getQueryBuilder();
122122
$insert->insert('mimetypes')
123123
->values([
124124
'mimetype' => $insert->createNamedParameter($mimetype)
125125
])
126126
->executeStatement();
127127
return $insert->getLastInsertId();
128-
} catch (DbalException $e) {
129-
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
130-
throw $e;
131-
}
132-
$qb = $this->dbConnection->getQueryBuilder();
133-
$qb->select('id')
134-
->from('mimetypes')
135-
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)));
136-
$result = $qb->executeQuery();
137-
$id = $result->fetchOne();
138-
$result->closeCursor();
139-
if ($id !== false) {
140-
return (int) $id;
141-
}
128+
}, $this->dbConnection);
129+
} catch (DbalException $e) {
130+
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
131+
throw $e;
132+
}
133+
134+
$qb = $this->dbConnection->getQueryBuilder();
135+
$qb->select('id')
136+
->from('mimetypes')
137+
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)));
138+
$result = $qb->executeQuery();
139+
$id = $result->fetchOne();
140+
$result->closeCursor();
141+
if ($id === false) {
142142
throw new \Exception("Database threw an unique constraint on inserting a new mimetype, but couldn't return the ID for this very mimetype");
143143
}
144-
}, $this->dbConnection);
145144

146-
if (!$mimetypeId) {
147-
throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it");
145+
$mimetypeId = (int) $id;
148146
}
149147

150148
$this->mimetypes[$mimetypeId] = $mimetype;

0 commit comments

Comments
 (0)