diff --git a/apptax/migrations/versions/3c4762751898_taxref_tree_v2.py b/apptax/migrations/versions/3c4762751898_taxref_tree_v2.py new file mode 100644 index 00000000..6345caa0 --- /dev/null +++ b/apptax/migrations/versions/3c4762751898_taxref_tree_v2.py @@ -0,0 +1,90 @@ +"""create vm_taxref_tree v2 + +Revision ID: 3c4762751898 +Revises: 83d7105edb76 +Create Date: 2024-12-03 13:30:26.521216 + +""" + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "3c4762751898" +down_revision = "83d7105edb76" +branch_labels = None +depends_on = None + + +def upgrade(): + op.execute("DROP MATERIALIZED VIEW IF EXISTS taxonomie.vm_taxref_tree") + op.execute( + """ + CREATE MATERIALIZED VIEW taxonomie.vm_taxref_tree AS + WITH RECURSIVE + biota AS ( + SELECT + t.cd_nom, + t.cd_ref::TEXT::ltree AS path + FROM + taxonomie.taxref t + WHERE + t.cd_nom = 349525 + UNION ALL + SELECT + child.cd_nom AS cd_nom, + parent.path || child.cd_ref::TEXT AS path + FROM + taxonomie.taxref child + JOIN + taxonomie.taxref child_ref ON child.cd_ref = child_ref.cd_nom + JOIN + biota parent ON parent.cd_nom = child_ref.cd_sup + ), + orphans AS ( + SELECT + t.cd_nom, + t.cd_ref::TEXT::ltree AS path + FROM + taxonomie.taxref t + JOIN + taxonomie.taxref t_ref ON t.cd_ref = t_ref.cd_nom + LEFT JOIN + taxonomie.taxref parent ON t_ref.cd_sup = parent.cd_nom AND parent.cd_nom != t_ref.cd_nom + WHERE + parent.cd_nom IS NULL + ) + SELECT + cd_nom, + path + FROM + biota + UNION DISTINCT -- do not include biota twice + SELECT + cd_nom, + path + FROM + orphans + 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") diff --git a/apptax/migrations/versions/83d7105edb76_taxref_tree.py b/apptax/migrations/versions/83d7105edb76_taxref_tree.py deleted file mode 100644 index caab1a5f..00000000 --- a/apptax/migrations/versions/83d7105edb76_taxref_tree.py +++ /dev/null @@ -1,77 +0,0 @@ -"""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 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") diff --git a/apptax/migrations/versions/83d7105edb76_taxref_tree_v1.py b/apptax/migrations/versions/83d7105edb76_taxref_tree_v1.py new file mode 100644 index 00000000..386bdcfd --- /dev/null +++ b/apptax/migrations/versions/83d7105edb76_taxref_tree_v1.py @@ -0,0 +1,25 @@ +"""create vm_taxref_tree v1 + +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(): + pass + + +def downgrade(): + op.execute("DROP MATERIALIZED VIEW IF EXISTS taxonomie.vm_taxref_tree") diff --git a/docs/changelog.md b/docs/changelog.md index c013dcdf..86782eb6 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,10 @@ # CHANGELOG +2.0.1 (unreleased) +------------------ + +- Optimisation de la VM `vm_taxref_tree` + 2.0.0 (2024-10-29) ------------------