From 1a517ea2df5f1eab7f5dc50055c139c831863e40 Mon Sep 17 00:00:00 2001 From: Irwan Fathurrahman Date: Mon, 3 Jun 2024 16:47:55 +0700 Subject: [PATCH] Clean migrations and improve query (#131) * Make GGMN as default of download data * Clean migrations * Added observed_properties_sensor * Add time to measures --- admin/download_request.py | 4 +- fixtures/measurement_parameter.json | 78 ++++++++++++------- .../commands/refresh_materialized_views.py | 2 +- management/commands/update_fixtures.py | 6 ++ migrations/0002_initial_term_data.py | 11 +-- migrations/0003_view_well_information.py | 19 ----- migrations/0013_view_well_information.py | 18 ----- migrations/0014_view_well_information_uuid.py | 10 --- migrations/0019_view_well_with_uuid.py | 42 ---------- migrations/0021_view_well_with_uuid.py | 54 ------------- migrations/0026_view_well_with_uuid.py | 54 ------------- migrations/0028_view_well_with_uuid.py | 59 -------------- migrations/0032_auto_20201214_0640.py | 3 - migrations/0033_view_well_with_uuid.py | 59 -------------- migrations/0037_auto_20201222_0943.py | 1 - migrations/0038_view_well_with_uuid.py | 60 -------------- migrations/0044_view_well_with_uuid.py | 61 --------------- migrations/0045_view_well_with_uuid.py | 61 --------------- migrations/0056_view_well.py | 50 +----------- migrations/0059_view_well.py | 46 ----------- migrations/0064_view_well.py | 50 +----------- migrations/0066_view_well.py | 51 +----------- migrations/0067_view_well.py | 69 +--------------- migrations/0068_auto_20230220_0322.py | 11 --- migrations/0069_view_well.py | 70 +---------------- migrations/0070_view_well.py | 71 +---------------- migrations/0077_views.py | 14 ---- migrations/0081_auto_20240322_1435.py | 16 +--- migrations/0082_views.py | 14 ---- migrations/0084_view_well.py | 12 --- migrations/0085_view_well.py | 9 +++ migrations/sql/gwml/functions.sql | 9 +-- migrations/sql/istsos/default.sql | 1 + migrations/sql/istsos/views.sql | 13 +++- migrations/sql/view_well_migrations.py | 29 +++++++ templates/download/form.html | 1 - views/download_request.py | 4 +- 37 files changed, 125 insertions(+), 1017 deletions(-) create mode 100644 migrations/0085_view_well.py create mode 100644 migrations/sql/view_well_migrations.py diff --git a/admin/download_request.py b/admin/download_request.py index af0cca8f..4610a496 100644 --- a/admin/download_request.py +++ b/admin/download_request.py @@ -5,9 +5,9 @@ class DownloadRequestAdmin(admin.ModelAdmin): list_display = ( - 'uuid', 'first_name', 'last_name', 'organization', + 'request_at', 'first_name', 'last_name', 'organization', 'organization_types', - 'email', 'country', 'request_at', 'age_hours' + 'email', 'country', 'data_type', 'age_hours' ) filter_horizontal = ('countries',) search_fields = ( diff --git a/fixtures/measurement_parameter.json b/fixtures/measurement_parameter.json index a8348128..4b1aaea5 100644 --- a/fixtures/measurement_parameter.json +++ b/fixtures/measurement_parameter.json @@ -8,7 +8,8 @@ "cm", "m", "ft" - ] + ], + "default_unit": "m" }, "Water depth [from the top of the well]": { "description": "", @@ -19,7 +20,8 @@ "cm", "m", "ft" - ] + ], + "default_unit": "m" }, "Water level elevation a.m.s.l.": { "description": "", @@ -30,7 +32,8 @@ "cm", "m", "ft" - ] + ], + "default_unit": "m" }, "EC": { "description": "Electrical conductivity", @@ -40,7 +43,8 @@ "units": [ "S/m", "μS/cm" - ] + ], + "default_unit": "S/m" }, "pH": { "description": "Acidity", @@ -58,7 +62,8 @@ "°C", "°F", "°K" - ] + ], + "default_unit": "°C" }, "NO₃": { "description": "Nitrate", @@ -68,7 +73,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "Cl": { "description": "Cloride", @@ -78,7 +84,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "Ca": { "description": "Calcium", @@ -88,7 +95,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "Mg": { "description": "Magnesium", @@ -98,7 +106,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "Na": { "description": "Sodium", @@ -108,7 +117,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "K": { "description": "Potassium", @@ -118,7 +128,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "HCO₃": { "description": "Bicarbonate", @@ -128,7 +139,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "SO₄": { "description": "Sulfate", @@ -138,7 +150,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "TDS": { "description": "Total dissolved solids", @@ -148,7 +161,8 @@ "units": [ "mg/L", "ppm" - ] + ], + "default_unit": "mg/L" }, "FC": { "description": "Fecal coliforms", @@ -158,7 +172,8 @@ "units": [ "cfu per mL", "cfu per 100 ml" - ] + ], + "default_unit": "cfu per mL" }, "FS": { "description": "Fecal streptococci", @@ -168,7 +183,8 @@ "units": [ "cfu per mL", "cfu per 100 ml" - ] + ], + "default_unit": "cfu per mL" }, "E Coli": { "description": "Escherichia coli", @@ -178,7 +194,8 @@ "units": [ "cfu per mL", "cfu per 100 ml" - ] + ], + "default_unit": "cfu per mL" }, "F": { "description": "Fluoride", @@ -189,7 +206,8 @@ "ng/L", "μg/L", "mg/L" - ] + ], + "default_unit": "mg/L" }, "As": { "description": "Soluble arsenic", @@ -200,7 +218,8 @@ "ng/L", "μg/L", "mg/L" - ] + ], + "default_unit": "mg/L" }, "U": { "description": "Soluble uranium", @@ -211,7 +230,8 @@ "ng/L", "μg/L", "mg/L" - ] + ], + "default_unit": "mg/L" }, "NH₄": { "description": "Ammonium", @@ -221,7 +241,8 @@ "units": [ "μg/L", "mg/L" - ] + ], + "default_unit": "mg/L" }, "Fe": { "description": "Soluble iron", @@ -230,7 +251,8 @@ ], "units": [ "mg/L" - ] + ], + "default_unit": "mg/L" }, "Mn": { "description": "Soluble manganese", @@ -241,7 +263,8 @@ "ng/L", "μg/L", "mg/L" - ] + ], + "default_unit": "mg/L" }, "P": { "description": "orthophosphate", @@ -251,7 +274,8 @@ "units": [ "μg/L", "mg/L" - ] + ], + "default_unit": "mg/L" }, "Yield": { "description": "Yield", @@ -263,7 +287,8 @@ "m³/h", "m³/min", "m³/s" - ] + ], + "default_unit": "m³/day" }, "Spring discharge": { "description": "Spring discharge", @@ -275,6 +300,7 @@ "m³/h", "m³/min", "m³/s" - ] + ], + "default_unit": "m³/day" } } \ No newline at end of file diff --git a/management/commands/refresh_materialized_views.py b/management/commands/refresh_materialized_views.py index 9b1d9385..7c1d02ff 100644 --- a/management/commands/refresh_materialized_views.py +++ b/management/commands/refresh_materialized_views.py @@ -10,7 +10,7 @@ class Command(BaseCommand): help = 'Refresh materialized views' views = [ 'mv_well_ggmn', 'mv_well', 'mv_well_measurement', - 'istsos.measures_group' + 'istsos.measures_group', 'istsos.observed_properties_sensor' ] def handle(self, *args, **options): diff --git a/management/commands/update_fixtures.py b/management/commands/update_fixtures.py index 8da25921..977fc68b 100644 --- a/management/commands/update_fixtures.py +++ b/management/commands/update_fixtures.py @@ -98,4 +98,10 @@ def handle(self, *args, **options): group.parameters.add(measurement) units = Unit.objects.filter(name__in=value['units']) measurement.units.add(*units) + try: + measurement.default_unit = Unit.objects.get( + name=value['default_unit'] + ) + except (Unit.DoesNotExist, KeyError): + pass measurement.save() diff --git a/migrations/0002_initial_term_data.py b/migrations/0002_initial_term_data.py index c5cc1784..5da3557e 100644 --- a/migrations/0002_initial_term_data.py +++ b/migrations/0002_initial_term_data.py @@ -1,15 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -import json -import os -from django.db import migrations -from django.core.management import call_command - -def import_data(apps, schema_editor): - # TODO : - # call command update_fixtures - pass +from django.db import migrations class Migration(migrations.Migration): @@ -19,5 +11,4 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(import_data), ] diff --git a/migrations/0003_view_well_information.py b/migrations/0003_view_well_information.py index 1080549b..1fc32030 100644 --- a/migrations/0003_view_well_information.py +++ b/migrations/0003_view_well_information.py @@ -8,24 +8,5 @@ class Migration(migrations.Migration): ('gwml2', '0002_initial_term_data'), ] - sql = """ - CREATE VIEW well_information AS - select w.id, - w.id as "ggis_id", - w.original_id as "original_id", - w.name as "name", - c.name as "country", - type.name as "feature_type", - hydro.aquifer_name as "aquifer_name", - concat('edit') as "editor", - w.location - From well as w - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN hydrogeology_parameter as hydro on w.hydrogeology_parameter_id = hydro.id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_information;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0013_view_well_information.py b/migrations/0013_view_well_information.py index 8e95d301..aebaa1fe 100644 --- a/migrations/0013_view_well_information.py +++ b/migrations/0013_view_well_information.py @@ -8,23 +8,5 @@ class Migration(migrations.Migration): ('gwml2', '0012_useruuid'), ] - sql = """ - CREATE VIEW well_information AS - select w.id, - w.original_id as "original_id", - w.name as "name", - c.name as "country", - type.name as "feature_type", - admins || editors || viewers as "users", - concat('detail') as detail, - w.location - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_information;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0014_view_well_information_uuid.py b/migrations/0014_view_well_information_uuid.py index 5a8a3f80..17eaf2f1 100644 --- a/migrations/0014_view_well_information_uuid.py +++ b/migrations/0014_view_well_information_uuid.py @@ -7,15 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0013_view_well_information'), ] - - sql = """ - CREATE VIEW well_information_uuid AS - select * - from well_information as w - LEFT JOIN user_uuid u ON w.users @> ARRAY [u.user_id]; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_information_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0019_view_well_with_uuid.py b/migrations/0019_view_well_with_uuid.py index d0829bff..cab02a68 100644 --- a/migrations/0019_view_well_with_uuid.py +++ b/migrations/0019_view_well_with_uuid.py @@ -8,47 +8,5 @@ class Migration(migrations.Migration): ('gwml2', '0018_formhelptext'), ] - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - c.name as "country", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - w.location, - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('detail') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0021_view_well_with_uuid.py b/migrations/0021_view_well_with_uuid.py index fb70eaf0..08466d4a 100644 --- a/migrations/0021_view_well_with_uuid.py +++ b/migrations/0021_view_well_with_uuid.py @@ -7,59 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0020_auto_20201202_0824'), ] - - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - w.last_edited_at as "last_edited_at", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('detail') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0026_view_well_with_uuid.py b/migrations/0026_view_well_with_uuid.py index 3031972e..a92775a7 100644 --- a/migrations/0026_view_well_with_uuid.py +++ b/migrations/0026_view_well_with_uuid.py @@ -7,59 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0025_geology_reference_elevation'), ] - - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - w.last_edited_at as "last_edited_at", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0028_view_well_with_uuid.py b/migrations/0028_view_well_with_uuid.py index c9360832..39a65489 100644 --- a/migrations/0028_view_well_with_uuid.py +++ b/migrations/0028_view_well_with_uuid.py @@ -7,64 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0027_useruuid_username'), ] - - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by on created_by.user_id = w.created_by - LEFT JOIN user_uuid last_edited_by on last_edited_by.user_id = w.last_edited_by - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0032_auto_20201214_0640.py b/migrations/0032_auto_20201214_0640.py index 8c1f3194..d484dc3d 100755 --- a/migrations/0032_auto_20201214_0640.py +++ b/migrations/0032_auto_20201214_0640.py @@ -9,9 +9,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL('DROP VIEW IF EXISTS well_information_uuid;'), - migrations.RunSQL('DROP VIEW IF EXISTS well_information;'), migrations.AlterField( model_name='construction', name='pump_installer', diff --git a/migrations/0033_view_well_with_uuid.py b/migrations/0033_view_well_with_uuid.py index c5114173..2672e988 100644 --- a/migrations/0033_view_well_with_uuid.py +++ b/migrations/0033_view_well_with_uuid.py @@ -8,64 +8,5 @@ class Migration(migrations.Migration): ('gwml2', '0032_auto_20201214_0640'), ] - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.public as "public", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by on created_by.user_id = w.created_by - LEFT JOIN user_uuid last_edited_by on last_edited_by.user_id = w.last_edited_by - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0037_auto_20201222_0943.py b/migrations/0037_auto_20201222_0943.py index 05f90bd0..652b85c5 100755 --- a/migrations/0037_auto_20201222_0943.py +++ b/migrations/0037_auto_20201222_0943.py @@ -10,7 +10,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), migrations.AlterField( model_name='well', name='name', diff --git a/migrations/0038_view_well_with_uuid.py b/migrations/0038_view_well_with_uuid.py index 7bb32f7f..941efb17 100644 --- a/migrations/0038_view_well_with_uuid.py +++ b/migrations/0038_view_well_with_uuid.py @@ -7,65 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0037_auto_20201222_0943'), ] - - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.public as "public", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by on created_by.user_id = w.created_by - LEFT JOIN user_uuid last_edited_by on last_edited_by.user_id = w.last_edited_by - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0044_view_well_with_uuid.py b/migrations/0044_view_well_with_uuid.py index 56b4357c..0805aca2 100644 --- a/migrations/0044_view_well_with_uuid.py +++ b/migrations/0044_view_well_with_uuid.py @@ -7,66 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0043_auto_20210106_0623'), ] - - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.public as "public", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by on created_by.user_id = w.created_by - LEFT JOIN user_uuid last_edited_by on last_edited_by.user_id = w.last_edited_by - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0045_view_well_with_uuid.py b/migrations/0045_view_well_with_uuid.py index 22154fa2..48b19827 100644 --- a/migrations/0045_view_well_with_uuid.py +++ b/migrations/0045_view_well_with_uuid.py @@ -7,66 +7,5 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0044_view_well_with_uuid'), ] - - # TODO: - # Always change gwmlw/models/views/well/WellWithUUID.py when this has changed - - sql = """ - CREATE VIEW well_with_uuid AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.public as "public", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - users.user_id as "user_id", - uuid.uuid as "uuid", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by on created_by.user_id = w.created_by - LEFT JOIN user_uuid last_edited_by on last_edited_by.user_id = w.last_edited_by - LEFT JOIN ( - select * - from ( - select w.id as well_id, org.name, unnest(org.admins || org.editors || org.viewers || 0) as user_id - from well as w - JOIN organisation org on org.id = w.organisation_id - ) well - UNION - ( - SELECT well_id, org.name, unnest(org.admins || org.editors || org.viewers) as user_id - from well_affiliate_organisations as aff - JOIN organisation org on org.id = aff.organisation_id - ) - ) AS users ON users.well_id = w.id - LEFT JOIN user_uuid uuid ON uuid.user_id = users.user_id - ORDER BY user_id; - """ - operations = [ - migrations.RunSQL('DROP VIEW IF EXISTS well_with_uuid;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0056_view_well.py b/migrations/0056_view_well.py index a4f70bc0..42b4adb4 100644 --- a/migrations/0056_view_well.py +++ b/migrations/0056_view_well.py @@ -8,52 +8,4 @@ class Migration(migrations.Migration): ('gwml2', '0055_auto_20220601_0451'), ] - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - sql = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.public as "public", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by, public from vw_well where "public" is True AND number_of_measurements > 0; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by, public from vw_well where "public" is True; - """ - - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(sql) - ] + operations = [] diff --git a/migrations/0059_view_well.py b/migrations/0059_view_well.py index ca2b2d9f..250da7b4 100644 --- a/migrations/0059_view_well.py +++ b/migrations/0059_view_well.py @@ -8,51 +8,5 @@ class Migration(migrations.Migration): ('gwml2', '0058_downloadrequest_organisationtype'), ] - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - sql = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well where number_of_measurements > 0; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well; - """ - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(sql) ] diff --git a/migrations/0064_view_well.py b/migrations/0064_view_well.py index b5aef742..cdc36c89 100644 --- a/migrations/0064_view_well.py +++ b/migrations/0064_view_well.py @@ -7,52 +7,4 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0063_auto_20221026_0442'), ] - - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - sql = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well where number_of_measurements > 0 and organisation is not null; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well; - """ - - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(sql) - ] + operations = [] diff --git a/migrations/0066_view_well.py b/migrations/0066_view_well.py index f2b8342e..9a924dfa 100644 --- a/migrations/0066_view_well.py +++ b/migrations/0066_view_well.py @@ -7,53 +7,4 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0065_organisation_active'), ] - - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - sql = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id - WHERE org.active = True; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well where number_of_measurements > 0 and organisation is not null; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well; - """ - - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(sql) - ] + operations = [] diff --git a/migrations/0067_view_well.py b/migrations/0067_view_well.py index 113c38f8..57054c9b 100644 --- a/migrations/0067_view_well.py +++ b/migrations/0067_view_well.py @@ -8,71 +8,4 @@ class Migration(migrations.Migration): ('gwml2', '0066_view_well'), ] - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - views = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id - WHERE org.active = True; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well where number_of_measurements > 0 and organisation is not null; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well; - """ - - functions = """ - DROP TRIGGER IF EXISTS trigger_organisation ON organisation; - DROP TRIGGER IF EXISTS trigger_user_uuid ON user_uuid; - DROP TRIGGER IF EXISTS trigger_well ON well; - DROP FUNCTION IF EXISTS update_mv(); - - CREATE FUNCTION update_mv() RETURNS trigger AS - $update_mv$ - BEGIN - refresh materialized view mv_well_ggmn; - refresh materialized view mv_well; - RETURN NEW; - END; - $update_mv$ LANGUAGE plpgsql; - - CREATE TRIGGER trigger_well AFTER INSERT ON well FOR EACH ROW EXECUTE PROCEDURE update_mv(); - """ - - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(views), - migrations.RunSQL(functions), - ] + operations = [] diff --git a/migrations/0068_auto_20230220_0322.py b/migrations/0068_auto_20230220_0322.py index 42e43638..fe79b4f8 100755 --- a/migrations/0068_auto_20230220_0322.py +++ b/migrations/0068_auto_20230220_0322.py @@ -7,18 +7,7 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0067_view_well'), ] - - functions = """ - DROP TRIGGER IF EXISTS trigger_organisation ON organisation; - DROP TRIGGER IF EXISTS trigger_user_uuid ON user_uuid; - DROP TRIGGER IF EXISTS trigger_well ON well; - DROP FUNCTION IF EXISTS update_mv(); - """ operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(functions), migrations.AlterField( model_name='well', name='original_id', diff --git a/migrations/0069_view_well.py b/migrations/0069_view_well.py index a4da517d..0d132a3e 100644 --- a/migrations/0069_view_well.py +++ b/migrations/0069_view_well.py @@ -7,72 +7,4 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0068_auto_20230220_0322'), ] - - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - views = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id - WHERE org.active = True; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well where number_of_measurements > 0 and organisation is not null; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status,organisation, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well; - """ - - functions = """ - DROP TRIGGER IF EXISTS trigger_organisation ON organisation; - DROP TRIGGER IF EXISTS trigger_user_uuid ON user_uuid; - DROP TRIGGER IF EXISTS trigger_well ON well; - DROP FUNCTION IF EXISTS update_mv(); - - CREATE FUNCTION update_mv() RETURNS trigger AS - $update_mv$ - BEGIN - refresh materialized view mv_well_ggmn; - refresh materialized view mv_well; - RETURN NEW; - END; - $update_mv$ LANGUAGE plpgsql; - - CREATE TRIGGER trigger_well AFTER INSERT ON well FOR EACH ROW EXECUTE PROCEDURE update_mv(); - """ - - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(views), - migrations.RunSQL(functions), - ] + operations = [] diff --git a/migrations/0070_view_well.py b/migrations/0070_view_well.py index 9cc63ad0..75490485 100644 --- a/migrations/0070_view_well.py +++ b/migrations/0070_view_well.py @@ -7,73 +7,4 @@ class Migration(migrations.Migration): dependencies = [ ('gwml2', '0069_view_well'), ] - - # TODO: - # Always change gwmlw/models/views/well/Well.py when this has changed - - views = """ - CREATE VIEW vw_well AS - select w.id, - w.number_of_measurements as "number_of_measurements", - w.ggis_uid as "ggis_uid", - w.original_id as "original_id", - w.name as "name", - type.name as "feature_type", - purpose.name as "purpose", - status.name as "status", - org.name as "organisation", - org.id as "organisation_id", - c.name as "country", - drill.year_of_drilling as "year_of_drilling", - hp.aquifer_name as "aquifer_name", - aquifer.name as "aquifer_type", - mg.manager as "manager", - w.location, - w.created_at as "created_at", - created_by.username as "created_by", - w.last_edited_at as "last_edited_at", - last_edited_by.username as "last_edited_by", - concat('Full Data Record') as detail - from well as w - LEFT JOIN organisation org on w.organisation_id = org.id - LEFT JOIN country c on w.country_id = c.id - LEFT JOIN term_feature_type type on w.feature_type_id = type.id - LEFT JOIN term_well_purpose purpose on w.purpose_id = purpose.id - LEFT JOIN term_well_status status on w.status_id = status.id - LEFT JOIN drilling drill on w.drilling_id = drill.id - LEFT JOIN hydrogeology_parameter hp on w.hydrogeology_parameter_id = hp.id - LEFT JOIN term_aquifer_type aquifer on hp.aquifer_type_id = aquifer.id - LEFT JOIN management mg on w.management_id = mg.id - LEFT JOIN user_uuid created_by ON w.created_by = created_by.user_id - LEFT JOIN user_uuid last_edited_by ON w.last_edited_by = last_edited_by.user_id - WHERE org.active = True; - - CREATE MATERIALIZED VIEW mv_well_ggmn AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status, organisation, organisation_id, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well where number_of_measurements > 0 and organisation is not null; - CREATE MATERIALIZED VIEW mv_well AS select DISTINCT ON (id) id, organisation || '-' || original_id AS ggis_uid, original_id, name, feature_type,purpose, status, organisation, organisation_id, country, year_of_drilling, aquifer_name, aquifer_type,manager, detail, location, created_at, created_by, last_edited_at, last_edited_by from vw_well; - """ - - functions = """ - DROP TRIGGER IF EXISTS trigger_organisation ON organisation; - DROP TRIGGER IF EXISTS trigger_user_uuid ON user_uuid; - DROP TRIGGER IF EXISTS trigger_well ON well; - DROP FUNCTION IF EXISTS update_mv(); - - CREATE FUNCTION update_mv() RETURNS trigger AS - $update_mv$ - BEGIN - refresh materialized view mv_well_ggmn; - refresh materialized view mv_well; - RETURN NEW; - END; - $update_mv$ LANGUAGE plpgsql; - - CREATE TRIGGER trigger_well AFTER INSERT ON well FOR EACH ROW EXECUTE PROCEDURE update_mv(); - """ - - operations = [ - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well_ggmn;'), - migrations.RunSQL('DROP MATERIALIZED VIEW IF EXISTS mv_well;'), - migrations.RunSQL('DROP VIEW IF EXISTS vw_well;'), - migrations.RunSQL(views), - migrations.RunSQL(functions), - ] + operations = [] diff --git a/migrations/0077_views.py b/migrations/0077_views.py index 81188c16..6d80230c 100644 --- a/migrations/0077_views.py +++ b/migrations/0077_views.py @@ -2,25 +2,11 @@ from django.db import migrations -from gwml2.migrations.sql.utils import load_sql - class Migration(migrations.Migration): dependencies = [ ('gwml2', '0076_auto_20231228_0814'), ] - gwml_default = load_sql('gwml', 'default.sql') - gwml_views = load_sql('gwml', 'views.sql') - gwml_functions = load_sql('gwml', 'functions.sql') - - istsos_default = load_sql('istsos', 'default.sql') - istsos_views = load_sql('istsos', 'views.sql') - operations = [ - migrations.RunSQL(istsos_default, istsos_default), - migrations.RunSQL(gwml_default, gwml_default), - migrations.RunSQL(gwml_views, gwml_default), - migrations.RunSQL(gwml_functions, gwml_default), - migrations.RunSQL(istsos_views, istsos_default), ] diff --git a/migrations/0081_auto_20240322_1435.py b/migrations/0081_auto_20240322_1435.py index 52077480..ab9c8fb2 100755 --- a/migrations/0081_auto_20240322_1435.py +++ b/migrations/0081_auto_20240322_1435.py @@ -1,7 +1,7 @@ # Generated by Django 3.2.20 on 2024-03-22 14:35 from django.db import migrations, models -from gwml2.migrations.sql.utils import load_sql +from gwml2.migrations.sql.view_well_migrations import ViewWellMigration class Migration(migrations.Migration): @@ -10,16 +10,7 @@ class Migration(migrations.Migration): ('gwml2', '0080_downloadrequest_organisations'), ] - gwml_default = load_sql('gwml', 'default.sql') - gwml_views = load_sql('gwml', 'views.sql') - gwml_functions = load_sql('gwml', 'functions.sql') - - istsos_default = load_sql('istsos', 'default.sql') - istsos_views = load_sql('istsos', 'views.sql') - operations = [ - migrations.RunSQL(istsos_default, istsos_default), - migrations.RunSQL(gwml_default, gwml_default), migrations.AlterField( model_name='well', name='ggis_uid', @@ -29,8 +20,5 @@ class Migration(migrations.Migration): model_name='well', name='name', field=models.CharField(blank=True, max_length=512, null=True, verbose_name='Name'), - ), - migrations.RunSQL(gwml_views, gwml_default), - migrations.RunSQL(gwml_functions, gwml_default), - migrations.RunSQL(istsos_views, istsos_default), + ) ] diff --git a/migrations/0082_views.py b/migrations/0082_views.py index 9a851751..da69d768 100644 --- a/migrations/0082_views.py +++ b/migrations/0082_views.py @@ -2,25 +2,11 @@ from django.db import migrations -from gwml2.migrations.sql.utils import load_sql - class Migration(migrations.Migration): dependencies = [ ('gwml2', '0081_auto_20240322_1435'), ] - gwml_default = load_sql('gwml', 'default.sql') - gwml_views = load_sql('gwml', 'views.sql') - gwml_functions = load_sql('gwml', 'functions.sql') - - istsos_default = load_sql('istsos', 'default.sql') - istsos_views = load_sql('istsos', 'views.sql') - operations = [ - migrations.RunSQL(istsos_default, istsos_default), - migrations.RunSQL(gwml_default, gwml_default), - migrations.RunSQL(gwml_views, gwml_default), - migrations.RunSQL(gwml_functions, gwml_default), - migrations.RunSQL(istsos_views, istsos_default), ] diff --git a/migrations/0084_view_well.py b/migrations/0084_view_well.py index d39ad4dd..c858dc7e 100644 --- a/migrations/0084_view_well.py +++ b/migrations/0084_view_well.py @@ -10,17 +10,5 @@ class Migration(migrations.Migration): ('gwml2', '0083_organisation_country'), ] - gwml_default = load_sql('gwml', 'default.sql') - gwml_views = load_sql('gwml', 'views.sql') - gwml_functions = load_sql('gwml', 'functions.sql') - - istsos_default = load_sql('istsos', 'default.sql') - istsos_views = load_sql('istsos', 'views.sql') - operations = [ - migrations.RunSQL(istsos_default, istsos_default), - migrations.RunSQL(gwml_default, gwml_default), - migrations.RunSQL(gwml_views, gwml_default), - migrations.RunSQL(gwml_functions, gwml_default), - migrations.RunSQL(istsos_views, istsos_default), ] diff --git a/migrations/0085_view_well.py b/migrations/0085_view_well.py new file mode 100644 index 00000000..bbfad8a8 --- /dev/null +++ b/migrations/0085_view_well.py @@ -0,0 +1,9 @@ +# Generated by Django 2.2.12 on 2020-07-03 09:03 + +from gwml2.migrations.sql.view_well_migrations import ViewWellMigration + + +class Migration(ViewWellMigration): + dependencies = [ + ('gwml2', '0084_view_well'), + ] diff --git a/migrations/sql/gwml/functions.sql b/migrations/sql/gwml/functions.sql index 4bf93c7a..f4b2ac03 100644 --- a/migrations/sql/gwml/functions.sql +++ b/migrations/sql/gwml/functions.sql @@ -1,16 +1,15 @@ CREATE FUNCTION update_mv() RETURNS trigger AS $update_mv$ BEGIN - refresh -materialized view mv_well_ggmn; - refresh -materialized view mv_well; + refresh materialized view mv_well_ggmn; + refresh materialized view mv_well; + RETURN NEW; RETURN NEW; END; $update_mv$ LANGUAGE plpgsql; CREATE TRIGGER trigger_well - AFTER INSERT + AFTER INSERT OR UPDATE ON well FOR EACH ROW EXECUTE PROCEDURE update_mv(); \ No newline at end of file diff --git a/migrations/sql/istsos/default.sql b/migrations/sql/istsos/default.sql index f4bc26d1..f542fa67 100644 --- a/migrations/sql/istsos/default.sql +++ b/migrations/sql/istsos/default.sql @@ -6,6 +6,7 @@ DROP VIEW IF EXISTS istsos.event_time; DROP VIEW IF EXISTS istsos.foi; DROP VIEW IF EXISTS istsos.off_proc; DROP VIEW IF EXISTS istsos.feature_type; +DROP MATERIALIZED VIEW IF EXISTS istsos.observed_properties_sensor; DROP VIEW IF EXISTS istsos.uoms; DROP VIEW IF EXISTS istsos.proc_obs; DROP VIEW IF EXISTS istsos.observed_properties; diff --git a/migrations/sql/istsos/views.sql b/migrations/sql/istsos/views.sql index fe7edd12..b57141f2 100644 --- a/migrations/sql/istsos/views.sql +++ b/migrations/sql/istsos/views.sql @@ -14,16 +14,17 @@ SELECT id as id_msr, null as id_qi_fk, unique_fk as id_pro_fk, well_id as id_prc_fk, + time as time_eti, default_value as val_msr from mv_well_measurement; -- MEASURES GROUP -- CREATE MATERIALIZED VIEW istsos.measures_group AS SELECT unique_fk as id_pro, - count(id) as count_measurement, + parameter_id as parameter_id, min(time) as begin_measurement, max(time) as end_measurement -from mv_well_measurement GROUP BY unique_fk; +from mv_well_measurement GROUP BY unique_fk, parameter_id; -- PROCEDURES -- CREATE VIEW istsos.procedures AS @@ -183,4 +184,10 @@ from well LEFT JOIN management on well.management_id = management.id LEFT JOIN hydrogeology_parameter on well.hydrogeology_parameter_id = hydrogeology_parameter.id LEFT JOIN term_aquifer_type on hydrogeology_parameter.aquifer_type_id = term_aquifer_type.id - LEFT JOIN term_confinement on hydrogeology_parameter.confinement_id = term_confinement.id; \ No newline at end of file + LEFT JOIN term_confinement on hydrogeology_parameter.confinement_id = term_confinement.id; + +-- OBSERVED PROPERTIES SENSOR -- +CREATE MATERIALIZED VIEW istsos.observed_properties_sensor AS + SELECT id_prc, def_opr, name_opr, desc_opr, constr_pro, name_uom, id_pro, po.begin_measurement as stime_prc, po.end_measurement as etime_prc, name_prc + FROM istsos.observed_properties opr, istsos.proc_obs po, istsos.procedures pr, istsos.uoms um + WHERE opr.id_opr=po.id_opr_fk AND pr.id_prc=po.id_prc_fk AND um.id_uom = po.id_uom_fk ORDER BY id_pro; \ No newline at end of file diff --git a/migrations/sql/view_well_migrations.py b/migrations/sql/view_well_migrations.py new file mode 100644 index 00000000..00732454 --- /dev/null +++ b/migrations/sql/view_well_migrations.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2.12 on 2020-07-03 09:03 + +from abc import ABC + +from django.db import migrations + +from gwml2.migrations.sql.utils import load_sql + + +class ViewWellMigration(ABC, migrations.Migration): + """View well migrations.""" + + gwml_default = load_sql('gwml', 'default.sql') + gwml_views = load_sql('gwml', 'views.sql') + gwml_functions = load_sql('gwml', 'functions.sql') + + istsos_default = load_sql('istsos', 'default.sql') + istsos_views = load_sql('istsos', 'views.sql') + + operations_defaults = [ + migrations.RunSQL(istsos_default, istsos_default), + migrations.RunSQL(gwml_default, gwml_default), + ] + operations_creations = [ + migrations.RunSQL(gwml_views, gwml_default), + migrations.RunSQL(gwml_functions, gwml_default), + migrations.RunSQL(istsos_views, istsos_default) + ] + operations = operations_defaults + operations_creations diff --git a/templates/download/form.html b/templates/download/form.html index 2f532b73..a6848473 100644 --- a/templates/download/form.html +++ b/templates/download/form.html @@ -145,7 +145,6 @@

Data Download

} { const placeholder = "Select an organization type or more or create by typing new one."; - console.log($('#id_organization_types select').val()) $('#id_organization_types select').select2({ placeholder: placeholder, tags: true, diff --git a/views/download_request.py b/views/download_request.py index 7a7343a6..97a0c252 100644 --- a/views/download_request.py +++ b/views/download_request.py @@ -8,7 +8,7 @@ from gwml2.forms.download_request import DownloadRequestForm from gwml2.models.download_request import ( - DownloadRequest, WELL_AND_MONITORING_DATA + DownloadRequest, GGMN ) from gwml2.models.general import Country from gwml2.tasks.downloader import prepare_download_file @@ -40,7 +40,7 @@ def get(self, request, *args, **kwargs): country = Country.objects.get(code=country) except Country.DoesNotExist: country = None - data_type = request.GET.get('data_type', WELL_AND_MONITORING_DATA) + data_type = request.GET.get('data_type', GGMN) context = { 'form': DownloadRequestForm( instance=DownloadRequest(