whenever sqlerror exit failure rollback
whenever oserror exit failure rollback

@@types/ut_object.tps
@@types/ut_objects_list.tps
@@types/ut_composite_object.tps
@@types/ut_executable.tps
@@types/ut_assert_result.tps
@@ut_assert.pks
@@types/ut_suite_reporter.tps
@@types/ut_reporters_list.tps
@@types/ut_composite_reporter.tps
@@types/ut_test_object.tps
@@types/ut_test.tps
@@types/ut_test_suite.tps
@@types/ut_reporter_decorator.tps
@@types/ut_dbms_output_suite_reporter.tps
@@ut_utils.pks
@@ut_metadata.pks
@@ut_suite_manager.pks

@@ut_utils.pkb
@@types/ut_assert_result.tpb
@@types/ut_suite_reporter.tpb
@@types/ut_object.tpb
@@types/ut_composite_object.tpb
@@types/ut_test.tpb
@@types/ut_test_suite.tpb
@@types/ut_executable.tpb
@@types/ut_composite_reporter.tpb
@@types/ut_reporter_decorator.tpb
@@types/ut_dbms_output_suite_reporter.tpb
@@ut_metadata.pkb
@@ut_assert.pkb
@@ut_suite_manager.pkb

exit success

Add query of user errors to see error results on CI Server.

whenever sqlerror exit failure rollback
whenever oserror exit failure rollback

@@types/ut_object.tps
@@types/ut_objects_list.tps
@@types/ut_composite_object.tps
@@types/ut_executable.tps
@@types/ut_assert_result.tps
@@ut_assert.pks
@@types/ut_suite_reporter.tps
@@types/ut_reporters_list.tps
@@types/ut_composite_reporter.tps
@@types/ut_test_object.tps
@@types/ut_test.tps
@@types/ut_test_suite.tps
@@types/ut_reporter_decorator.tps
@@types/ut_dbms_output_suite_reporter.tps
@@ut_utils.pks
@@ut_metadata.pks
@@ut_suite_manager.pks

@@ut_utils.pkb
@@types/ut_assert_result.tpb
@@types/ut_suite_reporter.tpb
@@types/ut_object.tpb
@@types/ut_composite_object.tpb
@@types/ut_test.tpb
@@types/ut_test_suite.tpb
@@types/ut_executable.tpb
@@types/ut_composite_reporter.tpb
@@types/ut_reporter_decorator.tpb
@@types/ut_dbms_output_suite_reporter.tpb
@@ut_metadata.pkb
@@ut_assert.pkb
@@ut_suite_manager.pkb

select * from user_errors
where name not like 'BIN$%';

exit success


-- Returns a scan result in such a way that I can get the JSON object back out of the database

CREATE OR REPLACE FUNCTION network_scan.export_scan(_scan_id bigint) RETURNS json AS $$
DECLARE
    scan_json json;
    scan_row network_scan.scans;
    current_host_id bigint;
    host_ids bigint[];
    host_json_array json[];
BEGIN
    -- This basically works in reverse from import_scan, though we have some views that help
    -- simplify returning the data out of the pipe

    -- First, grab the scan from the scan table, we'll need the info later, and this lets us
    -- check that we don't have gibberish coming in
    SELECT * INTO scan_row FROM network_scan.scans WHERE id=_scan_id;
    IF NOT FOUND THEN
        RAISE EXCEPTION 'Scan ID % not found', _scan_id;
    END IF;

    -- The host JSON generation was moved to it's own function
    host_ids := array(SELECT id FROM network_scan.hosts WHERE scan_id=_scan_id);
    FOREACH current_host_id IN ARRAY host_ids
    LOOP
        host_json_array := array_append(
            host_json_array, network_scan.export_host(current_host_id)
        );
    END LOOP;

    -- Final bit get the scan_type back out
    scan_json := json_build_object(
        'pg_id', scan_row.pg_id,
        'hosts', host_json_array,
        'scan_type', scan_row.scan_type,
        'scan_target', scan_row.scan_target
    );

    RETURN scan_json;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

Fix typo on export_scan function

-- Returns a scan result in such a way that I can get the JSON object back out of the database

CREATE OR REPLACE FUNCTION network_scan.export_scan(_scan_id bigint) RETURNS json AS $$
DECLARE
    scan_json json;
    scan_row network_scan.scans;
    current_host_id bigint;
    host_ids bigint[];
    host_json_array json[];
BEGIN
    -- This basically works in reverse from import_scan, though we have some views that help
    -- simplify returning the data out of the pipe

    -- First, grab the scan from the scan table, we'll need the info later, and this lets us
    -- check that we don't have gibberish coming in
    SELECT * INTO scan_row FROM network_scan.scans WHERE id=_scan_id;
    IF NOT FOUND THEN
        RAISE EXCEPTION 'Scan ID % not found', _scan_id;
    END IF;

    -- The host JSON generation was moved to it's own function
    host_ids := array(SELECT id FROM network_scan.hosts WHERE scan_id=_scan_id);
    FOREACH current_host_id IN ARRAY host_ids
    LOOP
        host_json_array := array_append(
            host_json_array, network_scan.export_host(current_host_id)
        );
    END LOOP;

    -- Final bit get the scan_type back out
    scan_json := json_build_object(
        'pg_id', scan_row.id,
        'hosts', host_json_array,
        'scan_type', scan_row.scan_type,
        'scan_target', scan_row.scan_target
    );

    RETURN scan_json;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;


CREATE TABLE file
    ( file_id       INTEGER PRIMARY KEY AUTOINCREMENT
    , file_sha1     BINARY(20)
    , file_name     TEXT UNIQUE
    , file_size     INTEGER NOT NULL
    , file_mime     TEXT NOT NULL
    , file_add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
    );

Add NOT NULL constraint to file.file_name

CREATE TABLE file
    ( file_id       INTEGER PRIMARY KEY AUTOINCREMENT
    , file_sha1     BINARY(20)
    , file_name     TEXT NOT NULL UNIQUE
    , file_size     INTEGER NOT NULL
    , file_mime     TEXT NOT NULL
    , file_add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
    );


CREATE TABLE nodes (node TEXT NOT NULL PRIMARY KEY);
ALTER TABLE nodes OWNER TO "buddycloud-server";

CREATE TABLE node_config (node TEXT NOT NULL REFERENCES nodes (node),
       	     		  "key" TEXT,
			  "value" TEXT,
			  updated TIMESTAMP,
			  PRIMARY KEY (node, "key"));
ALTER TABLE node_config OWNER TO "buddycloud-server";

CREATE TABLE items (node TEXT REFERENCES nodes (node),
       	     	    id TEXT NOT NULL,
		    author TEXT,
		    updated TIMESTAMP,
		    xml TEXT,
		    PRIMARY KEY (node, id));
ALTER TABLE items OWNER TO "buddycloud-server";
CREATE INDEX items_updated ON items (updated);

CREATE TABLE subscriptions (node TEXT REFERENCES nodes (node),
       	     		    "user" TEXT,
			    listener TEXT,
			    subscription TEXT,
 			    updated TIMESTAMP,
			    PRIMARY KEY (node, "user"));
ALTER TABLE subscriptions OWNER TO "buddycloud-server";
CREATE INDEX subscriptions_updated ON subscriptions (updated);

CREATE TABLE affiliations (node TEXT REFERENCES nodes (node),
       	     		   "user" TEXT,
			   affiliation TEXT,
 			   updated TIMESTAMP,
			   PRIMARY KEY (node, "user"));
ALTER TABLE affiliations OWNER TO "buddycloud-server";
CREATE INDEX affiliations_updated ON affiliations (updated);

Revert "Made the ownership of tables really clear. Should help avoid the problem that DD had the other day."

CREATE TABLE nodes (node TEXT NOT NULL PRIMARY KEY);
CREATE TABLE node_config (node TEXT NOT NULL REFERENCES nodes (node),
       	     		  "key" TEXT,
			  "value" TEXT,
			  updated TIMESTAMP,
			  PRIMARY KEY (node, "key"));
CREATE TABLE items (node TEXT REFERENCES nodes (node),
       	     	    id TEXT NOT NULL,
		    author TEXT,
		    updated TIMESTAMP,
		    xml TEXT,
		    PRIMARY KEY (node, id));
CREATE INDEX items_updated ON items (updated);
CREATE TABLE subscriptions (node TEXT REFERENCES nodes (node),
       	     		    "user" TEXT,
			    listener TEXT,
			    subscription TEXT,
 			    updated TIMESTAMP,
			    PRIMARY KEY (node, "user"));
CREATE INDEX subscriptions_updated ON subscriptions (updated);
CREATE TABLE affiliations (node TEXT REFERENCES nodes (node),
       	     		   "user" TEXT,
			   affiliation TEXT,
 			   updated TIMESTAMP,
			   PRIMARY KEY (node, "user"));
CREATE INDEX affiliations_updated ON affiliations (updated);


DROP TABLE IF EXISTS LOAN_MONITORING;

/*
The table LOAN_MONITORING will contain the default behavior of the group loan with individual monitoring on the MFI - Configuration
This information can not be configured through the UI, so it does need to be configured in the script
*/

CREATE TABLE LOAN_MONITORING (
  LOAN_MONITORING_ID INTEGER NOT NULL,
  INDIVIDUAL_MONITORING_FLAG SMALLINT NOT NULL,
  CREATED_BY SMALLINT,
  CREATED_DATE DATE,
  UPDATED_BY SMALLINT,
  UPDATED_DATE DATE,
  VERSION_NO INTEGER NOT NULL,
  PRIMARY KEY(LOAN_MONITORING_ID)
)
ENGINE=InnoDB CHARACTER SET utf8;

-- 0 by default: Don't Allow group loan account with individual monitoring
-- 1: Allow group loan account with individual monitoring

INSERT INTO LOAN_MONITORING (LOAN_MONITORING_ID,INDIVIDUAL_MONITORING_FLAG,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE,VERSION_NO)
VALUES(1,0,NULL,NULL,NULL,NULL,1);

UPDATE DATABASE_VERSION SET DATABASE_VERSION = 148 WHERE DATABASE_VERSION = 147;

Add comment (really,doing this mostly to kick off a new build).

DROP TABLE IF EXISTS LOAN_MONITORING;

/*
The table LOAN_MONITORING will contain the default behavior of the group loan with individual monitoring on the MFI - Configuration
This information can not be configured through the UI, so it does need to be configured in the script
*/

CREATE TABLE LOAN_MONITORING (
  LOAN_MONITORING_ID INTEGER NOT NULL,
  INDIVIDUAL_MONITORING_FLAG SMALLINT NOT NULL,
  CREATED_BY SMALLINT,
  CREATED_DATE DATE,
  UPDATED_BY SMALLINT,
  UPDATED_DATE DATE,
  VERSION_NO INTEGER NOT NULL,
  PRIMARY KEY(LOAN_MONITORING_ID)
)
ENGINE=InnoDB CHARACTER SET utf8;

-- 0: by default: Don't Allow group loan account with individual monitoring
-- 1: Allow group loan account with individual monitoring

INSERT INTO LOAN_MONITORING (LOAN_MONITORING_ID,INDIVIDUAL_MONITORING_FLAG,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE,VERSION_NO)
VALUES(1,0,NULL,NULL,NULL,NULL,1);

UPDATE DATABASE_VERSION SET DATABASE_VERSION = 148 WHERE DATABASE_VERSION = 147;


-- Type of spelling
-- a classic reference table
CREATE TABLE spelling (
  id TINYINT PRIMARY KEY,
  name VARCHAR(256) NOT NULL
);

CREATE TABLE words (
  word VARCHAR(256) NOT NULL,
  hyphenation VARCHAR(256) NOT NULL,
  spelling TINYINT NOT NULL,
  PRIMARY KEY(word, spelling),
  FOREIGN KEY(spelling) REFERENCES spelling(id)
);

INSERT INTO spelling VALUES
(0, 'Orthographische Konferenz von 1901'),
(1, 'Deutsche Rechtschreibreform von 1996');

Add comments on how to create a db and a db user

-- Create a db
-- CREATE DATABASE hyphenation CHARACTER SET utf8 COLLATE utf8_bin;

-- Create a user
-- CREATE USER 'hyphenation'@'localhost' IDENTIFIED BY 'sekret';
-- GRANT ALL ON hyphenation.* TO 'hyphenation'@'localhost';
-- FLUSH PRIVILEGES;

-- Type of spelling
-- a classic reference table
CREATE TABLE spelling (
  id TINYINT PRIMARY KEY,
  name VARCHAR(256) NOT NULL
);

CREATE TABLE words (
  word VARCHAR(256) NOT NULL,
  hyphenation VARCHAR(256) NOT NULL,
  spelling TINYINT NOT NULL,
  PRIMARY KEY(word, spelling),
  FOREIGN KEY(spelling) REFERENCES spelling(id)
);

INSERT INTO spelling VALUES
(0, 'Orthographische Konferenz von 1901'),
(1, 'Deutsche Rechtschreibreform von 1996');


-- MySQL dump 8.22
--
-- Host: localhost    Database: tbdb
---------------------------------------------------------
-- Server version	3.23.54-log

--
-- Dumping data for table 'sitevariables'
--

INSERT INTO sitevariables VALUES ('general/testvar',NULL,'43','A test variable');
INSERT INTO sitevariables VALUES ('web/nologins',NULL,'0','Non-zero value indicates that no non-admin user may log into the Web Interface.');
INSERT INTO sitevariables VALUES ('web/message',NULL,'','Message to place in large lettering under the login message on the Web Interface.');
INSERT INTO sitevariables VALUES ('idle/threshold',NULL,'4','Number of hours of inactivity for a node/expt to be considered idle.');
INSERT INTO sitevariables VALUES ('idle/mailinterval',NULL,'4','Number of hours since sending a swap request before sending another one. (Timing of first one is determined by idle/threshold.)');

Add idle/cc_grp_ldrs sitevar, for when we should start cc'ing leaders on idle msgs.

-- MySQL dump 8.22
--
-- Host: localhost    Database: tbdb
---------------------------------------------------------
-- Server version	3.23.54-log

--
-- Dumping data for table 'sitevariables'
--

INSERT INTO sitevariables VALUES ('general/testvar',NULL,'43','A test variable');
INSERT INTO sitevariables VALUES ('web/nologins',NULL,'0','Non-zero value indicates that no non-admin user may log into the Web Interface.');
INSERT INTO sitevariables VALUES ('web/message',NULL,'','Message to place in large lettering under the login message on the Web Interface.');
INSERT INTO sitevariables VALUES ('idle/threshold',NULL,'4','Number of hours of inactivity for a node/expt to be considered idle.');
INSERT INTO sitevariables VALUES ('idle/mailinterval',NULL,'4','Number of hours since sending a swap request before sending another one. (Timing of first one is determined by idle/threshold.)');
INSERT INTO sitevariables VALUES ('idle/cc_grp_ldrs',NULL,'3','Start CC\'ing group and project leaders on idle messages on the Nth message.');


CREATE DATABASE FAM_SARP;
\c fam_sarp

CREATE TABLE participants (
  subject smallserial NOT NULL,
  email character varying(40) UNIQUE NOT NULL,
  sessions_completed smallint NOT NULL,
  rng_seed integer NOT NULL,
  PRIMARY KEY(subject)
);

CREATE TABLE stimuli (
  id smallserial NOT NULL,
  target character varying(12) NOT NULL,
  semantic_cue_1 character varying(12) NOT NULL,
  semantic_cue_2 character varying(12) NOT NULL,
  semantic_cue_3 character varying(12) NOT NULL,
  episodic_cue character varying(12) NOT NULL,
  PRIMARY KEY(target)
);

COPY stimuli(target, semantic_cue_1, semantic_cue_2, semantic_cue_3, episodic_cue)
FROM 'C:\Users\will\source\FAM_SARP_experiment\db\stimuli_table.csv' DELIMITER ',' CSV HEADER;

Create roll and grant priveleges

CREATE DATABASE FAM_SARP;
\c fam_sarp

CREATE TABLE participants (
  subject smallserial NOT NULL,
  email character varying(40) UNIQUE NOT NULL,
  sessions_completed smallint NOT NULL,
  rng_seed integer NOT NULL,
  PRIMARY KEY(subject)
);

CREATE TABLE stimuli (
  id smallserial NOT NULL,
  target character varying(12) NOT NULL,
  semantic_cue_1 character varying(12) NOT NULL,
  semantic_cue_2 character varying(12) NOT NULL,
  semantic_cue_3 character varying(12) NOT NULL,
  episodic_cue character varying(12) NOT NULL,
  PRIMARY KEY(target)
);

COPY stimuli(target, semantic_cue_1, semantic_cue_2, semantic_cue_3, episodic_cue)
FROM 'C:\Users\will\source\FAM_SARP_experiment\db\stimuli_table.csv' DELIMITER ',' CSV HEADER;

CREATE ROLE will LOGIN;
GRANT ALL PRIVILEGES ON DATABASE "fam_sarp" TO will;
GRANT ALL PRIVILEGES ON SCHEMA public TO will;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO will;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO will;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO will;


SELECT CASE WHEN F(NULL) THEN 'null' WHEN NAME IS NOT NULL THEN name ELSE 'default' END FROM customers

Fix case in test case.

SELECT CASE WHEN F(NULL) THEN 'null' WHEN name IS NOT NULL THEN name ELSE 'default' END FROM customers


INSERT INTO media
(
  title,
  uri,
  rating,
  viewed_at,
  created_at
)
VALUES
("God's Not Dead 2", 'http://www.imdb.com/title/tt4824308/', 1000, '2016-08-24', now())

Fix escaping in data script

INSERT INTO media
(
  title,
  uri,
  rating,
  viewed_at,
  created_at
)
VALUES
('God\'s Not Dead 2', 'http://www.imdb.com/title/tt4824308/', 1000, '2016-08-24', now())


do $$
declare
	selected_role safetymaps.role%rowtype;
begin
	select *
	into selected_role
	from safetymaps.role
	where role = 'kro';

	if not found then
		insert into safetymaps.role(role, protected, description) values
		('kro', true, 'Benodigd voor het laden van kro');
	else
		raise notice 'Role already defined';
	end if;
end $$

create table safetymaps.kro (
	lbl varchar not null,
	ord varchar null,
	src varchar not null,
	disabled bool null,
	constraint kro_pkey primary key (lbl)
);

Add default config to kro table

do $$
declare
	selected_role safetymaps.role%rowtype;
begin
	select *
	into selected_role
	from safetymaps.role
	where role = 'kro';

	if not found then
		insert into safetymaps.role(role, protected, description) values
		('kro', true, 'Benodigd voor het laden van kro');
	else
		raise notice 'Role already defined';
	end if;
end $$

create table safetymaps.kro (
	lbl varchar not null,
	ord varchar null,
	src varchar not null,
	disabled bool null,
	constraint kro_pkey primary key (lbl)
);

insert into safetymaps.kro(lbl, ord, src, disabled)
values('Adres', null, 'dbk', true);

insert into safetymaps.kro(lbl, ord, src, disabled)
values('OMS nummer', null, 'dbk', true);


CREATE VIEW {# geotrek.maintenance #}.v_infrastructures AS (
	SELECT e.geom, e.id, t.*
	FROM infrastructure_infrastructure AS t, infrastructure_infrastructuretype AS b, core_topology AS e
	WHERE t.topo_object_id = e.id AND t.type_id = b.id
	AND e.deleted = FALSE
);

Add UUID to infrastructures view

CREATE VIEW {# geotrek.maintenance #}.v_infrastructures AS (
	SELECT e.geom, e.id, e.uuid, t.*
	FROM infrastructure_infrastructure AS t, infrastructure_infrastructuretype AS b, core_topology AS e
	WHERE t.topo_object_id = e.id AND t.type_id = b.id
	AND e.deleted = FALSE
);


CREATE OR REPLACE FUNCTION cron.Is_Valid_Function(_Function regprocedure)
RETURNS boolean
LANGUAGE sql
STABLE
SET search_path TO public, pg_temp
AS $FUNC$
SELECT TRUE FROM pg_catalog.pg_proc
-- All functions registered in Jobs must return BATCHJOBSTATE which is just an ENUM 'AGAIN' or 'DONE',
-- to indicate whether or not cron.Run() should run the function again or if the job is done.
-- This is to avoid accidents and to force the user to be explicit about what to do,
-- instead of using a plain boolean as return value, which could be misinterpreted.
WHERE oid = $1::oid
AND prorettype::regtype::text = 'batchjobstate'
AND EXISTS (
    -- It's not enough to just register the function using cron.Register().
    -- The user must also explicitly grant execute on the function to the pgcronjob role,
    -- using the command: GRANT EXECUTE ON FUNCTION ... TO pgcronjob.
    -- This is to reduce the risk of accidents from happening, in case of human errors.
    SELECT 1 FROM aclexplode(pg_catalog.pg_proc.proacl)
    WHERE aclexplode.privilege_type = 'EXECUTE'
    AND aclexplode.grantee = (
        SELECT pg_catalog.pg_user.usesysid FROM pg_catalog.pg_user
        WHERE pg_catalog.pg_user.usename = 'pgcronjob'
    )
)
$FUNC$;

ALTER FUNCTION cron.Is_Valid_Function(_Function regprocedure) OWNER TO pgcronjob;

Allow batchjobstate type to exist in any schema, not just public

CREATE OR REPLACE FUNCTION cron.Is_Valid_Function(_Function regprocedure)
RETURNS boolean
LANGUAGE sql
STABLE
SET search_path TO public, pg_temp
AS $FUNC$
SELECT TRUE FROM pg_catalog.pg_proc
-- All functions registered in Jobs must return BATCHJOBSTATE which is just an ENUM 'AGAIN' or 'DONE',
-- to indicate whether or not cron.Run() should run the function again or if the job is done.
-- This is to avoid accidents and to force the user to be explicit about what to do,
-- instead of using a plain boolean as return value, which could be misinterpreted.
WHERE oid = $1::oid
AND prorettype::regtype::text ~ '(^|\.)batchjobstate$'
AND EXISTS (
    -- It's not enough to just register the function using cron.Register().
    -- The user must also explicitly grant execute on the function to the pgcronjob role,
    -- using the command: GRANT EXECUTE ON FUNCTION ... TO pgcronjob.
    -- This is to reduce the risk of accidents from happening, in case of human errors.
    SELECT 1 FROM aclexplode(pg_catalog.pg_proc.proacl)
    WHERE aclexplode.privilege_type = 'EXECUTE'
    AND aclexplode.grantee = (
        SELECT pg_catalog.pg_user.usesysid FROM pg_catalog.pg_user
        WHERE pg_catalog.pg_user.usename = 'pgcronjob'
    )
)
$FUNC$;

ALTER FUNCTION cron.Is_Valid_Function(_Function regprocedure) OWNER TO pgcronjob;


CREATE TABLE IF NOT EXISTS identity_images(
    type VARCHAR,
    image_payload BLOB NOT NULL,
    width int,
    height int
    filesize int,
    resize_target int,
) WITHOUT ROWID;

Fix to identity_images migration sql

CREATE TABLE IF NOT EXISTS identity_images(
    type VARCHAR,
    image_payload BLOB NOT NULL,
    width int,
    height int,
    file_size int,
    resize_target int
) WITHOUT ROWID;


create table Users
       ( Id int not null primary key
       , Name string(128)
       , Email string(128)
       , Password binary(64)
       , Salt binary(64)
       )

create Table Groups
       ( Id not null primary key
       , Name string(128)
       )

create table UserGroupMaps
       ( UserId primary key references Users(Id)
       , GroupId primary key references Groups(Id)
       )

Clean up provider test script migrations.

create table Users
       ( Id int not null primary key
       , Name string(128)
       , Email string(128)
       , Password binary(64)
       , Salt binary(64)
       );

create Table Groups
       ( Id int not null primary key
       , Name string(128)
       );

create table UserGroupMaps
       ( UserId int not null primary key references Users(Id)
       , GroupId int not null primary key references Groups(Id)
       );


CREATE TABLE `pvpstats_faction` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `faction` BIT NOT NULL,
  `level` TINYINT UNSIGNED NOT NULL,
  `date` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE `pvpstats_players` (
  `character_guid` INT UNSIGNED NOT NULL,
  `level` TINYINT UNSIGNED NOT NULL,
  `date` DATETIME NOT NULL,
  PRIMARY KEY (`character_guid`,`date`)
) ENGINE=InnoDB;

DROP TABLE IF EXISTS before CREATE it

DROP TABLE IF EXISTS `pvpstats_faction`;
CREATE TABLE `pvpstats_faction` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `faction` BIT NOT NULL,
  `level` TINYINT UNSIGNED NOT NULL,
  `date` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

DROP TABLE IF EXISTS `pvpstats_players`;
CREATE TABLE `pvpstats_players` (
  `character_guid` INT UNSIGNED NOT NULL,
  `level` TINYINT UNSIGNED NOT NULL,
  `date` DATETIME NOT NULL,
  PRIMARY KEY (`character_guid`,`date`)
) ENGINE=InnoDB;


# --- !Ups

ALTER TABLE "party" DROP "duns";
ALTER TABLE audit."party" DROP "duns";

# --- !Downs

ALTER TABLE "party" ADD "duns" numeric(9);
ALTER TABLE "party" RENAME "url" TO "url_old";
ALTER TABLE "party" ADD "url" text;
UPDATE "party" SET url = url_old;
ALTER TABLE "party" DROP "url_old";

COMMENT ON COLUMN "party"."duns" IS 'http://en.wikipedia.org/wiki/DUNS';

ALTER TABLE audit."party" ADD "duns" numeric(9);
ALTER TABLE audit."party" RENAME "url" TO "url_old";
ALTER TABLE audit."party" ADD "url" text;
DO $do$ BEGIN
EXECUTE $$GRANT UPDATE ON TABLE audit.party TO $$ || quote_ident(current_user);;
UPDATE audit.party SET url = url_old;;
EXECUTE $$REVOKE UPDATE ON TABLE audit.party FROM $$ || quote_ident(current_user);;
END;; $do$;
ALTER TABLE audit."party" DROP "url_old";

Reset duns on down evolution

# --- !Ups

ALTER TABLE "party" DROP "duns";
ALTER TABLE audit."party" DROP "duns";

# --- !Downs

ALTER TABLE "party" ADD "duns" numeric(9);
ALTER TABLE "party" RENAME "url" TO "url_old";
ALTER TABLE "party" ADD "url" text;
UPDATE "party" SET url = url_old;
ALTER TABLE "party" DROP "url_old";

COMMENT ON COLUMN "party"."duns" IS 'http://en.wikipedia.org/wiki/DUNS';

UPDATE party SET duns = 0 WHERE id > 0 AND NOT EXISTS (SELECT * FROM account WHERE account.id = party.id);

ALTER TABLE audit."party" ADD "duns" numeric(9);
ALTER TABLE audit."party" RENAME "url" TO "url_old";
ALTER TABLE audit."party" ADD "url" text;
DO $do$ BEGIN
EXECUTE $$GRANT UPDATE ON TABLE audit.party TO $$ || quote_ident(current_user);;
UPDATE audit.party SET url = url_old;;
EXECUTE $$REVOKE UPDATE ON TABLE audit.party FROM $$ || quote_ident(current_user);;
END;; $do$;
ALTER TABLE audit."party" DROP "url_old";


/*
Sample grant for PostgreSQL

CREATE ROLE queue LOGIN
  PASSWORD '[CHANGE ME]'
  NOSUPERUSER NOINHERIT NOCREATEDB NOCREATEROLE;

*/

--
-- Table structure for table `queue`
--

DROP TABLE IF EXISTS queue;

CREATE TABLE queue
(
  queue_id serial NOT NULL,
  queue_name character varying(100) NOT NULL,
  timeout smallint NOT NULL DEFAULT 30,
  CONSTRAINT queue_pk PRIMARY KEY (queue_id)
)
WITH (OIDS=FALSE);
ALTER TABLE queue OWNER TO queue;

-- --------------------------------------------------------
--
-- Table structure for table `message`
--

DROP TABLE IF EXISTS message;

CREATE TABLE message
(
  message_id bigserial NOT NULL,
  queue_id integer,
  handle character(32),
  body character varying(8192) NOT NULL,
  md5 character(32) NOT NULL,
  timeout real,
  created integer,
  CONSTRAINT message_pk PRIMARY KEY (message_id),
  CONSTRAINT message_ibfk_1 FOREIGN KEY (queue_id)
      REFERENCES queue (queue_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (OIDS=FALSE);
ALTER TABLE message OWNER TO queue;

Fix ZF-11050: Zend_Queue can duplicate messages when using Postgres

/*
Sample grant for PostgreSQL

CREATE ROLE queue LOGIN
  PASSWORD '[CHANGE ME]'
  NOSUPERUSER NOINHERIT NOCREATEDB NOCREATEROLE;

*/

--
-- Table structure for table `queue`
--

DROP TABLE IF EXISTS queue;

CREATE TABLE queue
(
  queue_id serial NOT NULL,
  queue_name character varying(100) NOT NULL,
  timeout smallint NOT NULL DEFAULT 30,
  CONSTRAINT queue_pk PRIMARY KEY (queue_id)
)
WITH (OIDS=FALSE);
ALTER TABLE queue OWNER TO queue;

-- --------------------------------------------------------
--
-- Table structure for table `message`
--

DROP TABLE IF EXISTS message;

CREATE TABLE message
(
  message_id bigserial NOT NULL,
  queue_id integer,
  handle character(32),
  body character varying(8192) NOT NULL,
  md5 character(32) NOT NULL,
  timeout double precision,
  created integer,
  CONSTRAINT message_pk PRIMARY KEY (message_id),
  CONSTRAINT message_ibfk_1 FOREIGN KEY (queue_id)
      REFERENCES queue (queue_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (OIDS=FALSE);
ALTER TABLE message OWNER TO queue;


ALTER TABLE partners ADD COLUMN banned_at TIMESTAMP WITH TIME ZONE;
ALTER TABLE partners ADD COLUMN ban_reason VARCHAR(4098);

Store the information about who initiated the ban

ALTER TABLE partners ADD COLUMN banned_at TIMESTAMP WITH TIME ZONE;
ALTER TABLE partners ADD COLUMN ban_reason VARCHAR(4098);
ALTER TABLE partners ADD COLUMN banned_by BIGINT;


SELECT
  set,script,scale,
  pg_size_pretty(avg(dbsize)::int8) AS db_size,
--  pg_size_pretty(avg(dbsize)::int8 / scale) AS size_per_scale,
  clients,rate_limit,
  round(avg(tps)) as tps,
  round(avg(tps)/clients) as tps_per_client,
  round(1000 * avg(max_latency))/1000 AS max_latency ,
  to_char(avg(end_time -  start_time),'HH24:MI:SS') AS runtime
FROM tests
GROUP BY set,script,scale,clients,rate_limit
ORDER BY set,script,scale,clients,rate_limit;

Tweak tps per client report

SELECT
  set,script,scale,
  pg_size_pretty(avg(dbsize)::int8) AS db_size,
--  pg_size_pretty(avg(dbsize)::int8 / scale) AS size_per_scale,
  clients,rate_limit,
  round(avg(tps)) as tps,
  round(avg(tps)/clients) as tps_per_client,
  round(1000 * avg(percentile_90_latency))/1000 AS "90%<",
  round(1000 * avg(max_latency))/1000 AS max_latency
--  to_char(avg(end_time -  start_time),'HH24:MI:SS') AS runtime
FROM tests
GROUP BY set,script,scale,clients,rate_limit
ORDER BY set,script,scale,clients,rate_limit;


-- CIRCUS CS DB Migration 12

ALTER TABLE series_list DROP COLUMN is_deleted;
ALTER TABLE study_list DROP COLUMN is_deleted;
ALTER TABLE patient_list DROP COLUMN is_deleted;

Add "IF EXISTS" in drop column SQL

-- CIRCUS CS DB Migration 12

ALTER TABLE series_list DROP COLUMN IF EXISTS is_deleted;
ALTER TABLE study_list DROP COLUMN IF EXISTS is_deleted;
ALTER TABLE patient_list DROP COLUMN IF EXISTS is_deleted;


CREATE TABLE IF NOT EXISTS projects (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz DEFAULT current_timestamp,
  modified_at timestamptz DEFAULT current_timestamp,

  user uuid NOT NULL REFERENCES users(id),
  name text NOT NULL,

  width bigint NOT NULL,
  height bigint NOT NULL,
  layout text NOT NULL
) WITH (OIDS=FALSE);

CREATE TABLE IF NOT EXISTS pages (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz DEFAULT current_timestamp,

  user uuid NOT NULL REFERENCES users(id),
  project uuid NOT NULL REFERENCES projects(id),
  name text NOT NULL,
  data text NOT NULL,

  width bigint NOT NULL,
  height bigint NOT NULL,
  layout text NOT NULL,
) WITH (OIDS=FALSE);

Improve projects and pages migration file.

CREATE TABLE IF NOT EXISTS projects (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz DEFAULT current_timestamp,
  modified_at timestamptz DEFAULT current_timestamp,

  "user" uuid NOT NULL REFERENCES users(id),
  name text NOT NULL
) WITH (OIDS=FALSE);

CREATE TABLE IF NOT EXISTS pages (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz DEFAULT current_timestamp,

  "user" uuid NOT NULL REFERENCES users(id),
  project uuid NOT NULL REFERENCES projects(id),
  name text NOT NULL,
  data text,

  width bigint NOT NULL,
  height bigint NOT NULL,
  layout text NOT NULL
) WITH (OIDS=FALSE);


drop table if exists entries;
create table entries (
  slug text primary key,
  url text not null,
  timestamp datetime default CURRENT_TIMESTAMP
);

Include click count for each shortened url

drop table if exists entries;
create table entries (
  slug text primary key,
  url text not null,
  click_count integer default 0,
  timestamp datetime default CURRENT_TIMESTAMP
);


DROP TABLE IF EXISTS results_redux CASCADE;
CREATE TABLE results_redux (
	id SERIAL NOT NULL UNIQUE,									-- index of the result
	task TEXT NOT NULL,											-- which task was undertaken
	participant TEXT NOT NULL DEFAULT 'sql',					-- a unique identifier for the participant
	runtime TEXT NOT NULL,										-- the runtime of the task
	ending TEXT NOT NULL,										-- if the run was ended without winning
	created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),	-- when the task was completed
	updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),	-- when the task record was updated
	mode TEXT NOT NULL,											-- the task information, what mode it's in
	agent TEXT NOT NULL,										-- the user agent of the user performing the task
	robot_count INTEGER NOT NULL DEFAULT 0						-- the number of roboots involved in the task.
);

DROP INDEX IF EXISTS results_tasks_idx;
CREATE INDEX results_tasks_idx ON results_redux(task);

DROP INDEX IF EXISTS results_tasks_with_mode_idx;
CREATE INDEX results_tasks_with_mode_idx ON results_redux(task,mode);

DROP INDEX IF EXISTS results_participant_idx;
CREATE INDEX results_participant_idx ON results_redux(participant);

Add field for "extra" to DB.

DROP TABLE IF EXISTS results_redux CASCADE;
CREATE TABLE results_redux (
	id SERIAL NOT NULL UNIQUE,									-- index of the result
	task TEXT NOT NULL,											-- which task was undertaken
	participant TEXT NOT NULL DEFAULT 'sql',					-- a unique identifier for the participant
	runtime TEXT NOT NULL,										-- the runtime of the task
	ending TEXT NOT NULL,										-- if the run was ended without winning
	created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),	-- when the task was completed
	updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),	-- when the task record was updated
	mode TEXT NOT NULL,											-- the task information, what mode it's in
	agent TEXT NOT NULL,										-- the user agent of the user performing the task
	extra JSONB NOT NULL DEFAULT '{}',							-- "extra" task information
	robot_count INTEGER NOT NULL DEFAULT 0						-- the number of roboots involved in the task.
);

DROP INDEX IF EXISTS results_tasks_idx;
CREATE INDEX results_tasks_idx ON results_redux(task);

DROP INDEX IF EXISTS results_tasks_with_mode_idx;
CREATE INDEX results_tasks_with_mode_idx ON results_redux(task,mode);

DROP INDEX IF EXISTS results_participant_idx;
CREATE INDEX results_participant_idx ON results_redux(participant);


INSERT INTO ensembl_karyotypes (
    assembly_id,
    karyotype
) (
select
    assembly_id,
    karyotype
from load_karyotypes
) ON CONFLICT (assembly_id) DO UPDATE
SET
    karyotype = excluded.karyotype
;

Convert karyotype data to JSON

INSERT INTO ensembl_karyotypes (
    assembly_id,
    karyotype
) (
select
    assembly_id,
    karyotype::json
from load_karyotypes
) ON CONFLICT (assembly_id) DO UPDATE
SET
    karyotype = excluded.karyotype
;


DROP TABLE dbs_registry.vocab_metadata;
DROP TABLE dbs_registry.harvest_parameter;

CREATE  TABLE `dbs_registry`.`registry_object_identifier_relationships` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `registry_object_id` MEDIUMINT UNSIGNED NOT NULL ,
  `related_object_key` VARCHAR(255) NULL ,
  `related_object_identifier` VARCHAR(255) NOT NULL ,
  `related_info_type` VARCHAR(45) NULL ,
  `related_object_identifier_type` VARCHAR(45) NOT NULL ,
  `relation_type` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );

CREATE  TABLE `dbs_registry`.`registry_object_identifiers` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `registry_object_id` MEDIUMINT UNSIGNED NOT NULL ,
  `identifier` VARCHAR(255) NOT NULL ,
  `identifier_type`  VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) );

Remove database names and add tag tables

DROP TABLE vocab_metadata;
DROP TABLE harvest_parameter;

CREATE  TABLE `registry_object_identifier_relationships` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `registry_object_id` MEDIUMINT UNSIGNED NOT NULL ,
  `related_object_key` VARCHAR(255) NULL ,
  `related_object_identifier` VARCHAR(255) NOT NULL ,
  `related_info_type` VARCHAR(45) NULL ,
  `related_object_identifier_type` VARCHAR(45) NOT NULL ,
  `relation_type` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );

CREATE  TABLE `registry_object_identifiers` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `registry_object_id` MEDIUMINT UNSIGNED NOT NULL ,
  `identifier` VARCHAR(255) NOT NULL ,
  `identifier_type`  VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) );

CREATE TABLE `registry_object_tags` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `key` varchar(255) NOT NULL,
  `tag` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14855 DEFAULT CHARSET=utf8


SET search_path = relation, pg_catalog;

INSERT INTO "type" (name) VALUES ('self');

Define dummy relations for geospatial schema

SET search_path = relation, pg_catalog;

INSERT INTO "type" (name) VALUES ('self');

-- Dummy relations to satisfy geospatial requirements
SELECT relation.define_reverse(
    'HandoverRelation->Cell',
    relation.define(
        'Cell->HandoverRelation',
        $$SELECT 0 as source_id, 0 as target_id WHERE false;$$
    )
);

SELECT relation.define(
    'real_handover',
    $$SELECT 0 as source_id, 0 as target_id WHERE false;$$
);


-- Remove not null constraint on age tranche (data is incomplete)
ALTER TABLE agg_child_health ALTER COLUMN age_tranche DROP NOT NULL;

Change aggregation columns to nullable

-- Remove not null constraint on aggregated columns (data is sometimes null)
ALTER TABLE agg_child_health ALTER COLUMN age_tranche DROP NOT NULL;
ALTER TABLE agg_child_health ALTER COLUMN disabled DROP NOT NULL;
ALTER TABLE agg_child_health ALTER COLUMN resident DROP NOT NULL;
ALTER TABLE agg_child_health ALTER COLUMN caste DROP NOT NULL;
ALTER TABLE agg_child_health ALTER COLUMN minority DROP NOT NULL;
ALTER TABLE agg_child_health ALTER COLUMN gender DROP NOT NULL;
ALTER TABLE agg_ccs_record ALTER COLUMN registration_trimester_at_delivery DROP NOT NULL;
ALTER TABLE agg_ccs_record ALTER COLUMN disabled DROP NOT NULL;
ALTER TABLE agg_ccs_record ALTER COLUMN resident DROP NOT NULL;
ALTER TABLE agg_ccs_record ALTER COLUMN caste DROP NOT NULL;
ALTER TABLE agg_ccs_record ALTER COLUMN minority DROP NOT NULL;
ALTER TABLE agg_thr_data ALTER COLUMN disabled DROP NOT NULL;
ALTER TABLE agg_thr_data ALTER COLUMN resident DROP NOT NULL;
ALTER TABLE agg_thr_data ALTER COLUMN caste DROP NOT NULL;
ALTER TABLE agg_thr_data ALTER COLUMN minority DROP NOT NULL;


-- Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
-- Copyright [2016-2020] EMBL-European Bioinformatics Institute
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

CREATE UNIQUE INDEX stable_id_db_type ON stable_id_lookup(stable_id, db_type, object_type);
CREATE UNIQUE INDEX stable_id_object_type ON stable_id_lookup(stable_id, object_type);

Remove unique - some duplicated exists.

-- Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
-- Copyright [2016-2020] EMBL-European Bioinformatics Institute
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

CREATE INDEX stable_id_db_type ON stable_id_lookup(stable_id, db_type, object_type);
CREATE INDEX stable_id_object_type ON stable_id_lookup(stable_id, object_type);


INSERT INTO `ssbo_ticketing`.`tickets`
(`code`, `firstname`, `lastname`, `street`, `housenumber`, `postalcode`, `town`, `date`)
VALUES
('1911332118324935400', 'Max', 'Mustermann', 'Musterstraße', '1a', '12345', 'Musterdorf', '2014-02-07 16:02:41', '0'),
('9751755374018103000', 'Erika', 'Mustermann', 'Musterweg', '9', '54321', 'Musterstadt', '2014-02-07 16:31:00', '1');

Update demo data according to new DB

INSERT INTO `ssbo_ticketing`.`tickets`
(`code`, `firstname`, `lastname`, `street`, `housenumber`, `postalcode`, `town`, `email`, `date`)
VALUES
('2014-06-15_16:22:00_JZo_123', 'John', 'Zoidberg', 'Zukunftsstraße', '1a', '12345', 'Neu-New-York', 'wuwuwu@wu.wu', '2014-06-15 16:22:00'),
('2014-06-15_16:23:15_MMu_999', 'Max', 'Mustermann', 'Musterweg', '9', '54321', 'Musterstadt', 'max_mustermann@muster.de', '2014-06-15 16:23:15');


SELECT
	t.TaskDescription
	, t.StartDate
	, t.DueDate
	, tt.TypeDescription
	, t.PercentComplete
	, t.PriorityCode
	, t.TaskID
	, t.TaskSeqNum
	, t.Complete
	, t.TypeCode AS TaskType
	, '' AS RowMod
	, cust.CustID AS ID
	, cust.CustNum
	, t.Company
	, sr.SalesRepCode
FROM
	Erp.Task t with(nolock)

	LEFT OUTER JOIN Erp.SalesRep sr with(nolock) ON
	sr.Company = t.Company
	AND sr.SalesRepCode = t.SalesRepCode

	LEFT OUTER JOIN Erp.TaskType tt with(nolock) ON
	tt.Company = t.Company
	AND tt.TypeCode = t.TypeCode

	INNER JOIN Erp.Customer cust with(nolock) ON
	cust.Company = t.Company
	AND cust.CustNum = t.Key1
WHERE
	t.RelatedToFile = 'Customer'
	AND (sr.EMailAddress IS NULL OR sr.EMailAddress = '{EmailAddress}')
	AND t.Complete = 0
	--AND t.Company = 'YourCompanyID'  -- Set this to a specific company ID if you have more than one

Comment out WHERE clause that limits tasks to currently logged in user.

SELECT
	t.TaskDescription
	, t.StartDate
	, t.DueDate
	, tt.TypeDescription
	, t.PercentComplete
	, t.PriorityCode
	, t.TaskID
	, t.TaskSeqNum
	, t.Complete
	, t.TypeCode AS TaskType
	, '' AS RowMod
	, cust.CustID AS ID
	, cust.CustNum
	, t.Company
	, sr.SalesRepCode
FROM
	Erp.Task t with(nolock)

	LEFT OUTER JOIN Erp.SalesRep sr with(nolock) ON
	sr.Company = t.Company
	AND sr.SalesRepCode = t.SalesRepCode

	LEFT OUTER JOIN Erp.TaskType tt with(nolock) ON
	tt.Company = t.Company
	AND tt.TypeCode = t.TypeCode

	INNER JOIN Erp.Customer cust with(nolock) ON
	cust.Company = t.Company
	AND cust.CustNum = t.Key1
WHERE
	t.RelatedToFile = 'Customer'
	--AND (sr.EMailAddress IS NULL OR sr.EMailAddress = '{EmailAddress}')
	AND t.Complete = 0
	--AND t.Company = 'YourCompanyID'  -- Set this to a specific company ID if you have more than one


CREATE TABLE "queue_log" (
    "id" SERIAL,
    "time" TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
    "callid" character varying(50) NOT NULL,
    "queuename" character varying(50) NOT NULL,
    "agent" character varying(50) NOT NULL,
    "event" character varying(20) NOT NULL,
    "data" character varying(50) NOT NULL,
    "data1" character varying(50) NOT NULL,
    "data2" character varying(50) NOT NULL,
    "data3" character varying(50) NOT NULL,
    "data4" character varying(50) NOT NULL,
    "data5" character varying(50) NOT NULL,
    CONSTRAINT queue_log_pkey PRIMARY KEY (id)
) WITHOUT OIDS;

Set default values for DDL queue_log in PostgreSQL

CREATE TABLE "queue_log" (
    "id" SERIAL,
    "time" TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
    "callid" character varying(50) NOT NULL DEFAULT '',
    "queuename" character varying(50) NOT NULL DEFAULT '',
    "agent" character varying(50) NOT NULL DEFAULT '',
    "event" character varying(20) NOT NULL DEFAULT '',
    "data" character varying(50) NOT NULL DEFAULT '',
    "data1" character varying(50) NOT NULL DEFAULT '',
    "data2" character varying(50) NOT NULL DEFAULT '',
    "data3" character varying(50) NOT NULL DEFAULT '',
    "data4" character varying(50) NOT NULL DEFAULT '',
    "data5" character varying(50) NOT NULL DEFAULT '',
    CONSTRAINT queue_log_pkey PRIMARY KEY (id)
) WITHOUT OIDS;


CREATE TABLE IF NOT EXISTS `FILES` (
  `ID` bigint(20) NOT NULL,
  `ETAG` char(36) NOT NULL,
  `PREVIEW_ID` bigint(20) DEFAULT NULL,
  `CREATED_ON` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `CREATED_BY` bigint(20) NOT NULL,
  `METADATA_TYPE` ENUM('S3', 'EXTERNAL', 'PREVIEW') NOT NULL,
  `CONTENT_TYPE` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
  `CONTENT_SIZE` bigint(20) DEFAULT NULL,
  `CONTENT_MD5` varchar(100) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
  `BUCKET_NAME` varchar(100) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
  `NAME` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `KEY` varchar(700) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `STORAGE_LOCATION_ID` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `KEY_KEY` (`KEY`),
  KEY `MD5_KEY` (`CONTENT_MD5`),
  CONSTRAINT `PREVEIW_ID_FK` FOREIGN KEY (`PREVIEW_ID`) REFERENCES `FILES` (`ID`) ON DELETE SET NULL,
  CONSTRAINT `FILE_CREATED_BY_FK` FOREIGN KEY (`CREATED_BY`) REFERENCES `JDOUSERGROUP` (`ID`) ON DELETE CASCADE,
  CONSTRAINT `FILE_STORAGE_LOCATION_ID_FK` FOREIGN KEY (`STORAGE_LOCATION_ID`) REFERENCES `STORAGE_LOCATION` (`ID`)
)

Revert "remove not null on created on"

CREATE TABLE IF NOT EXISTS `FILES` (
  `ID` bigint(20) NOT NULL,
  `ETAG` char(36) NOT NULL,
  `PREVIEW_ID` bigint(20) DEFAULT NULL,
  `CREATED_ON` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `CREATED_BY` bigint(20) NOT NULL,
  `METADATA_TYPE` ENUM('S3', 'EXTERNAL', 'PREVIEW') NOT NULL,
  `CONTENT_TYPE` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
  `CONTENT_SIZE` bigint(20) DEFAULT NULL,
  `CONTENT_MD5` varchar(100) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
  `BUCKET_NAME` varchar(100) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
  `NAME` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `KEY` varchar(700) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `STORAGE_LOCATION_ID` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `KEY_KEY` (`KEY`),
  KEY `MD5_KEY` (`CONTENT_MD5`),
  CONSTRAINT `PREVEIW_ID_FK` FOREIGN KEY (`PREVIEW_ID`) REFERENCES `FILES` (`ID`) ON DELETE SET NULL,
  CONSTRAINT `FILE_CREATED_BY_FK` FOREIGN KEY (`CREATED_BY`) REFERENCES `JDOUSERGROUP` (`ID`) ON DELETE CASCADE,
  CONSTRAINT `FILE_STORAGE_LOCATION_ID_FK` FOREIGN KEY (`STORAGE_LOCATION_ID`) REFERENCES `STORAGE_LOCATION` (`ID`)
)


-- phpMyAdmin SQL Dump
-- version 4.7.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Aug 23, 2017 at 08:05 AM
-- Server version: 10.1.23-MariaDB-9+deb9u1
-- PHP Version: 7.0.22-1~dotdeb+8.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Database: `codymaze`
--

-- --------------------------------------------------------

--
-- Table structure for table `moves`
--

CREATE TABLE `moves` (
  `telegram_id` int(11) NOT NULL,
  `reached_on` datetime DEFAULT NULL,
  `cell` char(2) CHARACTER SET ascii NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `user_status`
--

CREATE TABLE `user_status` (
  `telegram_id` int(11) NOT NULL,
  `completed` bit(1) NOT NULL DEFAULT b'0',
  `certificate_id` char(36) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user_status`
--
ALTER TABLE `user_status`
  ADD PRIMARY KEY (`telegram_id`);
COMMIT;

Add certificate generation data on user table

-- phpMyAdmin SQL Dump
-- version 4.7.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Aug 23, 2017 at 03:54 PM
-- Server version: 10.1.23-MariaDB-9+deb9u1
-- PHP Version: 7.0.22-1~dotdeb+8.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Database: `codymaze`
--

-- --------------------------------------------------------

--
-- Table structure for table `moves`
--

CREATE TABLE `moves` (
  `telegram_id` int(11) NOT NULL,
  `reached_on` datetime DEFAULT NULL,
  `cell` char(2) CHARACTER SET ascii NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `user_status`
--

CREATE TABLE `user_status` (
  `telegram_id` int(11) NOT NULL,
  `completed` bit(1) NOT NULL DEFAULT b'0',
  `name` varchar(255) DEFAULT NULL,
  `email` varchar(1024) DEFAULT NULL,
  `certificate_id` char(36) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user_status`
--
ALTER TABLE `user_status`
  ADD PRIMARY KEY (`telegram_id`);
COMMIT;


BEGIN;

    CREATE TABLE package_managers
    ( id        bigserial       PRIMARY KEY
    , name      text            NOT NULL UNIQUE
     );

    CREATE TABLE packages
    ( id                    bigserial                   PRIMARY KEY
    , package_manager_id    bigint                      NOT NULL REFERENCES package_managers ON UPDATE CASCADE ON DELETE RESTRICT
    , name                  text                        NOT NULL
    , description           text                        NOT NULL DEFAULT ''
    , long_description      text                        NOT NULL DEFAULT ''
    , long_description_raw  text                        NOT NULL DEFAULT ''
    , long_description_type text                        NOT NULL DEFAULT ''
    , mtime                 timestamp with time zone    NOT NULL
    , UNIQUE (package_manager_id, name)
     );

    CREATE TABLE package_emails
    ( package_id    bigint  NOT NULL
    , email         text    NOT NULL
    , UNIQUE (package_id, email)
     )

END;

Add id column in emails and foreign key constraint to pacakges.

BEGIN;

    CREATE TABLE package_managers
    ( id        bigserial       PRIMARY KEY
    , name      text            NOT NULL UNIQUE
     );

    CREATE TABLE packages
    ( id                    bigserial                   PRIMARY KEY
    , package_manager_id    bigint                      NOT NULL REFERENCES package_managers ON UPDATE CASCADE ON DELETE RESTRICT
    , name                  text                        NOT NULL
    , description           text                        NOT NULL DEFAULT ''
    , long_description      text                        NOT NULL DEFAULT ''
    , long_description_raw  text                        NOT NULL DEFAULT ''
    , long_description_type text                        NOT NULL DEFAULT ''
    , mtime                 timestamp with time zone    NOT NULL
    , UNIQUE (package_manager_id, name)
     );

    CREATE TABLE package_emails
    ( id            bigserial   PRIMARY KEY
    , package_id    bigint      NOT NULL REFERENCES packages ON UPDATE CASCADE ON DELETE RESTRICT
    , email         text        NOT NULL
    , UNIQUE (package_id, email)
     )

END;


CREATE MATERIALIZED VIEW IF NOT EXISTS public.joint_fireincident AS
    SELECT * FROM public.fireincident UNION ALL
    SELECT * FROM department_nfirs.fireincident;

Allow for department's provided NFIRS to override NFPA NFIRS

CREATE MATERIALIZED VIEW joint_fireincident AS
SELECT * FROM public.fireincident fi
  WHERE (state, fdid, inc_date, inc_no, exp_no)
    NOT IN (select state, fdid, inc_date, inc_no, exp_no FROM departme
UNION ALL
SELECT * FROM department_nfirs.fireincident dfi;

CREATE UNIQUE INDEX ON joint_fireincident (state, fdid, inc_date, inc_no, exp_no);


--clear the password for the admin account
 UPDATE CMS_User
 SET UserName = 'administrator', UserIsGlobalAdministrator = 1, UserPassword = '', UserEnabled = 1
 WHERE UserID = 53

Add reset to password expiration

--clear the password for the admin account
UPDATE CMS_User
	SET UserName = 'administrator', UserIsGlobalAdministrator = 1, UserPassword = '', UserEnabled = 1
	WHERE UserID = 53

UPDATE CMS_UserSettings
	SET UserPasswordLastChanged = GETDATE()
	WHERE UserSettingsUserID = 53


INSERT INTO `django_content_type` (`name`, `app_label`, `model`) VALUES ('redirect','inproduct','redirect');

SELECT (@id:=`id`) FROM `django_content_type` WHERE `name` = 'redirect';

INSERT INTO auth_permission (`name`, `content_type_id`, `codename`) VALUES
    ('Can add inproduct redirects', @id, 'add_redirect'),
    ('Can change inproduct redirects', @id, 'change_redirect'),
    ('Can delete inproduct redirects', @id, 'delete_redirect');

Fix duplicate key issue on migration 151

INSERT INTO `django_content_type` (`name`, `app_label`, `model`) VALUES ('redirect','inproduct','redirect') ON DUPLICATE KEY UPDATE `name`='redirect';

SELECT (@id:=`id`) FROM `django_content_type` WHERE `name` = 'redirect';

INSERT INTO auth_permission (`name`, `content_type_id`, `codename`) VALUES
    ('Can add inproduct redirects', @id, 'add_redirect'),
    ('Can change inproduct redirects', @id, 'change_redirect'),
    ('Can delete inproduct redirects', @id, 'delete_redirect');


begin;
drop table if exists line_joints;
create table line_joints (
    synth_id    varchar(64),
    objects     varchar(64) array,
    terminal_id int[],
    area        geometry(polygon, 3857)
);

insert into line_joints (synth_id, terminal_id, area, objects)
    select concat('j', nextval('synthetic_objects')),
           array_agg(s.v) as terminal_id, st_union(t.area),
           array_agg(t.osm_id)
       from terminal_sets s
       join line_terminals t on t.id = s.v
       join power_line l on l.osm_id = t.osm_id
       group by s.k having count(*) > 2;

insert into power_station (osm_id, power_name, location, area)
    select synth_id, 'joint', st_centroid(area), area
        from line_joints;

insert into osm_objects (osm_id, objects)
    select synth_id, source_objects(objects) from line_joints;

update power_line l
   set terminals = minimal_terminals(l.extent, j.area, l.terminals)
         from line_joints j where l.osm_id = any(j.objects);

delete from terminal_intersections where id in (
    select id from terminal_intersections i
              join line_joints j on i.src = any(j.terminal_id) or i.dst = any(j.terminal_id)
);
delete from line_terminals where id in (
    select unnest(terminal_id) from line_joints
);

commit;

Remove useless join which gobbled memory

begin;
drop table if exists line_joints;
create table line_joints (
    synth_id    varchar(64),
    objects     varchar(64) array,
    terminal_id int[],
    area        geometry(polygon, 3857)
);

insert into line_joints (synth_id, terminal_id, area, objects)
    select concat('j', nextval('synthetic_objects')),
           array_agg(s.v) as terminal_id, st_union(t.area),
           array_agg(t.osm_id)
       from terminal_sets s
       join line_terminals t on t.id = s.v
       group by s.k having count(*) > 2;

insert into power_station (osm_id, power_name, location, area)
    select synth_id, 'joint', st_centroid(area), area
        from line_joints;

insert into osm_objects (osm_id, objects)
    select synth_id, source_objects(objects) from line_joints;

update power_line l
   set terminals = minimal_terminals(l.extent, j.area, l.terminals)
         from line_joints j where l.osm_id = any(j.objects);

delete from terminal_intersections where id in (
    select id from terminal_intersections i
              join line_joints j on i.src = any(j.terminal_id) or i.dst = any(j.terminal_id)
);
delete from line_terminals where id in (
    select unnest(terminal_id) from line_joints
);

commit;


CREATE TABLE STREAM_DEFINITIONS  (
	DEFINITION_NAME VARCHAR(255) NOT NULL PRIMARY KEY,
	DEFINITION TEXT DEFAULT NULL
) ENGINE=InnoDB;

CREATE TABLE STREAM_DEPLOYMENTS  (
	STREAM_NAME VARCHAR(255) NOT NULL PRIMARY KEY,
	DEPLOYMENT_PROPS VARCHAR(MAX)
) ENGINE=InnoDB;

Change deployment_props to varchar(1000) to facilitate testing

CREATE TABLE STREAM_DEFINITIONS  (
	DEFINITION_NAME VARCHAR(255) NOT NULL PRIMARY KEY,
	DEFINITION TEXT DEFAULT NULL
) ENGINE=InnoDB;

CREATE TABLE STREAM_DEPLOYMENTS  (
	STREAM_NAME VARCHAR(255) NOT NULL PRIMARY KEY,
	DEPLOYMENT_PROPS VARCHAR(1000)
) ENGINE=InnoDB;


INSERT INTO facility_types (id, code, name, active, displayOrder)
    VALUES ('e2faaa9e-4b2d-4212-bb60-fd62970b2113', 'warehouse', 'Warehouse', true, 1)
        ON CONFLICT (code) DO NOTHING;

Revert "OLMIS-2492: fixed inserting warehouse in migration"

DELETE FROM facility_types
    WHERE code = 'warehouse';

INSERT INTO facility_types (id, code, name, active, displayOrder)
    VALUES ('e2faaa9e-4b2d-4212-bb60-fd62970b2113', 'warehouse', 'Warehouse', true, 1);


create language plpythonu;

create or replace function get_selected_parts(explain_query text) returns text as
$$
rv = plpy.execute(explain_query)
search_text = 'Partition Selector'
result = []
result.append(0)
result.append(0)
for i in range(len(rv)):
    cur_line = rv[i]['QUERY PLAN']
    if search_text.lower() in cur_line.lower():
        j = i+1
        temp_line = rv[j]['QUERY PLAN']
        while temp_line.find('Partitions selected:') == -1:
            j += 1
            if j == len(rv) - 1:
                break
            temp_line = rv[j]['QUERY PLAN']

        if temp_line.find('Partitions selected:') != -1:
            result[0] = int(temp_line[temp_line.index('selected:  ')+10:temp_line.index(' (out')])
            result[1] = int(temp_line[temp_line.index('out of')+6:temp_line.index(')')])
return result
$$
language plpythonu;

drop table if exists foo;
create table foo(a int, b int, c int) partition by range (b) (start (1) end (101) every (10));
insert into foo select generate_series(1,5), generate_series(1,100), generate_series(1,10);
analyze foo;

Fix static parition selection test cases


SELECT
  month,
  practice,
  pct,
  SUM(quantity*dose_per_unit*ome_multiplier) AS total_ome
FROM
  {project}.hscic.normalised_prescribing_standard AS presc
JOIN
  ebmdatalab.richard.opioid_measure as opioid
ON
  CONCAT(SUBSTR(presc.bnf_code,0,9),'AA%',(SUBSTR(presc.bnf_code,-2,2)))=CONCAT(SUBSTR(opioid.bnf_code,0,11),'%',(SUBSTR(opioid.bnf_code,-2,2)))
GROUP BY
  month,
  pct,
  practice

Put curly braces around dataset name in SQL

SELECT
  month,
  practice,
  pct,
  SUM(quantity*dose_per_unit*ome_multiplier) AS total_ome
FROM
  {project}.{hscic}.normalised_prescribing_standard AS presc
JOIN
  ebmdatalab.richard.opioid_measure as opioid
ON
  CONCAT(SUBSTR(presc.bnf_code,0,9),'AA%',(SUBSTR(presc.bnf_code,-2,2)))=CONCAT(SUBSTR(opioid.bnf_code,0,11),'%',(SUBSTR(opioid.bnf_code,-2,2)))
GROUP BY
  month,
  pct,
  practice


-- Make the old field nullable to prepare for its future removal.
ALTER TABLE `addons` MODIFY COLUMN `whiteboard` longtext NULL;

-- Note: if the migration fails for you locally, remove the 'UNSIGNED' next to addon_id below.
CREATE TABLE `review_whiteboard` (
    `created` datetime(6) NOT NULL,
    `modified` datetime(6) NOT NULL,
    `addon_id` integer UNSIGNED  NOT NULL PRIMARY KEY,
    `private` longtext NOT NULL,
    `public` longtext NOT NULL
);
ALTER TABLE `review_whiteboard` ADD CONSTRAINT `addon_id_refs_id_3aa22f51` FOREIGN KEY (`addon_id`) REFERENCES `addons` (`id`);

-- Move the whiteboard from the addons table to the new one
INSERT INTO `review_whiteboard` (`created`, `modified`, `addon_id`, `private`, `public`)
    SELECT `created`, `modified`, `id`, '', `whiteboard` FROM `addons` WHERE `whiteboard` != ''

Fix syntax error in 993 migration

-- Make the old field nullable to prepare for its future removal.
ALTER TABLE `addons` MODIFY COLUMN `whiteboard` longtext NULL;

-- Note: if the migration fails for you locally, remove the 'UNSIGNED' next to addon_id below.
CREATE TABLE `review_whiteboard` (
    `created` datetime(6) NOT NULL,
    `modified` datetime(6) NOT NULL,
    `addon_id` integer UNSIGNED  NOT NULL PRIMARY KEY,
    `private` longtext NOT NULL,
    `public` longtext NOT NULL
);
ALTER TABLE `review_whiteboard` ADD CONSTRAINT `addon_id_refs_id_3aa22f51` FOREIGN KEY (`addon_id`) REFERENCES `addons` (`id`);

-- Move the whiteboard from the addons table to the new one
INSERT INTO `review_whiteboard` (`created`, `modified`, `addon_id`, `private`, `public`)
    SELECT `created`, `modified`, `id`, '', `whiteboard` FROM `addons` WHERE `whiteboard` != '';


-- git_hash table
--
-- Stores ONLY metadata about commits that MediaWiki CANNOT store.
--
-- The other tables git_status_modify_hash and git_edit_hash store
-- actual CHANGES of a commit that MediaWiki cannot store or cannot
-- store in a suitable format.

CREATE TABLE IF NOT EXISTS /*_*/git_hash(
    -- The primary key, contains the Git commit hash in a 40-character
    -- hex representation of SHA-1
    commit_hash VARBINARY(40) NOT NULL PRIMARY KEY,

    -- Parent commit hashes (up to 15, separated by commas) in 40-char
    -- hex of SHA-1
    commit_hash_parents VARBINARY(615),

    -- Email addresses and usernames can be changed in MediaWiki,
    -- however this shouldn't change every previous commit (since that would
    -- change the hashes). Also necessary for edits made via pull requests.
    author_name VARBINARY(255),
    author_email VARBINARY(255),
    author_tzOffset VARBINARY(5),

    -- With rebases sometimes you have different authors and committers. This
    -- has to be stored somehow to keep the "real" Git repository in sync.
    committer_name VARBINARY(255),
    committer_email VARBINARY(255),
    committer_tzOffset VARBINARY(5)
) /*$wgDBTableOptions*/;

Add committer timestamp field to to hash table

-- git_hash table
--
-- Stores ONLY metadata about commits that MediaWiki CANNOT store.
--
-- The other tables git_status_modify_hash and git_edit_hash store
-- actual CHANGES of a commit that MediaWiki cannot store or cannot
-- store in a suitable format.

CREATE TABLE IF NOT EXISTS /*_*/git_hash(
    -- The primary key, contains the Git commit hash in a 40-character
    -- hex representation of SHA-1
    commit_hash VARBINARY(40) NOT NULL PRIMARY KEY,

    -- Parent commit hashes (up to 15, separated by commas) in 40-char
    -- hex of SHA-1
    commit_hash_parents VARBINARY(615),

    -- Email addresses and usernames can be changed in MediaWiki,
    -- however this shouldn't change every previous commit (since that would
    -- change the hashes). Also necessary for edits made via pull requests.
    author_name VARBINARY(255),
    author_email VARBINARY(255),
    author_tzOffset VARBINARY(5),

    -- With rebases sometimes you have different authors and committers. This
    -- has to be stored somehow to keep the "real" Git repository in sync.
    committer_name VARBINARY(255),
    committer_email VARBINARY(255),
    committer_timestamp VARBINARY(14),
    committer_tzOffset VARBINARY(5)
) /*$wgDBTableOptions*/;


CREATE PROCEDURE [dbo].[GetUsersWithExpiredRegistrations]
AS
	SELECT
		Id,
		FirstName,
		LastName,
		Email,
		Password,
		Salt,
		PasswordProfileId,
		IsActive,
		FailedLoginAttempts,
		IsLocked
	FROM [User] u
	LEFT JOIN UserSecurityCode sc
		ON u.Id = sc.UserId
		AND sc.ExpiryTime > GETDATE()
	WHERE u.IsActive = 0
	AND sc.Code IS NULL
GO

Update get users for deletion sproc to eb more 'readable'

CREATE PROCEDURE [dbo].[GetUsersWithExpiredRegistrations]
AS
	SELECT
		Id,
		FirstName,
		LastName,
		Email,
		Password,
		Salt,
		PasswordProfileId,
		IsActive,
		FailedLoginAttempts,
		IsLocked
	FROM [User]
	WHERE IsActive = 0
	AND Id NOT IN
	(
		SELECT UserId
		FROM UserSecurityCode
		WHERE ExpiryTime >= GETDATE()
	)
GO


INSERT INTO tags (tag_text, blacklisted, restricted)
VALUES ('dynamic theme', 0, 1) ON DUPLICATE KEY UPDATE restricted = 1;

Fix 'dynamic theme' tag migration. The column blacklisted got renamed to denied.

INSERT INTO tags (tag_text, denied, restricted)
VALUES ('dynamic theme', 0, 1) ON DUPLICATE KEY UPDATE restricted = 1;


ALTER TABLE vaccine_distributions ADD COLUMN isNotificationSent Boolean default false;

Fix column already exist issue

DO $$
    BEGIN
        BEGIN
            ALTER TABLE vaccine_distributions ADD COLUMN isNotificationSent Boolean default false;
        EXCEPTION
            WHEN duplicate_column THEN RAISE NOTICE 'column isNotificationSent already exists in vaccine_distributions.';
        END;
    END;
$$


"CREATE TABLE IF NOT EXISTS " + Device::Table::Name +
"("
    "id_device INTEGER PRIMARY KEY AUTOINCREMENT,"
    "uuid TEXT UNIQUE ON CONFLICT FAIL,"
    "scheme TEXT,"
    "is_removable BOOLEAN,"
    "is_present BOOLEAN,"
    "last_seen UNSIGNED INTEGER"
")",

Store uuid as case insensitive

"CREATE TABLE IF NOT EXISTS " + Device::Table::Name +
"("
    "id_device INTEGER PRIMARY KEY AUTOINCREMENT,"
    "uuid TEXT COLLATE NOCASE UNIQUE ON CONFLICT FAIL,"
    "scheme TEXT,"
    "is_removable BOOLEAN,"
    "is_present BOOLEAN,"
    "last_seen UNSIGNED INTEGER"
")",


WITH
  prices_per_unit AS (
  SELECT
    month AS date,
    bnf_code,
    IEEE_DIVIDE(net_cost,quantity) AS price_per_unit
  FROM
    ebmdatalab.hscic.normalised_prescribing_standard)
SELECT
  DISTINCT date,
  bnf_code,
  percentile_cont(price_per_unit,
    0.5) OVER (PARTITION BY date, bnf_code) AS median_price_per_unit
FROM
  prices_per_unit
ORDER BY
  bnf_code,
  date

Fix project reference in BQ view SQL

WITH
  prices_per_unit AS (
  SELECT
    month AS date,
    bnf_code,
    IEEE_DIVIDE(net_cost,quantity) AS price_per_unit
  FROM
    {project}.hscic.normalised_prescribing_standard)
SELECT
  DISTINCT date,
  bnf_code,
  percentile_cont(price_per_unit,
    0.5) OVER (PARTITION BY date, bnf_code) AS median_price_per_unit
FROM
  prices_per_unit
ORDER BY
  bnf_code,
  date


DROP DATABASE IF EXISTS sonar;
CREATE DATABASE sonar;

USE mysql;

DELETE FROM mysql.db where USER = 'sonar';

GRANT USAGE ON *.* TO sonar@localhost;

GRANT USAGE ON *.* TO 'sonar'@'%';

DROP USER sonar@localhost;

DROP USER sonar;

CREATE USER sonar@localhost IDENTIFIED BY '';
CREATE USER 'sonar'@'%' IDENTIFIED BY '';

GRANT ALL PRIVILEGES ON sonar.* TO sonar@localhost IDENTIFIED BY '' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'%' IDENTIFIED BY '' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Fix MySQL/MariaDB issue with use privileges

-- Flushing privileges is done after each statement
-- to avoid any chance of this wierd isssue.
-- See: http://stackoverflow.com/a/17436813/441652
FLUSH PRIVILEGES;

-- TODO: Is it really needed if there is `DROP USER` below?
-- DELETE FROM mysql.db where USER = 'sonar';

FLUSH PRIVILEGES;

-- Create user to avoid failure of GRANT statement.
CREATE USER IF NOT EXISTS 'sonar'@'localhost' IDENTIFIED BY '';

FLUSH PRIVILEGES;

-- Create user to avoid failure of GRANT statement.
CREATE USER IF NOT EXISTS 'sonar'@'%' IDENTIFIED BY '';

FLUSH PRIVILEGES;

-- Make sure user records exist so that dropping user does not fail.
-- See: http://stackoverflow.com/a/3241918/441652
GRANT USAGE ON *.* TO 'sonar'@'localhost';

FLUSH PRIVILEGES;

-- Make sure user records exist so that dropping user does not fail.
-- See: http://stackoverflow.com/a/3241918/441652
GRANT USAGE ON *.* TO 'sonar'@'%';

FLUSH PRIVILEGES;

DROP USER sonar@localhost;

FLUSH PRIVILEGES;

DROP USER sonar;

FLUSH PRIVILEGES;

DROP DATABASE IF EXISTS sonar;

FLUSH PRIVILEGES;

CREATE DATABASE sonar;

FLUSH PRIVILEGES;

CREATE USER sonar@localhost IDENTIFIED BY '';

FLUSH PRIVILEGES;

CREATE USER 'sonar'@'%' IDENTIFIED BY '';

FLUSH PRIVILEGES;

GRANT ALL PRIVILEGES ON sonar.* TO sonar@localhost IDENTIFIED BY '' WITH GRANT OPTION;

FLUSH PRIVILEGES;

GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'%' IDENTIFIED BY '' WITH GRANT OPTION;

FLUSH PRIVILEGES;


CREATE DATABASE notes;

USE notes;

CREATE TABLE note_users (
    UserId CHAR(32) PRIMARY KEY,
    UserEmail VARCHAR(255),
    UserPassword VARCHAR(512),
    EmailConfirmation CHAR(32),
	Active TINYINT DEFAULT '0'
);

CREATE TABLE user_preferences (
	PreferenceId INT(4) PRIMARY KEY,
	UserId VARCHAR(60),
    TagColor VARCHAR(6) DEFAULT 'red',
	Pagination TINYINT DEFAULT '1',
	NoteOrder VARCHAR(15) DEFAULT 'oldest',
	SearchOptions VARCHAR(50) DEFAULT 'a:2:{i:0;s:4:"true";i:1;s:4:"true";}',
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);

CREATE TABLE note (
    NoteId INT(11) AUTO_INCREMENT PRIMARY KEY,
    NoteText VARCHAR(1024),
    NoteTags VARCHAR(1024),
	NoteTitle VARCHAR(256),
	NoteComplete TINYINT NULL DEFAULT '0',
	UserId VARCHAR(60),
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);

Add auto_increment to the user preferences primary key

CREATE DATABASE notes;

USE notes;

CREATE TABLE note_users (
    UserId CHAR(32) PRIMARY KEY,
    UserEmail VARCHAR(255),
    UserPassword VARCHAR(512),
    EmailConfirmation CHAR(32),
	Active TINYINT DEFAULT '0'
);

CREATE TABLE user_preferences (
	PreferenceId INT(4) PRIMARY KEY AUTO_INCREMENT,
	UserId VARCHAR(60),
    TagColor VARCHAR(6) DEFAULT 'red',
	Pagination TINYINT DEFAULT '1',
	NoteOrder VARCHAR(15) DEFAULT 'oldest',
	SearchOptions VARCHAR(50) DEFAULT 'a:2:{i:0;s:4:"true";i:1;s:4:"true";}',
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);

CREATE TABLE note (
    NoteId INT(11) AUTO_INCREMENT PRIMARY KEY,
    NoteText VARCHAR(1024),
    NoteTags VARCHAR(1024),
	NoteTitle VARCHAR(256),
	NoteComplete TINYINT NULL DEFAULT '0',
	UserId VARCHAR(60),
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);


INSERT INTO COI_DISCLOSURE_EVENT_TYPE ( EVENT_TYPE_CODE,DESCRIPTION,UPDATE_TIMESTAMP,UPDATE_USER,VER_NBR,OBJ_ID,EXCLUDE_FROM_MASTER_DISCL,ACTIVE_FLAG,USE_SLCT_BOX_1,REQ_SLCT_BOX_1,SLCT_BOX_1_LABEL,SLCT_BOX_1_VAL_FNDR,PROJECT_ID_LABEL,PROJECT_TITLE_LABEL)
    VALUES('16','Manual IACUC Protocol',SYSDATE,'admin',1,SYS_GUID(),'N','Y','Y','Y','Protocol Type','org.kuali.kra.iacuc.protocol.IacucProtocolTypeValuesFinder,'Protocol Number','Protocol Name')
/

Implement COI event type of "IACUC Protocol"

INSERT INTO COI_DISCLOSURE_EVENT_TYPE ( EVENT_TYPE_CODE,DESCRIPTION,UPDATE_TIMESTAMP,UPDATE_USER,VER_NBR,OBJ_ID,EXCLUDE_FROM_MASTER_DISCL,ACTIVE_FLAG,USE_SLCT_BOX_1,REQ_SLCT_BOX_1,SLCT_BOX_1_LABEL,SLCT_BOX_1_VAL_FNDR,PROJECT_ID_LABEL,PROJECT_TITLE_LABEL)
    VALUES('16','Manual IACUC Protocol',SYSDATE,'admin',1,SYS_GUID(),'N','Y','Y','Y','Protocol Type','org.kuali.kra.iacuc.protocol.IacucProtocolTypeValuesFinder','Protocol Number','Protocol Name')
/


INSERT INTO KRIM_ROLE_T (ACTV_IND,DESC_TXT,KIM_TYP_ID,LAST_UPDT_DT,NMSPC_CD,OBJ_ID,ROLE_ID,ROLE_NM,VER_NBR)
       VALUES ('Y','IACUC Approver',(SELECT KIM_TYP_ID FROM KRIM_TYP_T WHERE NM='UnitHierarchy'),SYSDATE(),'KC-UNT',SYS_GUID(),KRIM_ROLE_ID_BS_S.NEXTVAL,'IACUCApprover',1)
/

Create generic iacucadmin and iacucresearcher user ID's for testing/development

INSERT INTO KRIM_ROLE_T (ACTV_IND,DESC_TXT,KIM_TYP_ID,LAST_UPDT_DT,NMSPC_CD,OBJ_ID,ROLE_ID,ROLE_NM,VER_NBR)
       VALUES ('Y','IACUC Approver',(SELECT KIM_TYP_ID FROM KRIM_TYP_T WHERE NM='UnitHierarchy'),SYSDATE,'KC-UNT',SYS_GUID(),KRIM_ROLE_ID_BS_S.NEXTVAL,'IACUCApprover',1)
/


CREATE TABLE IF NOT EXISTS tickets (
	code		BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
	firstname	VARCHAR(50) NOT NULL,
	lastname	VARCHAR(50) NOT NULL,
	street		VARCHAR(50) NOT NULL,
	housenumber	VARCHAR(5) NOT NULL,
	postalcode	INT(5) UNSIGNED NOT NULL,
	town		VARCHAR(50) NOT NULL,
	date		TIMESTAMP NOT NULL,
	PRIMARY KEY (code)
);

Add 'used' attribute to DB

CREATE TABLE IF NOT EXISTS tickets (
	code		BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
	firstname	VARCHAR(50) NOT NULL,
	lastname	VARCHAR(50) NOT NULL,
	street		VARCHAR(50) NOT NULL,
	housenumber	VARCHAR(5) NOT NULL,
	postalcode	INT(5) UNSIGNED NOT NULL,
	town		VARCHAR(50) NOT NULL,
	date		TIMESTAMP NOT NULL,
	used		BOOLEAN NOT NULL DEFAULT false,
	PRIMARY KEY (code)
);


create extension if not exists pgrouting;

alter table hkiroads
  drop column if exists geom2d,
  drop column if exists source,
  drop column if exists target,
  drop column if exists x1,
  drop column if exists y1,
  drop column if exists x2,
  drop column if exists y2,
  drop column if exists cost;

alter table hkiroads
  add column geom2d geometry(LINESTRING, 3067),
  add column source bigint,
  add column target bigint,
  add column x1 double precision,
  add column y1 double precision,
  add column x2 double precision,
  add column y2 double precision,
  add column cost double precision;

update hkiroads set geom2d = st_force2D(geom);

update hkiroads set x1 = st_x(st_startpoint(geom2d)),
  y1 = st_y(st_startpoint(geom2d)),
  x2 = st_x(st_endpoint(geom2d)),
  y2 = st_y(st_endpoint(geom2d));

update hkiroads set cost = st_length(geom2d);

-- Create graph (i.e. source and target nodes of each edge);
select pgr_createtopology('hkiroads',0.001,the_geom:='geom2d',id:='gid');

Remove extension creation from database script


ALTER TABLE comments MODIFY COLUMN repo_name varchar(255) NULL;
ALTER TABLE pull_labels MODIFY COLUMN repo_name varchar(255);
ALTER TABLE pulls MODIFY COLUMN repo_name varchar(255);

ALTER TABLE pulls DROP KEY pulls_repo;
ALTER TABLE pulls ADD KEY pulls_repo (repo);

Remove alters since these columns are going to be dropped


CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- OCC

CREATE OR REPLACE FUNCTION handle_occ()
  RETURNS TRIGGER AS $occ$
  BEGIN
    IF (NEW.version != OLD.version) THEN
      RAISE EXCEPTION 'Version missmatch: expected % given %',
            OLD.version, NEW.version
            USING ERRCODE='P0002';
    ELSE
      NEW.version := NEW.version + 1;
    END IF;
    RETURN NEW;
  END;
$occ$ LANGUAGE plpgsql;

-- Modified At

CREATE OR REPLACE FUNCTION update_modified_at()
  RETURNS TRIGGER AS $updt$
  BEGIN
    NEW.modified_at := clock_timestamp();
    RETURN NEW;
  END;
$updt$ LANGUAGE plpgsql;

Add pgcrypto extension on the 0000 main migration.

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

-- OCC

CREATE OR REPLACE FUNCTION handle_occ()
  RETURNS TRIGGER AS $occ$
  BEGIN
    IF (NEW.version != OLD.version) THEN
      RAISE EXCEPTION 'Version missmatch: expected % given %',
            OLD.version, NEW.version
            USING ERRCODE='P0002';
    ELSE
      NEW.version := NEW.version + 1;
    END IF;
    RETURN NEW;
  END;
$occ$ LANGUAGE plpgsql;

-- Modified At

CREATE OR REPLACE FUNCTION update_modified_at()
  RETURNS TRIGGER AS $updt$
  BEGIN
    NEW.modified_at := clock_timestamp();
    RETURN NEW;
  END;
$updt$ LANGUAGE plpgsql;


alter table catalog_override_block_definition change `size` `bsize` decimal(15,9) NOT NULL;

Remove mysql specific backticks for migration file

alter table catalog_override_block_definition change size bsize decimal(15,9) NOT NULL;


INSERT INTO CONS_CONSTITUENT (`LAST_NAME`, `FIRST_NAME`) VALUES ('User','Kula');
INSERT INTO CORE_ORGANIZATION (`ORGANIZATION_NAME`, `ORGANIZATION_ABBREVIATION`, `ORGANIZATION_TYPE`, `ACTIVE`) VALUES ('Kula Univeristy', 'KULA', 'S', 1);
INSERT INTO CORE_TERM (`TERM_NAME`, `TERM_ABBREVIATION`, `START_DATE`, `END_DATE`) VALUES ('Kula Term', 'TERM', CURDATE(), CURDATE()+ INTERVAL 1 YEAR);
INSERT INTO CORE_ORGANIZATION_TERMS (`ORGANIZATION_ID`,`TERM_ID`) VALUES (1, 1);
INSERT INTO CORE_USER (`USER_ID`, `USERNAME`,`PASSWORD`,`ALLOW_AUTH_LOCAL`) VALUES (1, 'kula', '$2y$10$Ek.U.ZmrdSV03stfi/PG7ukuY5ZYOWxa0HUJ9tly9VW81cyLD8HJO', 1);

INSERT INTO CORE_USERGROUP (`USERGROUP_NAME`,`PORTAL`) VALUES ('Core System Administrator', 'core');
INSERT INTO CORE_USER_ROLES (`USER_ID`,`USERGROUP_ID`, `ORGANIZATION_ID`) VALUES (1, 1, 1);

Remove core from Sys Admin group name.

INSERT INTO CONS_CONSTITUENT (`LAST_NAME`, `FIRST_NAME`) VALUES ('User','Kula');
INSERT INTO CORE_ORGANIZATION (`ORGANIZATION_NAME`, `ORGANIZATION_ABBREVIATION`, `ORGANIZATION_TYPE`, `ACTIVE`) VALUES ('Kula Univeristy', 'KULA', 'S', 1);
INSERT INTO CORE_TERM (`TERM_NAME`, `TERM_ABBREVIATION`, `START_DATE`, `END_DATE`) VALUES ('Kula Term', 'TERM', CURDATE(), CURDATE()+ INTERVAL 1 YEAR);
INSERT INTO CORE_ORGANIZATION_TERMS (`ORGANIZATION_ID`,`TERM_ID`) VALUES (1, 1);
INSERT INTO CORE_USER (`USER_ID`, `USERNAME`,`PASSWORD`,`ALLOW_AUTH_LOCAL`) VALUES (1, 'kula', '$2y$10$Ek.U.ZmrdSV03stfi/PG7ukuY5ZYOWxa0HUJ9tly9VW81cyLD8HJO', 1);

INSERT INTO CORE_USERGROUP (`USERGROUP_NAME`,`PORTAL`) VALUES ('System Administrator', 'core');
INSERT INTO CORE_USER_ROLES (`USER_ID`,`USERGROUP_ID`, `ORGANIZATION_ID`) VALUES (1, 1, 1);


-- Reset post-Gratipocalypse values
UPDATE paydays p
   SET nactive = (SELECT DISTINCT count(*) FROM (
        SELECT participant FROM payments WHERE payday=p.id
       ) AS foo)
     , volume = (SELECT COALESCE(sum(amount), 0) FROM payments WHERE payday=p.id)
 WHERE ts_start > '2015-05-07'::timestamptz;

Make the volume stats accurate

-- Reset post-Gratipocalypse values
UPDATE paydays p
   SET nactive = (SELECT DISTINCT count(*) FROM (
        SELECT participant FROM payments WHERE payday=p.id
       ) AS foo)
     , volume = (SELECT COALESCE(sum(amount), 0)
                   FROM payments
                  WHERE payday=p.id AND direction='to-team')
 WHERE ts_start > '2015-05-07'::timestamptz;


CREATE USER itest;
CREATE DATABASE itest OWNER itest;

\c itest;
/* Create test tables */
create table users (
       id int not null,
       first_name varchar(80),
       last_name varchar(80),
       primary key (id)
);

Fix table def to autonumber id field.

CREATE USER itest;
CREATE DATABASE itest OWNER itest;

\c itest;
CREATE SEQUENCE users_id_sequence
/* Create test tables */
CREATE TABLE users (
       id int DEFAULT nextval('users_id_sequence') PRIMARY KEY,
       first_name varchar(80),
       last_name varchar(80)
);


DROP INDEX utxos_asset_id_contract_hash_idx;
ALTER TABLE utxos DROP contract_hash;
DROP VIEW IF EXISTS utxos_status;
CREATE VIEW utxos_status AS
 SELECT u.tx_hash,
    u.index,
    u.asset_id,
    u.amount,
    u.created_at,
    u.metadata,
    u.script,
    (b.tx_hash IS NOT NULL) AS confirmed
   FROM (utxos u
     LEFT JOIN blocks_utxos b ON (((u.tx_hash = b.tx_hash) AND (u.index = b.index))));

Drop items in dependency order

DROP INDEX utxos_asset_id_contract_hash_idx;
DROP VIEW IF EXISTS utxos_status;
ALTER TABLE utxos DROP contract_hash;
CREATE VIEW utxos_status AS
 SELECT u.tx_hash,
    u.index,
    u.asset_id,
    u.amount,
    u.created_at,
    u.metadata,
    u.script,
    (b.tx_hash IS NOT NULL) AS confirmed
   FROM (utxos u
     LEFT JOIN blocks_utxos b ON (((u.tx_hash = b.tx_hash) AND (u.index = b.index))));


INSERT INTO version_db_world (`sql_rev`) VALUES ('1527682153565076000');

DELETE FROM `smart_scripts` WHERE `entryorguid` = 177385;
INSERT INTO `smart_scripts` (`entryorguid`,`source_type`,`id`,`link`,`event_type`,`event_phase_mask`,`event_chance`,`event_flags`,`event_param1`,`event_param2`,`event_param3`,`event_param4`,`action_type`,`action_param1`,`action_param2`,`action_param3`,`action_param4`,`action_param5`,`action_param6`,`target_type`,`target_param1`,`target_param2`,`target_param3`,`target_x`,`target_y`,`target_z`,`target_o`,`comment`) VALUES
(177385,1,0,1,70,0,100,0,2,0,0,0,9,0,0,0,0,0,0,14,43178,175610,0,0,0,0,0,'Torch - On Gossip Hello - Set Gameobject State'),
(177385,1,1,0,61,0,100,0,0,0,0,0,106,16,0,0,0,0,0,20,176944,200,0,0,0,0,0,'Torch - On Gossip Hello - Remove Gameobject Flag');

Change to UPDATE command to be compliant with AC's guidelines

INSERT INTO version_db_world (`sql_rev`) VALUES ('1527682153565076000');

UPDATE `smart_scripts` SET
`event_type` = 70,
`event_param1` = 2,
`action_type` = 9,
`action_param1` = 0,
`target_type` = 14,
`target_param1` = 43178,
`target_param2` = 175610
WHERE `entryorguid` = 177385 AND `id` = 0;


CREATE PROCEDURE GetFinancialSystemWorkflowType
	@workflowType VARCHAR(4) OUTPUT
AS
BEGIN
	SET @workflowType = (SELECT Value FROM AssetPolicyGeneral WHERE Section = 'Integration' AND Identifier = 'IntegratedFinSys')

	IF @workflowType IS NULL OR LEN(RTRIM(LTRIM(@workflowType))) = 0
	BEGIN
	  THROW 51000, 'IntegratedFinSys value in AssetPolicyGeneral cannot be NULL or blank', 1;
	END

	IF @workflowType NOT IN ('NONE', 'PUSH', 'PULL')
	BEGIN
	  THROW 51000, 'Invalid IntegratedFinSys value. Correct values: NONE, PUSH OR PULL', 1;
	END
END;

Fix error caused by severity level over 18

CREATE PROCEDURE GetFinancialSystemWorkflowType
	@workflowType VARCHAR(4) OUTPUT
AS
BEGIN
	SET @workflowType = (SELECT Value FROM AssetPolicyGeneral WHERE Section = 'Integration' AND Identifier = 'IntegratedFinSys')

	IF @workflowType IS NULL OR LEN(RTRIM(LTRIM(@workflowType))) = 0
	BEGIN
	  THROW 18, 'IntegratedFinSys value in AssetPolicyGeneral cannot be NULL or blank', 1;
	END

	IF @workflowType NOT IN ('NONE', 'PUSH', 'PULL')
	BEGIN
	  THROW 18, 'Invalid IntegratedFinSys value. Correct values: NONE, PUSH OR PULL', 1;
	END
END;


\c grocery_store

COPY grocery_items (name, price, section) FROM '/Users/Somaya/Desktop/LGProjects/phase-3-challenge/part-2/grocery.csv' DELIMITER ',' CSV HEADER;


drop procedure if exists int_actions_list;
delimiter $$
create procedure int_actions_list(i_action_name varchar(128),
                                  i_action_description varchar(512),
                                  i_ssys_list text,
                                  out o_search_conditions text
)
 main_sql:
  begin
    declare v_search_conditions   text default '';

    if i_action_name is not null then
      set v_search_conditions   =
            concat(" and a.action_name like '", i_action_name, "%' ");
    end if;

    if i_action_description is not null then
      set v_search_conditions   =
            concat(v_search_conditions,
                   " and a.action_description like '%",
                   i_action_description,
                   "%' "
            );
    end if;

    if i_ssys_list is not null then
      set v_search_conditions   =
            concat(v_search_conditions,
                   " and instr(concat(',', '",
                   i_ssys_list,
                   "', ','), concat(',', ssys_ssys_id, ',')) > 0 "
            );
    end if;

    set o_search_conditions   = coalesce(v_search_conditions, '');
  end
$$
delimiter ;

Support actions substring filter (no uninject yet)

drop procedure if exists int_actions_list;
delimiter $$
create procedure int_actions_list(i_action_name varchar(128),
                                  i_action_description varchar(512),
                                  i_ssys_list text,
                                  out o_search_conditions text
)
 main_sql:
  begin
    declare v_search_conditions   text default '';

    if i_action_name is not null then
      set v_search_conditions   =
            concat(" and a.action_name like '%", i_action_name, "%' ");
    end if;

    if i_action_description is not null then
      set v_search_conditions   =
            concat(v_search_conditions,
                   " and a.action_description like '%",
                   i_action_description,
                   "%' "
            );
    end if;

    if i_ssys_list is not null then
      set v_search_conditions   =
            concat(v_search_conditions,
                   " and instr(concat(',', '",
                   i_ssys_list,
                   "', ','), concat(',', ssys_ssys_id, ',')) > 0 "
            );
    end if;

    set o_search_conditions   = coalesce(v_search_conditions, '');
  end
$$
delimiter ;


CREATE DATABASE IF NOT EXISTS event_detection;
USE event_detection;

CREATE TABLE IF NOT EXISTS sources (
	id varchar(255), reliability double,
	PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS feeds (
	id varchar(255), source varchar(255), url blob, scrapers blob, lastseen blob default NULL,
	primary key (id),
	foreign key (source) references sources(id)
);

CREATE TABLE IF NOT EXISTS articles (
	id INT UNSIGNED AUTO_INCREMENT, title varchar(255), source varchar(255), url blob, filename blob default NULL,
	primary key (id),
	foreign key (source) references sources(id),
	unique key (filename(100))
);

Remove an invalid uniqueness constraint.

CREATE DATABASE IF NOT EXISTS event_detection;
USE event_detection;

CREATE TABLE IF NOT EXISTS sources (
	id varchar(255), reliability double,
	PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS feeds (
	id varchar(255), source varchar(255), url blob, scrapers blob, lastseen blob default NULL,
	primary key (id),
	foreign key (source) references sources(id)
);

CREATE TABLE IF NOT EXISTS articles (
	id INT UNSIGNED AUTO_INCREMENT, title varchar(255), source varchar(255), url blob, filename blob default NULL,
	primary key (id),
	foreign key (source) references sources(id)
);


DROP TABLE IF EXISTS presto_test_sequence;

DROP TABLE IF EXISTS presto_test;

DROP TABLE IF EXISTS presto_test_partition_format;

DROP TABLE IF EXISTS presto_test_unpartitioned;

CREATE TABLE IF NOT EXISTS presto_test_offline (x INT);
ALTER TABLE presto_test_offline DISABLE OFFLINE;
DROP TABLE IF EXISTS presto_test_offline;

CREATE TABLE IF NOT EXISTS presto_test_offline_partition (x INT) PARTITIONED BY (ds STRING);
ALTER TABLE presto_test_offline_partition ADD IF NOT EXISTS PARTITION (ds='2012-12-30');
ALTER TABLE presto_test_offline_partition PARTITION (ds='2012-12-30') DISABLE OFFLINE;
DROP TABLE IF EXISTS presto_test_offline_partition;

DROP TABLE IF EXISTS presto_test_bucketed_by_string_int;
DROP TABLE IF EXISTS presto_test_bucketed_by_bigint_boolean;
DROP TABLE IF EXISTS presto_test_bucketed_by_double_float;

DROP VIEW IF EXISTS presto_test_view;

Remove missing Hive drop test table

DROP TABLE IF EXISTS presto_test_sequence;

DROP TABLE IF EXISTS presto_test;

DROP TABLE IF EXISTS presto_test_partition_format;

DROP TABLE IF EXISTS presto_test_unpartitioned;

CREATE TABLE IF NOT EXISTS presto_test_offline (x INT);
ALTER TABLE presto_test_offline DISABLE OFFLINE;
DROP TABLE IF EXISTS presto_test_offline;

CREATE TABLE IF NOT EXISTS presto_test_offline_partition (x INT) PARTITIONED BY (ds STRING);
ALTER TABLE presto_test_offline_partition ADD IF NOT EXISTS PARTITION (ds='2012-12-30');
ALTER TABLE presto_test_offline_partition PARTITION (ds='2012-12-30') DISABLE OFFLINE;
DROP TABLE IF EXISTS presto_test_offline_partition;

DROP TABLE IF EXISTS presto_test_bucketed_by_string_int;
DROP TABLE IF EXISTS presto_test_bucketed_by_bigint_boolean;
DROP TABLE IF EXISTS presto_test_bucketed_by_double_float;

DROP TABLE IF EXISTS presto_test_partition_schema_change;

DROP VIEW IF EXISTS presto_test_view;


--
-- example schema
-- --------------
-- schema file for sqlite3 database
-- to be used in this example
--

CREATE TABLE posts (
  `id`    INTEGER PRIMARY KEY,
  `uid`   INTEGER,
  `title` TEXT,
  `body`  TEXT,
  `size`  INTEGER,
  `date`  INTEGER
);

CREATE TABLE comments (
  `id`      INTEGER PRIMARY KEY,
  `post_id` INTEGER,
  `title`   TEXT,
  `body`    TEXT,
  `size`    INTEGER,
  `date`    INTEGER
);

Improve Sqlite3 schema by adding drop statement

--
-- example schema
-- --------------
-- schema file for sqlite3 database
-- to be used in this example
--

DROP TABLE IF EXISTS posts;
CREATE TABLE posts (
  `id`    INTEGER PRIMARY KEY,
  `uid`   INTEGER,
  `title` TEXT,
  `body`  TEXT,
  `size`  INTEGER,
  `date`  INTEGER
);

DROP TABLE IF EXISTS comments;
CREATE TABLE comments (
  `id`      INTEGER PRIMARY KEY,
  `post_id` INTEGER,
  `title`   TEXT,
  `body`    TEXT,
  `size`    INTEGER,
  `date`    INTEGER
);


-- ------------------------------------------------------------------------
-- Copyright 2020 Crown Copyright
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- ------------------------------------------------------------------------

-- stop note level warnings about objects (not)? existing
set @old_sql_notes=@@sql_notes, sql_notes=0;

CALL core_add_column_v1('IDX_SHRD', 'IDX_UUID', 'varchar(255) default NULL');

-- idempotent
UPDATE IDX_SHRD shard
SET shard.IDX_UUID = (
    SELECT ind.UUID
    FROM IDX ind
    WHERE ind.ID = shard.FK_IDX_ID);

CALL core_drop_constraint_v1('IDX_SHRD', 'FOREIGN KEY', 'IDX_SHRD_FK_IDX_ID');

CALL core_rename_column_v1('IDX_SHRD', 'FK_IDX_ID', 'OLD_IDX_ID', 'int(11) default NULL');

-- Reset to the original value
SET SQL_NOTES=@OLD_SQL_NOTES;

Fix sproc args in migration script

-- ------------------------------------------------------------------------
-- Copyright 2020 Crown Copyright
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- ------------------------------------------------------------------------

-- stop note level warnings about objects (not)? existing
set @old_sql_notes=@@sql_notes, sql_notes=0;

CALL core_add_column_v1('IDX_SHRD', 'IDX_UUID', 'varchar(255) default NULL');

-- idempotent
UPDATE IDX_SHRD shard
SET shard.IDX_UUID = (
    SELECT ind.UUID
    FROM IDX ind
    WHERE ind.ID = shard.FK_IDX_ID);

CALL core_drop_constraint_v1('IDX_SHRD', 'IDX_SHRD_FK_IDX_ID', 'FOREIGN KEY');

CALL core_rename_column_v1('IDX_SHRD', 'FK_IDX_ID', 'OLD_IDX_ID', 'int(11) default NULL');

-- Reset to the original value
SET SQL_NOTES=@OLD_SQL_NOTES;


ALTER TABLE Role_ DROP companyId;
ALTER TABLE Role_ DROP classNameId;
ALTER TABLE Role_ DROP classPK;
ALTER TABLE Role_ DROP type_;
ALTER TABLE Role_ DROP subtype;
ALTER TABLE Role_ DROP uuid_;
ALTER TABLE Role_ DROP userId;
ALTER TABLE Role_ DROP userName;
ALTER TABLE Role_ DROP modifiedDate;

-- Delete unused roles
delete from Role_ where roleId in (10120, 10123 ,10124 ,10125, 10126, 10127, 10128, 26021, 1015916,
    1015917, 1015918, 1411593);

Drop Role_ indexes before columns

DROP INDEX IX_A88E424E on Role_;
DROP INDEX IX_EBC931B8 on Role_;
DROP INDEX IX_449A10B9 on Role_;
DROP INDEX IX_CBE204   on Role_;
DROP INDEX IX_F436EC8E on Role_;
DROP INDEX IX_5EB4E2FB on Role_;
DROP INDEX IX_F3E1C6FC on Role_;
DROP INDEX IX_F92B66E6 on Role_;
DROP INDEX IX_26DB26C5 on Role_;
DROP INDEX IX_B9FF6043 on Role_;

ALTER TABLE Role_ DROP companyId;
ALTER TABLE Role_ DROP classNameId;
ALTER TABLE Role_ DROP classPK;
ALTER TABLE Role_ DROP type_;
ALTER TABLE Role_ DROP subtype;
ALTER TABLE Role_ DROP uuid_;
ALTER TABLE Role_ DROP userId;
ALTER TABLE Role_ DROP userName;
ALTER TABLE Role_ DROP modifiedDate;

-- Delete unused roles
delete from Role_ where roleId in (10120, 10123 ,10124 ,10125, 10126, 10127, 10128, 26021, 1015916,
    1015917, 1015918, 1411593);


# --- !Ups

CREATE TABLE users (

    email VARCHAR (100) PRIMARY KEY,
    name VARCHAR (255),
    password VARCHAR (255),

);

INSERT INTO  users (email, name, password) values ('blondeau.gui@gmail.com', 'Guillaume Blondeau', 'test');

# --- !Downs

DELETE FROM users where email = 'blondeau.gui@gmail.com';
DROP TABLE users;

Remove comma in db script

# --- !Ups

CREATE TABLE users (

    email VARCHAR (100) PRIMARY KEY,
    name VARCHAR (255),
    password VARCHAR (255)
);

INSERT INTO  users (email, name, password) values ('blondeau.gui@gmail.com', 'Guillaume Blondeau', 'test');

# --- !Downs

DELETE FROM users where email = 'blondeau.gui@gmail.com';
DROP TABLE users;


-- $Header$

DROP TABLE pgpool_catalog.dist_def;
DROP TABLE pgpool_catalog.query_cache;
DROP SCHEMA pgpool_catalog;

CREATE SCHEMA pgpool_catalog;

CREATE TABLE pgpool_catalog.dist_def(
	dbname TEXT,
	schema_name TEXT,
	table_name TEXT,
	col_name TEXT NOT NULL CHECK (col_name = ANY (col_list)),
	col_list TEXT[] NOT NULL,
	type_list TEXT[] NOT NULL,
	dist_def_func TEXT NOT NULL,
	PRIMARY KEY (dbname,schema_name,table_name)
);

CREATE TABLE pgpool_catalog.replicate_def(
	dbname TEXT,
	schema_name TEXT,
	table_name TEXT,
	col_list TEXT[] NOT NULL,
	type_list TEXT[] NOT NULL,
	PRIMARY KEY (dbname,schema_name,table_name)
);

CREATE TABLE pgpool_catalog.query_cache (
	hash TEXT,
	query TEXT,
	value bytea,
	dbname TEXT,
	create_time TIMESTAMP WITH TIME ZONE,
	PRIMARY KEY(hash, dbname)
);

Drop replicate_def table if exists.

-- $Header$

DROP TABLE pgpool_catalog.dist_def;
DROP TABLE pgpool_catalog.replicate_def;
DROP TABLE pgpool_catalog.query_cache;
DROP SCHEMA pgpool_catalog;

CREATE SCHEMA pgpool_catalog;

CREATE TABLE pgpool_catalog.dist_def(
	dbname TEXT,
	schema_name TEXT,
	table_name TEXT,
	col_name TEXT NOT NULL CHECK (col_name = ANY (col_list)),
	col_list TEXT[] NOT NULL,
	type_list TEXT[] NOT NULL,
	dist_def_func TEXT NOT NULL,
	PRIMARY KEY (dbname,schema_name,table_name)
);

CREATE TABLE pgpool_catalog.replicate_def(
	dbname TEXT,
	schema_name TEXT,
	table_name TEXT,
	col_list TEXT[] NOT NULL,
	type_list TEXT[] NOT NULL,
	PRIMARY KEY (dbname,schema_name,table_name)
);

CREATE TABLE pgpool_catalog.query_cache (
	hash TEXT,
	query TEXT,
	value bytea,
	dbname TEXT,
	create_time TIMESTAMP WITH TIME ZONE,
	PRIMARY KEY(hash, dbname)
);


ALTER TABLE equipment_types
  ADD COLUMN iscoldchain boolean NULL;

Make migration screen more robust by checking for column existence

DO $$
  BEGIN
    ALTER TABLE equipment_types ADD COLUMN iscoldchain boolean NULL;
  EXCEPTION
    WHEN duplicate_column THEN RAISE NOTICE 'column iscoldchain already exists in equipment_types.';
  END;
$$


-- Up
ALTER TABLE "media" ADD COLUMN "rgTrackGain" REAL;
ALTER TABLE "media" ADD COLUMN "rgTrackPeak" REAL;

-- Down

Add default ReplayGain pref to schema

-- Up
ALTER TABLE "media" ADD COLUMN "rgTrackGain" REAL;
ALTER TABLE "media" ADD COLUMN "rgTrackPeak" REAL;

INSERT INTO prefs (key,data) VALUES ('isReplayGainEnabled','false');

-- Down


ALTER TABLE `land` ADD `Type` SMALLINT(6) NOT NULL DEFAULT '0' AFTER `LandId`;
ALTER TABLE `house` ADD `HouseName` binary(23) DEFAULT "" AFTER `Comment`;
ALTER TABLE `house` ADD `HousePartModels` BINARY(48) DEFAULT "" AFTER `Endorsements`;
ALTER TABLE `house` ADD `HousePartColours` BINARY(48) DEFAULT "" AFTER `HousePartModels`;

Correct house db column sizes

ALTER TABLE `land` ADD `Type` SMALLINT(6) NOT NULL DEFAULT '0' AFTER `LandId`;
ALTER TABLE `house` ADD `HouseName` binary(23) DEFAULT "" AFTER `Comment`;
ALTER TABLE `house` ADD `HousePartModels` BINARY(32) DEFAULT "" AFTER `Endorsements`;
ALTER TABLE `house` ADD `HousePartColours` BINARY(8) DEFAULT "" AFTER `HousePartModels`;


-- Trigger function that will, at the database level, prevent
-- anyone changing the boardinghouse_schema.schema value for
-- a saved schema.

CREATE OR REPLACE FUNCTION reject_schema_column_change() RETURNS TRIGGER AS $$
  BEGIN
    RAISE EXCEPTION 'Schema % cannot be renamed', OLD.schema;
  END;
$$ LANGUAGE plpgsql;

DO $$
BEGIN
  IF NOT EXISTS(
    SELECT * FROM information_schema.triggers
    WHERE event_object_table = 'boardinghouse_schema'
    AND trigger_name = 'protect_boardinghouse_schema_column'
  ) AND EXISTS(
    SELECT * FROM information_schema.tables
    WHERE table_name = 'boardinghouse_schema'
    AND table_schema = 'public'
  )THEN
    CREATE TRIGGER protect_boardinghouse_schema_column
      BEFORE UPDATE OF schema ON public.boardinghouse_schema
      FOR EACH ROW
      WHEN (OLD.schema IS DISTINCT FROM NEW.schema)
      EXECUTE PROCEDURE reject_schema_column_change();
  END IF;
END;
$$

Raise database-level exception on schema name change.

-- Trigger function that will, at the database level, prevent
-- anyone changing the boardinghouse_schema.schema value for
-- a saved schema.

CREATE OR REPLACE FUNCTION reject_schema_column_change() RETURNS TRIGGER AS $$
  BEGIN
    RAISE EXCEPTION 'Schema cannot be renamed' USING HINT = OLD.schema;
  END;
$$ LANGUAGE plpgsql;

DO $$
BEGIN
  IF NOT EXISTS(
    SELECT * FROM information_schema.triggers
    WHERE event_object_table = 'boardinghouse_schema'
    AND trigger_name = 'protect_boardinghouse_schema_column'
  ) AND EXISTS(
    SELECT * FROM information_schema.tables
    WHERE table_name = 'boardinghouse_schema'
    AND table_schema = 'public'
  )THEN
    CREATE TRIGGER protect_boardinghouse_schema_column
      BEFORE UPDATE OF schema ON public.boardinghouse_schema
      FOR EACH ROW
      WHEN (OLD.schema IS DISTINCT FROM NEW.schema)
      EXECUTE PROCEDURE reject_schema_column_change();
  END IF;
END;
$$


-- 2015-11-27
alter table bookmarks add column notes text;

Change case of SQL statement to be more SQLey

-- 2015-11-27
ALTER TABLE "bookmarks" ADD COLUMN `notes` TEXT;


CREATE ROLE administrator;
CREATE USER administrator_user
  PASSWORD 'password'
  IN ROLE administrator;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE users, contracts, calls TO administrator;

CREATE OR REPLACE FUNCTION _check_time()
  RETURNS TRIGGER AS $$
    BEGIN
      IF (current_user = 'administrator_user')
      THEN
        IF NOT (current_time BETWEEN '8:00:00' AND '15:00:00')
        THEN
          RAISE EXCEPTION 'Current time is %s, you can work between 8:00 and 15:00', current_time;
        END IF;
      END IF;
      RETURN NULL;
    END;
  $$ LANGUAGE plpgsql;

CREATE TRIGGER check_time_trigger
  BEFORE INSERT OR UPDATE OR DELETE ON contracts
  EXECUTE PROCEDURE _check_time();

CREATE TRIGGER check_time_trigger
  BEFORE INSERT OR UPDATE OR DELETE ON users
  EXECUTE PROCEDURE _check_time();

CREATE TRIGGER check_time_trigger
  BEFORE INSERT OR UPDATE OR DELETE ON calls
  EXECUTE PROCEDURE _check_time();

Remove redundant CREATE TRIGGER calls


version https://git-lfs.github.com/spec/v1
oid sha256:acee4ec99930c401f2a9078a246ba54f8758d119dbd7b87fdbeec2af33dcad4e
size 2916070

Update SQL dump with latest release

version https://git-lfs.github.com/spec/v1
oid sha256:bbf3c2458396e1b5e1f7ec9a228f16c91eb36e227e4a9d01730b04297fc0c516
size 52540217


CREATE TABLE `HardCache` (
    `Id` varchar(255) NOT NULL,
    `Bucket` varchar(255) NOT NULL,
    `Value` longtext NOT NULL,
    `EndDate` datetime NOT NULL,
    `Created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `Modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `TTL` int(11) NOT NULL,

    PRIMARY KEY (`Id`,`Bucket`) USING BTREE,

    KEY `k_EndDate` (`EndDate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Add if exists to table create

CREATE TABLE IF NOT EXISTS `HardCache` (
    `Id` varchar(255) NOT NULL,
    `Bucket` varchar(255) NOT NULL,
    `Value` longtext NOT NULL,
    `EndDate` datetime NOT NULL,
    `Created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `Modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `TTL` int(11) NOT NULL,

    PRIMARY KEY (`Id`,`Bucket`) USING BTREE,

    KEY `k_EndDate` (`EndDate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- packages
DROP PACKAGE ATLASAPI;
DROP PACKAGE ATLASLDR;
DROP PACKAGE ATLASMGR;
DROP PACKAGE BODY ATLASAPI;
DROP PACKAGE BODY ATLASLDR;
DROP PACKAGE BODY ATLASMGR;

-- procedures
DROP PROCEDURE CUR_MERGEPROPERTYVALUE;

-- functions
DROP FUNCTION LIST_TO_TABLE;

-- tables
BEGIN
  FOR T in (SELECT table_name from user_tables) LOOP
  EXECUTE IMMEDIATE ('DROP TABLE ' || T.table_name || ' CASCADE CONSTRAINTS');
  END LOOP;
END;
/

-- views
BEGIN
  FOR T in (SELECT view_name from user_views) LOOP
  EXECUTE IMMEDIATE ('DROP VIEW ' || T.view_name);
  END LOOP;
END;
/

-- types
BEGIN
  FOR T IN (SELECT type_name FROM user_types) LOOP
    EXECUTE IMMEDIATE ('DROP TYPE ' || T.type_name);
  END LOOP;
END;
/

-- sequences
BEGIN
  FOR T in (SELECT sequence_name from user_sequences) LOOP
  EXECUTE IMMEDIATE ('DROP SEQUENCE ' || T.sequence_name);
  END LOOP;
END;
/

purge recyclebin;

quit;

Drop types force - clean out types more reliably.

-- packages
DROP PACKAGE ATLASAPI;
DROP PACKAGE ATLASLDR;
DROP PACKAGE ATLASMGR;
DROP PACKAGE BODY ATLASAPI;
DROP PACKAGE BODY ATLASLDR;
DROP PACKAGE BODY ATLASMGR;

-- procedures
DROP PROCEDURE CUR_MERGEPROPERTYVALUE;

-- functions
DROP FUNCTION LIST_TO_TABLE;

-- tables
BEGIN
  FOR T in (SELECT table_name from user_tables) LOOP
    EXECUTE IMMEDIATE ('DROP TABLE ' || T.table_name || ' CASCADE CONSTRAINTS');
  END LOOP;
END;
/

-- views
BEGIN
  FOR T in (SELECT view_name from user_views) LOOP
    EXECUTE IMMEDIATE ('DROP VIEW ' || T.view_name);
  END LOOP;
END;
/

-- types
BEGIN
  FOR T IN (SELECT type_name FROM user_types) LOOP
    EXECUTE IMMEDIATE ('DROP TYPE ' || T.type_name || ' FORCE');
  END LOOP;
END;
/

-- sequences
BEGIN
  FOR T in (SELECT sequence_name from user_sequences) LOOP
    EXECUTE IMMEDIATE ('DROP SEQUENCE ' || T.sequence_name);
  END LOOP;
END;
/

purge recyclebin;

quit;