DELIMITER $$

DROP PROCEDURE IF EXISTS `GetUpcomingBadges` $$

CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetUpcomingBadges`(IN `agent_name` VARCHAR(15))
    READS SQL DATA
BEGIN

DROP TABLE IF EXISTS UpcomingBadges;

CREATE TEMPORARY TABLE UpcomingBadges (
    stat VARCHAR(20),
    badge VARCHAR(20),
    next VARCHAR(10),
    progress DOUBLE(3,2),
    days_remaining DOUBLE(5,1)
);

SELECT MAX(date) INTO @latest_submission
  FROM Data
 WHERE agent = agent_name;

INSERT INTO UpcomingBadges (stat, badge, next, progress, days_remaining)
  SELECT b.stat,
         b.name,
         b.level,
         d.value / b.amount_required,
         (b.amount_required - d.value) / GetRateForAgentAndStat(agent_name, b.stat) `remaining`
    FROM Data d
    JOIN Badges b ON d.stat = b.stat
    JOIN Stats s ON d.stat = s.stat
   WHERE d.agent = agent_name AND
         d.date = @latest_submission AND
         b.amount_required > d.value AND
         s.prediction = 1
GROUP BY b.stat
ORDER BY remaining ASC;

DELETE FROM UpcomingBadges WHERE stat IN
       ('ap', 'portals_discovered', 'oldest_portal');

END $$
DELIMITER ;

Hide badges with no value in "Upcoming Badges"

DELIMITER $$

DROP PROCEDURE IF EXISTS `GetUpcomingBadges` $$

CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetUpcomingBadges`(IN `agent_name` VARCHAR(15))
    READS SQL DATA
BEGIN

DROP TABLE IF EXISTS UpcomingBadges;

CREATE TEMPORARY TABLE UpcomingBadges (
    stat VARCHAR(20),
    badge VARCHAR(20),
    next VARCHAR(10),
    progress DOUBLE(3,2),
    days_remaining DOUBLE(5,1)
);

SELECT MAX(date) INTO @latest_submission
  FROM Data
 WHERE agent = agent_name;

INSERT INTO UpcomingBadges (stat, badge, next, progress, days_remaining)
  SELECT b.stat,
         b.name,
         b.level,
         d.value / b.amount_required,
         ((b.amount_required - d.value) / GetRateForAgentAndStat(agent_name, b.stat)) remaining
    FROM Data d
    JOIN Badges b ON d.stat = b.stat
    JOIN Stats s ON d.stat = s.stat
   WHERE d.agent = agent_name AND
         d.date = @latest_submission AND
         b.amount_required > d.value AND
         d.value > 0 AND
         s.prediction = 1
GROUP BY b.stat
ORDER BY remaining ASC;

END $$
DELIMITER ;


CREATE TABLE cron.Processes (
ProcessID                 serial       NOT NULL,
JobID                     integer      NOT NULL REFERENCES cron.Jobs(JobID),
Running                   boolean      NOT NULL DEFAULT FALSE,
FirstRunStartedAt         timestamptz,
FirstRunFinishedAt        timestamptz,
LastRunStartedAt          timestamptz,
LastRunFinishedAt         timestamptz,
BatchJobState             batchjobstate,
LastSQLSTATE              text,
LastSQLERRM               text,
PgBackendPID              integer,
PRIMARY KEY (ProcessID),
CHECK(LastSQLSTATE ~ '^[0-9A-Z]{5}$')
);

ALTER TABLE cron.Processes OWNER TO pgcronjob;

Remove Process.PgBackendPID as this column is not set anymore. We instead log it per execution to Log.

CREATE TABLE cron.Processes (
ProcessID                 serial       NOT NULL,
JobID                     integer      NOT NULL REFERENCES cron.Jobs(JobID),
Running                   boolean      NOT NULL DEFAULT FALSE,
FirstRunStartedAt         timestamptz,
FirstRunFinishedAt        timestamptz,
LastRunStartedAt          timestamptz,
LastRunFinishedAt         timestamptz,
BatchJobState             batchjobstate,
LastSQLSTATE              text,
LastSQLERRM               text,
PRIMARY KEY (ProcessID),
CHECK(LastSQLSTATE ~ '^[0-9A-Z]{5}$')
);

ALTER TABLE cron.Processes OWNER TO pgcronjob;


-- Drop tables in reverse order of creation, otherwise
-- we may run into foreign key constraints
DROP TABLE IF EXISTS earned_badges;
DROP TABLE IF EXISTS earners;
DROP TABLE IF EXISTS available_badges;
DROP TABLE IF EXISTS issuers;

CREATE TABLE issuers
(
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  url TEXT NOT NULL,
  description TEXT NOT NULL,
  image TEXT,
  email TEXT
);

CREATE TABLE available_badges
(
  id INTEGER PRIMARY KEY,
  issuer_id INTEGER NOT NULL,
  name TEXT NOT NULL,
  description TEXT NOT NULL,
  image TEXT NOT NULL,
  criteria TEXT NOT NULL,
  FOREIGN KEY (issuer_id) REFERENCES issuers(id)
);

CREATE TABLE earners
(
  id INTEGER PRIMARY KEY,
  hash TEXT NOT NULL UNIQUE,
  type TEXT NOT NULL
);

CREATE TABLE earned_badges
(
  id INTEGER PRIMARY KEY,
  earner_id INTEGER NOT NULL,
  badge_id INTEGER NOT NULL,
  verification_type TEXT NOT NULL,
  verification_url TEXT NOT NULL,
  issued TEXT NOT NULL,
  image TEXT,
  evidence TEXT,
  expires TEXT,
  revoked INTEGER NOT NULL DEFAULT 0,
  FOREIGN KEY (earner_id) REFERENCES earners(id),
  FOREIGN KEY (badge_id) REFERENCES available_badges(id)
);

Change earned badges to use TEXT uid, in line with specification

-- Drop tables in reverse order of creation, otherwise
-- we may run into foreign key constraints
DROP TABLE IF EXISTS earned_badges;
DROP TABLE IF EXISTS earners;
DROP TABLE IF EXISTS available_badges;
DROP TABLE IF EXISTS issuers;

CREATE TABLE issuers
(
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  url TEXT NOT NULL,
  description TEXT NOT NULL,
  image TEXT,
  email TEXT
);

CREATE TABLE available_badges
(
  id INTEGER PRIMARY KEY,
  issuer_id INTEGER NOT NULL,
  name TEXT NOT NULL,
  description TEXT NOT NULL,
  image TEXT NOT NULL,
  criteria TEXT NOT NULL,
  FOREIGN KEY (issuer_id) REFERENCES issuers(id)
);

CREATE TABLE earners
(
  id INTEGER PRIMARY KEY,
  hash TEXT NOT NULL UNIQUE,
  type TEXT NOT NULL
);

CREATE TABLE earned_badges
(
  uid TEXT PRIMARY KEY,
  earner_id INTEGER NOT NULL,
  badge_id INTEGER NOT NULL,
  verification_type TEXT NOT NULL,
  verification_url TEXT NOT NULL,
  issued TEXT NOT NULL,
  image TEXT,
  evidence TEXT,
  expires TEXT,
  revoked INTEGER NOT NULL DEFAULT 0,
  FOREIGN KEY (earner_id) REFERENCES earners(id),
  FOREIGN KEY (badge_id) REFERENCES available_badges(id)
);


\set ON_ERROR_STOP 1

-- drop unused tables
DROP TABLE IF EXISTS alexa_topsites CASCADE;
DROP TABLE IF EXISTS top_crashes_by_signature CASCADE;
DROP TABLE IF EXISTS top_crashes_by_url CASCADE;
DROP TABLE IF EXISTS top_crashes_by_url_signature CASCADE;
DROP TABLE IF EXISTS signature_build CASCADE;
DROP TABLE IF EXISTS signature_first CASCADE;
DROP TABLE IF EXISTS signature_bugs_rollup CASCADE;
DROP TABLE IF EXISTS signature_productdims CASCADE;
DROP TABLE IF EXISTS release_build_type_map CASCADE;
DROP TABLE IF EXISTS builds CASCADE;
DROP TABLE IF EXISTS osdims CASCADE;
DROP TABLE IF EXISTS productdims_version_sort CASCADE;
DROP TABLE IF EXISTS product_visibility CASCADE;
DROP TABLE IF EXISTS productdims CASCADE;
DROP TABLE IF EXISTS urldims CASCADE;

-- rename product version id sequence
DO $f$
BEGIN
PERFORM 1 FROM pg_class
WHERE relname = 'productdims_id_seq1';

IF FOUND THEN
	ALTER TABLE productdims_id_seq1 RENAME TO product_version_id_seq;
	ALTER TABLE product_versions ALTER COLUMN product_version_id
		SET DEFAULT NEXTVAL('product_version_id_seq');
	ALTER SEQUENCE product_version_id_seq OWNED BY product_versions.product_version_id;
END IF;
END; $f$;

Purge plperl from database, as it is no longer required.

\set ON_ERROR_STOP 1

-- drop unused tables
DROP TABLE IF EXISTS alexa_topsites CASCADE;
DROP TABLE IF EXISTS top_crashes_by_signature CASCADE;
DROP TABLE IF EXISTS top_crashes_by_url CASCADE;
DROP TABLE IF EXISTS top_crashes_by_url_signature CASCADE;
DROP TABLE IF EXISTS signature_build CASCADE;
DROP TABLE IF EXISTS signature_first CASCADE;
DROP TABLE IF EXISTS signature_bugs_rollup CASCADE;
DROP TABLE IF EXISTS signature_productdims CASCADE;
DROP TABLE IF EXISTS release_build_type_map CASCADE;
DROP TABLE IF EXISTS builds CASCADE;
DROP TABLE IF EXISTS osdims CASCADE;
DROP TABLE IF EXISTS productdims_version_sort CASCADE;
DROP TABLE IF EXISTS product_visibility CASCADE;
DROP TABLE IF EXISTS productdims CASCADE;
DROP TABLE IF EXISTS urldims CASCADE;

DROP FUNCTION IF EXISTS tokenize_version(text);
DROP LANGUAGE IF EXISTS plperl;

-- rename product version id sequence
DO $f$
BEGIN
PERFORM 1 FROM pg_class
WHERE relname = 'productdims_id_seq1';

IF FOUND THEN
	ALTER TABLE productdims_id_seq1 RENAME TO product_version_id_seq;
	ALTER TABLE product_versions ALTER COLUMN product_version_id
		SET DEFAULT NEXTVAL('product_version_id_seq');
	ALTER SEQUENCE product_version_id_seq OWNED BY product_versions.product_version_id;
END IF;
END; $f$;


DROP DATABASE IF EXISTS fizzbuzz_twitterbot;
CREATE DATABASE fizzbuzz_twitterbot CHARACTER SET utf8;
USE fizzbuzz_twitterbot;

DROP TABLE IF EXISTS fizzbuzz;
CREATE TABLE fizzbuzz (
  id BIGINT NOT NULL AUTO_INCREMENT,
  number BIGINT NOT NULL,
  tweet VARCHAR(140) NOT NULL,
  tweet_id BIGINT,
  updated_at DATETIME NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id)
) ENGINE InnoDB CHARSET utf8;

DROP TABLE IF EXISTS post_errors;
CREATE TABLE post_errors (
  id BIGINT NOT NULL AUTO_INCREMENT,
  fizzbuzz_id BIGINT NOT NULL,
  error_message TEXT NOT NULL,
  updated_at DATETIME NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (fizzbuzz_id) REFERENCES fizzbuzz (id)
) ENGINE InnoDB CHARSET utf8;

Rename table name, fizzbuzz -> fizzbuzz_tweets.

DROP DATABASE IF EXISTS fizzbuzz_twitterbot;
CREATE DATABASE fizzbuzz_twitterbot CHARACTER SET utf8;
USE fizzbuzz_twitterbot;

DROP TABLE IF EXISTS fizzbuzz_tweets;
CREATE TABLE fizzbuzz_tweets (
  id BIGINT NOT NULL AUTO_INCREMENT,
  number BIGINT NOT NULL,
  tweet VARCHAR(140) NOT NULL,
  tweet_id BIGINT,
  updated_at DATETIME NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id)
) ENGINE InnoDB CHARSET utf8;

DROP TABLE IF EXISTS post_errors;
CREATE TABLE post_errors (
  id BIGINT NOT NULL AUTO_INCREMENT,
  fizzbuzz_tweet_id BIGINT NOT NULL,
  error_message TEXT NOT NULL,
  updated_at DATETIME NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (fizzbuzz_tweet_id) REFERENCES fizzbuzz_tweets (id)
) ENGINE InnoDB CHARSET utf8;


CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL auto_increment,
  `username` char(64) NOT NULL,
  `firstname` char(64) NULL,
  `lastname` char(64) NULL,
  `email` char(64) NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `result` (
  `id` int(11) NOT NULL auto_increment,
  `route_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `time` int(11) NOT NULL,
  `date` DATETIME NOT NULL,
   PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `route` (
  `id` int(11) NOT NULL auto_increment,
  `name` char(64) NOT NULL,
  `start_tag` char(64) NULL,
  `end_tag` char(64) NULL,
  `description` TEXT NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Update the column definition so we can track time down to the millisecond. This will be an important tie breaker for users who are using the NFC tags to track their time.

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL auto_increment,
  `username` char(64) NOT NULL,
  `firstname` char(64) NULL,
  `lastname` char(64) NULL,
  `email` char(64) NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `result` (
  `id` int(11) NOT NULL auto_increment,
  `route_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `time` decimal(8,3) NOT NULL,
  `date` DATETIME NOT NULL,
   PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `route` (
  `id` int(11) NOT NULL auto_increment,
  `name` char(64) NOT NULL,
  `start_tag` char(64) NULL,
  `end_tag` char(64) NULL,
  `description` TEXT NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE wf_docevents (
id INT PRIMARY KEY,
doc_id INT NOT NULL,
docstate_id INT NOT NULL,
user_id INT NOT NULL,
docaction_id INT NOT NULL,
data TEXT,
ctime TIMESTAMP NOT NULL,
FOREIGN KEY (doc_id) REFERENCES wf_documents(id),
FOREIGN KEY (docstate_id) REFERENCES wf_docstates_master(id),
FOREIGN KEY (user_id) REFERENCES users_masters(id),
FOREIGN KEY (docaction_id) REFERENCES wf_docactions_master(id)
);

Add a flag to table of document events to track application of events

CREATE TABLE wf_docevents (
id INT PRIMARY KEY,
doc_id INT NOT NULL,
docstate_id INT NOT NULL,
user_id INT NOT NULL,
docaction_id INT NOT NULL,
data TEXT,
ctime TIMESTAMP NOT NULL,
applied BOOLEAN NOT NULL,
FOREIGN KEY (doc_id) REFERENCES wf_documents(id),
FOREIGN KEY (docstate_id) REFERENCES wf_docstates_master(id),
FOREIGN KEY (user_id) REFERENCES users_masters(id),
FOREIGN KEY (docaction_id) REFERENCES wf_docactions_master(id)
);


WITH RECURSIVE trust_graph(confirmer_id, skill_claimant_id, depth, path, confirmations_in_graph) AS (
  SELECT
    conf1.confirmer_id,
    conf1.skill_claimant_id,
    1                                                   AS depth,
    ARRAY [conf1.confirmer_id, conf1.skill_claimant_id] AS path,
    ARRAY [conf1.id] AS confirmations_in_graph
  FROM confirmations conf1
  WHERE confirmer_id = 2
  UNION
  SELECT
    conf2.confirmer_id,
    conf2.skill_claimant_id,
    previous_results.depth + 1,
    previous_results.path || conf2.skill_claimant_id,
    previous_results.confirmations_in_graph || conf2.id
  FROM confirmations conf2, trust_graph previous_results
  WHERE conf2.confirmer_id = previous_results.skill_claimant_id
        AND depth < 3
        AND NOT conf2.skill_claimant_id = ANY (path)
        AND NOT conf2.id = ANY (previous_results.confirmations_in_graph)
        AND NOT (previous_results.path || conf2.skill_claimant_id) = previous_results.path
)
SELECT DISTINCT path,
  skill_claimant_id,
  confirmer_id,
  depth
FROM trust_graph
ORDER BY PATH;

Return users with the desired skills

WITH RECURSIVE trust_graph(confirmer_id, skill_claimant_id, skill_id, depth, path, confirmations_in_graph) AS (
  SELECT
    conf1.confirmer_id,
    conf1.skill_claimant_id,
    conf1.skill_id,
    1                                                   AS depth,
    ARRAY [conf1.confirmer_id, conf1.skill_claimant_id] AS path,
    ARRAY [conf1.id] AS confirmations_in_graph
  FROM confirmations conf1
  WHERE confirmer_id = 2
  UNION
  SELECT
    conf2.confirmer_id,
    conf2.skill_claimant_id,
    conf2.skill_id,
    previous_results.depth + 1,
    previous_results.path || conf2.skill_claimant_id,
    previous_results.confirmations_in_graph || conf2.id
  FROM confirmations conf2, trust_graph previous_results
  WHERE conf2.confirmer_id = previous_results.skill_claimant_id
        AND depth < 3
        AND NOT conf2.skill_claimant_id = ANY (path)
        AND NOT conf2.id = ANY (previous_results.confirmations_in_graph)
        AND NOT (previous_results.path || conf2.skill_claimant_id) = previous_results.path
)
SELECT DISTINCT skill_claimant_id
FROM trust_graph
WHERE skill_id = 1;

-- SELECT DISTINCT path,
--   skill_claimant_id,
--   confirmer_id,
--   skill_id,
--   depth
-- FROM trust_graph
--   WHERE skill_id = 5
-- ORDER BY PATH;


CREATE VIEW metric.replication_lag AS
SELECT
    pg_stat_replication.client_addr,
    public.wal_location_to_int(pg_current_xlog_location()) - public.wal_location_to_int(pg_stat_replication.replay_location) AS bytes_lag
FROM pg_stat_replication;

Replace use of wal_location_to_int function with native operator

CREATE VIEW metric.replication_lag AS
SELECT
    pg_stat_replication.client_addr,
    pg_current_xlog_location() - pg_stat_replication.replay_location AS bytes_lag
FROM pg_stat_replication;


group DAO;

ensureEntrySchemaInPlace() ::= <<

create table if not exists entry (entry_number serial primary key, sha256hex varchar, timestamp timestamp);

alter table entry alter column entry_number drop default;

create table if not exists total_entries (count integer);

--Insert query below initializes the total_entries table by 0 if it is not initialized yet
insert into total_entries (count) select 0 where not exists (select 1 from total_entries);

>>

Allow efficient finding of entries by item hash

group DAO;

ensureEntrySchemaInPlace() ::= <<

create table if not exists entry (entry_number serial primary key, sha256hex varchar, timestamp timestamp);

alter table entry alter column entry_number drop default;

create index if not exists entry_sha256hex_index on entry (sha256hex);

create table if not exists total_entries (count integer);

--Insert query below initializes the total_entries table by 0 if it is not initialized yet
insert into total_entries (count) select 0 where not exists (select 1 from total_entries);

>>


CREATE TABLE builds (
  id SERIAL,
  created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),

  key VARCHAR(100),
  name VARCHAR(100),
  status VARCHAR(20),
  started TIMESTAMP WITH TIME ZONE,
  finished TIMESTAMP WITH TIME ZONE
);

CREATE INDEX builds_idx_key ON builds (key);

Add schema to store build logs

CREATE TABLE builds (
  id SERIAL,
  created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),

  key VARCHAR(100),
  name VARCHAR(100),
  status VARCHAR(20),
  started TIMESTAMP WITH TIME ZONE,
  finished TIMESTAMP WITH TIME ZONE
);

CREATE INDEX builds_idx_key ON builds (key);

CREATE TABLE logs (
	id SERIAL,
	build_id INT NOT NULL,
	created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),

	type VARCHAR(50)
);

CREATE INDEX logs_idx_type ON logs (type);


create extension pgq;

\set ECHO none
\i structure/install.sql
\set ECHO all

create extension pgq_coop from 'unpackaged';
drop extension pgq_coop;

-- workaround for postgres bug
drop schema if exists pgq_coop;

create extension pgq_coop;

Remove 'workaround' as it was pgq_coop own bug

create extension pgq;

\set ECHO none
\i structure/install.sql
\set ECHO all

create extension pgq_coop from 'unpackaged';
drop extension pgq_coop;

create extension pgq_coop;


set define '^'
set verify off

ACCEPT default_tbs CHAR PROMPT 'Please enter the tablespace you would like to make the default [USERS]: '

declare
    l_new_tablespace varchar2(50);
begin

    l_new_tablespace := '^default_tbs';

    if l_new_tablespace IS NULL
    then
        l_new_tablespace := 'USERS';
    end if;

    execute immediate 'alter database default tablespace ' || l_new_tablespace;

end;
/

exit

Add comment about verifying default tablespace

/*

Default tablespace verified with the query:

select *
from database_properties
where property_name = 'DEFAULT_PERMANENT_TABLESPACE';

*/

set define '^'
set verify off

ACCEPT default_tbs CHAR PROMPT 'Please enter the tablespace you would like to make the default [USERS]: '

declare
    l_new_tablespace varchar2(50);
begin

    l_new_tablespace := '^default_tbs';

    if l_new_tablespace IS NULL
    then
        l_new_tablespace := 'USERS';
    end if;

    execute immediate 'alter database default tablespace ' || l_new_tablespace;

end;
/

exit


/*SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;*/
BEGIN TRANSACTION AppendStream;
    DECLARE @streamIdInternal AS INT;
    DECLARE @latestStreamVersion AS INT;

     SELECT @streamIdInternal = dbo.Streams.IdInternal
       FROM dbo.Streams
      WHERE dbo.Streams.Id = @streamId;

         IF @streamIdInternal IS NULL
            BEGIN
                INSERT INTO dbo.Streams (Id, IdOriginal) VALUES (@streamId, @streamIdOriginal);
                SELECT @streamIdInternal = SCOPE_IDENTITY();

                INSERT INTO dbo.Events (StreamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata)
                 SELECT @streamIdInternal,
                        StreamVersion,
                        Id,
                        Created,
                        [Type],
                        JsonData,
                        JsonMetadata
                   FROM @newEvents
               ORDER BY StreamVersion;
            END
       ELSE
           BEGIN
                 SELECT TOP(1)
                         @latestStreamVersion = dbo.Events.StreamVersion
                    FROM dbo.Events
                   WHERE dbo.Events.StreamIDInternal = @streamIdInternal
                ORDER BY dbo.Events.Ordinal DESC;

            INSERT INTO dbo.Events (StreamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata)
                 SELECT @streamIdInternal,
                        StreamVersion + @latestStreamVersion + 1,
                        Id,
                        Created,
                        [Type],
                        JsonData,
                        JsonMetadata
                   FROM @newEvents
               ORDER BY StreamVersion;
           END
COMMIT TRANSACTION AppendStream;

Set isolation level to 'Repeatable Read'. Seems to prevent index errors (WrongExpectedVersion) that default isolation level can generate. Doesn't result in deadlocks that 'Serializable' raises.

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANSACTION AppendStream;
    DECLARE @streamIdInternal AS INT;
    DECLARE @latestStreamVersion AS INT;

     SELECT @streamIdInternal = dbo.Streams.IdInternal
       FROM dbo.Streams
      WHERE dbo.Streams.Id = @streamId;

         IF @streamIdInternal IS NULL
            BEGIN
                INSERT INTO dbo.Streams (Id, IdOriginal) VALUES (@streamId, @streamIdOriginal);
                SELECT @streamIdInternal = SCOPE_IDENTITY();

                INSERT INTO dbo.Events (StreamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata)
                 SELECT @streamIdInternal,
                        StreamVersion,
                        Id,
                        Created,
                        [Type],
                        JsonData,
                        JsonMetadata
                   FROM @newEvents
               ORDER BY StreamVersion;
            END
       ELSE
           BEGIN
                 SELECT TOP(1)
                         @latestStreamVersion = dbo.Events.StreamVersion
                    FROM dbo.Events
                   WHERE dbo.Events.StreamIDInternal = @streamIdInternal
                ORDER BY dbo.Events.Ordinal DESC;

            INSERT INTO dbo.Events (StreamIdInternal, StreamVersion, Id, Created, [Type], JsonData, JsonMetadata)
                 SELECT @streamIdInternal,
                        StreamVersion + @latestStreamVersion + 1,
                        Id,
                        Created,
                        [Type],
                        JsonData,
                        JsonMetadata
                   FROM @newEvents
               ORDER BY StreamVersion;
           END
COMMIT TRANSACTION AppendStream;


UPDATE `settings` SET `cmumversion`='3.2.0', `dbversion`='3.2.0' WHERE `id`=1;
ALTER TABLE `groups` ADD `enabled` tinyint(1) NULL DEFAULT '1' AFTER `comment`;
ALTER TABLE `settings` DROP `dbversion`;
ALTER TABLE `settings` ADD `autoupdcheck` TINYINT(1) NULL AFTER `soonexpusrorder`;
UPDATE `settings` SET `autoupdcheck`='0' WHERE `id`=1;
ALTER TABLE `log_activity` ADD `data` VARCHAR(50) NULL AFTER `userid`;

Truncate manager activity log on upgrade

UPDATE `settings` SET `cmumversion`='3.2.0', `dbversion`='3.2.0' WHERE `id`=1;
ALTER TABLE `groups` ADD `enabled` tinyint(1) NULL DEFAULT '1' AFTER `comment`;
ALTER TABLE `settings` DROP `dbversion`;
ALTER TABLE `settings` ADD `autoupdcheck` TINYINT(1) NULL AFTER `soonexpusrorder`;
UPDATE `settings` SET `autoupdcheck`='0' WHERE `id`=1;
ALTER TABLE `log_activity` ADD `data` VARCHAR(50) NULL AFTER `userid`;
TRUNCATE TABLE `log_activity`;


SELECT name, is_broker_enabled, service_broker_guid FROM sys.databases;

ALTER DATABASE msdb SET ENABLE_BROKER;
ALTER DATABASE CURRENT SET NEW_BROKER  WITH ROLLBACK IMMEDIATE; -- Use ENABLE_BROKER if restoring db
GO

-- Content Queue Procs
DROP PROC dbo.UpdateRecommendationsFromSummary;
GO

DROP PROC dbo.ReadFromContentQueue;
GO

-- Content Queue DDL
DROP SERVICE [//Content/Blog/UpdateRecommendation]
GO

DROP SERVICE [//Recommendation/Blog/Update]
GO

DROP CONTRACT [//Content/Blog/UpdateRecommendationContract]
GO

DROP MESSAGE TYPE [//Recommendations/UpdateBlogPostMessage]
GO

DROP QUEUE [ContentQueue]
GO

-- Member Queue Procs
DROP PROC dbo.SendMemberWelcomePack
GO

DROP PROC dbo.ReadFromMemberQueue
GO

-- Member Queue DDL
DROP SERVICE [//Member/Email/WelcomePack]
GO

DROP SERVICE [//Email/SendAgent]
GO

DROP CONTRACT [//Member/Email/WelcomePackContract]
GO

DROP MESSAGE TYPE [//Member/Email/WelcomePackMessage]
GO

DROP QUEUE [MemberQueue];
GO

Update remove all script to reflect new object names

SELECT name, is_broker_enabled, service_broker_guid FROM sys.databases;

ALTER DATABASE msdb SET ENABLE_BROKER;
ALTER DATABASE CURRENT SET NEW_BROKER  WITH ROLLBACK IMMEDIATE; -- Use ENABLE_BROKER if restoring db
GO

-- Content Queue Procs
DROP PROC dbo.UpdateRecommendationsFromSummary;
GO

DROP PROC dbo.ReadFromContentQueue;
GO

-- Content Queue DDL
DROP SERVICE [//Content/Blog/UpdateRecommendation]
GO

DROP SERVICE [//Recommendation/Blog/Update]
GO

DROP CONTRACT [//Content/Blog/UpdateRecommendationContract]
GO

DROP MESSAGE TYPE [//Recommendations/UpdateBlogPostMessage]
GO

DROP QUEUE [ContentQueue]
GO

-- Member Queue Procs
DROP PROC dbo.SendMemberWelcomePack
GO

DROP PROC dbo.ReadFromMemberQueue
GO

-- Member Queue DDL
DROP SERVICE [//Member/Email/WelcomePack]
GO

DROP SERVICE [//Email/SendAgent]
GO

DROP CONTRACT [//Member/Email/WelcomePackContract]
GO

DROP MESSAGE TYPE [//Member/Email/WelcomePackMessage]
GO

DROP QUEUE [MemberQueue];
GO

-- Media Queue Procs

DROP PROC dbo.ReadFromMediaQueue
GO

-- Media Queue DDL

-- CDN Policy
DROP SERVICE [//Media/Cdn/SetPolicy]
GO

DROP SERVICE [//Cdn/PolicyAgent]
GO

DROP CONTRACT [//Media/Cdn/SetPolicyContract]
GO

DROP MESSAGE TYPE [//Media/Cdn/SetPolicyMessage]
GO

-- CDN Invalidation
DROP SERVICE [//Cdn/InvalidationAgent]
GO

DROP CONTRACT [//Media/Cdn/SetInvalidationContract]
GO

DROP SERVICE [//Media/Cdn/SetInvalidation]
GO

DROP MESSAGE TYPE [//Media/Cdn/SetInvalidationMessage]
GO

DROP QUEUE [MediaQueue];
GO


set linesize 200

set serveroutput on
set verify off

COLUMN MY_USER FORMAT A20
COLUMN DB FORMAT A15
COLUMN NOW FORMAT A40

--Show the details of the connection for confirmation
select user MY_USER, ora_database_name DB, systimestamp NOW from dual;

@"&1"

COLUMN MY_USER 	CLEAR
COLUMN DB 	    CLEAR
COLUMN NOW 	    CLEAR

show error

exit

Add more information to compile output

set linesize 200

set serveroutput on
set verify off

COLUMN MY_USER FORMAT A20
COLUMN DB FORMAT A15
COLUMN NOW FORMAT A40

prompt Database connection information

--Show the details of the connection for confirmation
select user MY_USER, ora_database_name DB, systimestamp NOW from dual;

prompt
prompt Compiling script: &1
prompt
@"&1"

COLUMN MY_USER 	CLEAR
COLUMN DB 	    CLEAR
COLUMN NOW 	    CLEAR

show error

exit


ALTER TABLE `tiki_mailin_accounts` add COLUMN `routing` char(1) NULL AFTER `attachments`;
ALTER TABLE `tiki_mailin_accounts` add COLUMN `leave_email` char(1) NULL AFTER `respond_email`;

Remove the AFTER column clauses from the update

ALTER TABLE `tiki_mailin_accounts` add COLUMN `routing` char(1) NULL;
ALTER TABLE `tiki_mailin_accounts` add COLUMN `leave_email` char(1) NULL;


--
-- Store session data in here.
-- Idea originally from: http://www.developertutorials.com/tutorials/php/saving-php-session-data-database-050711
--

CREATE TABLE `test`.`sess_tmp` (
  `session_store_id` int  NOT NULL AUTO_INCREMENT,
  `session_id` varchar(32)  NOT NULL,
  `user_id` varchar(16)  NOT NULL,
  `date_created` datetime  NOT NULL,
  `last_updated` datetime  NOT NULL,
  `session_data` LONGTEXT  NOT NULL,
  PRIMARY KEY (`session_store_id`)
)
ENGINE = MyISAM;

Make sure all tables are using InnoDB as the engine (for transaction support).

--
-- Store session data in here.
-- Idea originally from: http://www.developertutorials.com/tutorials/php/saving-php-session-data-database-050711
--

CREATE TABLE `test`.`sess_tmp` (
  `session_store_id` int  NOT NULL AUTO_INCREMENT,
  `session_id` varchar(32)  NOT NULL,
  `user_id` varchar(16)  NOT NULL,
  `date_created` datetime  NOT NULL,
  `last_updated` datetime  NOT NULL,
  `session_data` LONGTEXT  NOT NULL,
  PRIMARY KEY (`session_store_id`)
)
ENGINE = InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


/********************************************************************************
	file-centric-snapshot-language-unknown-acceptability.sql

	Assertion:
	Unknown acceptability for language reference set row

********************************************************************************/
	insert into qa_result (runid, assertionuuid, concept_id, details)
	select
		<RUNID>,
		'<ASSERTIONUUID>',
		a.acceptabilityid,
		concat('Language refset entry: ' , a.id, ' has unknown acceptability id=', a.acceptabilityid)
	from langrefset_s a
	where a.active = 1
		and a.acceptabilityid != '900000000000548007'
		and a.acceptabilityid != '900000000000549004';

Add curr_ to table name

/********************************************************************************
	file-centric-snapshot-language-unknown-acceptability.sql

	Assertion:
	Unknown acceptability for language reference set row

********************************************************************************/
	insert into qa_result (runid, assertionuuid, concept_id, details)
	select
		<RUNID>,
		'<ASSERTIONUUID>',
		a.acceptabilityid,
		concat('Language refset entry: ' , a.id, ' has unknown acceptability id=', a.acceptabilityid)
	from curr_langrefset_s a
	where a.active = 1
		and a.acceptabilityid != '900000000000548007'
		and a.acceptabilityid != '900000000000549004';


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);

Improve projects and pages migration.

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,
  modified_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,

  version bigint DEFAULT 0,

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

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

  data text,
  version bigint DEFAULT 0
) WITH (OIDS=FALSE);

CREATE OR REPLACE FUNCTION insert_page_history()
  RETURNS TRIGGER AS $history$
  BEGIN
    IF (TG_OP = 'DELETE') THEN
      DELETE FROM pages_history WHERE page = OLD.id;
      RETURN OLD;
    ELSIF (TG_OP = 'UPDATE') THEN
      INSERT INTO pages_history (page, created_at, data, version)
        VALUES (OLD.id, OLD.modified_at, OLD.data, OLD.version);

      NEW.version := OLD.version + 1;
      RETURN NEW;
    END IF;
    RETURN NULL; -- result is ignored since this is an AFTER trigger
  END;
$history$ LANGUAGE plpgsql;

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

CREATE TRIGGER pages_history_tgr BEFORE UPDATE OR DELETE ON pages
  FOR EACH ROW EXECUTE PROCEDURE insert_page_history();

CREATE TRIGGER pages_modified_at_tgr BEFORE UPDATE ON pages
  FOR EACH ROW EXECUTE PROCEDURE update_modified_at();


CREATE TABLE `users` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `username` varchar(255) default NULL,
    `password` varchar(255) default NULL,
    `email` varchar(255) default NULL,
    PRIMARY KEY (`ID`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `location` (
	`geolat` float(10,4) NOT NULL default '0',
	`geolng` float(10,4) NOT NULL default '0',
	`overall_happiness` float(4,2) default NULL,
	PRIMARY KEY (`geolat`, `geolng`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `data` (
	`user_ID` int(10) unsigned NOT NULL,
	`geolat` float(10,4) default NULL,
	`geolng` float(10,4) default NULL,
	`happiness` float(6,4) default NULL,
	`date_time` date default NULL,
	PRIMARY KEY (`user_ID`, `date_time`),
	FOREIGN KEY(user_ID) REFERENCES users(`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Update schema to include count of entries making up average happiness.

CREATE TABLE `users` (
    `ID` int(10) unsigned NOT NULL auto_increment,
    `username` varchar(255) default NULL,
    `password` varchar(255) default NULL,
    `email` varchar(255) default NULL,
    PRIMARY KEY (`ID`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `location` (
	`geolat` float(10,4) NOT NULL default '0',
	`geolng` float(10,4) NOT NULL default '0',
	`overall_happiness` float(4,2) default NULL,
	`number_of_entries` int(8) default 0,
	PRIMARY KEY (`geolat`, `geolng`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `data` (
	`user_ID` int(10) unsigned NOT NULL,
	`geolat` float(10,4) default NULL,
	`geolng` float(10,4) default NULL,
	`happiness` float(6,4) default NULL,
	`date_time` date default NULL,
	PRIMARY KEY (`user_ID`, `date_time`),
	FOREIGN KEY(user_ID) REFERENCES users(`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


/*Connect to database*/
\connect mobydq

/*Create table user group*/
CREATE TABLE base.user_group (
    id SERIAL PRIMARY KEY
  , name TEXT NOT NULL UNIQUE
  , created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  , updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE base.user_group IS
'User groups are used to managed visibility rules.';

CREATE TRIGGER user_group_updated_date BEFORE UPDATE
ON base.user_group FOR EACH ROW EXECUTE PROCEDURE
base.update_updated_date_column();

/*Create function to create user group role*/
CREATE OR REPLACE FUNCTION base.create_user_group_role()
RETURNS TRIGGER AS $$
DECLARE
    user_group TEXT := 'user_group_' || NEW.id;
BEGIN
    EXECUTE 'CREATE ROLE ' || user_group;
    RETURN NEW;
END;
$$ language plpgsql;

COMMENT ON FUNCTION base.create_user_group_role IS
'Function used to automatically create a role for a user group.';

CREATE TRIGGER user_group_create_user_group_role AFTER INSERT
ON base.user_group FOR EACH ROW EXECUTE PROCEDURE
base.create_user_group_role();

Correct trigger to cascade delete group membership when group is deleted

/*Connect to database*/
\connect mobydq

/*Create table user group*/
CREATE TABLE base.user_group (
    id SERIAL PRIMARY KEY
  , name TEXT NOT NULL UNIQUE
  , created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  , updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE base.user_group IS
'User groups are used to managed visibility rules.';

CREATE TRIGGER user_group_updated_date BEFORE UPDATE
ON base.user_group FOR EACH ROW EXECUTE PROCEDURE
base.update_updated_date_column();

CREATE TRIGGER user_group_delete_user_group_user BEFORE DELETE
ON base.user_group FOR EACH ROW EXECUTE PROCEDURE
base.delete_children('user_group_user', 'user_group_id');

/*Create function to create user group role*/
CREATE OR REPLACE FUNCTION base.create_user_group_role()
RETURNS TRIGGER AS $$
DECLARE
    user_group TEXT := 'user_group_' || NEW.id;
BEGIN
    EXECUTE 'CREATE ROLE ' || user_group;
    RETURN NEW;
END;
$$ language plpgsql;

COMMENT ON FUNCTION base.create_user_group_role IS
'Function used to automatically create a role for a user group.';

CREATE TRIGGER user_group_create_user_group_role AFTER INSERT
ON base.user_group FOR EACH ROW EXECUTE PROCEDURE
base.create_user_group_role();


CREATE TABLE credentials(
    provider    TEXT UNIQUE NOT NULL,
    api_key     TEXT NOT NULL
);

CREATE TABLE users(
    user_id     INT PRIMARY KEY NOT NULL,
    name        TEXT NOT NULL,
    email       TEXT NOT NULL
);

CREATE TABLE map_cache(
    place_id        TEXT PRIMARY KEY NOT NULL,
    lat             REAL NOT NULL,
    lng             REAL NOT NULL,
    city            TEXT NOT NULL,
    region          TEXT NOT NULL,
    country_name    TEXT NOT NULL,
    country_code    TEXT NOT NULL
);

CREATE TABLE location_history(
    user_id     INT NOT NULL,
    place_id    TEXT NOT NULL,
    timestamp   INT NOT NULL,
    FOREIGN KEY(user_id) REFERENCES users(user_id),
    FOREIGN KEY(place_id) REFERENCES map_cache(place_id),
    PRIMARY KEY (user_id, place_id)
);

Change type of timestamp col of location history table from INT to REAL

CREATE TABLE credentials(
    provider    TEXT UNIQUE NOT NULL,
    api_key     TEXT NOT NULL
);

CREATE TABLE users(
    user_id     INT PRIMARY KEY NOT NULL,
    name        TEXT NOT NULL,
    email       TEXT NOT NULL
);

CREATE TABLE map_cache(
    place_id        TEXT PRIMARY KEY NOT NULL,
    lat             REAL NOT NULL,
    lng             REAL NOT NULL,
    city            TEXT NOT NULL,
    region          TEXT NOT NULL,
    country_name    TEXT NOT NULL,
    country_code    TEXT NOT NULL
);

CREATE TABLE location_history(
    user_id     INT NOT NULL,
    place_id    TEXT NOT NULL,
    timestamp   REAL NOT NULL,
    FOREIGN KEY(user_id) REFERENCES users(user_id),
    FOREIGN KEY(place_id) REFERENCES map_cache(place_id),
    PRIMARY KEY (user_id, place_id)
);


DROP TABLE IF EXISTS log_activity_comment;

CREATE TABLE `log_activity_comment` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    `activity_log_id` integer NOT NULL,
    `comments` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
;

ALTER TABLE `log_activity_comment` ADD CONSTRAINT `activity_log_id_refs_id_4f8d99d4` FOREIGN KEY (`activity_log_id`) REFERENCES `log_activity` (`id`);
CREATE INDEX `log_activity_comment_3bf68f54` ON `log_activity_comment` (`activity_log_id`);

Fix the log activity comments migration.

DROP TABLE IF EXISTS log_activity_comment;

CREATE TABLE `log_activity_comment` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    `activity_log_id` int(11) NOT NULL,
    `comments` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
;

ALTER TABLE `log_activity_comment`
      ADD CONSTRAINT FOREIGN KEY (`activity_log_id`) REFERENCES `log_activity` (`id`);


INSERT INTO
  followers_users_users
    (follower, target)
VALUES
  (${currentUser}, ${target})
ON CONFLICT
  (follower, target)
DO NOTHING
RETURNING *;

Add sql query for adding user follows

INSERT INTO
  followers_users_users
    (follower, target)
VALUES
  (${userID}, ${targetID})
  -- (${currentUser}, ${target})
ON CONFLICT
  (follower, target)
DO NOTHING
RETURNING *;


CREATE DATABASE uncovery;

USE uncovery;

Create mark and message tables in database

CREATE DATABASE uncovery;

USE uncovery;

CREATE TABLE mark (
  mark_id int(5) AUTO_INCREMENT,
  loc_x float(10, 6),
  loc_y float(10, 6),
  loc_z float(10, 6),
  timestamp timestamp,
  UNIQUE (
    mark_id
  )
);

CREATE TABLE message (
  message_id int(5) AUTO_INCREMENT,
  message_string text,
  UNIQUE (
    message_id
  )
);


-- create the trigger

-- on insertion
DROP TRIGGER IF EXISTS extend_sentences_lists_add ;
delimiter |

CREATE TRIGGER extend_sentences_lists_add AFTER INSERT ON `sentences_sentences_lists`
FOR EACH ROW

	UPDATE `sentences_lists` AS sl
      SET sl.modified = NOW()
      WHERE sl.id = NEW.sentences_list_id
      LIMIT 1;

-- delete
DROP TRIGGER IF EXISTS extend_sentences_lists_delete |
CREATE TRIGGER extend_sentences_lists_delete AFTER DELETE ON `sentences_sentences_lists`
FOR EACH ROW
	UPDATE `sentences_lists` AS sl
      SET sl.modified = NOW()
      WHERE sl.id = OLD.sentences_list_id
      LIMIT 1;
|

delimiter ;

Work around trigger import/export bug

-- create the trigger

-- on insertion
DROP TRIGGER IF EXISTS extend_sentences_lists_add ;
delimiter |

CREATE TRIGGER extend_sentences_lists_add AFTER INSERT ON `sentences_sentences_lists`
FOR EACH ROW BEGIN
      UPDATE `sentences_lists` AS sl
      SET sl.modified = NOW()
      WHERE sl.id = NEW.sentences_list_id
      LIMIT 1;
END;

-- delete
DROP TRIGGER IF EXISTS extend_sentences_lists_delete |
CREATE TRIGGER extend_sentences_lists_delete AFTER DELETE ON `sentences_sentences_lists`
FOR EACH ROW BEGIN
      UPDATE `sentences_lists` AS sl
      SET sl.modified = NOW()
      WHERE sl.id = OLD.sentences_list_id
      LIMIT 1;
END;

|

delimiter ;


CREATE TABLE IF NOT EXISTS image_collections (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  deleted_at timestamptz DEFAULT NULL,

  version bigint NOT NULL DEFAULT 0,

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

  deleted boolean NOT NULL DEFAULT false
) WITH (OIDS=FALSE);

CREATE TRIGGER image_collections_occ_tgr BEFORE UPDATE ON image_collections
  FOR EACH ROW EXECUTE PROCEDURE handle_occ();

CREATE TRIGGER image_collections_modified_at_tgr BEFORE UPDATE ON image_collections
  FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

CREATE TABLE IF NOT EXISTS images (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  deleted_at timestamptz DEFAULT NULL,

  version bigint NOT NULL DEFAULT 0,
  "user" uuid NOT NULL REFERENCES users(id),

  collection uuid REFERENCES image_collections(id)
                  ON DELETE SET NULL
                  DEFAULT NULL,

  name text NOT NULL,
  path text NOT NULL
) WITH (OIDS=FALSE);

CREATE INDEX ON images (collection);

CREATE TRIGGER images_occ_tgr BEFORE UPDATE ON images
  FOR EACH ROW EXECUTE PROCEDURE handle_occ();

CREATE TRIGGER images_modified_at_tgr BEFORE UPDATE ON images
  FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

Add missing deleted field on images table.

CREATE TABLE IF NOT EXISTS image_collections (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  deleted_at timestamptz DEFAULT NULL,

  version bigint NOT NULL DEFAULT 0,

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

  deleted boolean NOT NULL DEFAULT false
) WITH (OIDS=FALSE);

CREATE TRIGGER image_collections_occ_tgr BEFORE UPDATE ON image_collections
  FOR EACH ROW EXECUTE PROCEDURE handle_occ();

CREATE TRIGGER image_collections_modified_at_tgr BEFORE UPDATE ON image_collections
  FOR EACH ROW EXECUTE PROCEDURE update_modified_at();

CREATE TABLE IF NOT EXISTS images (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
  deleted_at timestamptz DEFAULT NULL,

  version bigint NOT NULL DEFAULT 0,
  "user" uuid NOT NULL REFERENCES users(id),

  collection uuid REFERENCES image_collections(id)
                  ON DELETE SET NULL
                  DEFAULT NULL,

  name text NOT NULL,
  path text NOT NULL,

  deleted boolean NOT NULL DEFAULT false
) WITH (OIDS=FALSE);

CREATE INDEX ON images (collection);

CREATE TRIGGER images_occ_tgr BEFORE UPDATE ON images
  FOR EACH ROW EXECUTE PROCEDURE handle_occ();

CREATE TRIGGER images_modified_at_tgr BEFORE UPDATE ON images
  FOR EACH ROW EXECUTE PROCEDURE update_modified_at();


IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = N'users' AND type = 'U')
BEGIN
CREATE TABLE dbo.users
   (id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
    username varchar(45) NULL,
    email varchar(150) NULL,
    password varchar(45) NULL)
END;

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = N'roles' AND type = 'U')
BEGIN
CREATE TABLE dbo.roles
   (id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
    name varchar(100) NULL)
END;

IF EXISTS (SELECT table_name FROM information_schema.views WHERE table_name = 'email_view')
   DROP VIEW dbo.email_view
GO

CREATE VIEW dbo.email_view AS
SELECT dbo.users.email, dbo.users.password
FROM dbo.users;
GO

IF OBJECT_ID('dbo.users_count', 'P') IS NOT NULL
  DROP PROCEDURE dbo.users_count
GO

CREATE PROCEDURE dbo.users_count
(
  @Count int OUTPUT
)
AS
  BEGIN
    SELECT @Count = COUNT(*) FROM dbo.users
  END
GO

DROP TRIGGER IF EXISTS dbo.dummy_trigger;
CREATE TRIGGER dbo.dummy_trigger
ON dbo.users
AFTER INSERT
AS
  BEGIN
  END

Fix SQL Server trigger creation error

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = N'users' AND type = 'U')
BEGIN
CREATE TABLE dbo.users
   (id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
    username varchar(45) NULL,
    email varchar(150) NULL,
    password varchar(45) NULL)
END;

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = N'roles' AND type = 'U')
BEGIN
CREATE TABLE dbo.roles
   (id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
    name varchar(100) NULL)
END;

IF EXISTS (SELECT table_name FROM information_schema.views WHERE table_name = 'email_view')
   DROP VIEW dbo.email_view
GO

CREATE VIEW dbo.email_view AS
SELECT dbo.users.email, dbo.users.password
FROM dbo.users;
GO

IF OBJECT_ID('dbo.users_count', 'P') IS NOT NULL
  DROP PROCEDURE dbo.users_count
GO

CREATE PROCEDURE dbo.users_count
(
  @Count int OUTPUT
)
AS
  BEGIN
    SELECT @Count = COUNT(*) FROM dbo.users
  END
GO

IF EXISTS (SELECT * FROM sys.objects WHERE [type] = 'TR' AND [name] = 'dummy_trigger')
    DROP TRIGGER dbo.dummy_trigger;
GO

CREATE TRIGGER dbo.dummy_trigger
ON users
AFTER INSERT
AS
  BEGIN
  END


alter table mob add owner varchar(80);
alter table mob_imm add owner varchar(80);
alter table mob_extra add owner varchar(80);
alter table mobresponses add owner varchar(80);

alter table obj add owner varchar(80);
alter table objaffect add owner varchar(80);
alter table objextra add owner varchar(80);

alter table room add owner varchar(80);
alter table room add block int;
alter table roomexit add owner varchar(80);
alter table roomexit add block int;
alter table roomextra add owner varchar(80);
alter table roomextra add block int;

Add primary key changes to 0001 db migration

ALTER TABLE `mob`
    ADD `owner` char(32) default NULL,
    DROP PRIMARY KEY,
    ADD PRIMARY KEY  (`owner`,`vnum`);

ALTER TABLE `mob_extra`
    ADD `owner` varchar(32) default NULL,
    DROP PRIMARY KEY,
    ADD PRIMARY KEY  (`owner`,`vnum`,`keyword`);

ALTER TABLE `mob_imm`
    ADD `owner` varchar(32) default NULL,
    DROP PRIMARY KEY,
    ADD PRIMARY KEY  (`owner`,`vnum`,`type`);

ALTER TABLE `mobresponses`
    ADD `owner` varchar(32) default NULL;

ALTER TABLE `obj`
    ADD `owner` varchar(32) default NULL,
    DROP PRIMARY KEY,
    ADD PRIMARY KEY  (`owner`,`vnum`);

ALTER TABLE `objaffect`
    ADD `owner` varchar(32) default NULL;

ALTER TABLE `objextra`
    ADD `owner` varchar(32) default NULL;

ALTER TABLE `room`
    ADD `owner` varchar(32) default NULL,
    ADD `block` int(11) default NULL,
    DROP PRIMARY KEY,
    ADD PRIMARY KEY  (`owner`,`vnum`);

ALTER TABLE `roomexit`
    ADD `owner` varchar(32) default NULL,
    ADD `block` int(11) default NULL;

ALTER TABLE `roomextra`
    ADD `owner` varchar(32) default NULL,
    ADD `block` int(11) default NULL;


/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

CREATE TABLE IF NOT EXISTS users(
  id SERIAL PRIMARY KEY NOT NULL,
  email VARCHAR(254) UNIQUE,
  password_hash CHAR(60) NOT NULL
)

Add role to Users table

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

CREATE TABLE IF NOT EXISTS users(
  id SERIAL PRIMARY KEY NOT NULL,
  email VARCHAR(254) UNIQUE,
  password_hash CHAR(60) NOT NULL,
  role integer NOT NULL
)


## start naming files to reference the release version and git tag at which point the code will require the changes in this file

update CHROMOSOME_FEATURE set class = "GeneProduct" where class = "GeneProductImpl";

Drop unused columns from the taxon table

## start naming files to reference the release version and git tag at which point the code will require the changes in this file

update CHROMOSOME_FEATURE set class = "GeneProduct" where class = "GeneProductImpl";
alter table TAXON drop COLUMN `UNIGENE_PREFIX`, drop COLUMN `SWISS_PROT_SUFFIX`;


ALTER TABLE addays MODIFY COLUMN Day enum('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') NOT NULL;

Fix update to work with mysqldump restore data

ALTER TABLE addays MODIFY COLUMN Day enum('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') NOT NULL;||
UPDATE addays SET Day = 'Sun' WHERE Day = 'Sunday';||
UPDATE addays SET Day = 'Mon' WHERE Day = 'Monday';||
UPDATE addays SET Day = 'Tue' WHERE Day = 'Tuesday';||
UPDATE addays SET Day = 'Wed' WHERE Day = 'Wednesday';||
UPDATE addays SET Day = 'Thu' WHERE Day = 'Thursday';||
UPDATE addays SET Day = 'Fri' WHERE Day = 'Friday';||
UPDATE addays SET Day = 'Sat' WHERE Day = 'Saturday';||
UPDATE addays SET Day = 'Sun' WHERE Day = 'Sunday';||
ALTER TABLE addays MODIFY COLUMN Day enum('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') NOT NULL;


CREATE DATABASE `powerauth`;

CREATE USER 'powerauth'@'localhost';

GRANT ALL PRIVILEGES ON powerauth.* TO 'powerauth'@'localhost';

FLUSH PRIVILEGES;

Fix create schema sql script

CREATE DATABASE `powerauth`;

CREATE USER 'powerauth'@'%';

GRANT ALL PRIVILEGES ON powerauth.* TO 'powerauth'@'%';

FLUSH PRIVILEGES;

USE powerauth;


CREATE OR REPLACE FUNCTION create_ordering_for_link_table(link_table REGCLASS)
  RETURNS TEXT AS $$
BEGIN
  EXECUTE 'ALTER TABLE ' || link_table || ' ADD COLUMN ordering_1 SERIAL, ADD COLUMN ordering_2 SERIAL';
  EXECUTE 'UPDATE ' || link_table || ' SET ordering_1 = id_1, ordering_2 = id_2';

  RETURN link_table :: TEXT;
END
$$ LANGUAGE plpgsql;

SELECT create_ordering_for_link_table(table_name :: REGCLASS)
FROM information_schema.tables
WHERE table_schema = 'public' AND table_name LIKE 'link_table_%'
ORDER BY table_name;

DROP FUNCTION create_ordering_for_link_table( REGCLASS );

CREATE TABLE system_table_lang (
  table_id    BIGINT      NOT NULL,
  langtag     VARCHAR(50) NOT NULL,
  name        VARCHAR(255),
  description TEXT,

  PRIMARY KEY (table_id, langtag),
  FOREIGN KEY (table_id) REFERENCES system_table (table_id) ON DELETE CASCADE
);

INSERT INTO system_table_lang (table_id, langtag, name, description)
  SELECT table_id, (SELECT value::json->0 from system_settings where key = 'langtags'), user_table_name, null FROM system_table;

Fix langtag on update without extra double-quotes

CREATE OR REPLACE FUNCTION create_ordering_for_link_table(link_table REGCLASS)
  RETURNS TEXT AS $$
BEGIN
  EXECUTE 'ALTER TABLE ' || link_table || ' ADD COLUMN ordering_1 SERIAL, ADD COLUMN ordering_2 SERIAL';
  EXECUTE 'UPDATE ' || link_table || ' SET ordering_1 = id_1, ordering_2 = id_2';

  RETURN link_table :: TEXT;
END
$$ LANGUAGE plpgsql;

SELECT create_ordering_for_link_table(table_name :: REGCLASS)
FROM information_schema.tables
WHERE table_schema = 'public' AND table_name LIKE 'link_table_%'
ORDER BY table_name;

DROP FUNCTION create_ordering_for_link_table( REGCLASS );

CREATE TABLE system_table_lang (
  table_id    BIGINT      NOT NULL,
  langtag     VARCHAR(50) NOT NULL,
  name        VARCHAR(255),
  description TEXT,

  PRIMARY KEY (table_id, langtag),
  FOREIGN KEY (table_id) REFERENCES system_table (table_id) ON DELETE CASCADE
);

INSERT INTO system_table_lang (table_id, langtag, name, description)
  SELECT table_id, (SELECT value::json->>0 from system_settings where key = 'langtags'), user_table_name, null FROM system_table;


DROP TABLE IF EXISTS `addon_recommendations`;
DELETE FROM collections WHERE collection_type IN (1, 3); -- COLLECTION_SYNCHRONIZED or COLLECTION_RECOMMENDED
DELETE FROM waffle_flag WHERE name = 'disco-pane-show-recs';

Fix migration 854 (some collections had subscribers...)

DROP TABLE IF EXISTS `addon_recommendations`;
DELETE collection_subscriptions FROM collection_subscriptions
    INNER JOIN collections
    ON collection_subscriptions.collection_id=collections.id
    WHERE collections.collection_type IN (1, 3);
DELETE FROM collections WHERE collection_type IN (1, 3); -- COLLECTION_SYNCHRONIZED or COLLECTION_RECOMMENDED
DELETE FROM waffle_flag WHERE name = 'disco-pane-show-recs';


--
-- Add demo user
--

CREATE USER 'demo'@'localhost' IDENTIFIED BY 'welcome1';
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES, EXECUTE ON *.* TO 'demo'@'localhost';
CREATE USER 'demo'@'%' IDENTIFIED BY 'welcome1';
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES, EXECUTE ON *.* TO 'demo'@'%';

--
-- Add APM user
--

CREATE USER 'newrelic'@'localhost' IDENTIFIED BY 'welcome1';
GRANT REPLICATION CLIENT ON *.* TO 'newrelic'@'localhost' WITH MAX_USER_CONNECTIONS 5;

Update MySQL init to allow APM user to be on any IP

--
-- Add demo user
--

CREATE USER 'demo'@'%' IDENTIFIED BY 'welcome1';
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES, EXECUTE ON *.* TO 'demo'@'%';

--
-- Add APM user
--

CREATE USER 'newrelic'@'%' IDENTIFIED BY 'welcome1';
GRANT REPLICATION CLIENT ON *.* TO 'newrelic'@'%' WITH MAX_USER_CONNECTIONS 5;


-- Update any old file upload entries which have errors to
-- include a skeleton system error validator report.
UPDATE
    `file_uploads`
SET
    `validation` = '{"errors": 1, "success": false, "messages": [{"description": ["Validation was unable to complete successfully due to an unexpected error.", "The error has been logged, but please consider filing an issue report here: http://mzl.la/1DG0sFd"], "type": "error", "id": ["validator", "unexpected_exception"], "tier": 1, "for_appversions": null, "message": "An unexpected error has occurred.", "uid": "35432f419340461897aa8362398339c4"}], "metadata": {}}',
    `valid` = FALSE
WHERE
    `validation` IS NULL AND `task_error` IS NOT NULL
;

ALTER TABLE
    `file_uploads`
DROP COLUMN
    `task_error` BOOL DEFAULT FALSE
;

Fix syntax error in migration 820.

-- Update any old file upload entries which have errors to
-- include a skeleton system error validator report.
UPDATE
    `file_uploads`
SET
    `validation` = '{"errors": 1, "success": false, "messages": [{"description": ["Validation was unable to complete successfully due to an unexpected error.", "The error has been logged, but please consider filing an issue report here: http://mzl.la/1DG0sFd"], "type": "error", "id": ["validator", "unexpected_exception"], "tier": 1, "for_appversions": null, "message": "An unexpected error has occurred.", "uid": "35432f419340461897aa8362398339c4"}], "metadata": {}}',
    `valid` = FALSE
WHERE
    `validation` IS NULL AND `task_error` IS NOT NULL
;

ALTER TABLE
    `file_uploads`
DROP COLUMN
    `task_error`
;


WITH lastset AS (
SELECT max(set) as lastset from tests
)

SELECT
  set,script,scale,test,clients,workers,
  rate_limit AS limit,
  client_limit AS client_limit,
  round(tps) AS tps,
  round(1000*avg_latency)/1000 AS avg_latency,
  round(1000*percentile_90_latency)/1000 AS "90%<",
  1000*round(max_latency)/1000 AS max_latency,
  trans
FROM tests
WHERE set = (SELECT lastset FROM lastset)
ORDER BY set,script,scale,test,clients,rate_limit;

Add multi in place of workers in latest set report.

WITH lastset AS (
  SELECT max(set) as lastset from tests
)
SELECT
  set,script,scale,test,clients,multi,
  rate_limit AS limit,
  client_limit AS client_limit,
  round(tps) AS tps,
  round(1000*avg_latency)/1000 AS avg_latency,
  round(1000*percentile_90_latency)/1000 AS "90%<",
  1000*round(max_latency)/1000 AS max_latency,
  trans
FROM tests
WHERE set = (SELECT lastset FROM lastset)
ORDER BY set,script,scale,clients,rate_limit,multi,test;


SELECT * FROM users WHERE
(NOT EXISTS (SELECT * FROM challenges WHERE userNamenumber = $1 AND performanceid = $2)) AND
(SELECT voluntary FROM manage AS m WHERE m.usernamenumber = $1 AND m.performanceid = $2 ORDER BY id DESC LIMIT 1) OR
(
  SELECT substring($3, 2)::integer > 12 AND
  (
    NOT EXISTS (SELECT * FROM manage AS m WHERE m.usernamenumber = $1 AND m.performanceid = $2 ORDER BY id DESC LIMIT 1) OR
    (SELECT reason = 'Closed Spot' FROM manage AS m WHERE m.usernamenumber = $1 AND m.performanceid = $2 ORDER BY id DESC LIMIT 1)
  )
);

Fix can challenge for performance query

SELECT * FROM users WHERE
NOT EXISTS (SELECT * FROM challenges WHERE userNamenumber = $1 AND performanceid = $2) AND
((SELECT COALESCE(voluntary, true) FROM manage AS m WHERE m.usernamenumber = $1 AND m.performanceid = $2 ORDER BY id DESC LIMIT 1) OR
(
  SELECT substring($3, 2)::integer > 12 AND
  (
    NOT EXISTS (SELECT * FROM manage AS m WHERE m.usernamenumber = $1 AND m.performanceid = $2 ORDER BY id DESC LIMIT 1) OR
    (SELECT reason = 'Closed Spot' FROM manage AS m WHERE m.usernamenumber = $1 AND m.performanceid = $2 ORDER BY id DESC LIMIT 1)
  )
) AND nameNumber = $1);


CREATE TABLE "public"."media" (
    "id" serial NOT NULL,
    "iid" text NOT NULL,
    "document" jsonb NOT NULL,
    "created_at" timestamp NOT NULL,
    PRIMARY KEY ("id")
);

CREATE TABLE "public"."subscriptions" (
    "id" serial NOT NULL,
    "name" text NOT NULL,
    "lat" real NOT NULL,
    "long" real NOT NULL,
    "radius" int NOT NULL,
    PRIMARY KEY ("id")
);

ALTER TABLE "public"."media"
  ADD COLUMN "subscription_id" int NOT NULL,
  ADD FOREIGN KEY ("subscription_id") REFERENCES "public"."subscriptions"("id");

Add the script to create db with proper encoding

#CREATE DATABASE spoto OWNER spoto  ENCODING 'UTF-8' template template0;

CREATE TABLE "public"."media" (
    "id" serial NOT NULL,
    "iid" text NOT NULL,
    "document" jsonb NOT NULL,
    "created_at" timestamp NOT NULL,
    PRIMARY KEY ("id")
);

CREATE TABLE "public"."subscriptions" (
    "id" serial NOT NULL,
    "name" text NOT NULL,
    "lat" real NOT NULL,
    "long" real NOT NULL,
    "radius" int NOT NULL,
    PRIMARY KEY ("id")
);

ALTER TABLE "public"."media"
  ADD COLUMN "subscription_id" int NOT NULL,
  ADD FOREIGN KEY ("subscription_id") REFERENCES "public"."subscriptions"("id");


-- Deploy travis-logs:log_parts_created_at_not_null to pg
-- requires: structure

BEGIN;

  SET client_min_messages = WARNING;

  ALTER TABLE log_parts
  ALTER COLUMN created_at
  SET DEFAULT '2000-01-01'::timestamptz;

  ALTER TABLE log_parts
  ALTER COLUMN created_at
  SET NOT NULL;

COMMIT;

Switch default log_parts.created_at data type

-- Deploy travis-logs:log_parts_created_at_not_null to pg
-- requires: structure

BEGIN;

  SET client_min_messages = WARNING;

  ALTER TABLE log_parts
  ALTER COLUMN created_at
  SET DEFAULT '2000-01-01'::timestamp;

  ALTER TABLE log_parts
  ALTER COLUMN created_at
  SET NOT NULL;

COMMIT;


--drop proc sspBatchMPU  -- Monthly Paying User
create proc sspBatchMPU
as
set nocount on
declare @Day30DT datetimeoffset(7)
declare @CurrentDT datetimeoffset(7)
declare @nowdt datetime
declare @MPU bigint

set @nowdt = (select getutcdate())
set @CurrentDT = ((SELECT DATETIMEFROMPARTS (DATEPART(year, @nowdt), DATEPART(month,@nowdt), DATEPART(day, @nowdt), DATEPART(hour, @nowdt), 0, 0, 0 )))
set @Day30DT = (dateadd(day, -30, @CurrentDT))

set @MPU = (select count(*) from MemberItemPurchases where PurchaseDT between @Day30DT and @CurrentDT AND PurchaseCancelYN like 'N')
insert into StatsData(CategoryName, CountNum, Fields, Groups) values('MPU', @MPU, CONVERT(nvarchar(8), GETUTCDATE(), 112), '')
GO

Test the simple MPU query

--drop proc sspBatchMPU  -- Monthly Paying User
create proc sspBatchMPU
as
set nocount on
declare @Day30DT datetimeoffset(7)
declare @CurrentDT datetimeoffset(7)
declare @nowdt datetime
declare @MPU bigint

set @nowdt = (select getutcdate())
set @CurrentDT = ((SELECT DATETIMEFROMPARTS (DATEPART(year, @nowdt), DATEPART(month,@nowdt), DATEPART(day, @nowdt), DATEPART(hour, @nowdt), 0, 0, 0 )))
set @Day30DT = (dateadd(day, -30, @CurrentDT))

set @MPU = (select count(*) from MemberItemPurchases where PurchaseDT between @Day30DT and @CurrentDT AND PurchaseCancelYN like 'N')
insert into StatsData(CategoryName, CountNum, Fields, Groups) values('MPU', @MPU, CONVERT(nvarchar(8), GETUTCDATE(), 112), '')
GO

------------------------------------------------------------------
-- run test
--exec sspBatchMPU
------------------------------------------------------------------

/*
select * from StatsData order by createdat desc
select * from Members
select * from MemberItemPurchases
select count(*) from MemberItemPurchases where PurchaseDT between '2016-05-15 15:00:03.1749825 +00:00' and sysutcdatetime()

-- test data value
update Members set LastLoginDT = sysutcdatetime() where memberid like 'bbb'
update Members set LastLoginDT = sysutcdatetime() where memberid like 'ccc'
update Members set LastLoginDT = sysutcdatetime() where memberid like 'ddd'

select sysutcdatetime()
select dateadd(day, -30, sysutcdatetime())
select CONVERT(nvarchar(20), getutcdate(), 112)

declare @nowdt datetime
set @nowdt = (select getutcdate())
SELECT DATEPART(year, @nowdt) + '-' + DATEPART(month,@nowdt) + '-' +  DATEPART(day, @nowdt);
SELECT convert(datetime, getutcdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm
*/


/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.
 Use SQLCMD syntax to include a file in the post-deployment script.
 Example:      :r .\myfile.sql
 Use SQLCMD syntax to reference a variable in the post-deployment script.
 Example:      :setvar TableName MyTable
               SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/

Add the post deployment script

/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.
 Use SQLCMD syntax to include a file in the post-deployment script.
 Example:      :r .\myfile.sql
 Use SQLCMD syntax to reference a variable in the post-deployment script.
 Example:      :setvar TableName MyTable
               SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/

INSERT INTO Monster.Gender(GenderName, Active)
VALUES (N'Female', 1), (N'Male', 1), (N'Alien', 1);
go

INSERT INTO Monster.MonsterType(TypeName, Active)
VALUES (N'Vampire', 1), (N'Werewolf', 1), (N'Zombie', 1);
go

INSERT INTO Monster.Title(TitleName, Active)
VALUES (N'Sire', 1), (N'Lady', 1), (N'Count', 1);
go


DROP TABLE IF EXISTS wf_access_contexts;

--

CREATE TABLE wf_access_contexts (
    id INT NOT NULL AUTO_INCREMENT,
    ns_id INT NOT NULL,
    group_id INT NOT NULL,
    role_id INT NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (group_id) REFERENCES wf_groups_master(id),
    FOREIGN KEY (role_id) REFERENCES wf_roles_master(id)
);

Update SQL DDL to match renamed table

DROP TABLE IF EXISTS wf_ac_group_roles;

--

CREATE TABLE wf_ac_group_roles (
    id INT NOT NULL AUTO_INCREMENT,
    ns_id INT NOT NULL,
    group_id INT NOT NULL,
    role_id INT NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (group_id) REFERENCES wf_groups_master(id),
    FOREIGN KEY (role_id) REFERENCES wf_roles_master(id)
);


--
-- PostgreSQL database dump
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';

CREATE FUNCTION update_updated_at_column() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
  BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
  END;
$$;

CREATE TABLE deployment_tracker (
    id integer NOT NULL,
    dbname text,
    deployment_type text NOT NULL,
    deployment_name text,
    deployment_outcome text,
    created_at timestamp without time zone DEFAULT now(),
    updated_at timestamp without time zone DEFAULT now(),
    is_active boolean DEFAULT true,
    deployed_by character varying(32),
    deployed_as character varying(32),
    reference_url text
);

CREATE SEQUENCE deployment_tracker_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE deployment_tracker_id_seq OWNED BY deployment_tracker.id;

CREATE INDEX index_deployment_on_deployment_name ON deployment_tracker USING btree (deployment_name);

CREATE TRIGGER update_updated_at_modtime BEFORE UPDATE ON deployment_tracker FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

ALTER TABLE ONLY deployment_tracker ALTER COLUMN id SET DEFAULT nextval('deployment_tracker_id_seq'::regclass);
--
-- PostgreSQL database dump complete
--

Adjust a comment line that prevents cloud from deploying

--
-- PostgreSQL database dump
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

CREATE FUNCTION update_updated_at_column() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
  BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
  END;
$$;

CREATE TABLE deployment_tracker (
    id integer NOT NULL,
    dbname text,
    deployment_type text NOT NULL,
    deployment_name text,
    deployment_outcome text,
    created_at timestamp without time zone DEFAULT now(),
    updated_at timestamp without time zone DEFAULT now(),
    is_active boolean DEFAULT true,
    deployed_by character varying(32),
    deployed_as character varying(32),
    reference_url text
);

CREATE SEQUENCE deployment_tracker_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE deployment_tracker_id_seq OWNED BY deployment_tracker.id;

CREATE INDEX index_deployment_on_deployment_name ON deployment_tracker USING btree (deployment_name);

CREATE TRIGGER update_updated_at_modtime BEFORE UPDATE ON deployment_tracker FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

ALTER TABLE ONLY deployment_tracker ALTER COLUMN id SET DEFAULT nextval('deployment_tracker_id_seq'::regclass);
--
-- PostgreSQL database dump complete
--


SELECT
     ue.id_untersuchungseinheit AS t_id,
     'Point(' || ue.koordinate_x::text || ' ' || ue.koordinate_y::text || ')' AS geometrie,
     k.text AS name_untersuchungseinheit,
     COALESCE(replace(ue.konsultations_abstand,'m','')::integer,-99) AS konsultations_abstand,
     ue.parzellen_nummer,
     k.aktiv::boolean,
     ue.id_betrieb
  FROM afu_online_risk.untersuchungseinheit ue
    LEFT JOIN afu_online_risk.konstruktion k ON ue.id_untersuchungseinheit = k.id_konstruktion;

Fix 2 für Konsultationsabstand in Untersuchungseinheit

SELECT
     ue.id_untersuchungseinheit AS t_id,
     'Point(' || ue.koordinate_x::text || ' ' || ue.koordinate_y::text || ')' AS geometrie,
     k.text AS name_untersuchungseinheit,
     COALESCE(replace(ue.konsultations_abstand,'m','')::integer,0) AS konsultations_abstand,
     ue.parzellen_nummer,
     k.aktiv::boolean,
     ue.id_betrieb
  FROM afu_online_risk.untersuchungseinheit ue
    LEFT JOIN afu_online_risk.konstruktion k ON ue.id_untersuchungseinheit = k.id_konstruktion;


CREATE TABLE users(
   id             SERIAL PRIMARY KEY,
   username       TEXT    NOT NULL,
   email          TEXT    NOT NULL,
   password       TEXT    NOT NULL
);

CREATE TABLE roles(
   id             SERIAL PRIMARY KEY,
   name           TEXT    NOT NULL
);

CREATE OR REPLACE VIEW email_view AS
  SELECT users.email, users.password
  FROM users;

CREATE OR REPLACE FUNCTION user_count()
RETURNS bigint AS $$
  SELECT COUNT(*) FROM users AS total;
$$ LANGUAGE SQL;

DROP TRIGGER IF EXISTS dummy_trigger ON users;
CREATE OR REPLACE FUNCTION test_trigger_func() RETURNS TRIGGER AS $$
    BEGIN
      RETURN NULL;
    END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER dummy_trigger
    AFTER INSERT ON users
    FOR EACH ROW
    EXECUTE PROCEDURE test_trigger_func();

Fix typo so that function has the same name in all clients

CREATE TABLE users(
   id             SERIAL PRIMARY KEY,
   username       TEXT    NOT NULL,
   email          TEXT    NOT NULL,
   password       TEXT    NOT NULL
);

CREATE TABLE roles(
   id             SERIAL PRIMARY KEY,
   name           TEXT    NOT NULL
);

CREATE OR REPLACE VIEW email_view AS
  SELECT users.email, users.password
  FROM users;

CREATE OR REPLACE FUNCTION users_count()
RETURNS bigint AS $$
  SELECT COUNT(*) FROM users AS total;
$$ LANGUAGE SQL;

DROP TRIGGER IF EXISTS dummy_trigger ON users;
CREATE OR REPLACE FUNCTION test_trigger_func() RETURNS TRIGGER AS $$
    BEGIN
      RETURN NULL;
    END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER dummy_trigger
    AFTER INSERT ON users
    FOR EACH ROW
    EXECUTE PROCEDURE test_trigger_func();


-- create a new, hive-specific table for meta data and start using it for hive_sql_schema_version tracking:

CREATE TABLE hive_meta (
    meta_key                VARCHAR(80) NOT NULL PRIMARY KEY,
    meta_value              TEXT

) COLLATE=latin1_swedish_ci ENGINE=MyISAM;

    -- INSERT the 'hive_sql_schema_version' for the first time, then keep UPDATE'ing
INSERT INTO hive_meta (meta_key, meta_value) VALUES ('hive_sql_schema_version', '49');

    -- move 'hive_use_triggers' into hive_meta:
INSERT IGNORE INTO hive_meta (meta_key, meta_value) SELECT 'hive_use_triggers', COALESCE(MIN(REPLACE(meta_value, '"', '')), 0) FROM meta WHERE meta_key='hive_use_triggers';

    -- move 'pipeline_name' into hive_meta and rename it into 'hive_pipeline_name':
INSERT IGNORE INTO hive_meta (meta_key, meta_value) SELECT 'hive_pipeline_name', REPLACE(meta_value,'"','') FROM meta WHERE meta_key='pipeline_name' or meta_key='name';

Make this patch less mysql-dependent. Needs testing with PostgreSQL.

-- create a new, hive-specific table for meta data and start using it for hive_sql_schema_version tracking:

CREATE TABLE hive_meta (
    meta_key                VARCHAR(80) NOT NULL PRIMARY KEY,
    meta_value              TEXT

);

    -- INSERT the 'hive_sql_schema_version' for the first time, then keep UPDATE'ing
INSERT INTO hive_meta (meta_key, meta_value) VALUES ('hive_sql_schema_version', '49');

    -- move 'hive_use_triggers' into hive_meta:
INSERT INTO hive_meta (meta_key, meta_value) SELECT 'hive_use_triggers', COALESCE(MIN(REPLACE(meta_value, '"', '')), 0) FROM meta WHERE meta_key='hive_use_triggers';

    -- move 'pipeline_name' into hive_meta and rename it into 'hive_pipeline_name':
INSERT INTO hive_meta (meta_key, meta_value) SELECT 'hive_pipeline_name', REPLACE(meta_value,'"','') FROM meta WHERE meta_key='pipeline_name' or meta_key='name';


-- psql --username=testuser test

CREATE TABLE artist (
    name text NOT NULL,
    year int NOT NULL
);

Add a new test table

-- psql --username=testuser test

CREATE TABLE artist (
    name text NOT NULL,
    year int NOT NULL
);

CREATE table types (timestamp_no_tz timestamp without time zone);


INSERT INTO
  albums (title, artist)
VALUES
  ('Malibu', 'Anderson .Paak'),
  ('A Seat at the Table', 'Solange Knowles'),
  ('Melodrama', 'Lorde'),
  ('In Rainbows', 'Radiohead')
;

INSERT INTO
  users (name, email, password)
VALUES
  ('Arya Stark', 'NoOne@ravens.com', 'needle'),
  ('Jon Snow', 'KingOfTheNorth@ravens.com', 'ghost'),
  ('Daenery', 'Khaleesi@ravens.com', 'dragons')
  ;

  INSERT INTO
  reviews (content, user_id, album_id)
VALUES
  ('I liked it, good album.', 1, 1),
  ('It was ok', 1, 2),
  ('Reminds me of my youth.', 1, 3),
  ('It was terrible. ''nuff said.', 2, 1)
  ;

Fix typo in seed data

INSERT INTO
  albums (title, artist)
VALUES
  ('Malibu', 'Anderson .Paak'),
  ('A Seat at the Table', 'Solange Knowles'),
  ('Melodrama', 'Lorde'),
  ('In Rainbows', 'Radiohead')
;

INSERT INTO
  users (name, email, password)
VALUES
  ('Arya Stark', 'NoOne@ravens.com', 'needle'),
  ('Jon Snow', 'KingOfTheNorth@ravens.com', 'ghost'),
  ('Daenerys Targaryen', 'Khaleesi@ravens.com', 'dragons')
  ;

  INSERT INTO
  reviews (content, user_id, album_id)
VALUES
  ('I liked it, good album.', 1, 1),
  ('It was ok', 1, 2),
  ('Reminds me of my youth.', 1, 3),
  ('It was terrible. ''nuff said.', 2, 1)
  ;


CREATE TABLE CWF_DOMAIN (
	ID VARCHAR(20) PRIMARY KEY,
	NAME VARCHAR(255),
	ATTRIBUTES VARCHAR(255) DEFAULT '');

INSERT INTO CWF_DOMAIN (ID, NAME, ATTRIBUTES) VALUES
	('1', 'General Medicine Clinic', ''),
	('2','Emergency Room', ''),
	('3', 'Test Hospital', '');

CREATE TABLE CWF_USER (
	ID VARCHAR(20) PRIMARY KEY,
	USERNAME VARCHAR(40),
	PASSWORD VARCHAR(40),
	NAME VARCHAR(255),
	DOMAIN_ID VARCHAR(20),
	AUTHORITIES VARCHAR(255) DEFAULT '');

INSERT INTO CWF_USER (ID, USERNAME, PASSWORD, NAME, DOMAIN_ID, AUTHORITIES) VALUES
	('1', 'DOCTOR123', 'DOCTOR321$', 'Doctor, Test', '1', 'PRIV_PATIENT_SELECT'),
	('2', 'USER123', 'USER321$', 'User, Test', '*', 'PRIV_DEBUG,PRIV_CAREWEB_DESIGNER,PRIV_PATIENT_SELECT');

Add default attribute to Test Hospital domain.

CREATE TABLE CWF_DOMAIN (
	ID VARCHAR(20) PRIMARY KEY,
	NAME VARCHAR(255),
	ATTRIBUTES VARCHAR(255) DEFAULT '');

INSERT INTO CWF_DOMAIN (ID, NAME, ATTRIBUTES) VALUES
	('1', 'General Medicine Clinic', ''),
	('2','Emergency Room', ''),
	('3', 'Test Hospital', 'default=true');

CREATE TABLE CWF_USER (
	ID VARCHAR(20) PRIMARY KEY,
	USERNAME VARCHAR(40),
	PASSWORD VARCHAR(40),
	NAME VARCHAR(255),
	DOMAIN_ID VARCHAR(20),
	AUTHORITIES VARCHAR(255) DEFAULT '');

INSERT INTO CWF_USER (ID, USERNAME, PASSWORD, NAME, DOMAIN_ID, AUTHORITIES) VALUES
	('1', 'DOCTOR123', 'DOCTOR321$', 'Doctor, Test', '1', 'PRIV_PATIENT_SELECT'),
	('2', 'USER123', 'USER321$', 'User, Test', '*', 'PRIV_DEBUG,PRIV_CAREWEB_DESIGNER,PRIV_PATIENT_SELECT');


create table sguser (
  id serial not null primary key,
  email text not null unique,
  password text not null,
  username text,
  credentials text,
  challenges text,
  emailverified boolean,
  verificationtoken boolean,
  status text,
  modified timestamp with time zone default now(),
  realm text,
  firstname text,
  lastname text,
  roles json,
  uid text
);

create table accesstoken (
  id text not null unique primary key,
  ttl integer not null,
  created timestamp with time zone default now(),
  userid integer not null references sguser(id)
);

create table acl (
  id serial not null primary key,
  model text,
  property text,
  accesstype text,
  permission text,
  principaltype text,
  principalid text
);

create table role (
  id serial not null primary key,
  name text not null unique,
  description text,
  created timestamp with time zone default now(),
  modified timestamp with time zone
);

create table rolemapping (
  id serial not null primary key,
  principaltype text not null,
  principalid text not null,
  roleid text not null
);

create function update_modified_column()
returns trigger as $$
begin
    NEW.modified = now();
    return NEW;
end;
$$ language 'plpgsql';

create trigger update_sguser_modified_column
before update on sguser for each row execute procedure update_modified_column();

Update migration to add igg

create table sguser (
  id serial not null primary key,
  email text not null unique,
  password text not null,
  username text,
  credentials text,
  challenges text,
  emailverified boolean,
  verificationtoken boolean,
  status text,
  modified timestamp with time zone default now(),
  realm text,
  firstname text,
  lastname text,
  roles json,
  uid text,
  igg bigint
);

create table accesstoken (
  id text not null unique primary key,
  ttl integer not null,
  created timestamp with time zone default now(),
  userid integer not null references sguser(id)
);

create table acl (
  id serial not null primary key,
  model text,
  property text,
  accesstype text,
  permission text,
  principaltype text,
  principalid text
);

create table role (
  id serial not null primary key,
  name text not null unique,
  description text,
  created timestamp with time zone default now(),
  modified timestamp with time zone
);

create table rolemapping (
  id serial not null primary key,
  principaltype text not null,
  principalid text not null,
  roleid text not null
);

create function update_modified_column()
returns trigger as $$
begin
    NEW.modified = now();
    return NEW;
end;
$$ language 'plpgsql';

create trigger update_sguser_modified_column
before update on sguser for each row execute procedure update_modified_column();


SET XACT_ABORT ON;
BEGIN TRANSACTION;
  -- update the version
  UPDATE usr_schema_version SET version_value='46' WHERE version_key='schema_patch';

  DROP INDEX ix_usr_oguser_name ON usr_oguser;

  ALTER TABLE sec_future ADD userid varchar(255);
  UPDATE usr_oguser SET userid = name;
  ALTER TABLE sec_future ALTER COLUMN userid varchar(255) NOT NULL;
  ALTER TABLE sec_future DROP COLUMN name;

  ALTER TABLE usr_oguser ADD name varchar(255);
  ALTER TABLE usr_oguser ADD time_zone varchar(255);
  ALTER TABLE usr_oguser ADD email_address varchar(255);

  UPDATE usr_oguser SET name = userid;
  UPDATE usr_oguser SET time_zone = 'Europe/London';
  ALTER TABLE usr_oguser ALTER COLUMN time_zone varchar(255) NOT NULL;

  CREATE INDEX ix_usr_oguser_userid ON usr_oguser(userid);

COMMIT;

Fix usr_db V 46 migration script for SQL Server... third time lucky?

SET XACT_ABORT ON;
BEGIN TRANSACTION;
  -- update the version
  UPDATE usr_schema_version SET version_value='46' WHERE version_key='schema_patch';

  DROP INDEX ix_usr_oguser_name ON usr_oguser;

  ALTER TABLE usr_oguser ADD userid varchar(255);
  UPDATE usr_oguser SET userid = name;
  ALTER TABLE usr_oguser ALTER COLUMN userid varchar(255) NOT NULL;
  ALTER TABLE usr_oguser DROP COLUMN name;

  ALTER TABLE usr_oguser ADD name varchar(255);
  ALTER TABLE usr_oguser ADD time_zone varchar(255);
  ALTER TABLE usr_oguser ADD email_address varchar(255);

  UPDATE usr_oguser SET name = userid;
  UPDATE usr_oguser SET time_zone = 'Europe/London';
  ALTER TABLE usr_oguser ALTER COLUMN time_zone varchar(255) NOT NULL;

  CREATE INDEX ix_usr_oguser_userid ON usr_oguser(userid);

COMMIT;


DROP TRIGGER blobs_blobmeta_trigger ON blobs_blobmeta;
DROP FUNCTION mutate_blobs_blobmeta();
DROP VIEW blobs_blobmeta;
ALTER TABLE blobs_blobmeta_tbl RENAME TO blobs_blobmeta;

Fix SQL migration script to work on proxy

-- Re-create function so it works on the proxy as well as shards
-- which have differing function definitions. For some reason this
-- is necessary, but possibly only on the proxy?

-- temporary function to evaluate a string -> create function
create function tmp_eval(expression text) returns void as
$body$
declare
  result integer;
begin
  execute expression;
end;
$body$ language plpgsql;

-- temporary table holding get_blobmetas source
CREATE TEMPORARY TABLE temp_func_src(src TEXT);
INSERT INTO temp_func_src (src)
SELECT routine_definition
FROM information_schema.routines
WHERE specific_schema = 'public' AND routine_name = 'get_blobmetas';

-- do the stuff
DROP TRIGGER blobs_blobmeta_trigger ON blobs_blobmeta;
DROP FUNCTION mutate_blobs_blobmeta();
DROP FUNCTION get_blobmetas(TEXT[], SMALLINT);
DROP VIEW blobs_blobmeta;
ALTER TABLE blobs_blobmeta_tbl RENAME TO blobs_blobmeta;

-- re-create get_blobmetas function
SELECT tmp_eval(
    'CREATE OR REPLACE FUNCTION get_blobmetas(parent_ids TEXT[], '
    || 'type_code_ SMALLINT) RETURNS SETOF blobs_blobmeta AS $func$ '
    || src || ' $func$ LANGUAGE plproxy'
)
FROM temp_func_src;


UPDATE users SET updateTime = createtime where updatetime is null;
UPDATE items SET updateTime = createtime where updatetime is null
UPDATE rels SET updateTime = createtime where updatetime is null

ALTER TABLE users ALTER COLUMN updatetime SET DEFAULT extract(epoch from now())*1000;
ALTER TABLE items ALTER COLUMN updatetime SET DEFAULT extract(epoch from now())*1000;
ALTER TABLE rels ALTER COLUMN updatetime SET DEFAULT extract(epoch from now())*1000;

CREATE INDEX ON threads((parents[1]));
CREATE INDEX ON texts((parents[1]));
CREATE INDEX ON users((params->>'email'));
CREATE INDEX ON roomrels(presencetime);
CREATE INDEX ON roomrels("user");
CREATE INDEX ON roomrels using gin (roles);
CREATE INDEX ON roomrels(item);

Add index on thrads createtime

UPDATE users SET updateTime = createtime where updatetime is null;
UPDATE items SET updateTime = createtime where updatetime is null
UPDATE rels SET updateTime = createtime where updatetime is null

ALTER TABLE users ALTER COLUMN updatetime SET DEFAULT extract(epoch from now())*1000;
ALTER TABLE items ALTER COLUMN updatetime SET DEFAULT extract(epoch from now())*1000;
ALTER TABLE rels ALTER COLUMN updatetime SET DEFAULT extract(epoch from now())*1000;

CREATE INDEX ON threads((parents[1]));
CREATE INDEX ON threads(createtime);
CREATE INDEX ON texts((parents[1]));
CREATE INDEX ON users((params->>'email'));
CREATE INDEX ON roomrels(presencetime);
CREATE INDEX ON roomrels("user");
CREATE INDEX ON roomrels using gin (roles);
CREATE INDEX ON roomrels(item);


-- Module for all code for the logger utility.
CREATE OR REPLACE MODULE LOGGER;

-- Public functions and procedures.
-- Procedure to write logs.
ALTER MODULE LOGGER PUBLISH
 PROCEDURE LOG (
  IN LOGGERID ANCHOR LOGGER.CONF_LOGGERS.LOGGER_ID,
  IN LEVELID ANCHOR LOGGER.LEVELS.LEVEL_ID,
  IN MESSAGE ANCHOR LOGGER.LOGS.MESSAGE
 );

-- Function to register the logger.
ALTER MODULE LOGGER PUBLISH
 FUNCTION GET_LOGGER (
  IN NAME VARCHAR(64)
 ) RETURNS ANCHOR LOGGER.CONF_LOGGERS.LOGGER_ID;

-- Array to store the hierarhy of a logger.
ALTER MODULE LOGGER ADD
 TYPE HIERARCHY_ARRAY AS VARCHAR(32) ARRAY[16];

Drop module and no array

-- Drops the module if exist.
DROP MODULE LOGGER.LOGGER;

-- Module for all code for the logger utility.
CREATE OR REPLACE MODULE LOGGER;

-- Public functions and procedures.
-- Procedure to write logs.
ALTER MODULE LOGGER PUBLISH
 PROCEDURE LOG (
  IN LOGGERID ANCHOR LOGGER.CONF_LOGGERS.LOGGER_ID,
  IN LEVELID ANCHOR LOGGER.LEVELS.LEVEL_ID,
  IN MESSAGE ANCHOR LOGGER.LOGS.MESSAGE
 );

-- Function to register the logger.
ALTER MODULE LOGGER PUBLISH
 FUNCTION GET_LOGGER (
  IN NAME VARCHAR(64)
 ) RETURNS ANCHOR LOGGER.CONF_LOGGERS.LOGGER_ID;

-- Array to store the hierarhy of a logger.
--ALTER MODULE LOGGER ADD
-- TYPE HIERARCHY_ARRAY AS VARCHAR(32) ARRAY[16];


CREATE PROCEDURE [account].[GetPayeSchemes_ByAccountId]
	@accountId BIGINT
AS
	SELECT
		*
	FROM
		[account].[Paye] p
	inner join
		[account].[AccountHistory] ah on ah.PayeRef = p.Ref
	WHERE
		ah.AccountId = @accountId

Update to stored procedure for getting paye scheme information for an account to not make it get scheme information for schemes that have been removed

CREATE PROCEDURE [account].[GetPayeSchemes_ByAccountId]
	@accountId BIGINT
AS
	SELECT
		*
	FROM
		[account].[Paye] p
	inner join
		[account].[AccountHistory] ah on ah.PayeRef = p.Ref
	WHERE
		ah.RemovedDate is null and ah.AccountId = @accountId


create table sc_delegatednews_news (
        pubId		int		not null,
        instanceId		varchar(50) not null,
        status varchar(100) not null,
        contributorId varchar(50) not null,
        validatorId varchar(50) null,
        validationDate timestamp(0) null,
        beginDate timestamp(0) null,
        endDate timestamp(0) null,
        newsOrder int not null DEFAULT (0)
    );

Fix the creation of the table sc_delegatednews_news for Oracle: the declaration of the column newsOrder was incorrect

create table sc_delegatednews_news (
        pubId		int		not null,
        instanceId		varchar(50) not null,
        status varchar(100) not null,
        contributorId varchar(50) not null,
        validatorId varchar(50) null,
        validationDate timestamp(0) null,
        beginDate timestamp(0) null,
        endDate timestamp(0) null,
        newsOrder int DEFAULT (0) not null
    );


CREATE TABLE measurements (
  id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  time_stamp DATETIME,
  stroom_dal FLOAT,
  stroom_piek FLOAT,
  stroom_current FLOAT,
  gas FLOAT,

  diff_stroom_dal FLOAT,
  diff_stroom_piek FLOAT,
  diff_gas FLOAT
);

Drop database table when running init_b

DROP TABLE measurements;

CREATE TABLE measurements (
  id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  time_stamp DATETIME,
  stroom_dal FLOAT,
  stroom_piek FLOAT,
  stroom_current FLOAT,
  gas FLOAT,

  diff_stroom_dal FLOAT,
  diff_stroom_piek FLOAT,
  diff_gas FLOAT
);


CREATE TRIGGER logidze_on_<%= table_name %>
BEFORE UPDATE OR INSERT ON <%= table_name %> FOR EACH ROW
WHEN (coalesce(current_setting('logidze.disabled', true), '') <> 'on')
EXECUTE PROCEDURE logidze_logger(<%= logidze_logger_parameters %>);

Add comment to trigger definition

CREATE TRIGGER logidze_on_<%= table_name %>
BEFORE UPDATE OR INSERT ON <%= table_name %> FOR EACH ROW
WHEN (coalesce(current_setting('logidze.disabled', true), '') <> 'on')
-- Parameters: history_size_limit (integer), timestamp_column (text), filtered_columns (text[]),
-- include_columns (boolean), debounce_time_ms (integer)
EXECUTE PROCEDURE logidze_logger(<%= logidze_logger_parameters %>);


set echo off
set verify off
set feedback off
set heading off
set trimspool on
set termout off
set pagesize 0

spool &1

select to_char(workspace_id)
from apex_workspaces
where source_identifier is null;

spool off
exit

Fix filter Had `is null` instead of `is not null`, so it was returning only internal workspaces

set echo off
set verify off
set feedback off
set heading off
set trimspool on
set termout off
set pagesize 0

spool &1

select to_char(workspace_id)
from apex_workspaces
where source_identifier is not null;

spool off
exit


BEGIN;
  CREATE TABLE users (
    id serial NOT NULL PRIMARY KEY,
    username text NOT NULL UNIQUE,
    passwordHash text NOT NULL,
    salt text NOT NULL,
    admin boolean NOT NULL);

  CREATE TABLE drinktypes (
    id serial NOT NULL PRIMARY KEY,
    name text NOT NULL,
    description text NOT NULL);

  CREATE TABLE ingredients (
    id serial NOT NULL PRIMARY KEY,
    name text NOT NULL,
    abv integer NOT NULL);

  CREATE TABLE drinks (
    id serial NOT NULL PRIMARY KEY,
    primaryName text NOT NULL,
    preparation text NOT NULL,
    accepted boolean NOT NULL,
    type integer NOT NULL REFERENCES drinktypes (id),
    writer integer NOT NULL REFERENCES users (id));

  CREATE TABLE drinkIngredients (
    drink integer NOT NULL REFERENCES drinks (id),
    ingredient integer NOT NULL REFERENCES ingredients (id),
    amount integer NOT NULL,
    PRIMARY KEY (drink, ingredient));

  CREATE TABLE additionalDrinkNames (
    drink integer NOT NULL REFERENCES drinks (id),
    name text NOT NULL,
    PRIMARY KEY (drink, name));
COMMIT;

Set drinkIngredients and additionaldDrinkNames to cascade delete

BEGIN;
  CREATE TABLE users (
    id serial NOT NULL PRIMARY KEY,
    username text NOT NULL UNIQUE,
    passwordHash text NOT NULL,
    salt text NOT NULL,
    admin boolean NOT NULL);

  CREATE TABLE drinktypes (
    id serial NOT NULL PRIMARY KEY,
    name text NOT NULL,
    description text NOT NULL);

  CREATE TABLE ingredients (
    id serial NOT NULL PRIMARY KEY,
    name text NOT NULL,
    abv integer NOT NULL);

  CREATE TABLE drinks (
    id serial NOT NULL PRIMARY KEY,
    primaryName text NOT NULL,
    preparation text NOT NULL,
    accepted boolean NOT NULL,
    type integer NOT NULL REFERENCES drinktypes (id),
    writer integer NOT NULL REFERENCES users (id));

  CREATE TABLE drinkIngredients (
    drink integer NOT NULL REFERENCES drinks (id) ON DELETE CASCADE,
    ingredient integer NOT NULL REFERENCES ingredients (id),
    amount integer NOT NULL,
    PRIMARY KEY (drink, ingredient));

  CREATE TABLE additionalDrinkNames (
    drink integer NOT NULL REFERENCES drinks (id) ON DELETE CASCADE,
    name text NOT NULL,
    PRIMARY KEY (drink, name));
COMMIT;


-- Original SubsetIds are not known in RF2, so we need to populate those
INSERT INTO RF2_subset2refset (originalSubsetId, SubsetName, RefsetId, fsn, location)
values (100032, 'GB English Language reference set', 900000000000508004, 'Great Britain English language reference set (foundation metadata concept)', 'Refset/Content/');

INSERT INTO RF2_subset2refset (originalSubsetId, SubsetName, RefsetId, fsn, location)
values (100033, 'US English Language reference set', 900000000000509007, 'United States of America English language reference set (foundation metadata concept)','Refset/Content/');

INSERT INTO RF2_subset2refset (originalSubsetId, SubsetName, RefsetId, fsn, location)
values (103030, 'Spanish Language Edition', 450828004, 'Spanish language reference set (foundation metadata concept)','Refset/Content/');

Correct subset names for US/GB English

-- Original SubsetIds are not known in RF2, so we need to populate those
INSERT INTO RF2_subset2refset (originalSubsetId, SubsetName, RefsetId, fsn, location)
values (100032, 'GB English Dialect Subset', 900000000000508004, 'Great Britain English language reference set (foundation metadata concept)', 'Refset/Content/');

INSERT INTO RF2_subset2refset (originalSubsetId, SubsetName, RefsetId, fsn, location)
values (100033, 'US English Dialect Subset', 900000000000509007, 'United States of America English language reference set (foundation metadata concept)','Refset/Content/');

INSERT INTO RF2_subset2refset (originalSubsetId, SubsetName, RefsetId, fsn, location)
values (103030, 'Spanish Language Edition', 450828004, 'Spanish language reference set (foundation metadata concept)','Refset/Content/');


-- Generate core schema
\i schema/generate/01-schema.sql
\i schema/generate/02-augur_data.sql
\i schema/generate/03-augur_operations.sql
\i schema/generate/04-spdx.sql
\i schema/generate/05-seed_data.sql

-- Update scripts
\i schema/generate/06-schema_update_8.sql
\i schema/generate/07-schema_update_9.sql
\i schema/generate/08-schema_update_10.sql
\i schema/generate/09-schema_update_11.sql
\i schema/generate/10-schema_update_12.sql
\i schema/generate/10-schema_update_12.sql
\i schema/generate/11-schema_update_13.sql
\i schema/generate/12-schema_update_14.sql
\i schema/generate/13-schema_update_15.sql
\i schema/generate/13-schema_update_15.sql
\i schema/generate/15-schema_update_16.sql

Fix typo in schema creation script

-- Generate core schema
\i schema/generate/01-schema.sql
\i schema/generate/02-augur_data.sql
\i schema/generate/03-augur_operations.sql
\i schema/generate/04-spdx.sql
\i schema/generate/05-seed_data.sql

-- Update scripts
\i schema/generate/06-schema_update_8.sql
\i schema/generate/07-schema_update_9.sql
\i schema/generate/08-schema_update_10.sql
\i schema/generate/09-schema_update_11.sql
\i schema/generate/10-schema_update_12.sql
\i schema/generate/10-schema_update_12.sql
\i schema/generate/11-schema_update_13.sql
\i schema/generate/12-schema_update_14.sql
\i schema/generate/13-schema_update_15.sql
\i schema/generate/14-schema_update_16.sql


CREATE DATABASE debtmanager OWNER debtmanager;
USE debtmanager;

CREATE SCHEMA debtmanager;
SET search_path TO debtmanager;

CREATE TABLE users (
   id            serial PRIMARY KEY,
   email         varchar(120) UNIQUE,
   name          varchar(60) NOT NULL,
   bank_account  numeric(22,0) NOT NULL,
   password_hash char(64) NOT NULL
);

CREATE TABLE budgets (
   id            serial PRIMARY KEY,
   owner_id      int REFERENCES users(id) NOT NULL,
   name          varchar(50) NOT NULL,
   description   varchar(200)
);

CREATE TABLE user_budget (
   user_id       int REFERENCES users(id),
   budget_id     int REFERENCES budgets(id)
);

CREATE TABLE payments(
   id            int PRIMARY KEY,
   term          date NOT NULL,
   budget_id     int REFERENCES budgets(id) NOT NULL,
   user_id       int REFERENCES users(id),
   description   varchar(200),
   amount        numeric(6,2) NOT NULL
);

Add boolean field 'accounted' to payments

CREATE DATABASE debtmanager OWNER debtmanager;
USE debtmanager;

CREATE SCHEMA debtmanager;
SET search_path TO debtmanager;

CREATE TABLE users (
   id            serial PRIMARY KEY,
   email         varchar(120) UNIQUE,
   name          varchar(60) NOT NULL,
   bank_account  numeric(22,0) NOT NULL,
   password_hash char(64) NOT NULL
);

CREATE TABLE budgets (
   id            serial PRIMARY KEY,
   owner_id      int REFERENCES users(id) NOT NULL,
   name          varchar(50) NOT NULL,
   description   varchar(200)
);

CREATE TABLE user_budget (
   user_id       int REFERENCES users(id),
   budget_id     int REFERENCES budgets(id)
);

CREATE TABLE payments(
   id            int PRIMARY KEY,
   term          date NOT NULL,
   budget_id     int REFERENCES budgets(id) NOT NULL,
   user_id       int REFERENCES users(id),
   description   varchar(200),
   amount        numeric(6,2) NOT NULL,
   accounted     boolean DEFAULT FALSE
);


-- Allow to record the whole history of statuses, not only the last one
ALTER TABLE  `status` DROP INDEX  `unique_status`;
ALTER TABLE  `status` ADD  `isLatest` BOOLEAN NOT NULL AFTER  `rating`;
UPDATE `status` SET `isLatest` = 1;

Update dates were lost for all statuses on database upgrade

-- Allow to record the whole history of statuses, not only the last one
ALTER TABLE  `status` DROP INDEX  `unique_status`;
ALTER TABLE  `status` ADD  `isLatest` BOOLEAN NOT NULL AFTER  `rating`;
UPDATE `status` SET `isLatest` = 1, `dateUpdate` = `dateUpdate`;


-- Scenario to exercise the datacell implementation
-- using a single receptor and emitter
-- Monitor the aggregation level

set optimizer='datacell_pipe';

create table datacell.potin(
    id integer,
    tag timestamp,
    payload integer
);
create table datacell.potout( tag timestamp, cnt integer);

call datacell.receptor('datacell.potin','localhost',50500);

call datacell.emitter('datacell.potout','localhost',50600);

call datacell.query('datacell.putter', 'insert into datacell.potout select now(), count(*) from datacell.potin;');

call datacell.resume();
call datacell.dump();

-- externally, activate the sensor
--sensor --host=localhost --port=50500 --events=100 --columns=3 --delay=1
-- externally, activate the actuator server to listen
-- actuator

-- wrapup
call datacell.postlude();
drop table datacell.potin;
drop table datacell.potout;

Test batching of events Running this script shows grouping of events of 10 /step using the sensor setting: sensor --host=localhost --port=50500 --events=100 --columns=3 --delay=1

-- Scenario to exercise the datacell implementation
-- using a single receptor and emitter
-- Monitor the aggregation level

create schema datacell;
set optimizer='datacell_pipe';

create table datacell.potin(
    id integer,
    tag timestamp,
    payload integer
);
create table datacell.potout( tag timestamp, cnt integer);

call datacell.receptor('datacell.potin','localhost',50500);

call datacell.emitter('datacell.potout','localhost',50600);

call datacell.query('datacell.putter', 'insert into datacell.potout select now(), count(*) from datacell.potin;');

call datacell.resume();
call datacell.dump();

-- externally, activate the sensor
--sensor --host=localhost --port=50500 --events=100 --columns=3 --delay=1
-- externally, activate the actuator server to listen
-- actuator

-- wrapup
call datacell.postlude();
drop table datacell.potin;
drop table datacell.potout;


DROP TABLE IF EXISTS `civicrm_sms_conversation_contact`;
DROP TABLE IF EXISTS `civicrm_sms_conversation_action`;
DROP TABLE IF EXISTS `civicrm_sms_conversation_question`;
DROP TABLE IF EXISTS `civicrm_sms_conversation`;

Fix order of table delete for uninstall

DROP TABLE IF EXISTS `civicrm_sms_conversation`;
DROP TABLE IF EXISTS `civicrm_sms_conversation_question`;
DROP TABLE IF EXISTS `civicrm_sms_conversation_contact`;
DROP TABLE IF EXISTS `civicrm_sms_conversation_action`;


CREATE TABLE lysr_user (
	id BIGSERIAL PRIMARY KEY,
	email VARCHAR(255) UNIQUE,
	password VARCHAR(60)
);

CREATE TABLE lysr_user_settings (
	id BIGSERIAL PRIMARY KEY,
	user BIGINT NOT NULL REFERENCES lysr_user (id) ON DELETE CASCADE
);

CREATE TABLE lysr_feed (
	id BIGSERIAL PRIMARY KEY,
	url DATALINK UNIQUE
);

CREATE TABLE lysr_user_feed (
	id BIGSERIAL PRIMARY KEY,
	user BIGINT NOT NULL REFERENCES lysr_user (id) ON DELETE CASCADE,
	feed BIGINT NOT NULL REFERENCES lysr_feed (id) ON DELETE CASCADE
);

CREATE TABLE lysr_feed_entry (
	id BIGSERIAL PRIMARY KEY,
	feed BIGINT NOT NULL REFERENCES lysr_feed (id) ON DELETE CASCADE,
	-- feed contents
);

CREATE TABLE lysr_feed_entry_status (
	id BIGSERIAL PRIMARY KEY,
	user BIGINT NOT NULL REFERENCES lysr_user (id) ON DELETE CASCADE,
	feed_entry BIGINT NOT NULL REFERENCES lysr_feed_entry (id) ON DELETE CASCADE,
	read BOOLEAN,
	starred BOOLEAN
);

-- tags
CREATE TABLE lysr_tag (
	id BIGSERIAL PRIMARY KEY,
	name TEXT
);

CREATE TABLE lysr_tag_assoc (
	id BIGSERIAL PRIMARY KEY,
	tag BIGINT REFERENCES lysr_tag (id) ON DELETE CASCADE,
	feed BIGINT REFENECES lysr_user_feed (id) ON DELETE CASCADE
);
-- /tags

Change lysr_feed url type from DATALINK to TEXT

CREATE TABLE lysr_user (
	id BIGSERIAL PRIMARY KEY,
	email VARCHAR(255) UNIQUE,
	password VARCHAR(60)
);

CREATE TABLE lysr_user_settings (
	id BIGSERIAL PRIMARY KEY,
	user BIGINT NOT NULL REFERENCES lysr_user (id) ON DELETE CASCADE
);

CREATE TABLE lysr_feed (
	id BIGSERIAL PRIMARY KEY,
	url TEXT UNIQUE
);

CREATE TABLE lysr_user_feed (
	id BIGSERIAL PRIMARY KEY,
	user BIGINT NOT NULL REFERENCES lysr_user (id) ON DELETE CASCADE,
	feed BIGINT NOT NULL REFERENCES lysr_feed (id) ON DELETE CASCADE
);

CREATE TABLE lysr_feed_entry (
	id BIGSERIAL PRIMARY KEY,
	feed BIGINT NOT NULL REFERENCES lysr_feed (id) ON DELETE CASCADE,
	-- feed contents
);

CREATE TABLE lysr_feed_entry_status (
	id BIGSERIAL PRIMARY KEY,
	user BIGINT NOT NULL REFERENCES lysr_user (id) ON DELETE CASCADE,
	feed_entry BIGINT NOT NULL REFERENCES lysr_feed_entry (id) ON DELETE CASCADE,
	read BOOLEAN,
	starred BOOLEAN
);

-- tags
CREATE TABLE lysr_tag (
	id BIGSERIAL PRIMARY KEY,
	name TEXT
);

CREATE TABLE lysr_tag_assoc (
	id BIGSERIAL PRIMARY KEY,
	tag BIGINT REFERENCES lysr_tag (id) ON DELETE CASCADE,
	feed BIGINT REFENECES lysr_user_feed (id) ON DELETE CASCADE
);
-- /tags