CREATE TABLE public.newicons (icontype integer NOT NULL DEFAULT 0, id integer NOT NULL DEFAULT 0,  imagedata bytea);
ALTER TABLE groups ADD COLUMN type integer;
UPDATE groups SET type = 0;
ALTER TABLE vehicle ADD COLUMN type integer;
UPDATE vehicle SET type = 0;
ALTER TABLE vehicle ADD COLUMN active boolean;
UPDATE vehicle SET active = TRUE;
ALTER TABLE radio_gw ADD COLUMN name character varying(30);
UPDATE radio_gw SET name = 'unnamed' || \"ID\";
ALTER TABLE radio_gw ADD COLUMN gw_type integer;
UPDATE radio_gw SET gw_type = 0;
ALTER TABLE groups ADD COLUMN r_gw_id integer;
UPDATE groups SET r_gw_id = ( CASE WHEN ((select count(*) from radio_gw ) = 0) THEN 0 ELSE (select min(\"ID\") from radio_gw) END);
update subs_gateway set gateway_id = (CASE WHEN ((select count(*) from radio_gw where \"ID\" = 1 and \"GW_ID\" = 1) = 0) THEN (select min(\"GW_ID\") from radio_gw) ELSE 1 END ), radio_gw_id = (CASE WHEN ((select count(*) from radio_gw where \"ID\" = 1 and \"GW_ID\" = 1) = 0) THEN (select min(\"ID\") from radio_gw) ELSE 1 END) where gateway_id = 1 and radio_gw_id = 1;
CREATE TABLE subscriber_mobile( sc_id serial NOT NULL, phone_imei character varying(20), first_name character varying(50), last_name character varying(50), company character varying(30), os character varying(20), phone_type character varying(20), sip_pswd character varying(30), CONSTRAINT subscriber_mobile_pkey PRIMARY KEY (sc_id)) WITH ( OIDS=FALSE);
ALTER TABLE subscriber_mobile OWNER TO postgres;
CREATE TABLE mobile_contacts(idx serial NOT NULL, sc_id integer NOT NULL, contact_id integer NOT NULL, contact_type character varying(30), contact_type_id integer) WITH ( OIDS=FALSE);
ALTER TABLE mobile_contacts OWNER TO postgres;
ALTER TABLE sms ADD COLUMN user_id integer;
ALTER TABLE sms ALTER COLUMN user_id SET STORAGE PLAIN;
ALTER TABLE users ADD COLUMN backup_user_id integer default 0;
ALTER TABLE users ALTER COLUMN backup_user_id SET STORAGE PLAIN;
UPDATE users SET backup_user_id = -1;
ALTER TABLE subscriber_mobile ADD COLUMN has_gps boolean DEFAULT false;
ALTER TABLE subscriber_mobile ADD COLUMN has_voice boolean DEFAULT false;
ALTER TABLE subscriber_mobile ADD COLUMN has_text boolean DEFAULT false;
ALTER TABLE subscriber_mobile ADD COLUMN has_ticketing boolean DEFAULT false;
ALTER TABLE subscriber_mobile ADD COLUMN email text;
ALTER TABLE subscriber_mobile ADD COLUMN login text;
ALTER TABLE users ADD COLUMN sip_id integer;
ALTER TABLE users ALTER COLUMN sip_id SET STORAGE PLAIN;
ALTER TABLE subscriber_mobile ADD COLUMN lone_worker boolean DEFAULT false;
ALTER TABLE subscriber_mobile ADD COLUMN lone_worker_interval integer DEFAULT 0;
ALTER TABLE emergalarm ADD COLUMN user_id integer;
ALTER TABLE emergalarm ADD COLUMN timegmt_ack integer;
ALTER TABLE geozoneinout ADD COLUMN user_id integer;
ALTER TABLE geozoneinout ADD COLUMN timegmt_ack integer;
ALTER TABLE speedalarm ADD COLUMN user_id integer;
ALTER TABLE speedalarm ADD COLUMN timegmt_ack integer;
ALTER TABLE telemetry_history ADD COLUMN user_id integer;
ALTER TABLE telemetry_history ADD COLUMN timegmt_ack integer;
ALTER TABLE emergalarm ADD COLUMN emergency_type integer;
create table call_patch(idx serial not null, type integer, incoming text, outgoing text, userid integer);
CREATE INDEX msg_sc_id_idx ON messages USING btree (sc_id);
CREATE INDEX secv_idx ON messages USING btree (scevtime);
CREATE INDEX speed_idx ON messages USING btree (speed);
CREATE INDEX timegmt_idx ON messages USING btree (timegmt);
CREATE TABLE users_group(idx serial not null, userid integer not null, grp_id integer not null);
CREATE TABLE sip_manager(sip_id serial not null primary key, id integer not null, type integer not null);
CREATE UNIQUE INDEX unique_idx ON sip_manager (id, type);
ALTER SEQUENCE sip_manager_sip_id_seq RESTART WITH 10;
insert into sip_manager(id, type) select userid, 0 from users where user_type <> 1;
insert into sip_manager(id, type) select id, 1 from groups where type <> 0;
insert into sip_manager(id, type) select sc_id, 2 from subscriber;
insert into sip_manager(id, type) select \"ID\", 4 from radio_gw;
ALTER TABLE users DROP COLUMN sip_id; 
insert into \"userSettings\" (user_id, key, value) SELECT 0,'lanLinxServer','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'lanLinxServer');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'natLinxServer','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'natLinxServer');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'lanSipPort','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'lanSipPort');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'natSipPort','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'natSipPort');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'lanSigPort','41414' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'lanSigPort');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'natSigPort','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'natSigPort');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'rtpStart','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'rtpStart');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'rtpEnd','' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'rtpEnd');
INSERT INTO car(\"name\", id, pattern, displayed_name) VALUES ('linx_01', 138, 'linx_01', 'Linx 1'), ('linx_02', 139, 'linx_02', 'Linx 2');
ALTER TABLE subscriber_mobile ADD COLUMN has_man_down boolean DEFAULT false;
ALTER TABLE gateways ADD COLUMN active boolean default false;
UPDATE gateways SET active = true WHERE \"IP\" IN (SELECT ip FROM reg_gateway);
DROP TABLE reg_gateway;
ALTER TABLE subscriber_mobile RENAME lone_worker TO has_lone_worker;
ALTER TABLE subscriber_mobile ADD COLUMN has_emergency boolean DEFAULT true;
UPDATE subscriber_mobile SET lone_worker_interval = 1 WHERE lone_worker_interval = 0;
ALTER TABLE subscriber_mobile ALTER COLUMN lone_worker_interval SET DEFAULT 1;
ALTER TABLE sms ALTER COLUMN mess TYPE text;
ALTER TABLE alarm ALTER COLUMN mess TYPE text;
CREATE SEQUENCE radio_mobile_idx_seq START WITH 1 INCREMENT BY 1 NO MAXVALUE NO MINVALUE CACHE 1;
CREATE TABLE public.radio_mobile(idx integer NOT NULL DEFAULT nextval('radio_mobile_idx_seq'::regclass), radio_grp_id integer NOT NULL, mobile_grp_id integer NOT NULL);
insert into \"userSettings\" (user_id, key, value) SELECT 0,'sampleRate','8000' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'sampleRate');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'bufferMilliseconds','32' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'bufferMilliseconds');
insert into \"userSettings\" (user_id, key, value) SELECT 0,'bitDepth','16' where not exists (select * from \"userSettings\" where user_id = 0 and key = 'bitDepth');
alter table definesms add column user_id integer NOT NULL DEFAULT 0;
CREATE TABLE msg(idx serial, message text NOT NULL, dest_id int NOT NULL, seq_id text, sched_timegmt integer DEFAULT 0, email text NOT NULL, CONSTRAINT msg_pkey PRIMARY KEY (idx));
CREATE TABLE msg_details(idx serial, msg_id int NOT NULL, sip_id int, timegmt integer, lat double precision NOT NULL DEFAULT 0, lng double precision NOT NULL DEFAULT 0, speed double precision NOT NULL DEFAULT 0, altitude double precision NOT NULL DEFAULT 0, address character varying(255) NOT NULL DEFAULT 'no address'::character varying, is_sent boolean DEFAULT false, is_read boolean DEFAULT false, is_deleted boolean DEFAULT false, CONSTRAINT msg_det_pkey PRIMARY KEY (idx));
ALTER TABLE msg_details ADD CONSTRAINT msg_fk FOREIGN KEY (msg_id) REFERENCES msg (idx) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE;
ALTER TABLE msg_details ADD CONSTRAINT sip_manager_msg_det_fk FOREIGN KEY (sip_id) REFERENCES sip_manager (sip_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE msg ADD CONSTRAINT sip_manager_msg_fk FOREIGN KEY (dest_id) REFERENCES sip_manager (sip_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION;
CREATE INDEX msg_sip_id_idx ON msg_details USING btree (sip_id);
CREATE INDEX msg_timegmt_idx ON msg_details USING btree (timegmt);
ALTER TABLE msg_details ADD COLUMN msg_type integer not null default 0;
ALTER TABLE messages ADD COLUMN tallysman_log_id integer;
ALTER TABLE registration ADD COLUMN has_recordings boolean NOT NULL DEFAULT false;
ALTER TABLE \"userSettings\" alter column key type text;
CREATE INDEX idx_geozone_1 ON public.geozoneinout USING btree (sc_id, preview);
CREATE INDEX idx_emergalarm ON public.emergalarm USING btree (sc_id, preview);
CREATE INDEX idx_speedalarm ON public.speedalarm USING btree (sc_id, preview);
CREATE INDEX idx_telemetryalarm ON public.telemetry_history USING btree (sc_id, ack, alarm);
ALTER TABLE zonename ADD COLUMN \"callout\" BOOLEAN DEFAULT FALSE;
ALTER TABLE zonename ADD COLUMN \"callout_severity\" int DEFAULT 3;
ALTER TABLE place ADD COLUMN \"callout\" BOOLEAN DEFAULT FALSE;
ALTER TABLE place ADD COLUMN \"callout_severity\" int DEFAULT 3;