#[ ZACATEK SOUBORU ]#

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

#[ CREATE TABLE ZACATEK ]#
    idriadok SERIAL,
    stul integer,
    p_left integer,
    p_top integer,
    p_height integer,
    p_width integer,
    obrazok bytea,
    nazov character varying(50) COLLATE pg_catalog."default",
    id_mist integer,
    CONSTRAINT <TABLENAME>_<PRIMARYKEY> PRIMARY KEY (<PRIMARYKEY>)
#[ CREATE TABLE KONEC ]#

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



	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='stul' ) THEN
		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='stul' and lower(data_type)='integer') THEN
			ALTER TABLE <SCHEMA>.selstl ALTER COLUMN stul TYPE numeric(6, 1);
		END IF;
	END IF;

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

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='id_kas') THEN
		ALTER TABLE <SCHEMA>.selstl  ADD COLUMN id_kas character(2) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::bpchar;
	END IF;

	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='id_stul') THEN
		ALTER TABLE <SCHEMA>.selstl  ADD COLUMN id_stul INTEGER NOT NULL DEFAULT 0;
	END IF;
	IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema='<SCHEMA>' and table_name='selstl' and constraint_name='selstl_id_stul') THEN
		ALTER TABLE <SCHEMA>.selstl DROP CONSTRAINT selstl_id_stul;
	END IF;
      IF NOT EXISTS (SELECT constraint_name FROM information_schema.constraint_column_usage WHERE table_schema='<SCHEMA>' AND table_name = 'selstl'  and constraint_name = 'selstl_id_stul') THEN
    		ALTER TABLE <SCHEMA>.selstl   ADD CONSTRAINT selstl_id_stul UNIQUE (id_stul);
	END IF;
	IF NOT EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_schema='<SCHEMA>' and table_name='selstl' and constraint_name='selstl_id_stul_id_kas') THEN
		ALTER TABLE <SCHEMA>.selstl ADD CONSTRAINT selstl_id_stul_id_kas UNIQUE (id_stul, id_kas);
	END IF;

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

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='left') THEN
		ALTER TABLE <SCHEMA>.selstl   RENAME "left" TO p_left;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='top') THEN
		ALTER TABLE <SCHEMA>.selstl   RENAME top TO p_top;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='height') THEN
		ALTER TABLE <SCHEMA>.selstl   RENAME height TO p_height;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='width') THEN
		ALTER TABLE <SCHEMA>.selstl   RENAME width TO p_width;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='stlname') THEN
		ALTER TABLE <SCHEMA>.selstl   RENAME stlname TO jmenostolu;
		ALTER TABLE <SCHEMA>.selstl   ALTER COLUMN jmenostolu TYPE character(15) COLLATE pg_catalog."default";
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='obrazok') THEN
		ALTER TABLE <SCHEMA>.selstl   DROP COLUMN obrazok;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='nazov') THEN
		ALTER TABLE <SCHEMA>.selstl   DROP COLUMN nazov;
	END IF;

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

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='selstl' and column_name='selstl') THEN
		ALTER TABLE <SCHEMA>.selstl  ALTER COLUMN stul TYPE numeric(6, 1);
	END IF;

END IF;

#[ KONEC SOUBORU ]#