SafeDispatch/UpdateHelper/NewFields.sql

122 lines
5.0 KiB
MySQL
Raw Normal View History

CREATE OR REPLACE FUNCTION public.AddNewFields()
RETURNS void
LANGUAGE 'plpgsql'
AS
$BODY$
BEGIN
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'radio_gw' AND column_name = 'deleted') THEN
ALTER TABLE public.radio_gw ADD COLUMN deleted boolean NOT NULL DEFAULT false;
RAISE NOTICE '%', 'ADD COLUMN : deleted in table radio_gw !';
ELSE
RAISE NOTICE '%', 'COLUMN deleted already exists in table radio_gw !';
END IF;
IF EXISTS ( SELECT * FROM pg_constraint WHERE conname = 'uniqueKey_radio_gw' ) THEN
ALTER TABLE public.radio_gw DROP CONSTRAINT "uniqueKey_radio_gw";
RAISE NOTICE '%', 'DROP CONSTRAINT uniqueKey_radio_gw in table radio_gw !';
END IF;
IF NOT EXISTS ( SELECT * FROM pg_indexes WHERE tablename = 'radio_gw' and schemaname = 'public' and indexname = 'uniqueKey_radio_gw' ) THEN
CREATE UNIQUE INDEX "uniqueKey_radio_gw" ON radio_gw ("IP", "GW_ID", deleted) WHERE deleted = false;
RAISE NOTICE '%', 'CREATE UNIQUE INDEX uniqueKey_radio_gw in table radio_gw !';
ELSE
RAISE NOTICE '%', 'UNIQUE INDEX uniqueKey_radio_gw already exists in table radio_gw !';
END IF;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'gateways' AND column_name='deleted') THEN
ALTER TABLE public.gateways ADD COLUMN deleted boolean NOT NULL DEFAULT false;
RAISE NOTICE '%', 'ADD COLUMN : deleted in table gateways !';
ELSE
RAISE NOTICE '%', 'COLUMN deleted already exists in table gateways !';
END IF;
IF EXISTS ( SELECT * FROM pg_constraint WHERE conname = 'uniqueKey_gateways' ) THEN
ALTER TABLE public.gateways DROP CONSTRAINT "uniqueKey_gateways";
RAISE NOTICE '%', 'DROP CONSTRAINT uniqueKey_gateways in table gateways !';
END IF ;
IF NOT EXISTS ( SELECT * FROM pg_indexes WHERE tablename = 'gateways' and schemaname = 'public' and indexname = 'uniqueKey_gateways' ) THEN
CREATE UNIQUE INDEX "uniqueKey_gateways" ON gateways ("IP", deleted) WHERE deleted = false ;
RAISE NOTICE '%', 'CREATE UNIQUE INDEX uniqueKey_gateways in table gateways !';
ELSE
RAISE NOTICE '%', 'UNIQUE INDEX uniqueKey_gateways already exists in table gateways !';
END IF;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'subs_gateway' AND column_name = 'id') THEN
CREATE SEQUENCE IF NOT EXISTS subs_gateway_seq;
ALTER TABLE public.subs_gateway ADD COLUMN id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('subs_gateway_seq'::regclass) ;
RAISE NOTICE '%', 'ADD COLUMN : id in table subs_gateway and set it as primary key!';
ELSE
RAISE NOTICE '%', 'COLUMN id already exists in table subs_gateway !';
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'subs_gateway' AND column_name = 'gateway_id' AND is_nullable = 'NO') THEN
ALTER TABLE public.subs_gateway ALTER COLUMN gateway_id DROP NOT NULL;
RAISE NOTICE '%', 'DROP NOT NULL attribute FOR COLUMN : gateway_id in the table subs_gateway !';
ELSE
RAISE NOTICE '%', 'The attribute NULL already exists for the column : gateway_id in the table subs_gateway !';
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'subs_gateway' AND column_name = 'radio_gw_id' AND is_nullable = 'NO') THEN
ALTER TABLE public.subs_gateway ALTER COLUMN radio_gw_id DROP NOT NULL;
RAISE NOTICE '%', 'DROP NOT NULL attribute FOR COLUMN : radio_gw_id in the table subs_gateway !';
ELSE
RAISE NOTICE '%', 'The attribute NULL already exists for the column : radio_gw_id in the table subs_gateway !';
END IF;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'VoiceRecordings' AND column_name = 'dispatcher_id') THEN
ALTER TABLE public."VoiceRecordings" ADD COLUMN dispatcher_id integer NULL;
RAISE NOTICE '%', 'ADD COLUMN : dispatcher_id in table VoiceRecordings !';
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'VoiceRecordings' AND column_name = 'subscriber_id' AND is_nullable = 'NO') THEN
ALTER TABLE public."VoiceRecordings" ALTER COLUMN subscriber_id DROP NOT NULL;
RAISE NOTICE '%', 'DROP NOT NULL attribute FOR COLUMN : subscriber_id in the table VoiceRecordings !';
ELSE
RAISE NOTICE '%', 'The attribute NULL already exists for the column : subscriber_id in the table VoiceRecordings !';
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'VoiceRecordings' AND column_name = 'group_id' AND is_nullable = 'NO') THEN
ALTER TABLE public."VoiceRecordings" ALTER COLUMN group_id DROP NOT NULL;
RAISE NOTICE '%', 'DROP NOT NULL attribute FOR COLUMN : group_id in the table VoiceRecordings !';
ELSE
RAISE NOTICE '%', 'The attribute NULL already exists for the column : group_id in the table VoiceRecordings !';
END IF;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'vehicle' AND column_name = 'is_deleted' AND is_nullable = 'NO') THEN
ALTER TABLE vehicle ADD COLUMN is_deleted BOOLEAN NOT NULL DEFAULT FALSE;
RAISE NOTICE '%', 'ADD COLUMN : is_deleted in table vehicle and set it to FALSE!';
ELSE
RAISE NOTICE '%', 'COLUMN is_deleted already exists in table vehicle !';
END IF;
END;
$BODY$;