SELECT functions.to_global_col_names(:table_str);

SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.STATECD"  TO ".STATECD" $$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.UNITCD"   TO ".UNITCD"  $$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.COUNTYCD" TO ".COUNTYCD"$$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.PLOT"     TO ".PLOT"    $$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.INVYR"    TO ".INVYR"   $$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.CONDID"   TO ".CONDID"  $$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.SUBP"     TO ".SUBP"    $$);
SELECT functions.rename_if_exists($$ALTER TABLE "TREE" RENAME "TREE.SPCD"     TO ".SPCD"    $$);

SELECT functions.set_col_types(:table_str, ARRAY[
  ('.STATECD' , 'integer')
, ('.UNITCD'  , 'integer')
, ('.COUNTYCD', 'integer')
, ('.PLOT'    , 'integer')
, ('.INVYR'   , 'integer')
, ('.SUBP'    , 'integer')
, ('.CONDID'  , 'integer')
, ('.SPCD'    , 'integer')
]::functions.col_cast[]);

-- ("STATECD", "UNITCD", "COUNTYCD", "PLOT", "INVYR", "CONDID", "SUBP", "TREE", "STATUSCD") is not unique
SELECT functions.create_if_not_exists($$ALTER TABLE "TREE" ADD CONSTRAINT "TREE.unique" UNIQUE ("TREE.CN")$$);
SELECT functions.cluster_once(:table_str, '"TREE.unique"');

SELECT functions.create_if_not_exists($$CREATE INDEX "TREE.parent" ON "TREE" (".STATECD", ".UNITCD", ".COUNTYCD", ".PLOT", ".INVYR", ".CONDID", ".SUBP")$$);
