#[ ZACATEK SOUBORU ]#

#DEF <SCHEMA>    food600
#DEF <PRIMARYKEY> idriadok
#DEF <TABLENAME> obraty

#[ CREATE TABLE ZACATEK ]#
    c_stredisk integer NOT NULL DEFAULT 0,
    operace character(2) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    c_dokl integer NOT NULL DEFAULT 0,
    c_karty integer NOT NULL DEFAULT 0,
    mnozstvi numeric(20,10) NOT NULL DEFAULT (0),
    cena numeric(20,10) NOT NULL DEFAULT (0),
    cena_part numeric(20,10) NOT NULL DEFAULT (0),
    zustatek numeric(20,10) NOT NULL DEFAULT (0),
    cena_prum numeric(20,10) NOT NULL DEFAULT (0),
    dan numeric(5,3) NOT NULL DEFAULT 1,
    serial_no character(20) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    poradi integer NOT NULL DEFAULT 0,
    c_data smallint NOT NULL DEFAULT 1,
    idriadok SERIAL,
    spdan numeric(20,10) NOT NULL DEFAULT 0,
    spstupne numeric(20,10) NOT NULL DEFAULT 0,
    zust_old numeric(20,10),
    mnoz_old numeric(20,10),
    cena_prum_old numeric(20,10),
    c_mena_old smallint,
    CONSTRAINT <TABLENAME>_<PRIMARYKEY> PRIMARY KEY (idriadok)
#[ CREATE TABLE KONEC ]#


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

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_c_data') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_c_data;
CREATE INDEX obraty_c_data
    ON <SCHEMA>.obraty USING btree
    (c_data ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_c_karty') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_c_karty;
CREATE INDEX obraty_c_karty
    ON <SCHEMA>.obraty USING btree
    (c_karty ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_c_stredisk') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_c_stredisk;
CREATE INDEX obraty_c_stredisk
    ON <SCHEMA>.obraty USING btree
    (c_stredisk ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_mnozstvi') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_mnozstvi;
CREATE INDEX obraty_mnozstvi
    ON <SCHEMA>.obraty USING btree
    (mnozstvi ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_operace') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_operace;
CREATE INDEX obraty_operace
    ON <SCHEMA>.obraty USING btree
    (operace COLLATE pg_catalog."default" ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_poradi') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_poradi;
CREATE INDEX obraty_poradi
    ON <SCHEMA>.obraty USING btree
    (poradi ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;
-- Index: obraty_spolu


IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obraty_spolu') THEN
DROP INDEX IF EXISTS <SCHEMA>.obraty_spolu ;
CREATE INDEX obraty_spolu ON <SCHEMA>.obraty USING btree
    (c_data ASC NULLS LAST, c_stredisk ASC NULLS LAST, c_dokl ASC NULLS LAST, c_karty ASC NULLS LAST, poradi ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obratyc_dokl') THEN
DROP INDEX IF EXISTS <SCHEMA>.obratyc_dokl;
CREATE INDEX obratyc_dokl
    ON <SCHEMA>.obraty USING btree
    (c_dokl ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;

IF NOT EXISTS (SELECT indexname FROM pg_indexes WHERE schemaname='<SCHEMA>' and tablename='<TABLENAME>' and indexname='obratyidriadok') THEN
DROP INDEX IF EXISTS <SCHEMA>.obratyidriadok;
CREATE INDEX obratyidriadok
    ON <SCHEMA>.obraty USING btree
    (idriadok ASC NULLS LAST)
    TABLESPACE pg_default;
END IF;
 END IF;

IF EXISTS (SELECT table_name FROM information_schema.tables WHERE table_schema='<SCHEMA>' and table_name='obraty') THEN

	IF  NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='obraty' and column_name='zust_old') THEN
		ALTER TABLE <SCHEMA>.obraty ADD COLUMN zust_old numeric(20,10);
	END IF;

	IF  NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='obraty' and column_name='mnoz_old') THEN
		ALTER TABLE <SCHEMA>.obraty ADD COLUMN mnoz_old numeric(20,10);
	END IF;

	IF  NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='obraty' and column_name='cena_prum_old') THEN
		ALTER TABLE <SCHEMA>.obraty ADD COLUMN cena_prum_old numeric(20,10);
	END IF;

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

	IF not EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='obraty' and column_name='sscc') THEN
			ALTER TABLE <SCHEMA>.obraty ADD COLUMN sscc character varying;
	END IF;

END IF;

#[ KONEC SOUBORU ]#