DO
$$
BEGIN

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

		IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='c_mena' and lower(btrim(is_nullable))<>'no') THEN
                	UPDATE      <SCHEMA>.<TABLENAME> SET c_mena = 1 WHERE c_mena IS NULL;
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN c_mena 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='c_mena' and (btrim(column_default)<>'1' or column_default is null) ) THEN
			ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN c_mena SET DEFAULT 1 ;
		END IF;
	END IF;
END IF;

END
$$
