-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOP-16676] Use Postgres advisory locks to avoid insert conflicts
- Loading branch information
Showing
13 changed files
with
346 additions
and
179 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,49 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from sqlalchemy import select | ||
|
||
from data_rentgen.db.models.dataset_symlink import DatasetSymlink, DatasetSymlinkType | ||
from data_rentgen.db.repositories.base import Repository | ||
|
||
|
||
class DatasetSymlinkRepository(Repository[DatasetSymlink]): | ||
async def create_or_update( | ||
self, | ||
from_dataset_id: int, | ||
to_dataset_id: int, | ||
symlink_type: DatasetSymlinkType, | ||
) -> DatasetSymlink: | ||
result = await self._get(from_dataset_id, to_dataset_id) | ||
if not result: | ||
# try one more time, but with lock acquired. | ||
# if another worker already created the same row, just use it. if not - create with holding the lock. | ||
await self._lock(from_dataset_id, to_dataset_id) | ||
result = await self._get(from_dataset_id, to_dataset_id) | ||
|
||
if not result: | ||
return await self._create(from_dataset_id, to_dataset_id, symlink_type) | ||
return await self._update(result, symlink_type) | ||
|
||
async def _get(self, from_dataset_id: int, to_dataset_id: int) -> DatasetSymlink | None: | ||
query = select(DatasetSymlink).where( | ||
DatasetSymlink.from_dataset_id == from_dataset_id, | ||
DatasetSymlink.to_dataset_id == to_dataset_id, | ||
) | ||
return await self._session.scalar(query) | ||
|
||
async def _create( | ||
self, | ||
from_dataset_id: int, | ||
to_dataset_id: int, | ||
symlink_type: DatasetSymlinkType, | ||
) -> DatasetSymlink: | ||
result = DatasetSymlink(from_dataset_id=from_dataset_id, to_dataset_id=to_dataset_id, type=symlink_type) | ||
self._session.add(result) | ||
await self._session.flush([result]) | ||
return result | ||
|
||
async def _update(self, existing: DatasetSymlink, new_type: DatasetSymlinkType) -> DatasetSymlink: | ||
existing.type = new_type | ||
await self._session.flush([existing]) | ||
return existing |
This file contains 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 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
Oops, something went wrong.