From 580f6fd9ae8c9fa734770f743ef78d930e9d73b9 Mon Sep 17 00:00:00 2001 From: Ruslan Babayev Date: Tue, 29 Oct 2024 18:16:32 +0300 Subject: [PATCH] Version 1.14.4 (urban_api 0.22.4) - fix add service to object - new migration (add cron job for deleting copy of urban objects) --- ...10-29_dbc310a03ce1_cron_delete_copy_uod.py | 46 +++++++++++++++++++ .../urban_api/logic/impl/helpers/services.py | 19 ++++---- idu_api/urban_api/version.py | 4 +- pyproject.toml | 4 +- 4 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 idu_api/common/db/migrator/versions/2024-10-29_dbc310a03ce1_cron_delete_copy_uod.py diff --git a/idu_api/common/db/migrator/versions/2024-10-29_dbc310a03ce1_cron_delete_copy_uod.py b/idu_api/common/db/migrator/versions/2024-10-29_dbc310a03ce1_cron_delete_copy_uod.py new file mode 100644 index 00000000..ac9e6c5c --- /dev/null +++ b/idu_api/common/db/migrator/versions/2024-10-29_dbc310a03ce1_cron_delete_copy_uod.py @@ -0,0 +1,46 @@ +# pylint: disable=no-member,invalid-name,missing-function-docstring,too-many-statements +"""cron delete copy uod + +Revision ID: dbc310a03ce1 +Revises: 2efcf46bb693 +Create Date: 2024-10-29 17:54:16.220414 + +""" +from textwrap import dedent +from typing import Sequence, Union + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "dbc310a03ce1" +down_revision: Union[str, None] = "21b28a319b43" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade(): + op.execute("CREATE EXTENSION IF NOT EXISTS pg_cron;") + delete_duplicates_sql = sa.text( + dedent( + """ + DO $$ + BEGIN + DELETE FROM urban_objects_data uo + WHERE uo.urban_object_id NOT IN ( + SELECT MIN(uo_inner.urban_object_id) + FROM urban_objects_data uo_inner + WHERE uo_inner.service_id IS NULL + GROUP BY uo_inner.physical_object_id, uo_inner.object_geometry_id + ) + AND uo.service_id IS NULL; + END $$; + """ + ) + ) + op.execute(sa.text(dedent(f"SELECT cron.schedule('0 */6 * * *', $$ {delete_duplicates_sql} $$);"))) + + +def downgrade(): + op.execute("SELECT cron.unschedule('0 0 * * *');") + op.execute("DROP EXTENSION IF EXISTS pg_cron;") diff --git a/idu_api/urban_api/logic/impl/helpers/services.py b/idu_api/urban_api/logic/impl/helpers/services.py index f3ad2c31..f1de8948 100644 --- a/idu_api/urban_api/logic/impl/helpers/services.py +++ b/idu_api/urban_api/logic/impl/helpers/services.py @@ -304,19 +304,22 @@ async def add_service_to_object_in_db( raise EntityNotFoundById(service_id, "service") flag = False + urban_object_id = None for urban_object in urban_objects: - if urban_object.service_id is None: - statement = ( - update(urban_objects_data) - .where(urban_objects_data.c.urban_object_id == urban_object.urban_object_id) - .values(service_id=service_id) - .returning(urban_objects_data.c.urban_object_id) - ) + if urban_object.service_id is None and not flag: flag = True + urban_object_id = urban_object.urban_object_id if urban_object.service_id == service_id: raise EntityAlreadyExists("urban object", physical_object_id, object_geometry_id, service_id) - if not flag: + if flag: + statement = ( + update(urban_objects_data) + .where(urban_objects_data.c.urban_object_id == urban_object_id) + .values(service_id=service_id) + .returning(urban_objects_data.c.urban_object_id) + ) + else: statement = ( insert(urban_objects_data) .values( diff --git a/idu_api/urban_api/version.py b/idu_api/urban_api/version.py index bd7207f0..b74e6e2a 100644 --- a/idu_api/urban_api/version.py +++ b/idu_api/urban_api/version.py @@ -1,4 +1,4 @@ """Version constants are defined here and should be updated on release.""" -VERSION = "0.22.3" -LAST_UPDATE = "2024-10-28" +VERSION = "0.22.4" +LAST_UPDATE = "2024-10-29" diff --git a/pyproject.toml b/pyproject.toml index b0b4c6ba..52eb8aa4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "idu-api" -version = "1.14.3" +version = "1.14.4" description = "This is a Digital Territories Platform API in two versions to access and manipulate basic territories data." authors = ["Babayev Ruslan "] readme = "README.md" @@ -57,7 +57,7 @@ target-version = ['py311'] [tool.pylint.format] max-line-length = 120 -expected-line-ending-format = "CRLF" +expected-line-ending-format = "LF" max-locals = 20 max-attributes = 10 max-args = 9