-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/resources/db/migration/0300/V317__MonitoringPlotHistories.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters