-- 
-- Apply upgrade patches to survey definitions database
--

-- Upgrade to:  13.08 from 13.07 =======
-- None
-- Upgrade to:  13.09 from 13.08 =======
-- None

-- Upgrade to:  13.10 from 13.09 =======
CREATE SEQUENCE change_history_seq START 1;
ALTER SEQUENCE change_history_seq OWNER TO ws;

-- Upgrade to:  13.11 from 13.10 =======
CREATE SEQUENCE changeset_seq START 1;
ALTER SEQUENCE changeset_seq OWNER TO ws;

CREATE TABLE changeset (
	id INTEGER DEFAULT NEXTVAL('changeset_seq') CONSTRAINT pk_changeset PRIMARY KEY,
	s_id INTEGER,
  	user_name text,		/* User making change */
  	change_reason text,
  	description text,
  	reversed boolean default false,
  	reversed_by_user text,
	changed_ts TIMESTAMP WITH TIME ZONE
	);
ALTER TABLE changeset OWNER TO ws;

CREATE TABLE change_history (
	id INTEGER DEFAULT NEXTVAL('change_history_seq') CONSTRAINT pk_change_history PRIMARY KEY,
	c_id INTEGER REFERENCES changeset(id) ON DELETE CASCADE,		/* Changeset Id */
  	q_id INTEGER,
  	r_id INTEGER,
  	oname text,
  	old_value text,
  	new_value text,
  	qname text,
  	qtype text,
  	tablename text
	);
ALTER TABLE change_history OWNER TO ws;

-- Upgrade to:  15.11 from 15.10 =======
alter table change_history add column question_column_name text;
alter table change_history add column option_column_name text;

--Upgrade to: 16.07 from 16.06 ===========
CREATE EXTENSION fuzzystrmatch;

-- Upgrade to: 17.11 ============
CREATE SEQUENCE sync_seq START 1;
ALTER SEQUENCE sync_seq OWNER TO ws;

create TABLE sync (
	id INTEGER DEFAULT NEXTVAL('sync_seq') CONSTRAINT pk_sync PRIMARY KEY,
	s_id integer,
	n_id integer,
	prikey integer				-- Primary key of synchronised record
	);
ALTER TABLE sync OWNER TO ws;

CREATE SEQUENCE cat_seq START 1;
ALTER SEQUENCE cat_seq OWNER TO ws;

CREATE TABLE IF NOT EXISTS case_alert_triggered (
	id integer DEFAULT NEXTVAL('cat_seq') CONSTRAINT pk_cat PRIMARY KEY,
	a_id integer,
	table_name text,
	thread text,
	final_status text,
	alert_sent TIMESTAMP WITH TIME ZONE	
	);
ALTER TABLE case_alert_triggered OWNER TO ws;

CREATE SEQUENCE sct_seq START 1;
ALTER SEQUENCE sct_seq OWNER TO ws;

CREATE TABLE IF NOT EXISTS server_calc_triggered (
	id integer DEFAULT NEXTVAL('sct_seq') CONSTRAINT pk_sct PRIMARY KEY,
	n_id integer,
	table_name text,
	question_name text,
	value text,
	thread text,
	updated_value boolean,	  -- the value of the updated flag when this event was triggered
	notification_sent TIMESTAMP WITH TIME ZONE
	);
ALTER TABLE server_calc_triggered OWNER TO ws;

-- Upgrade to: 25.01 ============
-- Remove any duplicates before creating the indexes
DELETE FROM case_alert_triggered a USING case_alert_triggered b
WHERE
    a.id < b.id
	AND a.a_id = b.a_id
	AND a.table_name = b.table_name
	AND a.thread = b. thread;

DELETE FROM server_calc_triggered a USING server_calc_triggered b
WHERE
    a.id < b.id
	AND a.n_id = b.n_id
	AND a.table_name = b.table_name
	AND a.question_name = b.question_name
	AND a.value = b.value
	AND a.thread = b. thread;

-- Add Unique indexes for multi-server subscriber support
CREATE UNIQUE INDEX IF NOT EXISTS case_alert_triggered_unique
ON case_alert_triggered(a_id, table_name, thread);

-- Support the closed-case prune (deletes by table_name + thread); the unique index above
-- leads with a_id so cannot serve this lookup efficiently.
CREATE INDEX IF NOT EXISTS case_alert_triggered_table_thread
ON case_alert_triggered(table_name, thread);

CREATE UNIQUE INDEX IF NOT EXISTS server_calc_triggered_unique
ON server_calc_triggered(n_id, table_name, question_name, value, thread);
