Skip to content

Commit 1ae2f5c

Browse files
authored
Merge pull request #256 from akrherz/cscap
🔥 Fix datateam problems
2 parents 6fa3d2d + 3fedee3 commit 1ae2f5c

File tree

5 files changed

+334
-116
lines changed

5 files changed

+334
-116
lines changed

init/sustainablecorn.sql

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CREATE TABLE plotids(
2828
south varchar,
2929
plotid varchar
3030
);
31+
alter table plotids owner to mesonet;
3132
GRANT SELECT on plotids to nobody;
3233

3334
---=========================================================================
@@ -140,8 +141,52 @@ CREATE TABLE operations(
140141
iron varchar,
141142
biomassdate1 date
142143
);
144+
alter table operations owner to mesonet;
143145
GRANT SELECT on operations to nobody;
144146

147+
CREATE TABLE public.metadata_master (
148+
leadpi character varying,
149+
co_leaders character varying,
150+
institutionname character varying,
151+
unit character varying,
152+
officialfarmname character varying,
153+
uniqueid character varying,
154+
nwlon character varying,
155+
nwlat character varying,
156+
swlon character varying,
157+
swlat character varying,
158+
selon character varying,
159+
selat character varying,
160+
nelon character varying,
161+
nelat character varying,
162+
rawlonlat character varying,
163+
latitude character varying,
164+
longitude character varying,
165+
state character varying,
166+
county character varying,
167+
city character varying,
168+
landscapeslope character varying,
169+
tiledepth character varying,
170+
tilespacing character varying,
171+
sitearea character varying,
172+
plotsize character varying,
173+
numberofplots character varying,
174+
establishmentyear character varying,
175+
y1forcap character varying,
176+
epaecoregionlevel4codeandname character varying,
177+
iemclimatesite character varying,
178+
additionalinformation character varying,
179+
pre_2011 character varying,
180+
notes2011 character varying,
181+
notes2012 character varying,
182+
notes2013 character varying,
183+
notes2014 character varying,
184+
notes2015 character varying
185+
);
186+
187+
ALTER TABLE public.metadata_master OWNER TO mesonet;
188+
GRANT SELECT ON TABLE public.metadata_master TO nobody;
189+
145190
--- ========================================================================
146191
--- Storage of Soil Data
147192
---
@@ -155,6 +200,8 @@ CREATE TABLE soil_data(
155200
subsample varchar(12),
156201
updated timestamptz default now()
157202
);
203+
alter table soil_data owner to mesonet;
204+
grant select on soil_data to nobody;
158205

159206
CREATE TABLE soil_data_log(
160207
site varchar(24),
@@ -236,6 +283,8 @@ CREATE TABLE agronomic_data(
236283
value varchar(32),
237284
updated timestamptz default now()
238285
);
286+
alter table agronomic_data owner to mesonet;
287+
grant select on agronomic_data to nobody;
239288

240289
CREATE TABLE agronomic_data_log(
241290
site varchar(24),

init/td.sql

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,286 @@ CREATE TABLE tileflow_data(
1616
discharge_mm_qc real);
1717
CREATE INDEX tileflow_data_idx on tileflow_data(uniqueid, plotid, valid);
1818
GRANT SELECT on tileflow_data to nobody;
19+
CREATE TABLE public.water_stage_data (
20+
siteid text,
21+
location text,
22+
date date,
23+
stage real
24+
);
25+
grant select on water_stage_data to nobody;
26+
ALTER TABLE public.water_stage_data OWNER TO mesonet;
27+
CREATE TABLE public.water_quality_data (
28+
siteid text,
29+
plotid text,
30+
location text,
31+
height real,
32+
date date,
33+
sample_type text,
34+
nitrate_n_concentration text,
35+
ammonia_n_concentration text,
36+
total_n_filtered_concentration real,
37+
total_n_unfiltered_concentration real,
38+
ortho_p_filtered_concentration text,
39+
ortho_p_unfiltered_concentration text,
40+
total_p_filtered_concentration text,
41+
total_p_unfiltered_concentration real,
42+
ph real,
43+
water_ec real
44+
);
45+
grant select on water_quality_data to nobody;
46+
ALTER TABLE public.water_quality_data OWNER TO mesonet;
47+
48+
CREATE TABLE public.td_data_dictionary (
49+
sheet_name character varying,
50+
"primary" character varying,
51+
scope_td_tab character varying,
52+
"#_td_sites_collecting_v._12.2015_*est." character varying,
53+
american_units character varying,
54+
bmp_db character varying,
55+
code_column_heading character varying,
56+
comments_for_data_team character varying,
57+
created character varying,
58+
created_by character varying,
59+
cscap_sorting_column character varying,
60+
cscap_team__required_status character varying,
61+
data_type character varying,
62+
frequency character varying,
63+
icasa_code character varying,
64+
"included_in_m&m_export_text" character varying,
65+
methodology_of_cscap_team character varying,
66+
methodology_of_td_team character varying,
67+
modified character varying,
68+
modified_by character varying,
69+
responsibility character varying,
70+
scope_cscap_tab character varying,
71+
short_description character varying,
72+
stewards_code character varying,
73+
stewards_method_name character varying,
74+
stewards_sample_types character varying,
75+
"stewards_units format" character varying,
76+
"td_sites_collecting_v._12.2015" character varying,
77+
td_sorting_column character varying,
78+
td_team__requirement_status character varying,
79+
team character varying,
80+
units character varying,
81+
value_range character varying,
82+
value_range_american_units character varying
83+
);
84+
grant select on td_data_dictionary to nobody;
85+
ALTER TABLE public.td_data_dictionary OWNER TO mesonet;
86+
87+
88+
CREATE TABLE public.weather_data (
89+
siteid text,
90+
station text,
91+
date date,
92+
precipitation real,
93+
relative_humidity real,
94+
air_temp_avg real,
95+
air_temp_min real,
96+
air_temp_max real,
97+
dew_point_temp_avg real,
98+
solar_radiation real,
99+
wind_speed real,
100+
wind_direction real,
101+
et real,
102+
et_method text
103+
);
104+
grant select on weather_data to nobody;
105+
ALTER TABLE public.weather_data OWNER TO mesonet;
106+
107+
108+
CREATE TABLE public.tile_flow_and_n_loads_data (
109+
siteid text,
110+
plotid text,
111+
location text,
112+
date date,
113+
dwm_treatment text,
114+
tile_flow real,
115+
discharge real,
116+
nitrate_n_load real,
117+
nitrate_n_removed real,
118+
tile_flow_filled real,
119+
nitrate_n_load_filled real,
120+
comments text
121+
);
122+
grant select on tile_flow_and_n_loads_data to nobody;
123+
ALTER TABLE public.tile_flow_and_n_loads_data OWNER TO mesonet;
124+
125+
126+
127+
CREATE TABLE public.soil_moisture_data (
128+
siteid text,
129+
plotid text,
130+
location text,
131+
depth real,
132+
date date,
133+
soil_moisture real,
134+
soil_temperature real,
135+
soil_ec real
136+
);
137+
grant select on soil_moisture_data to nobody;
138+
ALTER TABLE public.soil_moisture_data OWNER TO mesonet;
139+
140+
141+
CREATE TABLE public.water_table_data (
142+
siteid text,
143+
plotid text,
144+
location text,
145+
reading_type text,
146+
date date,
147+
water_table_depth real
148+
);
149+
grant select on water_table_data to nobody;
150+
ALTER TABLE public.water_table_data OWNER TO mesonet;
151+
152+
153+
CREATE TABLE public.soil_properties_data (
154+
siteid text,
155+
plotid text,
156+
location text,
157+
subsample text,
158+
depth text,
159+
year integer,
160+
date date,
161+
soil_texture text,
162+
percent_sand real,
163+
percent_silt real,
164+
percent_clay real,
165+
bulk_density real,
166+
hydraulic_conductivity text,
167+
infiltration_rate real,
168+
matric_potential real,
169+
water_content real,
170+
som real,
171+
ph_water real,
172+
ph_salt real,
173+
lime_index real,
174+
neutralizable_acidity real,
175+
cec real,
176+
k_saturation real,
177+
ca_saturation real,
178+
mg_saturation real,
179+
na_saturation real,
180+
k_concentration real,
181+
ca_concentation real,
182+
mg_concentration real,
183+
na_concentration real,
184+
k_amount real,
185+
ca_amount real,
186+
mg_amount real,
187+
sar real,
188+
salinity_paste real,
189+
salinity_water real,
190+
soc real,
191+
total_n real,
192+
no3_concentration text,
193+
nh4_concentration real,
194+
no3_amount real,
195+
nh4_amount real,
196+
p_b1_concentration real,
197+
p_m3_concentration real,
198+
p_b1_amount real
199+
);
200+
alter table soil_properties_data owner to mesonet;
201+
grant select on soil_properties_data to nobody;
202+
203+
CREATE TABLE public.agronomic_data (
204+
siteid text,
205+
plotid text,
206+
location text,
207+
crop text,
208+
trt_2 text,
209+
trt_value_2 text,
210+
year integer,
211+
date date,
212+
leaf_area_index real,
213+
final_plant_population real,
214+
grain_moisture real,
215+
crop_yield real,
216+
standard_moisture text,
217+
whole_plant_biomass real,
218+
vegetative_biomass real,
219+
grain_biomass real,
220+
corn_cob_biomass real,
221+
forage_biomass real,
222+
whole_plant_total_n real,
223+
vegetative_total_n real,
224+
grain_total_n real,
225+
corn_cob_total_n real,
226+
vegetative_total_c real,
227+
grain_total_c real,
228+
corn_cob_total_c real
229+
);
230+
alter table agronomic_data owner to mesonet;
231+
ALTER TABLE public.agronomic_data OWNER TO mesonet;
232+
233+
CREATE TABLE public.agronomic_data_log (
234+
uniqueid character varying(24),
235+
plotid character varying(24),
236+
varname character varying(24),
237+
year smallint,
238+
value character varying(32),
239+
updated timestamp with time zone DEFAULT now()
240+
);
241+
242+
243+
ALTER TABLE public.agronomic_data_log OWNER TO mesonet;
244+
CREATE FUNCTION public.agronomic_insert_before_f() RETURNS trigger
245+
LANGUAGE plpgsql SECURITY DEFINER
246+
AS $$
247+
DECLARE
248+
result INTEGER;
249+
BEGIN
250+
result = (select count(*) from agronomic_data
251+
where uniqueid = new.uniqueid and plotid = new.plotid and
252+
varname = new.varname and year = new.year and
253+
(value = new.value or (value is null and new.value is null))
254+
);
255+
256+
-- Data is duplication, no-op
257+
IF result = 1 THEN
258+
RETURN null;
259+
END IF;
260+
261+
result = (select count(*) from agronomic_data
262+
where uniqueid = new.uniqueid and plotid = new.plotid and
263+
varname = new.varname and year = new.year);
264+
265+
-- Data is a new value!
266+
IF result = 1 THEN
267+
UPDATE agronomic_data SET value = new.value, updated = now()
268+
WHERE uniqueid = new.uniqueid and plotid = new.plotid and
269+
varname = new.varname and year = new.year;
270+
INSERT into agronomic_data_log SELECT * from agronomic_data WHERE
271+
uniqueid = new.uniqueid and plotid = new.plotid and
272+
varname = new.varname and year = new.year;
273+
RETURN null;
274+
END IF;
275+
276+
INSERT into agronomic_data_log (uniqueid, plotid, varname, year, value)
277+
VALUES (new.uniqueid, new.plotid, new.varname, new.year, new.value);
278+
279+
280+
-- The default branch is to return "NEW" which
281+
-- causes the original INSERT to go forward
282+
RETURN new;
283+
284+
END; $$;
285+
286+
CREATE TABLE public.meta_treatment_identifier (
287+
siteid text,
288+
plotid text,
289+
dwmid text,
290+
irrid text,
291+
year integer,
292+
drainage_water_management text,
293+
irrigation text,
294+
comments text
295+
);
296+
alter table meta_treatment_identifier owner to mesonet;
297+
ALTER TABLE public.meta_treatment_identifier OWNER TO mesonet;
298+
19299

20300
-- Storage of water table data
21301
CREATE TABLE watertable_data(

upgrade/sustainablecorn/14.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ CREATE TABLE website_edits(
1212
GRANT ALL on website_edits to nobody;
1313

1414
-- Allow editing
15+
alter table tileflow_data owner to mesonet;
1516
GRANT ALL on tileflow_data to nobody;
17+
alter table decagon_data owner to mesonet;
1618
GRANT ALL on decagon_data to nobody;
19+
alter table watertable_data owner to mesonet;
1720
GRANT ALL on watertable_data to nobody;

0 commit comments

Comments
 (0)