Skip to content

Commit

Permalink
SW-6251 Add monitoring plot histories table (#2609)
Browse files Browse the repository at this point in the history
To prepare for more flexible planting site editing that will allow subzones
to be redefined even when they contain existing monitoring plots, create a
new table to hold the change history of monitoring plots.
  • Loading branch information
sgrimm authored Nov 19, 2024
1 parent 95b0ca1 commit 25990e1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -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")),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions src/main/resources/db/migration/R__Comments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 25990e1

Please sign in to comment.