-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taxref): add taxref_tree materialized view (#567)
* feat(taxref): add parent relationship * feat(taxref): add taxref_tree materialized view * feat(taxref): taxons comparison through path
- Loading branch information
Showing
7 changed files
with
152 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""create vm_taxref_tree | ||
Revision ID: 83d7105edb76 | ||
Revises: 44447746cacc | ||
Create Date: 2024-10-05 17:40:11.302423 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "83d7105edb76" | ||
down_revision = "6a20cd1055ec" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute("CREATE EXTENSION IF NOT EXISTS ltree") | ||
op.execute( | ||
""" | ||
CREATE MATERIALIZED VIEW taxonomie.vm_taxref_tree AS | ||
WITH RECURSIVE childs AS ( | ||
SELECT | ||
t.cd_nom, | ||
t.cd_ref::TEXT::ltree AS path, | ||
1 AS path_length, | ||
t_ref.cd_sup AS cd_sup | ||
FROM | ||
taxonomie.taxref t | ||
JOIN taxonomie.taxref t_ref ON | ||
t.cd_ref = t_ref.cd_nom | ||
UNION ALL | ||
SELECT | ||
child.cd_nom AS cd_nom, | ||
parent.cd_ref::TEXT || child.path AS path, | ||
child.path_length + 1 AS path_length, | ||
parent_ref.cd_sup AS cd_sup | ||
FROM | ||
childs child | ||
JOIN taxonomie.taxref parent ON | ||
child.cd_sup = parent.cd_nom | ||
JOIN taxonomie.taxref parent_ref ON | ||
parent.cd_ref = parent_ref.cd_nom | ||
) | ||
SELECT | ||
DISTINCT ON | ||
(cd_nom) cd_nom, | ||
path | ||
FROM | ||
childs | ||
ORDER BY | ||
cd_nom, | ||
path_length DESC | ||
WITH DATA; | ||
""" | ||
) | ||
op.create_index( | ||
index_name="taxref_tree_cd_nom_idx", | ||
schema="taxonomie", | ||
table_name="vm_taxref_tree", | ||
columns=["cd_nom"], | ||
unique=True, | ||
) | ||
# required for these operators: <, <=, =, >=, >, @>, <@, @, ~, ? | ||
op.create_index( | ||
index_name="taxref_tree_path_idx", | ||
schema="taxonomie", | ||
table_name="vm_taxref_tree", | ||
columns=["path"], | ||
postgresql_using="gist", | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.execute("DROP MATERIALIZED VIEW taxonomie.vm_taxref_tree") |
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,46 @@ | ||
import pytest | ||
import sqlalchemy as sa | ||
|
||
from .fixtures import * | ||
from apptax.taxonomie.models import Taxref, TaxrefTree | ||
|
||
|
||
@pytest.mark.usefixtures("client_class", "temporary_transaction") | ||
class TestModels: | ||
def test_taxref_tree_comparison(self): | ||
animalia = db.session.execute( | ||
sa.select(TaxrefTree).where(TaxrefTree.cd_nom == 183716) | ||
).scalar_one() | ||
capra_ibex = db.session.execute( | ||
sa.select(TaxrefTree).where(TaxrefTree.cd_nom == 61098) | ||
).scalar_one() | ||
cinnamon = db.session.execute( | ||
sa.select(TaxrefTree).where(TaxrefTree.cd_nom == 706584) | ||
).scalar_one() | ||
|
||
assert animalia <= animalia | ||
assert capra_ibex <= animalia | ||
assert not animalia <= capra_ibex | ||
assert not cinnamon <= animalia | ||
assert not animalia <= cinnamon | ||
assert not cinnamon <= capra_ibex | ||
assert not capra_ibex <= cinnamon | ||
|
||
def test_taxref_comparison(self): | ||
animalia = db.session.execute( | ||
sa.select(Taxref).where(Taxref.cd_nom == 183716) | ||
).scalar_one() | ||
capra_ibex = db.session.execute( | ||
sa.select(Taxref).where(Taxref.cd_nom == 61098) | ||
).scalar_one() | ||
cinnamon = db.session.execute( | ||
sa.select(Taxref).where(Taxref.cd_nom == 706584) | ||
).scalar_one() | ||
|
||
assert animalia <= animalia | ||
assert capra_ibex <= animalia | ||
assert not animalia <= capra_ibex | ||
assert not cinnamon <= animalia | ||
assert not animalia <= cinnamon | ||
assert not cinnamon <= capra_ibex | ||
assert not capra_ibex <= cinnamon |
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