#[ ZACATEK SOUBORU ]#

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

#[ CREATE TABLE ZACATEK ]#
    c_data smallint NOT NULL DEFAULT 1,
    c_karty integer NOT NULL DEFAULT 0,
    nazev character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    c_druh integer DEFAULT 0,
    c_druhs integer DEFAULT 0,
    c_druhk integer DEFAULT 0,
    c_obalu smallint DEFAULT (0)::smallint,
    cena_prum numeric DEFAULT 0.0000,
    cena_nakup numeric DEFAULT 0.0000,
    obal_obsah numeric DEFAULT 0.0000,
    cena01 numeric DEFAULT 0.0000,
    cena02 numeric DEFAULT 0.0000,
    cena03 numeric DEFAULT 0.0000,
    cena04 numeric DEFAULT 0.0000,
    cena05 numeric DEFAULT 0.0000,
    cena06 numeric DEFAULT 0.0000,
    cena07 numeric DEFAULT 0.0000,
    cena08 numeric DEFAULT 0.0000,
    cena09 numeric DEFAULT 0.0000,
    cena10 numeric DEFAULT 0.0000,
    cena11 numeric DEFAULT 0.0000,
    cena12 numeric DEFAULT 0.0000,
    cena13 numeric DEFAULT 0.0000,
    cena14 numeric DEFAULT 0.0000,
    cena15 numeric DEFAULT 0.0000,
    cena16 numeric DEFAULT 0.0000,
    cena17 numeric DEFAULT 0.0000,
    cena18 numeric DEFAULT 0.0000,
    cena19 numeric DEFAULT 0.0000,
    cena20 numeric DEFAULT 0.0000,
    cena21 numeric DEFAULT 0.0000,
    cena22 numeric DEFAULT 0.0000,
    cena23 numeric DEFAULT 0.0000,
    cena24 numeric DEFAULT 0.0000,
    cena25 numeric DEFAULT 0.0000,
    cena26 numeric DEFAULT 0.0000,
    cena27 numeric DEFAULT 0.0000,
    cena28 numeric DEFAULT 0.0000,
    cena29 numeric DEFAULT 0.0000,
    cena30 numeric DEFAULT 0.0000,
    cena31 numeric DEFAULT 0.0000,
    cena32 numeric DEFAULT 0.0000,
    cena33 numeric DEFAULT 0.0000,
    cena34 numeric DEFAULT 0.0000,
    cena35 numeric DEFAULT 0.0000,
    cena36 numeric DEFAULT 0.0000,
    cena37 numeric DEFAULT 0.0000,
    cena38 numeric DEFAULT 0.0000,
    cena39 numeric DEFAULT 0.0000,
    cena40 numeric DEFAULT 0.0000,
    pocet numeric DEFAULT 1.0000,
    bar_kod1 character(26) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    bar_kod2 character(13) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    kod character(13) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    dan1 character(1) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    dph numeric DEFAULT 0.000,
    nazev1 character(27) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    nazev2 character(27) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    nazev3 character(27) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    nazev4 character(27) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    nazev5 character(27) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    naz1 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    naz2 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    naz3 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    naz4 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    naz5 character(30) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    vaha_obalu numeric DEFAULT 0.0000,
    vaha_koef numeric DEFAULT 0.0000,
    tolerance numeric DEFAULT 0.0000,
    vaha character(10) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    lieh numeric DEFAULT 0.0000,
    objem numeric DEFAULT 0.0000,
    pov_objem numeric DEFAULT 0.0000,
    c_mena smallint NOT NULL DEFAULT (0)::smallint,
    c_mena_n smallint NOT NULL DEFAULT (0)::smallint,
    vyr_postup text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text,
    idriadok SERIAL,
    label smallint DEFAULT 0,
    zmena timestamp without time zone NOT NULL DEFAULT now(),
    stav character(1) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    CONSTRAINT karty_c_karty UNIQUE (c_karty),
    CONSTRAINT karty_nazev UNIQUE (nazev, c_data),
    CONSTRAINT <TABLENAME>_<PRIMARYKEY> PRIMARY KEY (idriadok)
#[ CREATE TABLE KONEC ]#

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

DROP INDEX IF EXISTS <SCHEMA>.c_karty1;
CREATE INDEX c_karty1
    ON <SCHEMA>.karty USING btree
    (c_karty ASC NULLS LAST)
    TABLESPACE pg_default;

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='karty' and column_name='stav') THEN
		ALTER TABLE <SCHEMA>.karty  ADD COLUMN stav character(1) NOT NULL DEFAULT ''::bpchar;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='karty' and column_name='c_karty_master') THEN
		IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='karty' and column_name='c_k_m')          THEN
			ALTER TABLE <SCHEMA>.karty  RENAME c_karty_master TO c_k_m;
		END IF;
	END IF;

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

	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN bar_kod1 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN bar_kod1 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN bar_kod2 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN bar_kod2 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN dan1 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN dan1 SET NOT NULL;

	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev1 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev1 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev2 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev2 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev3 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev3 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev4 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev4 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev5 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN nazev5 SET NOT NULL;

	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz1 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz1 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz2 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz2 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz3 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz3 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz4 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz4 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz5 SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN naz5 SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN vaha SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN vaha SET NOT NULL;
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN vyr_postup SET DEFAULT '';
	ALTER TABLE <SCHEMA>.karty   ALTER COLUMN vyr_postup SET NOT NULL;


	UPDATE <SCHEMA>.karty SET nazev='' WHERE nazev is null;
	UPDATE <SCHEMA>.karty SET bar_kod1='' WHERE bar_kod1 is null;
	UPDATE <SCHEMA>.karty SET bar_kod2='' WHERE bar_kod2 is null;
	UPDATE <SCHEMA>.karty SET dan1='' WHERE dan1 is null;
	UPDATE <SCHEMA>.karty SET nazev1='' WHERE nazev1 is null;
	UPDATE <SCHEMA>.karty SET nazev2='' WHERE nazev2 is null;
	UPDATE <SCHEMA>.karty SET nazev3='' WHERE nazev3 is null;
	UPDATE <SCHEMA>.karty SET nazev4='' WHERE nazev4 is null;
	UPDATE <SCHEMA>.karty SET nazev5='' WHERE nazev5 is null;
	UPDATE <SCHEMA>.karty SET naz1='' WHERE naz1 is null;
	UPDATE <SCHEMA>.karty SET naz2='' WHERE naz2 is null;
	UPDATE <SCHEMA>.karty SET naz3='' WHERE naz3 is null;
	UPDATE <SCHEMA>.karty SET naz4='' WHERE naz4 is null;
	UPDATE <SCHEMA>.karty SET naz5='' WHERE naz5 is null;
	UPDATE <SCHEMA>.karty SET vaha='' WHERE vaha is null;
	UPDATE <SCHEMA>.karty SET vyr_postup='' WHERE vyr_postup is null;

	IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema='<SCHEMA>' and table_name='karty' and constraint_name='karty_nazev') THEN
		ALTER TABLE <SCHEMA>.karty  DROP CONSTRAINT karty_nazev;
	END IF;

	IF NOT EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema='<SCHEMA>' and table_name='karty' and constraint_name='karty_nazev_cdata') THEN
		ALTER TABLE <SCHEMA>.karty  ADD CONSTRAINT karty_nazev_cdata UNIQUE (nazev, c_data);
	END IF;

END IF;

#[ KONEC SOUBORU ]#