Skip to content

Commit

Permalink
fix(import): entity & errors
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
bouttier committed Feb 22, 2024
1 parent e3c63b4 commit 395e5bd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
5 changes: 3 additions & 2 deletions backend/geonature/core/imports/checks/dataframe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
},
Expand Down
6 changes: 5 additions & 1 deletion backend/geonature/core/imports/checks/sql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# revision identifiers, used by Alembic.
revision = "bfc90691737d"
down_revision = "2b0b3bd0248c"
down_revision = "02e9b8758709"
branch_labels = None
depends_on = None

Expand All @@ -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

0 comments on commit 395e5bd

Please sign in to comment.