From 395e5bd6a35ef21711b4fd5d7b8ab68e094e4a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Thu, 22 Feb 2024 14:58:33 +0100 Subject: [PATCH] fix(import): entity & errors - small fix regarding report of entity in sql checks - replace unique constraint on (id_import, id_error, column_error) by two partial unique indexes: - (id_import, id_error, column_error) WHEN id_entity IS NULL - (id_import, id_entity, id_error, column_error) WHEN id_entity IS NOT NULL --- .../core/imports/checks/dataframe/utils.py | 5 ++- .../core/imports/checks/sql/utils.py | 6 ++- ...90691737d_add_id_entity_in_users_errors.py | 41 +++++++++++++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/backend/geonature/core/imports/checks/dataframe/utils.py b/backend/geonature/core/imports/checks/dataframe/utils.py index 54a05ff406..f9944e28b6 100644 --- a/backend/geonature/core/imports/checks/dataframe/utils.py +++ b/backend/geonature/core/imports/checks/dataframe/utils.py @@ -62,14 +62,15 @@ def report_error(imprt, entity, df, error): { "id_import": imprt.id_import, "id_error": error_type.pk, - "id_entity": entity.id_entity if entity else None, + "id_entity": entity.id_entity, "column_error": column, "id_rows": ordered_invalid_rows, "comment": error.get("comment"), } ) stmt = stmt.on_conflict_do_update( - constraint="t_user_errors_un", # unique (import, error_type, column) + index_elements=("id_import", "id_entity", "id_error", "column_error"), + index_where=ImportUserError.id_entity.isnot(None), set_={ "id_rows": func.array_cat(ImportUserError.rows, stmt.excluded["id_rows"]), }, diff --git a/backend/geonature/core/imports/checks/sql/utils.py b/backend/geonature/core/imports/checks/sql/utils.py index 2dd675b632..7e4be98bd2 100644 --- a/backend/geonature/core/imports/checks/sql/utils.py +++ b/backend/geonature/core/imports/checks/sql/utils.py @@ -82,7 +82,11 @@ def report_erroneous_rows(imprt, entity, error_type, error_column, whereclause): } if entity is not None: - ImportUserError.id_entity: literal(entity.id_entity).label("id_entity") + insert_args.update( + { + ImportUserError.id_entity: literal(entity.id_entity).label("id_entity"), + } + ) # Create the final insert statement error_select = select(insert_args.values()).alias("error") diff --git a/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py b/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py index a455111918..1a309f1092 100644 --- a/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py +++ b/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py @@ -12,7 +12,7 @@ # revision identifiers, used by Alembic. revision = "bfc90691737d" -down_revision = "2b0b3bd0248c" +down_revision = "02e9b8758709" branch_labels = None depends_on = None @@ -37,14 +37,49 @@ def upgrade(): onupdate="CASCADE", ondelete="CASCADE", ) - pass + op.drop_constraint( + schema="gn_imports", + table_name="t_user_errors", + constraint_name="t_user_errors_un", + ) + op.create_index( + index_name="t_user_errors_un", + schema="gn_imports", + table_name="t_user_errors", + columns=("id_import", "id_error", "column_error"), + unique=True, + postgresql_where="id_entity IS NULL", + ) + op.create_index( + index_name="t_user_errors_entity_un", + schema="gn_imports", + table_name="t_user_errors", + columns=("id_import", "id_entity", "id_error", "column_error"), + unique=True, + postgresql_where="id_entity IS NOT NULL", + ) def downgrade(): + op.drop_index( + schema="gn_imports", + table_name="t_user_errors", + index_name="t_user_errors_entity_un", + ) + op.drop_index( + schema="gn_imports", + table_name="t_user_errors", + index_name="t_user_errors_un", + ) + op.create_unique_constraint( + constraint_name="t_user_errors_un", + schema="gn_imports", + table_name="t_user_errors", + columns=("id_import", "id_error", "column_error"), + ) op.drop_constraint( schema="gn_imports", table_name="t_user_errors", constraint_name="t_user_errors_id_entity_fkey", ) op.drop_column(schema="gn_imports", table_name="t_user_errors", column_name="id_entity") - pass