#[ ZACATEK SOUBORU ]#

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

#[ 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 ]#

DROP INDEX IF EXISTS <SCHEMA>.obra_buf_c_dokl;
CREATE INDEX obra_buf_c_dokl
    ON <SCHEMA>.obra_buf USING btree
    (c_dokl ASC NULLS LAST)
    TABLESPACE pg_default;

DROP INDEX IF EXISTS <SCHEMA>.obra_buf_spolu;
CREATE INDEX obra_buf_spolu  ON <SCHEMA>.obra_buf 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;

DROP INDEX IF EXISTS <SCHEMA>.obra_bufidriadok;
CREATE INDEX obra_bufidriadok
    ON <SCHEMA>.obra_buf USING btree
    (idriadok ASC NULLS LAST)
    TABLESPACE pg_default;
    
	IF  NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='obra_buf' and column_name='zust_old') THEN
		ALTER TABLE <SCHEMA>.obra_buf 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='obra_buf' and column_name='mnoz_old') THEN
		ALTER TABLE <SCHEMA>.obra_buf 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='obra_buf' and column_name='cena_prum_old') THEN
		ALTER TABLE <SCHEMA>.obra_buf 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='obra_buf' and column_name='c_mena_old') THEN
		ALTER TABLE <SCHEMA>.obra_buf 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='obra_buf' and column_name='sscc') THEN
		ALTER TABLE <SCHEMA>.obra_buf ADD COLUMN sscc character varying;
	END IF;

END IF;

#[ KONEC SOUBORU ]#