Skip to content

Commit ab798af

Browse files
hf-kkleinKonstantin
andauthored
feat(sql): add index on path columns (important for comparing ahbs) (#116)
* feat(sql): add index on path columns (important for comparing ahbs) * try to crate unique index * catch correct exception def do_execute(self, cursor, statement, parameters, context=None): > cursor.execute(statement, parameters) E sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: ahb_hierarchy_materialized.path, ahb_hierarchy_materialized.pruefidentifikator, ahb_hierarchy_materialized.edifact_format_version E [SQL: CREATE UNIQUE INDEX idx_hierarchy_path_per_ahb ON ahb_hierarchy_materialized (path, pruefidentifikator, edifact_format_version)] E (Background on this error at: https://sqlalche.me/e/20/gkpj) --------- Co-authored-by: Konstantin <[email protected]>
1 parent 4854086 commit ab798af

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/fundamend/sqlmodels/ahbview.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ def create_ahb_view(session: Session) -> None:
4949
for bare_statement in bare_statements:
5050
statement = bare_statement.strip()
5151
if statement:
52-
session.execute(sqlalchemy.text(statement))
52+
try:
53+
session.execute(sqlalchemy.text(statement))
54+
except sqlalchemy.exc.IntegrityError:
55+
if " UNIQUE " in bare_statement:
56+
session.execute(sqlalchemy.text(bare_statement.replace(" UNIQUE ", " ")))
57+
else:
58+
raise
5359
session.commit()
5460
number_of_inserted_rows = session.scalar(
5561
select(func.count(AhbHierarchyMaterialized.id)) # type:ignore[arg-type] # pylint:disable=not-callable #

src/fundamend/sqlmodels/materialize_ahb_view.sql

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITH RECURSIVE
1212
ordered_roots AS (SELECT sg.primary_key,
1313
sg.position,
1414
'segment_group' AS type,
15-
'SG' || sg.id AS root_id_text,
15+
'SG' || sg.id AS root_id_text,
1616
sg.name,
1717
sg.ahb_status,
1818
sg.anwendungsfall_primary_key,
@@ -186,7 +186,7 @@ WITH RECURSIVE
186186
'segment',
187187
h.source_id,
188188
h.sort_path || substr('00000' || s.position, -5) || '-' AS sort_path,
189-
h.id_path || s.id || '>' AS id_path,
189+
h.id_path || s.id || '>' AS id_path,
190190
h.pruefidentifikator,
191191
h.format,
192192
h.versionsnummer,
@@ -241,7 +241,7 @@ WITH RECURSIVE
241241
'dataelementgroup',
242242
h.source_id,
243243
h.sort_path || substr('00000' || deg.position, -5) || '-' AS sort_path,
244-
h.id_path || deg.id || '>' AS id_path,
244+
h.id_path || deg.id || '>' AS id_path,
245245
h.pruefidentifikator,
246246
h.format,
247247
h.versionsnummer,
@@ -296,7 +296,7 @@ WITH RECURSIVE
296296
'dataelement',
297297
h.source_id,
298298
h.sort_path || substr('00000' || de.position, -5) || '-' AS sort_path,
299-
h.id_path || de.id || '>' AS id_path,
299+
h.id_path || de.id || '>' AS id_path,
300300
h.pruefidentifikator,
301301
h.format,
302302
h.versionsnummer,
@@ -352,7 +352,7 @@ WITH RECURSIVE
352352
'dataelement',
353353
h.source_id,
354354
h.sort_path || substr('00000' || de.position, -5) || '-' AS sort_path,
355-
h.id_path || de.id || '>' AS id_path,
355+
h.id_path || de.id || '>' AS id_path,
356356
h.pruefidentifikator,
357357
h.format,
358358
h.versionsnummer,
@@ -407,7 +407,7 @@ WITH RECURSIVE
407407
'code',
408408
h.source_id,
409409
h.sort_path || substr('00000' || c.position, -5) || '-' AS sort_path,
410-
h.id_path || c.value || '>' AS id_path,
410+
h.id_path || c.value || '>' AS id_path,
411411
h.pruefidentifikator,
412412
h.format,
413413
h.versionsnummer,
@@ -486,6 +486,9 @@ CREATE INDEX idx_hierarchy_code_description ON ahb_hierarchy_materialized (code_
486486
CREATE INDEX idx_hierarchy_code_value ON ahb_hierarchy_materialized (code_value);
487487
CREATE INDEX idx_hierarchy_code_ahb_status ON ahb_hierarchy_materialized (code_ahb_status);
488488
CREATE INDEX idx_hierarchy_code_position ON ahb_hierarchy_materialized (code_position);
489-
490-
---
489+
CREATE INDEX idx_hierarchy_path ON ahb_hierarchy_materialized (path);
490+
CREATE INDEX idx_hierarchy_id_path ON ahb_hierarchy_materialized (id_path);
491+
-- if the unique part of the following indexes raises an integrity error, this is handled by the calling python code
492+
CREATE UNIQUE INDEX idx_hierarchy_path_per_ahb ON ahb_hierarchy_materialized (path, pruefidentifikator, edifact_format_version);
493+
CREATE UNIQUE INDEX idx_hierarchy_id_path_per_ahb ON ahb_hierarchy_materialized (id_path, pruefidentifikator, edifact_format_version);
491494

0 commit comments

Comments
 (0)