-
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 cd_ref path (ltree) mat. view
- Loading branch information
Showing
2 changed files
with
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""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 x AS ( | ||
SELECT t.cd_nom as cd_ref, | ||
t.cd_nom::text::ltree AS path | ||
FROM taxonomie.taxref t | ||
WHERE t.cd_sup IS NULL AND t.cd_nom = t.cd_ref | ||
UNION ALL | ||
SELECT y.cd_nom AS cd_ref, | ||
ltree_addtext(x_1.path, y.cd_nom::text) AS path | ||
FROM x x_1, | ||
taxonomie.taxref y | ||
WHERE y.cd_nom = y.cd_ref AND x_1.cd_ref = y.cd_sup | ||
) | ||
SELECT x.cd_ref, | ||
x.path | ||
FROM x | ||
WITH DATA | ||
""" | ||
) | ||
op.create_index( | ||
index_name="taxref_tree_cd_nom_idx", | ||
schema="taxonomie", | ||
table_name="vm_taxref_tree", | ||
columns=["cd_ref"], | ||
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