#[ ZACATEK SOUBORU ]#

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

#[ CREATE TABLE ZACATEK ]#
    c_uzaverka integer NOT NULL DEFAULT 0,
    ucet integer NOT NULL DEFAULT 0,
    autor character(3) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    u_cas character(8) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    u_minut numeric(4,0) NOT NULL DEFAULT 0,
    mutace character(1) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    cas_atrib character(1) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    pocet_h numeric(4,0) NOT NULL DEFAULT 0,
    stul numeric(6,1) NOT NULL DEFAULT 0,
    text_sleva character(16) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    text_link character(25) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    ducis character(4) COLLATE pg_catalog."default" NOT NULL DEFAULT ' '::bpchar,
    lim_datum date,
    id_limit integer NOT NULL DEFAULT 0,
    round50 numeric(4,2) NOT NULL DEFAULT 0,
    pohladavka smallint NOT NULL DEFAULT 0,
    c_data smallint NOT NULL DEFAULT 1,
    idriadok SERIAL,
    id_den integer NOT NULL DEFAULT 0,
    id_chod integer NOT NULL DEFAULT 0,
    id_slave character(2) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar,
    id_miestnost integer NOT NULL DEFAULT 0,
    u_datetime timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    loyalman uuid,
    CONSTRAINT <TABLENAME>_<PRIMARYKEY> PRIMARY KEY (idriadok)
#[ CREATE TABLE KONEC ]#

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

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='sk__fin' AND column_name='loyalman')  THEN
		ALTER TABLE <SCHEMA>.sk__fin   ADD COLUMN loyalman uuid;
	END IF;	

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

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

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='sk_fin' and column_name='round50') THEN
		ALTER TABLE <SCHEMA>.sk__fin ALTER COLUMN round50 TYPE numeric(5, 2);
	END IF;

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='sk__fin' and column_name='id_slave') THEN
		ALTER TABLE <SCHEMA>.sk__fin ADD COLUMN id_slave character(2) NOT NULL DEFAULT '';
	END IF;

	update <SCHEMA>.sk__fin set id_den=0 where id_den is null;
	update <SCHEMA>.sk__fin set id_chod=0 where id_chod is null;

	ALTER TABLE <SCHEMA>.sk__fin  ALTER COLUMN id_den SET DEFAULT 0;
	ALTER TABLE <SCHEMA>.sk__fin  ALTER COLUMN id_den SET NOT NULL;

	ALTER TABLE <SCHEMA>.sk__fin  ALTER COLUMN id_chod SET DEFAULT 0;
	ALTER TABLE <SCHEMA>.sk__fin  ALTER COLUMN id_chod SET NOT NULL;

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

END IF;

#[ KONEC SOUBORU ]#