SELECT util.search_path_append('util');

SELECT set_col_types('"COND"', ARRAY[
  ('*STATECD', 'integer')
, ('*UNITCD', 'integer')
, ('*COUNTYCD', 'integer')
, ('locationName', 'integer')
, ('*INVYR', 'integer')
, ('authorEventCode', 'integer')
]::col_cast[]);

SELECT mk_derived_col(('"COND"', 'oldGrowth'), $$
-- created by Brad
-- modified from sftp://nimoy.nceas.ucsb.edu/home/boyle/bien2/geoscrub/fia/flag_fia_oldGrowth.sql
(
    "*COND--STDAGE"::integer>80
    AND ("*COND--TRTCD1"='0' OR "*COND--TRTCD1" IS NULL)
    AND (("*COND--DSTRBCD1"<>'30' AND "*COND--DSTRBCD1"<>'31' AND "*COND--DSTRBCD1"<>'32' AND "*COND--DSTRBCD1"<>'80') OR "*COND--DSTRBCD1" IS NULL)
    AND ("*COND--STUMP_CD_PNWRS"='N' OR "*COND--STUMP_CD_PNWRS" IS NULL)
    AND "*COND--HARVEST_TYPE1_SRS" IS NULL
    AND "*COND--PRESNFCD" IS NULL
    AND ("*COND--STDORGCD"='0' OR "*COND--STDORGCD" IS NULL)
)
OR "*COND--STND_COND_CD_PNWRS"='7'
$$);

SELECT create_if_not_exists($$ALTER TABLE "COND" ADD CONSTRAINT "COND.ID"     UNIQUE ("*COND--CN")$$);
SELECT create_if_not_exists($$ALTER TABLE "COND" ADD CONSTRAINT "COND.unique" UNIQUE ("*STATECD", "*UNITCD", "*COUNTYCD", "locationName", "*INVYR", "authorEventCode")$$);
SELECT cluster_once('"COND"', '"COND.unique"');
