From 87f4bf6bde7eb979711d5b64b0d458de973866fc Mon Sep 17 00:00:00 2001 From: timofejmalinin Date: Tue, 24 Oct 2023 17:02:59 +0700 Subject: [PATCH] fix sql updates --- service/management/commands/create_procedures.py | 8 ++++---- service/utils.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/service/management/commands/create_procedures.py b/service/management/commands/create_procedures.py index 6ee2b42..dc22c84 100644 --- a/service/management/commands/create_procedures.py +++ b/service/management/commands/create_procedures.py @@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand from service.utils import run_sql_command, log_to_telegram CMD_PIVOT_DIST = """CREATE OR REPLACE VIEW compact_placementpoint AS -SELECT id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry FROM service_placementpoint; +SELECT id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry, delta_first, delta_current FROM service_placementpoint; CREATE OR REPLACE procedure pivot_dist() AS $BODY$ DECLARE columnNames TEXT; @@ -23,7 +23,7 @@ FROM CROSSTAB( LEFT JOIN compact_placementpoint ON placement_point_id=id' ,columnNames); ELSE -CREATE MATERIALIZED VIEW points_with_dist AS SELECT placement_point_id, compact_placementpoint.id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry FROM service_placementpointpvzdistance LEFT JOIN compact_placementpoint ON placement_point_id=compact_placementpoint.id; +CREATE MATERIALIZED VIEW points_with_dist AS SELECT placement_point_id, compact_placementpoint.id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry, delta_first, delta_current FROM service_placementpointpvzdistance LEFT JOIN compact_placementpoint ON placement_point_id=compact_placementpoint.id; END IF; END; $BODY$ @@ -31,7 +31,7 @@ LANGUAGE plpgsql; """ CMD_PIVOT_DIST_PRE = """CREATE OR REPLACE VIEW compact_preplacementpoint AS -SELECT id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, delta_first, delta_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry FROM service_preplacementpoint; +SELECT id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry, delta_first, delta_current FROM service_preplacementpoint; CREATE OR REPLACE procedure prepivot_dist() AS $BODY$ DECLARE columnNames TEXT; @@ -52,7 +52,7 @@ FROM CROSSTAB( LEFT JOIN compact_preplacementpoint ON preplacement_point_id=id' ,columnNames); ELSE -CREATE MATERIALIZED VIEW prepoints_with_dist AS SELECT placement_point_id, compact_preplacementpoint.id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, delta_first, delta_current,doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry FROM service_preplacementpointpvzdistance LEFT JOIN compact_preplacementpoint ON placement_point_id=compact_preplacementpoint.id; +CREATE MATERIALIZED VIEW prepoints_with_dist AS SELECT placement_point_id, compact_preplacementpoint.id, status, category, age_day, fact, area_id, district_id, prediction_first, prediction_current, doors, flat_cnt, rival_post_cnt, rival_pvz_cnt, target_post_cnt, flats_cnt, tc_cnt, culture_cnt, mfc_cnt, public_stop_cnt, supermarket_cnt, target_dist, metro_dist, geometry, delta_first, delta_current FROM service_preplacementpointpvzdistance LEFT JOIN compact_preplacementpoint ON placement_point_id=compact_preplacementpoint.id; END IF; END; $BODY$ diff --git a/service/utils.py b/service/utils.py index 946f4ab..186bdbb 100644 --- a/service/utils.py +++ b/service/utils.py @@ -47,7 +47,7 @@ def run_psql_command(): ) try: cursor = connection.cursor() - command = "REFRESH MATERIALIZED VIEW public.points_with_dist;REFRESH MATERIALIZED VIEW public.prepoints_with_dist;" + command = "CALL public.pivot_dist();CALL public.prepivot_dist();REFRESH MATERIALIZED VIEW public.points_with_dist;REFRESH MATERIALIZED VIEW public.prepoints_with_dist;" cursor.execute(command) connection.commit() except psycopg2.Error as e: