Skip to content

Commit

Permalink
BUGFIX: Fix Workspace Metadata upsert
Browse files Browse the repository at this point in the history
adjusts `WorkspaceMetadataAndRoleRepository::updateWorkspaceMetadata()` so that it does not fail with

> Failed to update metadata for workspace "workspace-name" (Content Repository "default"): An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'default-workspace-name' for key 'PRIMARY'

Background:

Doctrine DBAL has no support for "upserts" built-in and the [suggested work around](doctrine/dbal#1320 (comment)) had a flaw that lead to the above exception when the incoming data wasn't actually changed (because `$affectedRows` is 0 in this case).
This is fixed simply by issuing a `SELECT` first
  • Loading branch information
bwaidelich committed Dec 5, 2024
1 parent 2d735b2 commit 72cefb1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,30 @@ public function updateWorkspaceMetadata(ContentRepositoryId $contentRepositoryId
$data = array_filter([
'title' => $title,
'description' => $description,
], fn ($value) => $value !== null);
], static fn ($value) => $value !== null);

$table = self::TABLE_NAME_WORKSPACE_METADATA;
$query = <<<SQL
SELECT
content_repository_id
FROM
{$table}
WHERE
content_repository_id = :contentRepositoryId
AND workspace_name = :workspaceName
SQL;
try {
$affectedRows = $this->dbal->update(self::TABLE_NAME_WORKSPACE_METADATA, $data, [
'content_repository_id' => $contentRepositoryId->value,
'workspace_name' => $workspace->workspaceName->value,
]);
if ($affectedRows === 0) {
$this->dbal->insert(self::TABLE_NAME_WORKSPACE_METADATA, [
$rowExists = $this->dbal->fetchOne($query, [
'contentRepositoryId' => $contentRepositoryId->value,
'workspaceName' => $workspace->workspaceName->value,
]) !== false;
if ($rowExists) {
$this->dbal->update($table, $data, [
'content_repository_id' => $contentRepositoryId->value,
'workspace_name' => $workspace->workspaceName->value,
]);
} else {
$this->dbal->insert($table, [
'content_repository_id' => $contentRepositoryId->value,
'workspace_name' => $workspace->workspaceName->value,
'description' => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Feature: Neos WorkspaceService related features
| Title | Description | Classification | Owner user id |
| Some new workspace title | | ROOT | |

Scenario: Change title of root workspace to the same value
When the root workspace "some-root-workspace" with title "some title" and description "some description" is created
And the title of workspace "some-root-workspace" is set to "some title"
Then the workspace "some-root-workspace" should have the following metadata:
| Title | Description | Classification | Owner user id |
| some title | some description | ROOT | |

Scenario: Set title of root workspace without metadata
When a root workspace "some-root-workspace" exists without metadata
And the title of workspace "some-root-workspace" is set to "Some new workspace title"
Expand All @@ -66,6 +73,13 @@ Feature: Neos WorkspaceService related features
| Title | Description | Classification | Owner user id |
| some-root-workspace | Some new workspace description | ROOT | |

Scenario: Change description of root workspace to the same value
When the root workspace "some-root-workspace" with title "some title" and description "some description" is created
And the description of workspace "some-root-workspace" is set to "some description"
Then the workspace "some-root-workspace" should have the following metadata:
| Title | Description | Classification | Owner user id |
| some title | some description | ROOT | |

Scenario: Change description of root workspace without metadata
When a root workspace "some-root-workspace" exists without metadata
And the description of workspace "some-root-workspace" is set to "Some new workspace description"
Expand Down

0 comments on commit 72cefb1

Please sign in to comment.