Created At: 2026-06-14T16:14:49Z
Completed At: 2026-06-14T16:14:49Z
File Path: `file:///C:/Users/HP/.gemini/antigravity-ide/brain/d31b1fe2-ad06-4c7a-9ab1-c82bd4c8dd20/implementation_plan.md`
Total Lines: 63
Total Bytes: 4281
Showing lines 1 to 63
The following code has been modified to include a line number before every line, in the format: <line_number>: <original_line>. Please note that any changes targeting the original code should remove the line number, colon, and leading space.
1: # Implementation Plan - Fix Database Image Erasure on Save
2: 
3: This plan outlines the changes to resolve the issue where uploaded images disappear/get lost in the database upon saving settings.
4: 
5: ## User Review Required
6: 
7: > [!IMPORTANT]
8: > - We will modify the database queries (`save_historical` and `save_future`) to prevent empty strings or null values from overwriting existing image URLs and graph parameters.
9: > - The description column will continue to update unconditionally.
10: > - This query-level protection guarantees that even if a form submits empty image fields, the database will keep the previously saved image URLs intact.
11: 
12: ## Proposed Changes
13: 
14: ### Climate System Backend Router
15: 
16: #### [MODIFY] [index.php](file:///c:/Users/HP/Downloads/climate-system/climate-system/index.php)
17: 
18: - **`save_historical` API Action** (around lines 494-503):
19:   Modify the `DO UPDATE SET` clause to use conditional `CASE WHEN` logic:
20:   ```sql
21:   ON CONFLICT (location_id, variable_id) 
22:   DO UPDATE SET 
23:       description = EXCLUDED.description,
24:       image_annual_url = CASE WHEN EXCLUDED.image_annual_url != '' THEN EXCLUDED.image_annual_url ELSE historical_climate.image_annual_url END,
25:       image_trend_url = CASE WHEN EXCLUDED.image_trend_url != '' THEN EXCLUDED.image_trend_url ELSE historical_climate.image_trend_url END,
26:       image_time_series_url = CASE WHEN EXCLUDED.image_time_series_url != '' THEN EXCLUDED.image_time_series_url ELSE historical_climate.image_time_ser
<truncated 1000 bytes>
2 ELSE future_climate.image_climate_map_url_2 END,
40:       image_time_series_url = CASE WHEN EXCLUDED.image_time_series_url != '' THEN EXCLUDED.image_time_series_url ELSE future_climate.image_time_series_url END,
41:       image_climate_stripe_url = CASE WHEN EXCLUDED.image_climate_stripe_url != '' THEN EXCLUDED.image_climate_stripe_url ELSE future_climate.image_climate_stripe_url END,
42:       graph_base_value = CASE WHEN EXCLUDED.graph_base_value IS NOT NULL THEN EXCLUDED.graph_base_value ELSE future_climate.graph_base_value END,
43:       graph_uncertainty = CASE WHEN EXCLUDED.graph_uncertainty IS NOT NULL THEN EXCLUDED.graph_uncertainty ELSE future_climate.graph_uncertainty END,
44:       graph_ssp1_slope = CASE WHEN EXCLUDED.graph_ssp1_slope IS NOT NULL THEN EXCLUDED.graph_ssp1_slope ELSE future_climate.graph_ssp1_slope END,
45:       graph_ssp2_slope = CASE WHEN EXCLUDED.graph_ssp2_slope IS NOT NULL THEN EXCLUDED.graph_ssp2_slope ELSE future_climate.graph_ssp2_slope END,
46:       graph_ssp3_slope = CASE WHEN EXCLUDED.graph_ssp3_slope IS NOT NULL THEN EXCLUDED.graph_ssp3_slope ELSE future_climate.graph_ssp3_slope END,
47:       graph_ssp5_slope = CASE WHEN EXCLUDED.graph_ssp5_slope IS NOT NULL THEN EXCLUDED.graph_ssp5_slope ELSE future_climate.graph_ssp5_slope END,
48:       csv_data = CASE WHEN EXCLUDED.csv_data IS NOT NULL AND EXCLUDED.csv_data != '' THEN EXCLUDED.csv_data ELSE future_climate.csv_data END,
49:       updated_at = CURRENT_TIMESTAMP
50:   ```
51: 
52: ---
53: 
54: ## Verification Plan
55: 
56: ### Automated/Scripted Verification
57: - Run a verification script that inserts test data, updates it with empty string image URLs, and checks that original URLs are kept.
58: 
59: ### Manual Verification
60: 1. Open settings panel, save a URL for Historical climate.
61: 2. Re-save the same combination but with the image field left blank.
62: 3. Reload main page, verify the image is still loaded and not deleted.
63: 
The above content shows the entire, complete file contents of the requested file.
