|
| 1 | +"""add atlas view |
| 2 | +
|
| 3 | +Revision ID: 643743e807f6 |
| 4 | +Revises: 26d6515219fe |
| 5 | +Create Date: 2023-06-05 12:20:36.897280 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | + |
| 11 | + |
| 12 | +# revision identifiers, used by Alembic. |
| 13 | +revision = "643743e807f6" |
| 14 | +down_revision = "26d6515219fe" |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | +# This revision performs 2 things: |
| 19 | +# - replaces the materialized view by a view |
| 20 | +# - treats better the downgrade part by removing the slugify function |
| 21 | +# used only for the view (thus this function need to be re-created) |
| 22 | + |
| 23 | + |
| 24 | +def upgrade(): |
| 25 | + # Recreate function here if it is dropped by the |
| 26 | + op.execute( |
| 27 | + """ |
| 28 | + CREATE OR REPLACE FUNCTION pr_zh.slugify("value" TEXT) |
| 29 | + RETURNS TEXT AS $$ |
| 30 | + -- removes accents (diacritic signs) from a given string -- |
| 31 | + WITH "unaccented" AS ( |
| 32 | + SELECT unaccent("value") AS "value" |
| 33 | + ), |
| 34 | + -- lowercases the string |
| 35 | + "lowercase" AS ( |
| 36 | + SELECT lower("value") AS "value" |
| 37 | + FROM "unaccented" |
| 38 | + ), |
| 39 | + -- replaces anything that's not a letter, number, hyphen('-'), or underscore('_') with a hyphen('-') |
| 40 | + "hyphenated" AS ( |
| 41 | + SELECT regexp_replace("value", '[^a-z0-9\\-_]+', '-', 'gi') AS "value" |
| 42 | + FROM "lowercase" |
| 43 | + ), |
| 44 | + -- trims hyphens('-') if they exist on the head or tail of the string |
| 45 | + "trimmed" AS ( |
| 46 | + SELECT regexp_replace(regexp_replace("value", '\\-+$', ''), '^\\-', '') AS "value" |
| 47 | + FROM "hyphenated" |
| 48 | + ) |
| 49 | + SELECT "value" FROM "trimmed"; |
| 50 | + $$ LANGUAGE SQL STRICT IMMUTABLE; |
| 51 | + """ |
| 52 | + ) |
| 53 | + op.execute("DROP MATERIALIZED VIEW IF EXISTS pr_zh.atlas_app") |
| 54 | + op.execute( |
| 55 | + """ |
| 56 | + CREATE OR REPLACE VIEW pr_zh.atlas_app |
| 57 | + AS SELECT tzh.id_zh AS id, |
| 58 | + tzh.main_name AS nom, |
| 59 | + ( SELECT pr_zh.slugify(tzh.main_name::text) AS slugify) AS slug, |
| 60 | + tzh.code, |
| 61 | + tzh.create_date AS date, |
| 62 | + tzh.geom AS polygon_4326, |
| 63 | + ( SELECT st_area(st_transform(st_setsrid(tzh.geom, 4326), 2154)) AS st_area) AS superficie, |
| 64 | + bo.nom_organisme AS operateur, |
| 65 | + sdage.cd_nomenclature AS type_code, |
| 66 | + sdage.mnemonique AS type, |
| 67 | + thread.mnemonique AS menaces, |
| 68 | + diag_bio.mnemonique AS diagnostic_bio, |
| 69 | + diag_hydro.mnemonique AS diagnostic_hydro, |
| 70 | + ( SELECT array_agg(DISTINCT tn.mnemonique) AS array_agg) AS criteres_delim, |
| 71 | + ( SELECT array_agg(DISTINCT la.area_name) AS array_agg) AS communes, |
| 72 | + ( SELECT array_agg(DISTINCT trb.name) AS array_agg) AS bassin_versant, |
| 73 | + ( SELECT COALESCE(json_agg(t.*), '[]'::json) AS "coalesce" |
| 74 | + FROM ( SELECT t_medias.title_fr AS label, |
| 75 | + t_medias.media_path AS url |
| 76 | + FROM gn_commons.t_medias |
| 77 | + WHERE t_medias.uuid_attached_row = tzh.zh_uuid) t) AS images |
| 78 | + FROM pr_zh.t_zh tzh |
| 79 | + LEFT JOIN pr_zh.cor_lim_list cll ON tzh.id_lim_list = cll.id_lim_list |
| 80 | + LEFT JOIN ref_nomenclatures.t_nomenclatures tn ON cll.id_lim = tn.id_nomenclature |
| 81 | + LEFT JOIN pr_zh.cor_zh_area cza ON tzh.id_zh = cza.id_zh |
| 82 | + LEFT JOIN ref_geo.l_areas la ON cza.id_area = la.id_area |
| 83 | + LEFT JOIN pr_zh.cor_zh_rb czr ON tzh.id_zh = czr.id_zh |
| 84 | + LEFT JOIN pr_zh.t_river_basin trb ON czr.id_rb = trb.id_rb |
| 85 | + LEFT JOIN gn_commons.t_medias med ON med.uuid_attached_row = tzh.zh_uuid |
| 86 | + LEFT JOIN utilisateurs.t_roles tr ON tr.id_role = tzh.create_author |
| 87 | + LEFT JOIN utilisateurs.bib_organismes bo ON bo.id_organisme = tr.id_organisme |
| 88 | + LEFT JOIN ref_nomenclatures.t_nomenclatures sdage ON sdage.id_nomenclature = tzh.id_sdage |
| 89 | + LEFT JOIN ref_nomenclatures.t_nomenclatures thread ON thread.id_nomenclature = tzh.id_thread |
| 90 | + LEFT JOIN ref_nomenclatures.t_nomenclatures diag_bio ON diag_bio.id_nomenclature = tzh.id_diag_bio |
| 91 | + LEFT JOIN ref_nomenclatures.t_nomenclatures diag_hydro ON diag_hydro.id_nomenclature = tzh.id_diag_hydro |
| 92 | + WHERE cza.cover IS NOT NULL |
| 93 | + GROUP BY tzh.id_zh, bo.nom_organisme, sdage.cd_nomenclature, sdage.mnemonique, thread.mnemonique, diag_bio.mnemonique, diag_hydro.mnemonique |
| 94 | + ORDER BY tzh.id_zh; |
| 95 | + """ |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def downgrade(): |
| 100 | + op.execute("DROP VIEW pr_zh.atlas_app") |
| 101 | + op.execute("DROP FUNCTION pr_zh.slugify") |
0 commit comments