#[ ZACATEK SOUBORU ]#

#DEF <SCHEMA>    food600
#DEF <PRIMARYKEY> id_rov
#DEF <TABLENAME> nlimitrov

#[ CREATE TABLE ZACATEK ]#
    id_limit integer NOT NULL DEFAULT 0,
    id_den integer NOT NULL DEFAULT 0,
    id_rov SERIAL,
    id_chod integer NOT NULL DEFAULT 0,
    limit1 numeric(20,10) NOT NULL DEFAULT 0,
    limit2 numeric(20,10) NOT NULL DEFAULT 0,
    poznamka text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::character varying,
    pozn_int text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::character varying,
    c_kasy integer NOT NULL DEFAULT 0,
    hladina character(2) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    txt1 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    txt2 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    poradie smallint NOT NULL DEFAULT 0,
    label smallint NOT NULL DEFAULT 0,
    c_data smallint NOT NULL DEFAULT 1,
    pocet1 numeric(20,10) NOT NULL DEFAULT 0,
    pocet2 numeric(20,10) NOT NULL DEFAULT 0,
    uzaverka character(17) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    u_cis character(7) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    cenhlad character(2) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    zlava numeric(6,2),
    c_dokl integer NOT NULL DEFAULT 0,
    CONSTRAINT <TABLENAME>_<PRIMARYKEY> PRIMARY KEY (<PRIMARYKEY>)
#[ CREATE TABLE KONEC ]#

#[ IF EXIST ZACATEK ]#
#[ PRIMARYKEY ]#

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='c_dokl') THEN
		ALTER TABLE <SCHEMA>.nlimitrov ADD COLUMN c_dokl integer NOT NULL DEFAULT 0;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='poznamka') THEN
		ALTER TABLE <SCHEMA>.nlimitrov  ALTER COLUMN poznamka TYPE text;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='pozn_int') THEN
		ALTER TABLE <SCHEMA>.nlimitrov  ALTER COLUMN pozn_int TYPE text;
	END IF;

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='id_rov_master') THEN
		ALTER TABLE <SCHEMA>.nlimitrov  ADD COLUMN id_rov_master integer NOT NULL DEFAULT 0;
	END IF;

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='id_sync') THEN
		ALTER TABLE <SCHEMA>.nlimitrov  ADD COLUMN id_sync integer NOT NULL DEFAULT 0;
	END IF;

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='id_rov_master') THEN
		ALTER TABLE <SCHEMA>.nlimitrov  ADD COLUMN id_rov_master integer NOT NULL DEFAULT 0;
	END IF;
	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='nlimitrov' and column_name='id_sync') THEN
		ALTER TABLE <SCHEMA>.nlimitrov  ADD COLUMN id_sync integer NOT NULL DEFAULT 0;
	END IF;

	update <SCHEMA>.nlimitrov set zlava=0 where zlava is null;
	ALTER TABLE <SCHEMA>.nlimitrov   ALTER COLUMN zlava SET DEFAULT 0;
	ALTER TABLE <SCHEMA>.nlimitrov   ALTER COLUMN zlava SET NOT NULL;

END IF;

#[ KONEC SOUBORU ]#