diff --git a/jooq/src/main/kotlin/com/terraformation/backend/jooq/Config.kt b/jooq/src/main/kotlin/com/terraformation/backend/jooq/Config.kt index 8cd333fea69..c67f80011c6 100644 --- a/jooq/src/main/kotlin/com/terraformation/backend/jooq/Config.kt +++ b/jooq/src/main/kotlin/com/terraformation/backend/jooq/Config.kt @@ -312,6 +312,9 @@ val ID_WRAPPERS = listOf( IdWrapper("DeliveryId", listOf("deliveries\\.id", ".*\\.delivery_id")), IdWrapper("DraftPlantingSiteId", listOf("draft_planting_sites\\.id")), + IdWrapper( + "MonitoringPlotHistoryId", + listOf("monitoring_plot_histories\\.id", ".*\\.monitoring_plot_history_id")), IdWrapper("MonitoringPlotId", listOf("monitoring_plots\\.id", ".*\\..*_plot_id")), IdWrapper("ObservationId", listOf("observations\\.id", ".*\\.observation_id")), IdWrapper("ObservedPlotCoordinatesId", listOf("observed_plot_coordinates\\.id")), @@ -327,7 +330,9 @@ val ID_WRAPPERS = "planting_site_summaries\\.id", ".*\\.planting_site_id")), IdWrapper("PlantingSiteNotificationId", listOf("planting_site_notifications\\.id")), - IdWrapper("PlantingSubzoneHistoryId", listOf("planting_subzone_histories\\.id")), + IdWrapper( + "PlantingSubzoneHistoryId", + listOf("planting_subzone_histories\\.id", ".*\\.planting_subzone_history_id")), IdWrapper( "PlantingSubzoneId", listOf("planting_subzones\\.id", ".*\\.planting_subzone_id")), diff --git a/src/main/resources/db/migration/0300/V317__MonitoringPlotHistories.sql b/src/main/resources/db/migration/0300/V317__MonitoringPlotHistories.sql new file mode 100644 index 00000000000..26e0db2a0b1 --- /dev/null +++ b/src/main/resources/db/migration/0300/V317__MonitoringPlotHistories.sql @@ -0,0 +1,49 @@ +-- Previously, we only kept edit history for sites, zones, and subzones because it wasn't allowed to +-- change a subzone's boundaries such that an existing plot would move. That will no longer be the +-- case, so start keeping history for monitoring plots too, minus the plot boundaries which don't +-- change with map edits. +CREATE TABLE tracking.monitoring_plot_histories ( + id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, + monitoring_plot_id BIGINT NOT NULL REFERENCES tracking.monitoring_plots ON DELETE CASCADE, + planting_subzone_id BIGINT REFERENCES tracking.planting_subzones ON DELETE SET NULL, + planting_site_id BIGINT NOT NULL REFERENCES tracking.planting_sites ON DELETE CASCADE, + planting_subzone_history_id BIGINT REFERENCES tracking.planting_subzone_histories ON DELETE CASCADE, + planting_site_history_id BIGINT NOT NULL REFERENCES tracking.planting_site_histories ON DELETE CASCADE, + created_by BIGINT NOT NULL REFERENCES users, + created_time TIMESTAMP WITH TIME ZONE NOT NULL, + name TEXT NOT NULL, + full_name TEXT NOT NULL +); + +CREATE INDEX ON tracking.monitoring_plot_histories (created_by); +CREATE INDEX ON tracking.monitoring_plot_histories (monitoring_plot_id); +CREATE INDEX ON tracking.monitoring_plot_histories (planting_subzone_id); +CREATE INDEX ON tracking.monitoring_plot_histories (planting_site_id); +CREATE INDEX ON tracking.monitoring_plot_histories (planting_subzone_history_id); +CREATE INDEX ON tracking.monitoring_plot_histories (planting_site_history_id); + +-- Populate the plot histories with the current data for each plot. +INSERT INTO tracking.monitoring_plot_histories ( + monitoring_plot_id, + planting_subzone_id, + planting_site_id, + created_by, + created_time, + name, + full_name, + planting_subzone_history_id, + planting_site_history_id +) +SELECT mp.id, + mp.planting_subzone_id, + mp.planting_site_id, + mp.created_by, + mp.created_time, + mp.name, + mp.full_name, + MAX(pszh.id), + MAX(pzh.planting_site_history_id) +FROM tracking.monitoring_plots mp +JOIN tracking.planting_subzone_histories pszh ON mp.planting_subzone_id = pszh.planting_subzone_id +JOIN tracking.planting_zone_histories pzh ON pszh.planting_zone_history_id = pzh.id +GROUP BY 1, 2, 3, 4, 5, 6, 7; diff --git a/src/main/resources/db/migration/R__Comments.sql b/src/main/resources/db/migration/R__Comments.sql index 064dcd14b7b..8cad61ed4f8 100644 --- a/src/main/resources/db/migration/R__Comments.sql +++ b/src/main/resources/db/migration/R__Comments.sql @@ -349,6 +349,10 @@ COMMENT ON TABLE tracking.monitoring_plot_overlaps IS 'Which monitoring plots ov COMMENT ON COLUMN tracking.monitoring_plot_overlaps.monitoring_plot_id IS 'ID of the newer monitoring plot.'; COMMENT ON COLUMN tracking.monitoring_plot_overlaps.overlaps_plot_id IS 'ID of the older monitoring plot.'; +COMMENT ON TABLE tracking.monitoring_plot_histories IS 'Versions of monitoring plots over time. Each time a planting site changes, its monitoring plots are added to this table.'; +COMMENT ON COLUMN tracking.monitoring_plot_histories.created_by IS 'Which user created or edited the monitoring plot or its planting site.'; +COMMENT ON COLUMN tracking.monitoring_plot_histories.created_time IS 'When the monitoring plot was created, edited, or associated with a new planting site history record. May differ from the time when the subzone or planting site was created or edited since plots can be created when observations start.'; + COMMENT ON TABLE tracking.monitoring_plots IS 'Regions within planting subzones that can be comprehensively surveyed in order to extrapolate results for the entire zone. Any monitoring plot in a subzone is expected to have roughly the same number of plants of the same species as any other monitoring plot in the same subzone.'; COMMENT ON COLUMN tracking.monitoring_plots.boundary IS 'Boundary of the monitoring plot. Coordinates always use SRID 4326 (WGS 84 latitude/longitude).'; COMMENT ON COLUMN tracking.monitoring_plots.created_by IS 'Which user created the monitoring plot.'; diff --git a/src/test/kotlin/com/terraformation/backend/db/SchemaDocsGenerator.kt b/src/test/kotlin/com/terraformation/backend/db/SchemaDocsGenerator.kt index 37641c7e52d..57f8ea18d7f 100644 --- a/src/test/kotlin/com/terraformation/backend/db/SchemaDocsGenerator.kt +++ b/src/test/kotlin/com/terraformation/backend/db/SchemaDocsGenerator.kt @@ -322,6 +322,7 @@ class SchemaDocsGenerator : DatabaseTest() { mapOf( "deliveries" to setOf(ALL, TRACKING), "draft_planting_sites" to setOf(ALL, TRACKING), + "monitoring_plot_histories" to setOf(ALL, TRACKING), "monitoring_plot_overlaps" to setOf(ALL, TRACKING), "monitoring_plots" to setOf(ALL, TRACKING), "observable_conditions" to setOf(ALL, TRACKING),