DO
$$
BEGIN
IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok') THEN

-------------------------------------------------------------------    idriadok    -------------------------------------------------------------------
	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok' and lower(btrim(is_nullable))='no') THEN
                UPDATE      <SCHEMA>.<TABLENAME> SET          idriadok = 9 WHERE idriadok IS NULL;
		ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN idriadok SET NOT NULL ;
	END IF;

	IF NOT EXISTS (SELECT column_default FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok' and column_default LIKE 'nextval%') THEN

		IF NOT EXISTS (SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema='<SCHEMA>' and sequence_name= '<TABLENAME>_idriadok_seq') THEN
			CREATE SEQUENCE <SCHEMA>.<TABLENAME>_idriadok_seq AS integer INCREMENT BY 1 MINVALUE 1 START 1 CACHE 1 NO CYCLE ;
			IF EXISTS (SELECT idriadok FROM <SCHEMA>.<TABLENAME>) THEN
				PERFORM SETVAL ('<SCHEMA>.<TABLENAME>_idriadok_seq', (SELECT greatest(coalesce(MAX(idriadok)+3, 1), 1) FROM <SCHEMA>.<TABLENAME>)::int);
			END IF;
		END IF;
		ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN idriadok SET DEFAULT nextval('<SCHEMA>.<TABLENAME>_idriadok_seq'::regclass) ;
	END IF;


-------------------------------------------------------------------    idriadok_master    -------------------------------------------------------------------
	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok_master') THEN
		ALTER TABLE <SCHEMA>.<TABLENAME> ADD COLUMN idriadok_master int4 NOT NULL DEFAULT 0 ;
	ELSE

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok_master' and udt_name<>'int4') THEN
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN idriadok_master TYPE int4 ;
		END IF;

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok_master' and lower(btrim(is_nullable))<>'no') THEN
	                UPDATE      <SCHEMA>.<TABLENAME> SET idriadok_master = 0 WHERE idriadok_master IS NULL;
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN idriadok_master SET NOT NULL  ;
		END IF;

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='idriadok_master' and (btrim(column_default)<>'0' or column_default is null) ) THEN
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN idriadok_master SET DEFAULT 0 ;
		END IF;
	END IF;


-------------------------------------------------------------------    id_sync    -------------------------------------------------------------------
	IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='id_sync') THEN
		ALTER TABLE <SCHEMA>.<TABLENAME> ADD COLUMN id_sync int2 NOT NULL DEFAULT 0 ;
	ELSE

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='id_sync' and udt_name<>'int2') THEN
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN id_sync TYPE int2 ;
		END IF;

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='id_sync' and lower(btrim(is_nullable))<>'no') THEN
	                UPDATE      <SCHEMA>.<TABLENAME> SET id_sync = 0 WHERE id_sync IS NULL;
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN id_sync SET NOT NULL  ;
		END IF;

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='id_sync' and (btrim(column_default)<>'0' or column_default is null) ) THEN
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN id_sync SET DEFAULT 0 ;
		END IF;
	END IF;

END IF;
END
$$
