CREATE TYPE partition AS ENUM ('system', 'schema', 'user');

-- A table that will store the whole transaction log
-- of the database.
CREATE TABLE IF NOT EXISTS txlog (
  id bigint PRIMARY KEY,
  part partition,
  facts bytea,
  created_at timestamptz default now()
) WITH (OIDS=FALSE);

-- A table that will store the key value properties
-- and will be used usually for storing settings.
CREATE TABLE IF NOT EXISTS properties (
  key text PRIMARY KEY;
  value bytea
) WITH (OIDS=FALSE);

-- A table that represents the materialized view of
-- the current applied schema.
CREATE TABLE IF NOT EXISTS schemaview (
  name text PRIMARY KEY,
  opts bytea,
  created_at timestamptz default now(),
  modified_at timestamptz default now(),
  txid bigint references txlog(id)
) WITH (OIDS=FALSE);

-- A table that will stores all the entities
CREATE TABLE IF NOT EXISTS entity (
  id uuid PRIMARY KEY,
  attributes text[],
  created_at timestamptz default now(),
  modified_at timestamptz
) WITH (OIDS=FALSE);

Add initial attributes tables (schema related attributes).

CREATE TYPE partition AS ENUM ('system', 'schema', 'user');

-- A table that will store the whole transaction log
-- of the database.
CREATE TABLE IF NOT EXISTS txlog (
  id bigint PRIMARY KEY,
  part partition,
  facts bytea,
  created_at timestamptz default now()
) WITH (OIDS=FALSE);

-- A table that will store the key value properties
-- and will be used usually for storing settings.
CREATE TABLE IF NOT EXISTS properties (
  key text PRIMARY KEY;
  value bytea
) WITH (OIDS=FALSE);

-- A table that represents the materialized view of
-- the current applied schema.
CREATE TABLE IF NOT EXISTS schemaview (
  name text PRIMARY KEY,
  opts bytea,
  created_at timestamptz default now(),
  modified_at timestamptz default now(),
  txid bigint references txlog(id)
) WITH (OIDS=FALSE);

-- A table that will stores all the entities
CREATE TABLE IF NOT EXISTS entity (
  id uuid PRIMARY KEY,
  part partition,
  attributes text[],
  created_at timestamptz default now(),
  modified_at timestamptz
) WITH (OIDS=FALSE);

-- Schema related attributes tables

CREATE TABLE IF NOT EXISTS db_schema__ident (
  eid uuid PRIMARY KEY,
  txid bigint,
  created_at timestamptz default now(),
  modified_at timestamptz,
  content bytea
) WITH (OIDS=FALSE);

CREATE TABLE IF NOT EXISTS db_schema__unique (
  eid uuid PRIMARY KEY,
  txid bigint,
  created_at timestamptz default now(),
  modified_at timestamptz,
  content boolean
) WITH (OIDS=FALSE);

CREATE TABLE IF NOT EXISTS db_schema__index (
  eid uuid PRIMARY KEY,
  txid bigint,
  created_at timestamptz default now(),
  modified_at timestamptz,
  content bytea
) WITH (OIDS=FALSE);

CREATE TABLE IF NOT EXISTS db_schema__index (
  eid uuid PRIMARY KEY,
  txid bigint,
  created_at timestamptz default now(),
  modified_at timestamptz,
  content bytea
) WITH (OIDS=FALSE);


USE pufferpanel;

SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `_meta` (
  `metaId` INT(11) NOT NULL AUTO_INCREMENT,
  `metaKey` VARCHAR(20) NOT NULL,
  `metaValue` VARCHAR(200) NOT NULL,
  PRIMARY KEY (`metaId`),
  UNIQUE INDEX `UK_metaKey` (`metaKey`)
);

INSERT INTO _meta (metaKey, metaValue) VALUES
  ('version', '1.1.2'),
  ('updateDate', CURRENT_TIMESTAMP)
  ON DUPLICATE KEY UPDATE
  metaKey=VALUES(metaKey),
  metaValue=VALUES(metaValue);

SET FOREIGN_KEY_CHECKS = 1;

Make sure _meta table has originalVersion and installDate

USE pufferpanel;

SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `_meta` (
  `metaId` INT(11) NOT NULL AUTO_INCREMENT,
  `metaKey` VARCHAR(20) NOT NULL,
  `metaValue` VARCHAR(200) NOT NULL,
  PRIMARY KEY (`metaId`),
  UNIQUE INDEX `UK_metaKey` (`metaKey`)
);

INSERT IGNORE INTO _meta (metaKey, metaValue) VALUES
  ('originalVersion', '1.1.2'),
  ('installDate', CURRENT_TIMESTAMP);

INSERT INTO _meta (metaKey, metaValue) VALUES
  ('version', '1.1.2'),
  ('updateDate', CURRENT_TIMESTAMP)
  ON DUPLICATE KEY UPDATE
  metaKey=VALUES(metaKey),
  metaValue=VALUES(metaValue);

SET FOREIGN_KEY_CHECKS = 1;


-- Script: create_database.sql
--
-- Description: Creates (or recreates) the ABRAID-MP database itself. Must be run as a user that is able to create databases.
--
-- Parameters:
--      database_name: The ABRAID-MP database name
--      application_username: The ABRAID-MP application's username
--      application_password: The ABRAID-MP application's password
--
-- Copyright (c) 2014 University of Oxford

-- Drop and create the database
DROP DATABASE IF EXISTS :database_name;
CREATE DATABASE :database_name;

-- Change context to the newly-created database
\c :database_name

-- Enable PostGIS
CREATE EXTENSION postgis;

-- Create schema objects
\i tables.sql
\i views.sql
\i constraints.sql
\i indexes.sql
\i roles.sql

Hide NOTICE messages when creating the database, especially when the database is not dropped because it does not exist.

-- Script: create_database.sql
--
-- Description: Creates (or recreates) the ABRAID-MP database itself. Must be run as a user that is able to create databases.
--
-- Parameters:
--      database_name: The ABRAID-MP database name
--      application_username: The ABRAID-MP application's username
--      application_password: The ABRAID-MP application's password
--
-- Copyright (c) 2014 University of Oxford

SET client_min_messages TO WARNING;

-- Drop and create the database
DROP DATABASE IF EXISTS :database_name;
CREATE DATABASE :database_name;

-- Change context to the newly-created database
\c :database_name

-- Enable PostGIS
CREATE EXTENSION postgis;

-- Create schema objects
\i tables.sql
\i views.sql
\i constraints.sql
\i indexes.sql
\i roles.sql


Title	ISSN	eISSN
Cancer Discovery	2159-8274	2159-8290
Cancer Epidemiology, Biomarkers & Prevention	1055-9965	1538-7755
Cancer Immunology Research	x	x
Cancer Prevention Research	1940-6207	1940-6215
Cancer Research	0008-5472	1538-7445
Molecular Cancer Research	1541-7786	1557-3125
Molecular Cancer Therapeutics	1535-7163	1538-8514

Add AACR's Clinical Cancer Research metadata

Title	ISSN	eISSN
Cancer Discovery	2159-8274	2159-8290
Cancer Epidemiology, Biomarkers & Prevention	1055-9965	1538-7755
Cancer Immunology Research	x	x
Cancer Prevention Research	1940-6207	1940-6215
Cancer Research	0008-5472	1538-7445
Clinical Cancer Research	1078-0432	1557-3265
Molecular Cancer Research	1541-7786	1557-3125
Molecular Cancer Therapeutics	1535-7163	1538-8514


CREATE TABLE IF NOT EXISTS content (
  id integer PRIMARY KEY AUTOINCREMENT,
  hashId text UNIQUE NOT NULL,
  url text UNIQUE NOT NULL,
  state text NOT NULL DEFAULT('initialized'),
  initializedAt datetime DEFAULT(CURRENT_TIMESTAMP),
  activeAt datetime,
  completedAt datetime,
  mime text,
  size integer,
  progress real NOT NULL DEFAULT(0.0),
  dzi_width integer,
  dzi_height integer,
  dzi_tileSize integer,
  dzi_tileOverlap integer,
  dzi_tileFormat text
);

CREATE INDEX IF NOT EXISTS state_index ON content (state);

CREATE INDEX IF NOT EXISTS url_index ON content (url);

CREATE INDEX IF NOT EXISTS hashId_index ON content (hashId);

CREATE UNIQUE INDEX IF NOT EXISTS hashId_index_unique ON content (hashId);

CREATE UNIQUE INDEX IF NOT EXISTS url_index_unique ON content (url);

Move default values into application logic

CREATE TABLE IF NOT EXISTS content (
  id integer PRIMARY KEY AUTOINCREMENT,
  hashId text UNIQUE NOT NULL,
  url text UNIQUE NOT NULL,
  state text NOT NULL,
  initializedAt datetime DEFAULT(CURRENT_TIMESTAMP),
  activeAt datetime,
  completedAt datetime,
  mime text,
  size integer,
  progress real NOT NULL DEFAULT(0.0),
  dzi_width integer,
  dzi_height integer,
  dzi_tileSize integer,
  dzi_tileOverlap integer,
  dzi_tileFormat text
);

CREATE INDEX IF NOT EXISTS state_index ON content (state);

CREATE INDEX IF NOT EXISTS url_index ON content (url);

CREATE INDEX IF NOT EXISTS hashId_index ON content (hashId);

CREATE UNIQUE INDEX IF NOT EXISTS hashId_index_unique ON content (hashId);

CREATE UNIQUE INDEX IF NOT EXISTS url_index_unique ON content (url);


-- Update Script for Database from Master to layout_symbols
ALTER TABLE `resilienz-test`.`layouts` ADD COLUMN `symbol` VARCHAR(2) NOT NULL AFTER `name`;

UPDATE `resilienz-test`.`layouts` SET `symbol`='A' WHERE `id`='1';
UPDATE `resilienz-test`.`layouts` SET `symbol`='B' WHERE `id`='2';
UPDATE `resilienz-test`.`layouts` SET `symbol`='C' WHERE `id`='3';
UPDATE `resilienz-test`.`layouts` SET `symbol`='D' WHERE `id`='4';
UPDATE `resilienz-test`.`layouts` SET `symbol`='E' WHERE `id`='5';
UPDATE `resilienz-test`.`layouts` SET `symbol`='F' WHERE `id`='6';
UPDATE `resilienz-test`.`layouts` SET `symbol`='G' WHERE `id`='7';
UPDATE `resilienz-test`.`layouts` SET `symbol`='H' WHERE `id`='8';

Fix of update.sql (wrong table)

-- Update Script for Database from Master to layout_symbols
ALTER TABLE `resilienz`.`layouts` ADD COLUMN `symbol` VARCHAR(2) NOT NULL AFTER `name`;

UPDATE `resilienz`.`layouts` SET `symbol`='A' WHERE `id`='1';
UPDATE `resilienz`.`layouts` SET `symbol`='B' WHERE `id`='2';
UPDATE `resilienz`.`layouts` SET `symbol`='C' WHERE `id`='3';
UPDATE `resilienz`.`layouts` SET `symbol`='D' WHERE `id`='4';
UPDATE `resilienz`.`layouts` SET `symbol`='E' WHERE `id`='5';
UPDATE `resilienz`.`layouts` SET `symbol`='F' WHERE `id`='6';
UPDATE `resilienz`.`layouts` SET `symbol`='G' WHERE `id`='7';
UPDATE `resilienz`.`layouts` SET `symbol`='H' WHERE `id`='8';


INSERT INTO library_recordlabel (SELECT RefCode, labelid FROM library);||
ALTER TABLE `library` DROP FOREIGN KEY `Record`;||
ALTER TABLE `library` DROP INDEX `Label_idx`;||
ALTER TABLE `library` DROP COLUMN `labelid`;

Move recordlabel info to int table before deleting it

INSERT INTO library_recordlabel (SELECT RefCode, labelid FROM library);||
UPDATE library_recordlabel
  SET recordlabel_LabelNumber=(SELECT LabelNumber FROM recordlabel WHERE Name='Self-Released')
  WHERE recordlabel_LabelNumber IN (SELECT LabelNumber FROM recordlabel WHERE Name IN ('Self Released', 'Independent', 'SR'));||
UPDATE recordlabel SET name_alias_duplicate=NULL WHERE Name='Self-Released';||
ALTER TABLE `library` DROP FOREIGN KEY `Record`;||
ALTER TABLE `library` DROP INDEX `Label_idx`;||
ALTER TABLE `library` DROP COLUMN `labelid`;||
DELETE FROM recordlabel WHERE Name IN ('Self Released', 'Independent', 'SR');


-- Create extensions and types.
create extension hstore;
create domain uint64 as numeric(20,0);

-- Create users for different types of connections and authentication.
create user pgx_ssl PASSWORD 'secret';
set password_encryption = md5;
create user pgx_md5 PASSWORD 'secret';
set password_encryption = 'scram-sha-256';
create user pgx_pw PASSWORD 'secret';
create user pgx_scram PASSWORD 'secret';

-- The tricky test user, below, has to actually exist so that it can be used in a test
-- of aclitem formatting. It turns out aclitems cannot contain non-existing users/roles.
create user " tricky, ' } "" \\ test user " superuser password 'secret';

Fix unix domain socket tests on CI

-- Create extensions and types.
create extension hstore;
create domain uint64 as numeric(20,0);

-- Create users for different types of connections and authentication.
create user pgx_ssl PASSWORD 'secret';
set password_encryption = md5;
create user pgx_md5 PASSWORD 'secret';
set password_encryption = 'scram-sha-256';
create user pgx_pw PASSWORD 'secret';
create user pgx_scram PASSWORD 'secret';
\set whoami `whoami`
create user :whoami; -- unix domain socket user

-- The tricky test user, below, has to actually exist so that it can be used in a test
-- of aclitem formatting. It turns out aclitems cannot contain non-existing users/roles.
create user " tricky, ' } "" \\ test user " superuser password 'secret';


CREATE
    ALGORITHM = UNDEFINED
    DEFINER = `root`@`localhost`
    SQL SECURITY DEFINER
VIEW `global_addressbook` AS
    SELECT
        SUBSTR((UNIX_TIMESTAMP(`vmail`.`mailbox`.`passwordlastchange`) +
                UNIX_TIMESTAMP(`vmail`.`mailbox`.`modified`)) -
                UNIX_TIMESTAMP(`vmail`.`mailbox`.`created`),
            -(8)) AS `ID`,
        `vmail`.`mailbox`.`name` AS `name`,
        `vmail`.`mailbox`.`username` AS `email`,
        `vmail`.`mailbox`.`domain` AS `domain`
    FROM
        `vmail`.`mailbox`;

Fix SQL Scripts to Support Newer MySQL Versions

CREATE
    ALGORITHM = UNDEFINED
    DEFINER = `root`@`localhost`
    SQL SECURITY DEFINER
VIEW `global_addressbook` AS
    SELECT
        SUBSTR((UNIX_TIMESTAMP(`vmail`.`mailbox`.`created`) -
        CHAR_LENGTH(`vmail`.`mailbox`.`password`)), -(8)) AS `ID`,
        `vmail`.`mailbox`.`name` AS `name`,
        `vmail`.`mailbox`.`username` AS `email`,
        `vmail`.`mailbox`.`domain` AS `domain`
    FROM
        `vmail`.`mailbox`;


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

Add comment about where opioid_measure data comes from

SELECT
  month,
  practice,
  pct,
  SUM(quantity*dose_per_unit*ome_multiplier) AS total_ome
FROM
  {project}.{hscic}.normalised_prescribing_standard AS presc
JOIN
  -- data in richard.opioid_measure comes from:
  -- https://docs.google.com/spreadsheets/d/1IjnHbYVszZKPmVSYydtMVzbDLPOmq8bOFq45QsSu6sE/edit?usp=sharing
  ebmdatalab.richard.opioid_measure as opioid
ON
  CONCAT(SUBSTR(presc.bnf_code,0,9),'AA%',(SUBSTR(presc.bnf_code,-2,2)))=CONCAT(SUBSTR(opioid.bnf_code,0,11),'%',(SUBSTR(opioid.bnf_code,-2,2)))
GROUP BY
  month,
  pct,
  practice


use x2data
SELECT
  'state_id',
  'local_id',
  'full_name',
  'staff_type',
  'homeroom',
  'status',
  'login_name',
  'school_local_id'
UNION ALL
SELECT
  STF_ID_STATE,
  STF_ID_LOCAL,
  stf_name_view,
  STF_STAFF_TYPE,
  STF_HOMEROOM,
  STF_STATUS,
  USR_LOGIN_NAME,
  SKL_SCHOOL_ID
FROM staff
INNER JOIN school
  ON staff.STF_SKL_OID=school.SKL_OID
INNER JOIN person
  ON staff.STF_PSN_OID=person.PSN_OID
INNER JOIN user_info
  ON person.PSN_OID=user_info.USR_PSN_OID
WHERE STF_STATUS = 'Active'
AND STF_ID_LOCAL IS NOT NULL
  INTO OUTFILE "E:/_BACKUP_MYSQL/CodeForAmerica/educators_export.txt"
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'

Include educators with no LASID in educators export file

use x2data
SELECT
  'state_id',
  'local_id',
  'full_name',
  'staff_type',
  'homeroom',
  'status',
  'login_name',
  'school_local_id'
UNION ALL
SELECT
  STF_ID_STATE,
  STF_ID_LOCAL,
  stf_name_view,
  STF_STAFF_TYPE,
  STF_HOMEROOM,
  STF_STATUS,
  USR_LOGIN_NAME,
  SKL_SCHOOL_ID
FROM staff
INNER JOIN school
  ON staff.STF_SKL_OID=school.SKL_OID
INNER JOIN person
  ON staff.STF_PSN_OID=person.PSN_OID
INNER JOIN user_info
  ON person.PSN_OID=user_info.USR_PSN_OID
WHERE STF_STATUS = 'Active'
  INTO OUTFILE "E:/_BACKUP_MYSQL/CodeForAmerica/educators_export.txt"
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'


CREATE TABLE `castro_shop_offers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `image` varchar(255) DEFAULT NULL,
  `give_item` int(11) DEFAULT 0,
  `give_item_amount` int(11) DEFAULT 0,
  `charges` int(11) DEFAULT 1,
  `container_give_item` varchar(255) DEFAULT NULL,
  `container_give_amount` varchar(255) DEFAULT NULL,
  `container_give_charges` int(11) DEFAULT 1,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Fix shop offer table structure

CREATE TABLE `castro_shop_offers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `image` varchar(255) DEFAULT NULL,
  `give_item` int(11) DEFAULT 0,
  `give_item_amount` int(11) DEFAULT 0,
  `charges` int(11) DEFAULT 1,
  `container_give_item` varchar(255) DEFAULT '',
  `container_give_amount` varchar(255) DEFAULT '',
  `container_give_charges` varchar(255) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


CREATE TABLE `wi_adminsetting` (
  `id` int(11) NOT NULL auto_increment,
  `startregion` varchar(255) NOT NULL,
  `userdir` varchar(255) NOT NULL,
  `griddir` varchar(255) NOT NULL,
  `assetdir` varchar(255) NOT NULL,
  `lastnames` varchar(10) NOT NULL,
  `adress` varchar(32) NOT NULL,
  `region` text NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM  AUTO_INCREMENT=2 ;

INSERT INTO `wi_adminsetting` (`id`, `startregion`, `userdir`, `griddir`, `assetdir`, `lastnames`, `adress`, `region`) VALUES
(1, '', '', '', '', '0', '0', '0');

Update the admin settings table with the new verify users and allow registration parts.

CREATE TABLE `wi_adminsetting` (
  `id` int(11) NOT NULL auto_increment,
  `startregion` varchar(255) NOT NULL,
  `userdir` varchar(255) NOT NULL,
  `griddir` varchar(255) NOT NULL,
  `assetdir` varchar(255) NOT NULL,
  `lastnames` varchar(10) NOT NULL,
  `adress` varchar(32) NOT NULL,
  `region` text NOT NULL,
  `allowRegistrations` varchar(10) NOT NULL,
  `verifyUsers` varchar(10) NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM  AUTO_INCREMENT=2 ;

INSERT INTO `wi_adminsetting` (`id`, `startregion`, `userdir`, `griddir`, `assetdir`, `lastnames`, `adress`, `region`,'allowRegistrations','verifyUsers') VALUES
(1, '', '', '', '', '0', '0', '0','1','1');


CREATE TABLE `kwc_mail_views` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`mail_component_id` VARCHAR( 255 ) NOT NULL ,
`recipient_id` INT( 11 ) NOT NULL ,
`recipient_model_shortcut` VARCHAR( 255 ) NOT NULL ,
`ip` VARCHAR( 255 ) NOT NULL ,
`date` DATETIME NOT NULL ,
INDEX (  `mail_component_id` )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;

Check if kwc_mail_views table exists before creating it in update script

CREATE TABLE IF NOT EXISTS `kwc_mail_views` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`mail_component_id` VARCHAR( 255 ) NOT NULL ,
`recipient_id` INT( 11 ) NOT NULL ,
`recipient_model_shortcut` VARCHAR( 255 ) NOT NULL ,
`ip` VARCHAR( 255 ) NOT NULL ,
`date` DATETIME NOT NULL ,
INDEX (  `mail_component_id` )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;


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

  key VARCHAR(100) NOT NULL
);

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),
	content_type VARCHAR(100),
	content_type_parameter VARCHAR(200),
	size BIGINT
);

CREATE INDEX logs_idx_type ON logs (type);
CREATE INDEX logs_idx_build_id ON logs (build_id);

Set mandatory data as non-null

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

  key VARCHAR(100) NOT NULL
);

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) NOT NULL,

	content_type VARCHAR(100) NOT NULL,
	content_type_parameter VARCHAR(200) NOT NULL,

	size BIGINT
);

CREATE INDEX logs_idx_type ON logs (type);
CREATE INDEX logs_idx_build_id ON logs (build_id);


ALTER TABLE awa_blog ADD COLUMN `url` VARCHAR(255) BINARY NOT NULL;

ALTER TABLE awa_post CHANGE `text` `text` TEXT NOT NULL;
ALTER TABLE awa_question CHANGE `description` `description` TEXT NOT NULL;
ALTER TABLE awa_answer CHANGE `answer` `answer` TEXT NOT NULL;

ALTER TABLE awa_comments CHANGE `date` `create_date` DATETIME NOT NULL;
ALTER TABLE awa_comments CHANGE `message` `message` TEXT NOT NULL;
ALTER TABLE awa_comments ADD COLUMN `status` INTEGER NOT NULL;

ALTER TABLE awa_post ADD COLUMN `allow_comments` TINYINT NOT NULL;

RENAME TABLE awa_comments TO awa_comment;

Update the migration SQL script to add the new column in the awa_comment table

ALTER TABLE awa_blog ADD COLUMN `url` VARCHAR(255) BINARY NOT NULL;

ALTER TABLE awa_post CHANGE `text` `text` TEXT NOT NULL;
ALTER TABLE awa_question CHANGE `description` `description` TEXT NOT NULL;
ALTER TABLE awa_answer CHANGE `answer` `answer` TEXT NOT NULL;

ALTER TABLE awa_comments CHANGE `date` `create_date` DATETIME NOT NULL;
ALTER TABLE awa_comments CHANGE `message` `message` TEXT NOT NULL;
ALTER TABLE awa_comments ADD COLUMN `status` INTEGER NOT NULL;

ALTER TABLE awa_post ADD COLUMN `allow_comments` TINYINT NOT NULL;

RENAME TABLE awa_comments TO awa_comment;

ALTER TABLE awa_comment ADD COLUMN `format` INTEGER NOT NULL;


/********************************************************************************
file-centric-snapshot-mdrs-no-version-skew.sql
    Assertion:
        "Target modules in the MDRS must have the same version"
********************************************************************************/

insert into qa_result (runid, assertionuuid, concept_id, details)
select
  <RUNID>,
  '<ASSERTIONUUID>',
  a.referencedcomponentid,
  concat('Version skew in dependency on module: ', b.referencedcomponentid, ' - ', a.targeteffectivetime, ' and ', b.targeteffectivetime)
from curr_moduledependencyrefset_s a, curr_moduledependencyrefset_s b
where a.active = '1' and b.active = '1'
  and a.referencedcomponentid = b.referencedcomponentid and a.sourceeffectivetime = b.sourceeffectivetime
  and a.targeteffectivetime != b.targeteffectivetime;

Fix tabs in file as it causes fails in the execution


-- Returns a JSON array describing the tags for question question_id.

CREATE OR REPLACE FUNCTION
    tags_for_question (question_id bigint) RETURNS JSONB AS $$
SELECT
    JSONB_AGG(JSONB_BUILD_OBJECT(
        'name',tag.name,
        'id',tag.id,
        'color',tag.color,
        'number', tag.number
    ) ORDER BY tag.number, tag.id)
FROM
    tags AS tag
    JOIN question_tags AS qt ON (qt.tag_id = tag.id AND qt.question_id = tags_for_question.question_id)
$$ LANGUAGE SQL STABLE;

Change tag order to match infoQuestion.json order

-- Returns a JSON array describing the tags for question question_id.

CREATE OR REPLACE FUNCTION
    tags_for_question (question_id bigint) RETURNS JSONB AS $$
SELECT
    JSONB_AGG(JSONB_BUILD_OBJECT(
        'name',tag.name,
        'id',tag.id,
        'color',tag.color
    ) ORDER BY qt.number, tag.id)
FROM
    tags AS tag
    JOIN question_tags AS qt ON (qt.tag_id = tag.id AND qt.question_id = tags_for_question.question_id)
$$ LANGUAGE SQL STABLE;


DROP TABLE IF EXISTS presto_test_sequence;

DROP TABLE IF EXISTS presto_test;

DROP TABLE IF EXISTS presto_test_partition_format;

DROP TABLE IF EXISTS presto_test_unpartitioned;

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

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

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

DROP VIEW IF EXISTS presto_test_view;

Remove missing Hive drop test table

DROP TABLE IF EXISTS presto_test_sequence;

DROP TABLE IF EXISTS presto_test;

DROP TABLE IF EXISTS presto_test_partition_format;

DROP TABLE IF EXISTS presto_test_unpartitioned;

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

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

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

DROP TABLE IF EXISTS presto_test_partition_schema_change;

DROP VIEW IF EXISTS presto_test_view;


# Updating the schema version

UPDATE meta SET meta_value = 50 where meta_key = "schema_version";

# DN/DS

Add patch for new link between genomic_align_tree and genomic_align_group

# Updating the schema version

UPDATE meta SET meta_value = 50 where meta_key = "schema_version";

# e!49: genomic_align_tree.node_id linked to genomic_align.genomic_align_id
# e!50: genomic_align_tree.node_id now links to genomic_align_group.group_id and
#       genomic_align_group.genomic_align_id to genomic_align.genomic_align_id
# This is required to support composite segments in the GenomicAlignTrees. This
# patch assumes no data exists in the genomic_align_group table and there are no
# composite segments in the existing database. This is true for e!49

INSERT INTO genomic_align_group SELECT node_id, "epo", node_id FROM genomic_align_tree;

# DN/DS


create table Block (
	id serial,

	createdate timestamp not null,
	modified_date timestamp not null,

	displayorder integer not null default 0,
	attachment integer null references Attachment(id) on delete set null,
	media integer null references Media(id) on delete set null,
	image integer null references Image(id) on delete set null,
	bodytext text null,

	primary key (id)
);

Update sql to store timestamp without a timezone.

create table Block (
	id serial,

	createdate timestamp not null,
	modified_date timestamp not null default LOCALTIMESTAMP(0),

	displayorder integer not null default 0,
	attachment integer null references Attachment(id) on delete set null,
	media integer null references Media(id) on delete set null,
	image integer null references Image(id) on delete set null,
	bodytext text null,

	primary key (id)
);


start transaction;

create table table3388 (k int,b int);
insert into table3388 values (1,2);
insert into table3388 values (2,2);
insert into table3388 values (3,3);
insert into table3388 values (4,65);
insert into table3388 values (5,21);
insert into table3388 values (6,null);
insert into table3388 values (7,null);
insert into table3388 values (8,null);
insert into table3388 values (9,null);

select sum(b) from table3388 group by k order by sum(b);

rollback;

Fix test. The previous version was used to produce the desired output, this is what we want to test.

start transaction;

create table table3388 (k int,b int);
insert into table3388 values (1,2);
insert into table3388 values (2,2);
insert into table3388 values (3,3);
insert into table3388 values (4,65);
insert into table3388 values (5,21);
insert into table3388 values (6,null);
insert into table3388 values (7,null);
insert into table3388 values (8,null);
insert into table3388 values (9,null);

select sum(b) from table3388 group by k order by case when sum(b) is null then 1 else 0 end,sum(b);

rollback;


ALTER TABLE app_info
DROP COLUMN IF EXISTS certificate;

ALTER TABLE module_config
DROP COLUMN IF EXISTS certificate CASCADE,
DROP COLUMN IF EXISTS private_key CASCADE;

DROP TABLE IF EXISTS app_certificates;

Remove unmatched migration down column


IF EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='dw' and TABLE_NAME='f_virta_otp_kvliikkuvuus'
  and COLUMN_NAME='d_opiskeluoikeudentyyppi_id'
) BEGIN
  ALTER TABLE dw.f_virta_otp_kvliikkuvuus DROP COLUMN d_opiskeluoikeudentyyppi_id
END

IF NOT EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='dw' and TABLE_NAME='f_virta_otp_kvliikkuvuus'
  and COLUMN_NAME='cimo_opintoaste'
) BEGIN
  ALTER TABLE dw.f_virta_otp_kvliikkuvuus ADD cimo_opintoaste varchar(2) NULL
END
/* revert
ALTER TABLE dw.f_virta_otp_kvliikkuvuus ADD d_opiskeluoikeudentyyppi_id int NOT NULL

ALTER TABLE dw.f_virta_otp_kvliikkuvuus DROP COLUMN cimo_opintoaste
--*/

Add some note (just to get scripts executed again).

IF EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='dw' and TABLE_NAME='f_virta_otp_kvliikkuvuus'
  and COLUMN_NAME='d_opiskeluoikeudentyyppi_id'
) BEGIN
  ALTER TABLE dw.f_virta_otp_kvliikkuvuus DROP COLUMN d_opiskeluoikeudentyyppi_id
END

IF NOT EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='dw' and TABLE_NAME='f_virta_otp_kvliikkuvuus'
  and COLUMN_NAME='cimo_opintoaste'
) BEGIN
  ALTER TABLE dw.f_virta_otp_kvliikkuvuus ADD cimo_opintoaste varchar(2) NULL
END
/* revert
-- should empty table before or something similar!
ALTER TABLE dw.f_virta_otp_kvliikkuvuus ADD d_opiskeluoikeudentyyppi_id int NOT NULL

ALTER TABLE dw.f_virta_otp_kvliikkuvuus DROP COLUMN cimo_opintoaste
--*/


CREATE PROCEDURE CreateUserSecurityCode
	@Code varchar(50),
	@UserId varchar(50),
	@CodeType int,
	@ExpiryTime datetime,
	@ReturnUrl varchar(max),
	@PendingValue nvarchar(255)
AS
	IF NOT EXISTS (SELECT Code FROM UserSecurityCode WHERE Code = @Code AND UserId = @UserId)
		BEGIN
			INSERT INTO UserSecurityCode
			(Code, UserId, CodeType, ExpiryTime, ReturnUrl, PendingValue)
			VALUES
			(@Code, @UserId, @CodeType, @ExpiryTime, @ReturnUrl, PendingValue)
		END
GO

Fix create user security code sproc

CREATE PROCEDURE CreateUserSecurityCode
	@Code varchar(50),
	@UserId varchar(50),
	@CodeType int,
	@ExpiryTime datetime,
	@ReturnUrl varchar(max),
	@PendingValue nvarchar(255)
AS
	IF NOT EXISTS (SELECT Code FROM UserSecurityCode WHERE Code = @Code AND UserId = @UserId)
		BEGIN
			INSERT INTO UserSecurityCode
			(Code, UserId, CodeType, ExpiryTime, ReturnUrl, PendingValue)
			VALUES
			(@Code, @UserId, @CodeType, @ExpiryTime, @ReturnUrl, @PendingValue)
		END
GO


INSERT INTO Banks (BankID, Protocol, Host, Port, Path) VALUES ('TESTBANK001', 'https', 'www.testbank1.com', 443, '/api');
INSERT INTO Banks (BankID, Protocol, Host, Port, Path) VALUES ('TESTBANK002', 'https', 'www.testbank2.com', 443, '/api');

Call Register_Bank() instead of doing hard-coded INSERT INTO Banks

SELECT Register_Bank(
    _BankID   := 'TESTBANK001',
    _Protocol := 'https',
    _Host     := 'www.testbank1.com',
    _Port     := 443,
    _Path     := '/api'
);

SELECT Register_Bank(
    _BankID   := 'TESTBANK002',
    _Protocol := 'https',
    _Host     := 'www.testbank2.com',
    _Port     := 443,
    _Path     := '/api'
);


-- add timestap column
-- https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
-- at most one TIMESTAMP column per table could be automatically initialized or updated to the current date and time
-- timestamp column is not null by default
-- Usage: you must set NULL to create_at column during insert. create_at column will be inserted with current timestamp.
-- Sample: insert into project (name, description, created_at) values ('project test', 'project test', null)
ALTER TABLE project ADD created_at timestamp DEFAULT 0;
ALTER TABLE project ADD updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

ALTER TABLE organization ADD created_at timestamp DEFAULT 0;
ALTER TABLE organization ADD updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

ALTER TABLE user ADD created_at timestamp DEFAULT 0;
ALTER TABLE user ADD updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

Revert "Fix DDL on timestamp"

-- add timestap column
-- timestamp column is not null by default
ALTER TABLE project ADD create_time timestamp DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE project ADD change_time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

ALTER TABLE organization ADD create_time timestamp DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE organization ADD change_time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

ALTER TABLE user ADD create_time timestamp DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE user ADD change_time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;


CREATE USER itest;
CREATE DATABASE itest OWNER itest;

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

Fix permissions for user itest.

CREATE USER itest;
CREATE DATABASE itest OWNER itest;
GRANT ALL PRIVILEGES ON DATABASE itest TO itest;

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

GRANT ALL PRIVILEGES ON TABLE users TO itest;
GRANT ALL PRIVILEGES ON SEQUENCE users_id_sequence TO itest;


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

Increase size of deployment properties varchar for mysql

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


use x2data
SELECT
  'state_id',
  'school_id',
  'assessment_id',
  'assessment_date',
  'assessment_scale_score',
  'assessment_perf_level',
  'assessment_name',
  'assessment_subject',
  'assessment_test'
UNION ALL
SELECT DISTINCT
  ASM_STD_OID,
  ASM_SKL_OID,
  ASM_ASD_OID,
  ASM_DATE,
  ASM_SCALE_SCORE,
  ASM_PERFORMANCE_LEVEL,
  ASD_NAME,
  ASD_SUBJECT,
  SUBSTRING_INDEX(SUBSTRING_INDEX(ASD_NAME, ' ', 1), ' ', -1) as ASD_TEST
FROM student_assessment
LEFT JOIN assessment_definition
  ON student_assessment.ASM_ASD_OID=assessment_definition.ASD_OID
INNER JOIN student
  ON student.STD_OID=student_assessment.ASM_STD_OID
INNER JOIN school
ON student.STD_SKL_OID=school.SKL_OID
WHERE STD_ENROLLMENT_STATUS = 'Active'
AND STD_ID_STATE IS NOT NULL
AND STD_OID IS NOT NULL
  INTO OUTFILE "E:\_BACKUP_MYSQL\CodeForAmerica\assessment_export.txt"
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n';

Use student state ID and school local ID for assessment export

use x2data
SELECT
  'state_id',
  'school_local_id',
  'assessment_id',
  'assessment_date',
  'assessment_scale_score',
  'assessment_perf_level',
  'assessment_name',
  'assessment_subject',
  'assessment_test'
UNION ALL
SELECT DISTINCT
  STD_ID_STATE,
  SKL_SCHOOL_ID,
  ASM_ASD_OID,
  ASM_DATE,
  ASM_SCALE_SCORE,
  ASM_PERFORMANCE_LEVEL,
  ASD_NAME,
  ASD_SUBJECT,
  SUBSTRING_INDEX(SUBSTRING_INDEX(ASD_NAME, ' ', 1), ' ', -1) as ASD_TEST
FROM student_assessment
LEFT JOIN assessment_definition
  ON student_assessment.ASM_ASD_OID=assessment_definition.ASD_OID
INNER JOIN student
  ON student.STD_OID=student_assessment.ASM_STD_OID
INNER JOIN school
ON student.STD_SKL_OID=school.SKL_OID
WHERE STD_ENROLLMENT_STATUS = 'Active'
AND STD_ID_STATE IS NOT NULL
AND STD_OID IS NOT NULL
  INTO OUTFILE "E:\_BACKUP_MYSQL\CodeForAmerica\assessment_export.txt"
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n';


insert into user(id, name, login, password) values (1,'Roy','roy','spring');
insert into user(id, name, login, password) values (2,'SomeOne','so','spring');

Update initial list of users

insert into user(id, name, login, password) values (1,'Roy','roy','spring');
insert into user(id, name, login, password) values (2,'Craig','craig','spring');
insert into user(id, name, login, password) values (3,'Greg','greg','spring');


create sequence players_id_seq as integer;
create table players(
    id integer not null default nextval('players_id_seq') primary key,
    first_name varchar(128) not null,
    last_name varchar(128) not null,
    birth_date date not null,
    weight int,
    height int
);

create table divisions(
  name varchar(32) primary key
);

create table teams(
   id  serial primary key,
   name varchar(128) not null,
   division varchar(32) not null,
   foreign key (division) references divisions(name)
);

create table roster(
  team_id bigint not null ,
  player_id bigint not null ,
  foreign key (team_id) references teams(id),
  foreign key (player_id) references players(id)
);

Revert "Be explicit about creating players_id_seq"

create table players(
    id  serial primary key,
    first_name varchar(128) not null,
    last_name varchar(128) not null,
    birth_date date not null,
    weight int,
    height int
);

create table divisions(
  name varchar(32) primary key
);

create table teams(
   id  serial primary key,
   name varchar(128) not null,
   division varchar(32) not null,
   foreign key (division) references divisions(name)
);

create table roster(
  team_id bigint not null ,
  player_id bigint not null ,
  foreign key (team_id) references teams(id),
  foreign key (player_id) references players(id)
);


CREATE TABLE sessions
(
    id INTEGER PRIMARY KEY,
    parent_id INTEGER DEFAULT 0,
    crdate INTEGER DEFAULT 0,
    tstamp INTEGER DEFAULT 0,
    closed INTEGER DEFAULT 0,
    title TEXT DEFAULT NULL,
    FOREIGN KEY(parent_id) REFERENCES sessions(id)
);

CREATE TABLE tabs
(
    id INTEGER PRIMARY KEY,
    session_id INTEGER NOT NULL,
    uri TEXT DEFAULT NULL,
    icon TEXT DEFAULT NULL,
    title TEXT DEFAULT NULL,
    crdate INTEGER DEFAULT 0,
    tstamp INTEGER DEFAULT 0,
    closed INTEGER DEFAULT 0,
    FOREIGN KEY(session_id) REFERENCES sessions(id)
);

CREATE TABLE tab_history
(
    id INTEGER PRIMARY KEY,
    tab_id INTEGER,
    url TEXT,
    icon TEXT,
    title TEXT,
    FOREIGN KEY(tab_id) REFERENCES tabs(id)
);

Add 'IF NOT EXISTS' to tables for Tabby

CREATE TABLE IF NOT EXISTS sessions
(
    id INTEGER PRIMARY KEY,
    parent_id INTEGER DEFAULT 0,
    crdate INTEGER DEFAULT 0,
    tstamp INTEGER DEFAULT 0,
    closed INTEGER DEFAULT 0,
    title TEXT DEFAULT NULL,
    FOREIGN KEY(parent_id) REFERENCES sessions(id)
);

CREATE TABLE IF NOT EXISTS tabs
(
    id INTEGER PRIMARY KEY,
    session_id INTEGER NOT NULL,
    uri TEXT DEFAULT NULL,
    icon TEXT DEFAULT NULL,
    title TEXT DEFAULT NULL,
    crdate INTEGER DEFAULT 0,
    tstamp INTEGER DEFAULT 0,
    closed INTEGER DEFAULT 0,
    FOREIGN KEY(session_id) REFERENCES sessions(id)
);

CREATE TABLE IF NOT EXISTS tab_history
(
    id INTEGER PRIMARY KEY,
    tab_id INTEGER,
    url TEXT,
    icon TEXT,
    title TEXT,
    FOREIGN KEY(tab_id) REFERENCES tabs(id)
);


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

-- We add membership to current state so that we don't need to join against
-- room_memberships, which can be surprisingly costly (we do such queries
-- very frequently).
ALTER TABLE current_state_events ADD membership TEXT;

INSERT INTO background_updates (update_name, progress_json) VALUES
  ('current_state_events_membership', '{}');

Update comment for new column

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

-- We add membership to current state so that we don't need to join against
-- room_memberships, which can be surprisingly costly (we do such queries
-- very frequently).
-- This will be null for non-membership events and the content.membership key
-- for membership events. (Will also be null for membership events until the
-- background update job has finished).
ALTER TABLE current_state_events ADD membership TEXT;

INSERT INTO background_updates (update_name, progress_json) VALUES
  ('current_state_events_membership', '{}');


CREATE TABLE projects (
    id serial PRIMARY KEY,
    keys text[]
);

CREATE TABLE documents (
    id serial PRIMARY KEY,
    locale varchar(128) NOT NULL CHECK (locale <> ''),
    pairs hstore,
    project_id integer REFERENCES projects (id) ON DELETE CASCADE
);

Add locale, project unique constraint

CREATE TABLE projects (
    id serial PRIMARY KEY,
    keys text[]
);

CREATE TABLE documents (
    id serial PRIMARY KEY,
    locale varchar(128) NOT NULL CHECK (locale <> ''),
    pairs hstore,
    project_id integer REFERENCES projects (id) ON DELETE CASCADE,
    UNIQUE (locale, project_id)
);


BEGIN;

CREATE TYPE LINKTYPE AS ENUM ('gopher', 'html');

CREATE TABLE "url" (
	"id" BIGSERIAL,
	"url" TEXT NOT NULL,
	PRIMARY KEY ("id")
);

CREATE INDEX "url__url" ON "url" ("url");

CREATE TABLE "link" (
	"id" BIGSERIAL,
	"url" BIGINT NOT NULL,
	"type" LINKTYPE NOT NULL,
	"clicks" BIGINT NOT NULL DEFAULT 0,
	PRIMARY KEY ("id"),
	FOREIGN KEY ("url") REFERENCES "url" ("id")
);

COMMIT;

-- vim: set nocin ai ts=8 sw=8 noet:

Make some indices unique as appropriate.

BEGIN;

CREATE TYPE LINKTYPE AS ENUM ('gopher', 'html');

CREATE TABLE "url" (
	"id" BIGSERIAL,
	"url" TEXT NOT NULL,
	PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "url__url" ON "url" ("url");

CREATE TABLE "link" (
	"id" BIGSERIAL,
	"url" BIGINT NOT NULL,
	"type" LINKTYPE NOT NULL,
	"clicks" BIGINT NOT NULL DEFAULT 0,
	PRIMARY KEY ("id"),
	FOREIGN KEY ("url") REFERENCES "url" ("id")
);

CREATE UNIQUE INDEX "link__url_type" ON "link" ("url", "type");

COMMIT;

-- vim: set nocin ai ts=8 sw=8 noet:


CREATE OR REPLACE FUNCTION {{routine_name}}(now {{table_name}}.eta%TYPE)
RETURNS SETOF {{table_name}} AS $$
DECLARE
    found_id {{table_name}}.id%TYPE;
BEGIN
    SELECT id INTO found_id
    FROM {{table_name}}
    WHERE eta <= now AND pg_try_advisory_xact_lock(id)
    ORDER BY eta
    LIMIT 1
    FOR UPDATE;

    IF FOUND THEN
        RETURN QUERY EXECUTE 'DELETE FROM {{table_name}} WHERE id = $1 RETURNING *' USING found_id;
    END IF;
END;
$$ LANGUAGE plpgsql;

Fix concurrency issue with pgsql stored function

CREATE OR REPLACE FUNCTION {{routine_name}}(now {{table_name}}.eta%type)
RETURNS SETOF {{table_name}} AS $$
DECLARE
    r {{table_name}}%rowtype;
BEGIN
    SELECT * INTO r
    FROM {{table_name}}
    WHERE eta <= now
    ORDER BY eta
    LIMIT 1
    FOR UPDATE;

    IF FOUND THEN
        DELETE FROM {{table_name}} WHERE id = r.id;
        RETURN NEXT r;
    END IF;
END;
$$ LANGUAGE plpgsql;


CREATE TABLE `cj_base_table` (
`id` int(11) unsigned NOT NULL,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedAt` timestamp NULL DEFAULT NULL,
  `live` enum('1') COLLATE utf8_unicode_ci DEFAULT '1',
  `archivedAt` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DELIMITER //
CREATE TRIGGER `updateModifiedAt` BEFORE UPDATE ON `cj_base_table` FOR EACH ROW SET NEW.modifiedAt = CURRENT_TIMESTAMP
//
DELIMITER ;

ALTER TABLE `cj_base_table` ADD PRIMARY KEY (`id`);

ALTER TABLE `cj_base_table` MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;

Update initial sql query for the base table

--
-- Table structure for table `cj_base_table`
--

CREATE TABLE `cj_base_table` (
  `id` int(11) unsigned NOT NULL,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modifiedAt` timestamp NULL DEFAULT NULL,
  `live` enum('1') COLLATE utf8_unicode_ci DEFAULT '1',
  `archivedAt` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Triggers `cj_base_table`
--
DELIMITER $$
CREATE TRIGGER `updateModifiedAt` BEFORE UPDATE ON `cj_base_table`
FOR EACH ROW SET NEW.modifiedAt = CURRENT_TIMESTAMP
$$
DELIMITER ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `cj_base_table`
--
ALTER TABLE `cj_base_table`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `cj_base_table`
--
ALTER TABLE `cj_base_table`
MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;


-- README:
-- Do not remove the field with a `-- DEFAULT` suffix.
-- That's the default tables/fields needed by Ocsigen-start

CREATE DATABASE ocsipersist;

CREATE EXTENSION citext; --DEFAULT
-- You may remove the above line if you use the type TEXT for emails instead of CITEXT

CREATE SCHEMA ocsigen_start
  CREATE TABLE users ( -- DEFAULT
         userid bigserial primary key, -- DEFAULT
         firstname text NOT NULL,
         lastname text NOT NULL,
         main_email citext NOT NULL,
         password text,
         avatar text
  )

  CREATE TABLE emails ( -- DEFAULT
         email citext primary key, -- DEFAULT
         userid bigint NOT NULL references users(userid), -- DEFAULT
         validated boolean NOT NULL DEFAULT(false)
  )

  CREATE TABLE activation ( -- DEFAULT
         activationkey text primary key, -- DEFAULT
         userid bigint NOT NULL references users(userid), -- DEFAULT
         email citext NOT NULL,
         autoconnect boolean NOT NULL,
         validity bigint NOT NULL,
         action text NOT NULL,
         data text NOT NULL,
         creationdate timestamptz NOT NULL default now()
  )

  CREATE TABLE groups ( -- DEFAULT
         groupid bigserial primary key, -- DEFAULT
         name text NOT NULL, -- DEFAULT
         description text -- DEFAULT
  )

  CREATE TABLE user_groups ( -- DEFAULT
         userid bigint NOT NULL references users(userid), -- DEFAULT
         groupid bigint NOT NULL references groups(groupid) -- DEFAULT
  )

  CREATE TABLE preregister (
         email citext NOT NULL
  );

Add language in the table.

-- README:
-- Do not remove the field with a `-- DEFAULT` suffix.
-- That's the default tables/fields needed by Ocsigen-start

CREATE DATABASE ocsipersist;

CREATE EXTENSION citext; --DEFAULT
-- You may remove the above line if you use the type TEXT for emails instead of CITEXT

CREATE SCHEMA ocsigen_start
  CREATE TABLE users ( -- DEFAULT
         userid bigserial primary key, -- DEFAULT
         firstname text NOT NULL,
         lastname text NOT NULL,
         main_email citext NOT NULL,
         password text,
         avatar text,
         language text
  )

  CREATE TABLE emails ( -- DEFAULT
         email citext primary key, -- DEFAULT
         userid bigint NOT NULL references users(userid), -- DEFAULT
         validated boolean NOT NULL DEFAULT(false)
  )

  CREATE TABLE activation ( -- DEFAULT
         activationkey text primary key, -- DEFAULT
         userid bigint NOT NULL references users(userid), -- DEFAULT
         email citext NOT NULL,
         autoconnect boolean NOT NULL,
         validity bigint NOT NULL,
         action text NOT NULL,
         data text NOT NULL,
         creationdate timestamptz NOT NULL default now()
  )

  CREATE TABLE groups ( -- DEFAULT
         groupid bigserial primary key, -- DEFAULT
         name text NOT NULL, -- DEFAULT
         description text -- DEFAULT
  )

  CREATE TABLE user_groups ( -- DEFAULT
         userid bigint NOT NULL references users(userid), -- DEFAULT
         groupid bigint NOT NULL references groups(groupid) -- DEFAULT
  )

  CREATE TABLE preregister (
         email citext NOT NULL
  );


/*
	Copyright 2016 Comcast Cable Communications Management, LLC

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	    http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
alter table deliveryservice add column geo_limit_countries varchar(750); -- 250 country codes * (2 letters plus a comma)
update deliveryservice set geo_limit_countries = "US" where geo_limit = 2;
update deliveryservice set geo_limit_countries = "CA" where geo_limit = 3;
update deliveryservice set geo_limit = 2 where geo_limit = 3;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
alter table deliveryservice drop column geo_limit_countries;

Fix TO db migration comment breaking goose

/*
	Copyright 2016 Comcast Cable Communications Management, LLC

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	    http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
alter table deliveryservice add column geo_limit_countries varchar(750);
update deliveryservice set geo_limit_countries = "US" where geo_limit = 2;
update deliveryservice set geo_limit_countries = "CA" where geo_limit = 3;
update deliveryservice set geo_limit = 2 where geo_limit = 3;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
alter table deliveryservice drop column geo_limit_countries;


DELIMITER $$
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetLeaderboardForStat`(IN `stat_name` VARCHAR(20))
    READS SQL DATA
BEGIN

DROP TABLE IF EXISTS LeaderboardForStat;

CREATE TEMPORARY TABLE LeaderboardForStat AS
    SELECT @rank := @rank + 1 AS rank,
           `agent`,
           `value`,
           MIN(`age`) `age`
      FROM (SELECT d.`agent`,
                   d.`value`,
                   DATEDIFF(CURDATE(), CAST(d.`timestamp` AS DATE)) `age`
              FROM Data d
                   INNER JOIN (SELECT `agent`,
                                      `value`
                                 FROM `Data` d
                           INNER JOIN (SELECT MAX(timestamp) `timestamp`
                                         FROM `Data`
                                     GROUP BY agent) q0
                                 ON d.timestamp = q0.timestamp
                                WHERE stat = stat_name AND
                                      value > 0
                             ORDER BY value DESC) q1
                         ON d.`agent` = q1.`agent` AND
                            d.`value` = q1.`value`) q2,
                   (SELECT @rank := 0) r
     WHERE age <= 30
GROUP BY `agent`
ORDER BY `value` DESC;

END $$
DELIMITER ;

Refactor for Data table changes

DELIMITER $$
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetLeaderboardForStat`(IN `stat_key` VARCHAR(20))
    READS SQL DATA
BEGIN

DROP TABLE IF EXISTS LeaderboardForStat;

CREATE TEMPORARY TABLE LeaderboardForStat AS
SELECT @rank := @rank + 1 AS rank,
       agent,
       date,
       value,
       age
  FROM (SELECT q.agent,
               q.date,
               d.value,
               DATEDIFF(NOW(), q.date) `age`
          FROM (  SELECT agent,
                         MAX(date) `date`
                    FROM Data
                GROUP BY agent) q
                LEFT JOIN Data d ON  q.agent = d.agent AND
                                     q.date = d.date
          WHERE d.stat = stat_key AND
                d.value > 0
       ORDER BY value DESC) q1,
       (SELECT @rank := 0) r
WHERE age <= 30;

END $$
DELIMITER ;


create table MediaSet (
	id serial,

	instance           integer default null references Instance(id) on delete cascade,
	shortname          varchar(255),
	use_cdn            boolean not null default false,
	obfuscate_filename boolean not null default false,

	primary key (id)
);

create index MediaSet_instance_index on MediaSet(instance);
create index MediaSet_shortname_index on MediaSet(shortname);

Add private boolean to media-set to force content-signing

create table MediaSet (
	id serial,

	instance           integer default null references Instance(id) on delete cascade,
	shortname          varchar(255),
	use_cdn            boolean not null default false,
	obfuscate_filename boolean not null default false,
	private            boolean not null default false,

	skin               varchar(50),

	primary key (id)
);

create index MediaSet_instance_index on MediaSet(instance);
create index MediaSet_shortname_index on MediaSet(shortname);


DROP PROCEDURE IF EXISTS selectOrganization;

DELIMITER $$
CREATE PROCEDURE selectOrganization(p_orgid INT UNSIGNED)
BEGIN
	SELECT org.orgid, org.org_name, usr.person_name, usr.email, usr.email_is_verified, org.user_id,
	org.org_website, org.money_url, org.mission, org.abbreviated_name, org.customer_notice, org.customer_contact, org.admin_contact, org.active_ind
	FROM org INNER JOIN app_user usr on usr.user_id = org.user_id WHERE org.orgid = p_orgid;
END
$$

DELIMITER ;

GRANT EXECUTE ON PROCEDURE selectOrganization TO movemusr@localhost;

Add update timestamp to retrieved columns

DROP PROCEDURE IF EXISTS selectOrganization;

DELIMITER $$
CREATE PROCEDURE selectOrganization(p_orgid INT UNSIGNED)
BEGIN
	SELECT org.orgid, org.org_name, usr.person_name, usr.email, usr.email_is_verified, org.user_id, org.update_timestamp,
	org.org_website, org.money_url, org.mission, org.abbreviated_name, org.customer_notice, org.customer_contact, org.admin_contact, org.active_ind
	FROM org INNER JOIN app_user usr on usr.user_id = org.user_id WHERE org.orgid = p_orgid;
END
$$

DELIMITER ;

GRANT EXECUTE ON PROCEDURE selectOrganization TO movemusr@localhost;


ALTER TABLE service
  ADD service_provider_class VARCHAR(255) DEFAULT NULL;
ALTER TABLE plan
  ADD service_provider_class VARCHAR(255) DEFAULT NULL;

Add guard for Flyway migration 1_0_8 (add column service_provider)

DROP PROCEDURE IF EXISTS add_service_provider_to_service_table;
DELIMITER //

CREATE PROCEDURE add_service_provider_to_service_table()
  BEGIN

    SET @col_exists_in_service = 1;
    SELECT 0
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'service'
          AND column_name = 'service_provider_class'
          AND table_schema = database()
    INTO @col_exists_in_service;

    IF @col_exists_in_service = 1 THEN
      ALTER TABLE service
        ADD service_provider_class VARCHAR(255) DEFAULT NULL;
    END IF;

    SET @col_exists_in_plan = 1;
    SELECT 0
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'plan'
          AND column_name = 'service_provider_class'
          AND table_schema = database()
    INTO @col_exists_in_plan;

    IF @col_exists_in_plan = 1 THEN
      ALTER TABLE plan
        ADD service_provider_class VARCHAR(255) DEFAULT NULL;
    END IF;

  END//

DELIMITER ;
CALL add_service_provider_to_service_table();


CREATE TABLE IF NOT EXISTS `queue_default` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `queue` varchar(64) NOT NULL,
  `data` text NOT NULL,
  `status` smallint(1) NOT NULL,
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `scheduled` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `executed` datetime NULL DEFAULT NULL,
  `finished` datetime NULL DEFAULT NULL,
  `message` text DEFAULT NULL,
  `trace` text,
  PRIMARY KEY (`id`),
  KEY `status` (`status`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Use more efficient indices in sample configuration

CREATE TABLE IF NOT EXISTS `queue_default` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `queue` varchar(64) NOT NULL,
  `data` text NOT NULL,
  `status` smallint(1) NOT NULL,
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `scheduled` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `executed` datetime NULL DEFAULT NULL,
  `finished` datetime NULL DEFAULT NULL,
  `message` text DEFAULT NULL,
  `trace` text,
  PRIMARY KEY (`id`),
  KEY `pop` (`status`,`queue`,`scheduled`),
  KEY `prune` (`status`,`queue`,`finished`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS hpo_phenotype;
CREATE TABLE hpo_phenotype
(
    hpo_id character varying(30) COLLATE pg_catalog."C",
    hpo_label text COLLATE pg_catalog."C",
    gene_id integer,
    gene_name character varying(255) COLLATE pg_catalog."C"
);

DROP TABLE IF EXISTS hpo_disease;
CREATE TABLE hpo_disease
(
    disease_id character varying(30) COLLATE pg_catalog."C",
    gene_name character varying(255) COLLATE pg_catalog."C",
    gene_id integer,
    hpo_id character varying(30) COLLATE pg_catalog."C",
    hpo_label text COLLATE pg_catalog."C"
);

COPY hpo_phenotype FROM '/var/regovar/databases/hpo_phenotype.txt' HEADER DELIMITER E'\t' CSV ;
COPY hpo_disease FROM '/var/regovar/databases/hpo_disease.txt' HEADER DELIMITER E'\t' CSV;

Use \COPY instead of COPY

DROP TABLE IF EXISTS hpo_phenotype;
CREATE TABLE hpo_phenotype
(
    hpo_id character varying(30) COLLATE pg_catalog."C",
    hpo_label text COLLATE pg_catalog."C",
    gene_id integer,
    gene_name character varying(255) COLLATE pg_catalog."C"
);

DROP TABLE IF EXISTS hpo_disease;
CREATE TABLE hpo_disease
(
    disease_id character varying(30) COLLATE pg_catalog."C",
    gene_name character varying(255) COLLATE pg_catalog."C",
    gene_id integer,
    hpo_id character varying(30) COLLATE pg_catalog."C",
    hpo_label text COLLATE pg_catalog."C"
);

\COPY hpo_phenotype FROM '/var/regovar/databases/hpo_phenotype.txt' HEADER DELIMITER E'\t' CSV ;
\COPY hpo_disease FROM '/var/regovar/databases/hpo_disease.txt' HEADER DELIMITER E'\t' CSV;


INSERT INTO schema_version(key, extra) values ('003', 'schema version');

-- Fake tmitchel data
-- INSERT INTO identity
--     (provider_url, eppn)
--   VALUES
--     ('https://cetaganda.gpolab.bbn.com:8444/idp/shibboleth',
--      'tmitchel@gpolab.bbn.com');

Add a minimal request rspec.

INSERT INTO schema_version(key, extra) values ('003', 'schema version');

-- Add a request rspec for a single node.
INSERT INTO rspec(name, schema, schema_version, description, rspec)
  VALUES
  ('One compute node', 'GENI', '3', 'Any one compute node.',
   '<?xml version="1.0" encoding="UTF-8"?>
<rspec xmlns="http://www.geni.net/resources/rspec/3"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/request.xsd"
       type="request">
  <node client_id="foo"/>
</rspec>'
);


/* File generated automatically by dynamo */
/* Entity types */
CREATE TABLE entity_type (
  /* the entity type identifier */
  `id` INTEGER  AUTO_INCREMENT,
  /* the entity type name (table name) */
  `name` VARCHAR(127) UNIQUE NOT NULL,
  PRIMARY KEY (`id`)
);
/* Sequence generator */
CREATE TABLE sequence (
  /* the sequence name */
  `name` VARCHAR(127) NOT NULL,
  /* the sequence record version */
  `version` int ,
  /* the sequence value */
  `value` BIGINT ,
  /* the sequence block size */
  `block_size` BIGINT ,
  PRIMARY KEY (`name`)
);
INSERT INTO entity_type (name) VALUES
("entity_type")
,("sequence")
;

Define the ENGINE to use InnoDB for the table and force UTF-8

/* File generated automatically by dynamo */
/* Entity types */
CREATE TABLE entity_type (
  /* the entity type identifier */
  `id` INTEGER  AUTO_INCREMENT,
  /* the entity type name (table name) */
  `name` VARCHAR(127) UNIQUE NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/* Sequence generator */
CREATE TABLE sequence (
  /* the sequence name */
  `name` VARCHAR(127) NOT NULL,
  /* the sequence record version */
  `version` int ,
  /* the sequence value */
  `value` BIGINT ,
  /* the sequence block size */
  `block_size` BIGINT ,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;;
INSERT INTO entity_type (name) VALUES
("entity_type")
,("sequence")
;


# add a study_id column in the tables structural_variation and variation_annotation

ALTER TABLE structural_variation ADD COLUMN study_id int(10);
ALTER TABLE structural_variation ADD INDEX study_idx (study_id);

ALTER TABLE variation_annotation ADD COLUMN local_study_id int(10);
ALTER TABLE variation_annotation ADD INDEX local_study_idx (local_study_id);

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL,'patch', 'patch_61_62_f.sql|add a study_id column in the tables structural_variation and variation_annotation');

Drop the local_stable_id column in the variation_annotation table

# add a study_id column in the tables structural_variation and variation_annotation

ALTER TABLE structural_variation ADD COLUMN study_id int(10);
ALTER TABLE structural_variation ADD INDEX study_idx (study_id);

ALTER TABLE variation_annotation ADD COLUMN local_study_id int(10);
ALTER TABLE variation_annotation ADD INDEX local_study_idx (local_study_id);
ALTER TABLE variation_annotation DROP COLUMN local_stable_id;

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL,'patch', 'patch_61_62_f.sql|add a study_id column in the tables structural_variation and variation_annotation');


ALTER TABLE `AlarmVO`  ADD COLUMN `type` varchar(32) NOT NULL;

CREATE TABLE IF NOT EXISTS `V2VConversionCacheVO` (
    `id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT,
    `conversionHostUuid` varchar(32) NOT NULL,
    `srcVmUrl` varchar(255) NOT NULL,
    `installPath` varchar(255) NOT NULL,
    `deviceId` int unsigned NOT NULL,
    `virtualSize` bigint unsigned NOT NULL,
    `actualSize` bigint unsigned NOT NULL,
    `bootMode` varchar(64) DEFAULT NULL,
    `lastOpDate` timestamp ON UPDATE CURRENT_TIMESTAMP,
    `createDate` timestamp,
    PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Set up default value to alarm type

ALTER TABLE `AlarmVO`  ADD COLUMN `type` varchar(32) NOT NULL;
UPDATE `AlarmVO` SET `type` = 'Any';

CREATE TABLE IF NOT EXISTS `V2VConversionCacheVO` (
    `id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT,
    `conversionHostUuid` varchar(32) NOT NULL,
    `srcVmUrl` varchar(255) NOT NULL,
    `installPath` varchar(255) NOT NULL,
    `deviceId` int unsigned NOT NULL,
    `virtualSize` bigint unsigned NOT NULL,
    `actualSize` bigint unsigned NOT NULL,
    `bootMode` varchar(64) DEFAULT NULL,
    `lastOpDate` timestamp ON UPDATE CURRENT_TIMESTAMP,
    `createDate` timestamp,
    PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


create table plm_log_api
(
  id            bigint       not null primary key,
  date          datetime    not null,
  method        varchar(255) not null,
  status_code   SMALLINT not null,
  body_request  mediumtext   null,
  body_response mediumtext   null,
  apiName           varchar(255) not null,
  url           TEXT not null
);

create table plm_log_header
(
  id         bigint       not null primary key,
  id_log_api bigint       not null,
  type       varchar(255) not null,
  name      varchar(255) not null,
  value      TEXT not null,
  constraint plm_log_header_plm_log_api_id_fk
    foreign key (id_log_api) references plm_log_api (id)
);

INSERT INTO PLM_ROLE_PERMISSION VALUES(1, 'MANAGE_API_LOGS');

Fix mysql log api script

create table plm_log_api
(
    `id`            bigint       not null primary key,
    `date`          datetime    not null,
    `method`        varchar(255) not null,
    `status_code`   SMALLINT not null,
    `body_request`  mediumtext   null,
    `body_response` mediumtext   null,
    `apiName`           varchar(255) not null,
    `url`           TEXT not null
);

create table plm_log_header
(
    `id`         bigint       not null primary key,
    `id_log_api` bigint       not null,
    `type`       varchar(255) not null,
    `name`      varchar(255) not null,
    `value`      TEXT not null,
    constraint plm_log_header_plm_log_api_id_fk
        foreign key (id_log_api) references plm_log_api (id)
);

INSERT INTO PLM_ROLE_PERMISSION VALUES(1, 'MANAGE_API_LOGS');


SELECT
    ogc_fid AS t_id,
    ST_Multi(wkb_geometry) AS geometrie,
    zcode,
    zcode_text,
    gem_bfs,
    exact,
    bearbeiter,
    verifiziert,
    datum
FROM
    digizone.zonenplan
WHERE
    archive = 0
AND
    gem_bfs NOT IN (SELECT DISTINCT t_datasetname::integer FROM arp_npl.nutzungsplanung_grundnutzung) --Jene Gemeinden ausschliessen, welche die Nutzungsplanung schon abgeschlossen haben!
;

Select auf Schema arp_npl geändert

SELECT
    ogc_fid AS t_id,
    ST_Multi(wkb_geometry) AS geometrie,
    zcode,
    zcode_text,
    gem_bfs,
    exact,
    bearbeiter,
    verifiziert,
    datum
FROM
    digizone.zonenplan
WHERE
    archive = 0
AND
    gem_bfs NOT IN (2405,2408,2457,2473,2474,2476,2498,2501,2502,2580,2613,2614,2615)
;


SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
--

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

--
-- PostgreSQL database dump complete
--

SET search_path TO "$user", public;

Remove postgres 9.6 specific settings

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
--

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

--
-- PostgreSQL database dump complete
--

SET search_path TO "$user", public;


metadata    :name        => "rkhunter",
            :description => "Utility to assist in the update/management of rkhunter",
            :author      => "Drew A. Blessing <drew.blessing@mac.com>",
            :license     => "MIT",
            :version     => "0.1",
            :url         => "http://github.com/dblessing/mcollective-rkhunter"
            :timeout     => 15

action "propupd", :description => "Runs rkhunter with the --propupd flag" do
  output  :status,
          :description  => "The status from the rkhunter command",
          :display_as   => "Command Status",
          :default      => "unknown status"

  output  :out,
          :description  => "The output from the rkhunter command",
          :display_as   => "Command Output",
          :default      => "unknown output"

  output  :err,
          :description  => "The error output from the rkhunter command",
          :display_as   => "Command Error Output",
          :default      => "unknown error output"
end

Fix syntax error in DDL

metadata    :name        => "rkhunter",
            :description => "Utility to assist in the update/management of rkhunter",
            :author      => "Drew A. Blessing <drew.blessing@mac.com>",
            :license     => "MIT",
            :version     => "0.1",
            :url         => "http://github.com/dblessing/mcollective-rkhunter",
            :timeout     => 15

action "propupd", :description => "Runs rkhunter with the --propupd flag" do
  output  :status,
          :description  => "The status from the rkhunter command",
          :display_as   => "Command Status",
          :default      => "unknown status"

  output  :out,
          :description  => "The output from the rkhunter command",
          :display_as   => "Command Output",
          :default      => "unknown output"

  output  :err,
          :description  => "The error output from the rkhunter command",
          :display_as   => "Command Error Output",
          :default      => "unknown error output"
end


CREATE TABLE projects (
	id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(64) NOT NULL,
	active BOOLEAN,
	modified_time BIGINT NOT NULL,
	create_time BIGINT NOT NULL,
	version INT,
	last_modified_by VARCHAR(64) NOT NULL,
	description VARCHAR(255),
	enc_type TINYINT,
	settings_blob LONGBLOB,
	UNIQUE INDEX project_id (id)
);

CREATE INDEX project_name ON projects(name);

Update default char limit for project description

CREATE TABLE projects (
	id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(64) NOT NULL,
	active BOOLEAN,
	modified_time BIGINT NOT NULL,
	create_time BIGINT NOT NULL,
	version INT,
	last_modified_by VARCHAR(64) NOT NULL,
	description VARCHAR(2048),
	enc_type TINYINT,
	settings_blob LONGBLOB,
	UNIQUE INDEX project_id (id)
);

CREATE INDEX project_name ON projects(name);


DO $$
BEGIN

--------------------------------------------------------------------------------
-- planet_osm_line
--------------------------------------------------------------------------------

-- indexes on existing columns
PERFORM mz_create_partial_index_if_not_exists('planet_osm_line_waterway', 'planet_osm_line', 'waterway', 'waterway IS NOT NULL');

-- indexes on functions
CREATE INDEX planet_osm_line_road_level_index ON planet_osm_line(mz_calculate_road_level(highway, railway, aeroway)) WHERE mz_calculate_road_level(highway, railway, aeroway) IS NOT NULL;
CREATE INDEX planet_osm_line_admin_boundaries_index ON planet_osm_line(boundary) WHERE boundary='administrative';
CREATE INDEX planet_osm_line_road_level_index ON planet_osm_line(mz_calculate_road_level(highway, railway, aeroway, tags->'network')) WHERE mz_calculate_road_level(highway, railway, aeroway, tags->'network') IS NOT NULL;
CREATE INDEX planet_osm_line_transit_level_index ON planet_osm_line(mz_calculate_transit_level(route)) WHERE mz_calculate_transit_level(route) IS NOT NULL;

END $$;

Remove previous road level index

DO $$
BEGIN

--------------------------------------------------------------------------------
-- planet_osm_line
--------------------------------------------------------------------------------

-- indexes on existing columns
PERFORM mz_create_partial_index_if_not_exists('planet_osm_line_waterway', 'planet_osm_line', 'waterway', 'waterway IS NOT NULL');

-- indexes on functions
CREATE INDEX planet_osm_line_admin_boundaries_index ON planet_osm_line(boundary) WHERE boundary='administrative';
CREATE INDEX planet_osm_line_road_level_index ON planet_osm_line(mz_calculate_road_level(highway, railway, aeroway, tags->'network')) WHERE mz_calculate_road_level(highway, railway, aeroway, tags->'network') IS NOT NULL;
CREATE INDEX planet_osm_line_transit_level_index ON planet_osm_line(mz_calculate_transit_level(route)) WHERE mz_calculate_transit_level(route) IS NOT NULL;

END $$;


COPY(
  SELECT urs_taxid,
    xref.taxid as taxid,
    gene || '|' || external_id || '|' || gene_synonym || '|' || optional_id  as external_id,
    description,
    seq_version,
    region_start,
    region_stop,
    rsr.chromosome,
    strand,
    rna_type,
    COALESCE(seq_short, seq_long) as seq
  FROM rnc_accessions
  JOIN xref
  ON xref.ac = rnc_accessions.accession

  JOIN rna
  ON xref.upi = rna.upi

  JOIN rnc_accession_sequence_region rasr
  ON rasr.accession = xref.ac

  JOIN rnc_sequence_regions rsr
  ON rsr.id = region_id

  ) TO STDOUT CSV HEADER

Add assembly id for expression atlas lookup dump

COPY(
  SELECT urs_taxid,
    xref.taxid as taxid,
    gene || '|' || external_id || '|' || gene_synonym || '|' || optional_id  as external_id,
    description,
    seq_version,
    assembly_id,
    region_start,
    region_stop,
    rsr.chromosome,
    strand,
    rna_type,
    COALESCE(seq_short, seq_long) as seq
  FROM rnc_accessions
  JOIN xref
  ON xref.ac = rnc_accessions.accession

  JOIN rna
  ON xref.upi = rna.upi

  JOIN rnc_accession_sequence_region rasr
  ON rasr.accession = xref.ac

  JOIN rnc_sequence_regions rsr
  ON rsr.id = region_id

  ) TO STDOUT CSV HEADER


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

Mark build key as not null

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

  key VARCHAR(100) NOT NULL,

  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 DATABASE IF NOT EXISTS `laika-db`;

CREATE TABLE IF NOT EXISTS `feature`(
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `created_at` DATE NOT NULL,
  PRIMARY KEY (id),
  UNIQUE (name));

CREATE TABLE IF NOT EXISTS `environment` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `created_at` DATE NOT NULL,
  PRIMARY KEY (id),
  UNIQUE (name));

CREATE TABLE IF NOT EXISTS `feature_status` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `feature_id` INT NOT NULL,
  `environment_id` INT NOT NULL,
  `enabled` BOOLEAN NOT NULL,
  `created_at` DATE NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (feature_id)
    REFERENCES feature(id),
  FOREIGN KEY (environment_id)
    REFERENCES environment(id));

CREATE TABLE IF NOT EXISTS `feature_status_history` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `feature_id` INT NOT NULL,
  `environment_id` INT NOT NULL,
  `feature_status_id` INT NOT NULL,
  `enabled` BOOLEAN NOT NULL,
  `created_at` DATE NOT NULL,
  `timestamp` DATE NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (feature_id)
    REFERENCES feature(id),
  FOREIGN KEY (environment_id)
    REFERENCES environment(id),
  FOREIGN KEY (feature_status_id)
    REFERENCES feature_status(id));

Add whitespace to SQL schema

CREATE DATABASE IF NOT EXISTS `laika-db`;

CREATE TABLE IF NOT EXISTS `feature`(
  `id`         INT NOT NULL AUTO_INCREMENT,
  `name`       VARCHAR(255) NOT NULL,
  `created_at` DATE NOT NULL,

  PRIMARY KEY (id),
  UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS `environment` (
  `id`         INT NOT NULL AUTO_INCREMENT,
  `name`       VARCHAR(255) NOT NULL,
  `created_at` DATE NOT NULL,

  PRIMARY KEY (id),
  UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS `feature_status` (
  `id`             INT NOT NULL AUTO_INCREMENT,
  `feature_id`     INT NOT NULL,
  `environment_id` INT NOT NULL,
  `enabled`        BOOLEAN NOT NULL,
  `created_at`     DATE NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (feature_id) REFERENCES feature(id),
  FOREIGN KEY (environment_id) REFERENCES environment(id)
);

CREATE TABLE IF NOT EXISTS `feature_status_history` (
  `id`                INT NOT NULL AUTO_INCREMENT,
  `feature_id`        INT NOT NULL,
  `environment_id`    INT NOT NULL,
  `feature_status_id` INT NOT NULL,
  `enabled`           BOOLEAN NOT NULL,
  `created_at`        DATE NOT NULL,
  `timestamp`         DATE NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (feature_id) REFERENCES feature(id),
  FOREIGN KEY (environment_id) REFERENCES environment(id),
  FOREIGN KEY (feature_status_id) REFERENCES feature_status(id)
);


CREATE TABLE propertystorage
    id SERIAL NOT NULL,
    path VARCHAR(1024) NOT NULL,
    name VARCHAR(100) NOT NULL,
    value TEXT
);

ALTER TABLE ONLY propertystorage
    ADD CONSTRAINT propertystorage_pkey PRIMARY KEY (id);

CREATE UNIQUE INDEX propertystorage_ukey
    ON propertystorage (path, name);

Fix a typo in PostgreSQL files

CREATE TABLE propertystorage (
    id SERIAL NOT NULL,
    path VARCHAR(1024) NOT NULL,
    name VARCHAR(100) NOT NULL,
    value TEXT
);

ALTER TABLE ONLY propertystorage
    ADD CONSTRAINT propertystorage_pkey PRIMARY KEY (id);

CREATE UNIQUE INDEX propertystorage_ukey
    ON propertystorage (path, name);


# Add index on the source_id column in variation_feature and phenotype_feature
ALTER TABLE variation_feature ADD key source_idx (source_id);
ALTER TABLE phenotype_feature ADD key source_idx (source_id);

#patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_75_76_d.sql|Add index on the source_id column in variation_feature and phenotype_feature');

Fix patch number in meta

# Add index on the source_id column in variation_feature and phenotype_feature
ALTER TABLE variation_feature ADD key source_idx (source_id);
ALTER TABLE phenotype_feature ADD key source_idx (source_id);

#patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_75_76_f.sql|Add index on the source_id column in variation_feature and phenotype_feature');


delete from configuration_settings where key = 'RNR_HIDE_SKIPPED_PRODUCTS';

INSERT INTO configuration_settings (key, name, groupname, description, value, valueType,displayOrder)
  values ('RNR_HIDE_SKIPPED_PRODUCTS', 'Hide Skipped Products', 'R & R Options', '','true',  'BOOL', 50);

Add a configuration Option for hide non full supply tab.

delete from configuration_settings where key = 'RNR_HIDE_SKIPPED_PRODUCTS';

INSERT INTO configuration_settings (key, name, groupname, description, value, valueType,displayOrder)
  values ('RNR_HIDE_SKIPPED_PRODUCTS', 'Hide Skipped Products', 'R & R Options', '','true',  'BOOLEAN', 50);

delete from configuration_settings where key = 'RNR_HIDE_NON_FULL_SUPPLY_TAB';

INSERT INTO configuration_settings (key, name, groupname, description, value, valueType,displayOrder)
  values ('RNR_HIDE_NON_FULL_SUPPLY_TAB', 'Hide Non Full Supply Tab', 'R & R Options', '','false',  'BOOLEAN', 51);


# --- !Ups

CREATE TABLE user (
  id INT(11) NOT NULL AUTO_INCREMENT,
  username VARCHAR(255) NOT NULL,
  password VARCHAR(255) NOT NULL,
  admin BOOLEAN NOT NULL,
  PRIMARY KEY (id)
);

# --- !Downs

DROP TABLE IF EXISTS user;

Add UNIQUE constraint on username

# --- !Ups

CREATE TABLE user (
  id INT(11) NOT NULL AUTO_INCREMENT,
  username VARCHAR(255) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  admin BOOLEAN NOT NULL,
  PRIMARY KEY (id)
);

# --- !Downs

DROP TABLE IF EXISTS user;


# 07/07/2009

ALTER TABLE osdial_xfer_log ADD uniqueid VARCHAR(20) NOT NULL default '';
CREATE INDEX uniqueid ON osdial_xfer_log (uniqueid);

ALTER TABLE osdial_agent_log ADD uniqueid VARCHAR(20) NOT NULL default '';
CREATE INDEX uniqueid ON osdial_agent_log (uniqueid);

ALTER TABLE recording_log ADD uniqueid VARCHAR(20) NOT NULL default '';
CREATE INDEX uniqueid ON recording_log (uniqueid);

UPDATE system_settings SET version='2.1.4.028';
UPDATE system_settings SET last_update_check=DATE_SUB(NOW(), INTERVAL 1 DAY);

Fix for live_agents with not enough closer_campaign space.

# 07/07/2009

ALTER TABLE osdial_xfer_log ADD uniqueid VARCHAR(20) NOT NULL default '';
CREATE INDEX uniqueid ON osdial_xfer_log (uniqueid);

ALTER TABLE osdial_agent_log ADD uniqueid VARCHAR(20) NOT NULL default '';
CREATE INDEX uniqueid ON osdial_agent_log (uniqueid);

ALTER TABLE recording_log ADD uniqueid VARCHAR(20) NOT NULL default '';
CREATE INDEX uniqueid ON recording_log (uniqueid);

ALTER TABLE osdial_live_agents MODIFY closer_campaigns TEXT default '';

UPDATE system_settings SET version='2.1.4.028';
UPDATE system_settings SET last_update_check=DATE_SUB(NOW(), INTERVAL 1 DAY);


CREATE TABLE IF NOT EXISTS minecord (
    id      TINYINT(1) PRIMARY KEY CHECK (id = 0),
    version TINYINT(2) NOT NULL
);
INSERT INTO minecord (id, version)
    VALUES(0, 1);
CREATE TABLE IF NOT EXISTS guild (
    id       BIGINT(20) PRIMARY KEY NOT NULL,
    banned   TINYINT(1) NOT NULL DEFAULT 0,
    prefix   TEXT(8),
    lang     TEXT(5),
    use_menu TINYINT(1)
);
CREATE TABLE IF NOT EXISTS channel (
    id       BIGINT(20) PRIMARY KEY NOT NULL,
    guild_id BIGINT(20) NOT NULL UNIQUE,
    banned   TINYINT(1) NOT NULL DEFAULT 0,
    prefix   TEXT(8),
    lang     TEXT(5),
    use_menu TINYINT(1)
);
CREATE TABLE IF NOT EXISTS user (
    id       BIGINT(20) PRIMARY KEY NOT NULL,
    banned   TINYINT(1) NOT NULL DEFAULT 0,
    elevated TINYINT(1) NOT NULL DEFAULT 0,
    prefix   TEXT(8),
    lang     TEXT(5),
    use_menu TINYINT(1)
);

Make guild_id in channel table not unique

CREATE TABLE IF NOT EXISTS minecord (
    id      TINYINT(1) PRIMARY KEY CHECK (id = 0),
    version TINYINT(2) NOT NULL
);
INSERT INTO minecord (id, version)
    VALUES(0, 1);
CREATE TABLE IF NOT EXISTS guild (
    id       BIGINT(20) PRIMARY KEY NOT NULL,
    banned   TINYINT(1) NOT NULL DEFAULT 0,
    prefix   TEXT(8),
    lang     TEXT(5),
    use_menu TINYINT(1)
);
CREATE TABLE IF NOT EXISTS channel (
    id       BIGINT(20) PRIMARY KEY NOT NULL,
    guild_id BIGINT(20) NOT NULL,
    banned   TINYINT(1) NOT NULL DEFAULT 0,
    prefix   TEXT(8),
    lang     TEXT(5),
    use_menu TINYINT(1)
);
CREATE TABLE IF NOT EXISTS user (
    id       BIGINT(20) PRIMARY KEY NOT NULL,
    banned   TINYINT(1) NOT NULL DEFAULT 0,
    elevated TINYINT(1) NOT NULL DEFAULT 0,
    prefix   TEXT(8),
    lang     TEXT(5),
    use_menu TINYINT(1)
);


--
-- Database: `idgeneration`
--
CREATE SCHEMA `idgeneration` ;
-- --------------------------------------------------------

--
-- Table structure for table `id_generated`
--
USE idgeneration;

CREATE TABLE IF NOT EXISTS `id_generated` (
`Id` bigint(20) unsigned NOT NULL,
  `service` varchar(250) DEFAULT NULL,
  `hashValue` binary(250) DEFAULT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10001 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `id_generated`
--
ALTER TABLE `id_generated`
 ADD PRIMARY KEY (`Id`), ADD UNIQUE KEY `hashValue` (`hashValue`);

--
-- AUTO_INCREMENT for dumped tables
--
-- AUTO_INCREMENT for table `id_generated`
--
ALTER TABLE `id_generated`
MODIFY `Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10001;

Change back to varchar because otherwise cannot compare

--
-- Database: `idgeneration`
--
CREATE SCHEMA `idgeneration` ;
-- --------------------------------------------------------

--
-- Table structure for table `id_generated`
--
USE idgeneration;

CREATE TABLE IF NOT EXISTS `id_generated` (
`Id` bigint(20) unsigned NOT NULL,
  `service` varchar(250) DEFAULT NULL,
  `hashValue` varchar(250) DEFAULT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10001 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `id_generated`
--
ALTER TABLE `id_generated`
 ADD PRIMARY KEY (`Id`), ADD UNIQUE KEY `hashValue` (`hashValue`);

--
-- AUTO_INCREMENT for dumped tables
--
-- AUTO_INCREMENT for table `id_generated`
--
ALTER TABLE `id_generated`
MODIFY `Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10001;


CREATE TABLE IF NOT EXISTS `uploadfile` (
  `id` int(11) NOT NULL auto_increment,
  `filename` varchar(255) NOT NULL,
  `filesize` int(11) NOT NULL DEFAULT '0',
  `sha1sum` varchar(40) NOT NULL,
  PRIMARY KEY(`id`),
  KEY `sha1sum` (`sha1sum`)
);

CREATE TABLE IF NOT EXISTS `build2uploadfile` (
  `fileid` bigint(11) NOT NULL,
  `buildid` bigint(11) NOT NULL,
  KEY `fileid` (`fileid`),
  KEY `buildid` (`buildid`)
);

Add new table to upgrade SQL file

CREATE TABLE IF NOT EXISTS `uploadfile` (
  `id` int(11) NOT NULL auto_increment,
  `filename` varchar(255) NOT NULL,
  `filesize` int(11) NOT NULL DEFAULT '0',
  `sha1sum` varchar(40) NOT NULL,
  PRIMARY KEY(`id`),
  KEY `sha1sum` (`sha1sum`)
);

CREATE TABLE IF NOT EXISTS `build2uploadfile` (
  `fileid` bigint(11) NOT NULL,
  `buildid` bigint(11) NOT NULL,
  KEY `fileid` (`fileid`),
  KEY `buildid` (`buildid`)
);

CREATE TABLE IF NOT EXISTS client_jobschedule2submission (
  scheduleid bigint(20) NOT NULL,
  submissionid bigint(11) NOT NULL,
  PRIMARY KEY (`submissionid`),
  UNIQUE KEY `scheduleid` (`scheduleid`)
);


--DROP FUNCTION o2p_aggregate_polygon_relation(bigint);
CREATE OR REPLACE FUNCTION o2p_aggregate_polygon_relation(
  bigint
) returns geometry[] AS $o2p_aggregate_polygon_relation$
DECLARE
  v_rel_id ALIAS for $1;
  v_polygons geometry[];
BEGIN

SELECT
  array_agg(polygon) polygons
FROM (
  SELECT
    CASE
      WHEN holes[1] IS NULL THEN st_makepolygon(shell)
      ELSE st_makepolygon(shell, holes)
    END polygon
  FROM (
    SELECT
      outside.line AS shell,
      array_agg(inside.line) AS holes
    FROM (
      SELECT
        geom as line,
        role
      FROM
        (SELECT ST_ForceRHR(unnest(geom)) as geom, unnest(role) as role from o2p_aggregate_relation(v_rel_id)) out_sub
      WHERE
        role != 'inner' AND
        ST_IsClosed(geom)
    ) outside LEFT OUTER JOIN (
      SELECT
        geom as line,
        role
      FROM
        (SELECT ST_Reverse(ST_ForceRHR(unnest(geom))) as geom, unnest(role) as role from o2p_aggregate_relation(v_rel_id)) in_sub
      WHERE
        role = 'inner' AND
        ST_IsClosed(geom)
    ) inside ON ST_CONTAINS(st_makepolygon(outside.line), inside.line)
  GROUP BY
    outside.line) polys
) poly_array
INTO
  v_polygons;

RETURN v_polygons;
END;
$o2p_aggregate_polygon_relation$ LANGUAGE plpgsql;

Clean up the polygon holes

--DROP FUNCTION o2p_aggregate_polygon_relation(bigint);
CREATE OR REPLACE FUNCTION o2p_aggregate_polygon_relation(
  bigint
) returns geometry[] AS $o2p_aggregate_polygon_relation$
DECLARE
  v_rel_id ALIAS for $1;
  v_polygons geometry[];
BEGIN

SELECT
  array_agg(polygon) polygons
FROM (
  SELECT
    ST_ForceRHR(CASE
      WHEN holes[1] IS NULL THEN st_makepolygon(shell)
      ELSE st_makepolygon(shell, holes)
    END) polygon
  FROM (
    SELECT
      outside.line AS shell,
      array_agg(inside.line) AS holes
    FROM (
      SELECT
        geom as line,
        role
      FROM
        (SELECT unnest(geom) as geom, unnest(role) as role from o2p_aggregate_relation(v_rel_id)) out_sub
      WHERE
        role != 'inner' AND
        ST_IsClosed(geom)
    ) outside LEFT OUTER JOIN (
      SELECT
        geom as line,
        role
      FROM
        (SELECT unnest(geom) as geom, unnest(role) as role from o2p_aggregate_relation(v_rel_id)) in_sub
      WHERE
        role = 'inner' AND
        ST_IsClosed(geom)
    ) inside ON ST_CONTAINS(st_makepolygon(outside.line), inside.line)
  GROUP BY
    outside.line) polys
) poly_array
INTO
  v_polygons;

RETURN v_polygons;
END;
$o2p_aggregate_polygon_relation$ LANGUAGE plpgsql;


-- Script: Tabelas
-- Learn: http://www.postgresql.org/docs/current/static/sql-createtable.html

CREATE TABLE usuarios (
    username VARCHAR(30) PRIMARY KEY,
    password VARCHAR(30)
);

CREATE TABLE batalha_usuario (
    username VARCHAR(30) PRIMARY KEY FOREIGN KEY,
    password VARCHAR(30) PRIMARY KEY FOREIGN KEY
);

CREATE TABLE batalha_usuario (
    id INT PRIMARY KEY,
    turnos INT,
    vencedor VARCHAR(30) FOREIGN KEY
);

Add references oldstyle for easy understandings

-- Script: Tabelas
-- Learn: http://www.postgresql.org/docs/current/static/sql-createtable.html

CREATE TABLE USUARIOS (
    username VARCHAR(30) PRIMARY KEY,
    password VARCHAR(30)
);

CREATE TABLE BATALHA_USUARIO (
    username VARCHAR(30),
    batalha_id INT,
    PRIMARY KEY (username, password),
    FOREIGN KEY batalha_id REFERENCES BATALHAS(id)
    FOREIGN KEY username REFERENCES USUARIOS(username)
);

CREATE TABLE BATALHAS (
    id INT,
    turnos INT,
    vencedor VARCHAR(30),
    PRIMARY KEY (id),
    FOREIGN KEY vencedor REFERENCES USUARIOS(username)
);

delete from USUARIOS;
delete from BATALHA_USUARIO;
delete from BATALHAS;


metadata    :name        => "rkhunter",
            :description => "Utility to assist in the update/management of rkhunter",
            :author      => "Drew A. Blessing <drew.blessing@mac.com>",
            :license     => "MIT",
            :version     => "0.1",
            :url         => "http://github.com/dblessing/mcollective-rkhunter"
            :timeout     => 15,

action "propupd", :description => "Runs rkhunter with the --propupd flag" do
  output  :status,
          :description  => "The status from the rkhunter command",
          :display_as   => "Command Status",
          :default      => "unknown status"

  output  :out,
          :description  => "The output from the rkhunter command",
          :display_as   => "Command Output",
          :default      => "unknown output"

  output  :err,
          :description  => "The error output from the rkhunter command",
          :display_as   => "Command Error Output",
          :default      => "unknown error output"
end

Fix syntax error in DDL

metadata    :name        => "rkhunter",
            :description => "Utility to assist in the update/management of rkhunter",
            :author      => "Drew A. Blessing <drew.blessing@mac.com>",
            :license     => "MIT",
            :version     => "0.1",
            :url         => "http://github.com/dblessing/mcollective-rkhunter"
            :timeout     => 15

action "propupd", :description => "Runs rkhunter with the --propupd flag" do
  output  :status,
          :description  => "The status from the rkhunter command",
          :display_as   => "Command Status",
          :default      => "unknown status"

  output  :out,
          :description  => "The output from the rkhunter command",
          :display_as   => "Command Output",
          :default      => "unknown output"

  output  :err,
          :description  => "The error output from the rkhunter command",
          :display_as   => "Command Error Output",
          :default      => "unknown error output"
end


-- Copyright (C) 2017 Alaa Masoud
-- See the LICENSE file in the project root for more information.

CREATE TABLE IF NOT EXISTS log(
	id TEXT NOT NULL PRIMARY KEY,
	message TEXT NOT NULL,
	messageTemplate TEXT NOT NULL,
	level VARCHAR(64) NOT NULL,
	timestamp DATETIME NOT NULL,
	exception TEXT NULL
);

CREATE INDEX IF NOT EXISTS log_message_idx ON log(message);

CREATE INDEX IF NOT EXISTS log_level_idx ON log(level);

CREATE INDEX IF NOT EXISTS log_timestamp_idx ON log(timestamp);

CREATE INDEX IF NOT EXISTS log_exception_idx ON log(exception);

CREATE TABLE IF NOT EXISTS log_property(
	id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
	logId TEXT NOT NULL,
	name TEXT NOT NULL,
	value TEXT NULL,
	FOREIGN KEY(logId) REFERENCES log(id)
);

CREATE INDEX IF NOT EXISTS log_property_logId_idx ON log_property(logId);

CREATE INDEX IF NOT EXISTS log_property_name_idx ON log_property(name);

CREATE INDEX IF NOT EXISTS log_property_value_idx ON log_property(value);

CREATE TABLE IF NOT EXISTS log_query(
	id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
	name VARCHAR(255) NOT NULL,
	query TEXT NOT NULL
);

Add index on log_query(name) column

-- Copyright (C) 2017 Alaa Masoud
-- See the LICENSE file in the project root for more information.

CREATE TABLE IF NOT EXISTS log(
	id TEXT NOT NULL PRIMARY KEY,
	message TEXT NOT NULL,
	messageTemplate TEXT NOT NULL,
	level VARCHAR(64) NOT NULL,
	timestamp DATETIME NOT NULL,
	exception TEXT NULL
);

CREATE INDEX IF NOT EXISTS log_message_idx ON log(message);

CREATE INDEX IF NOT EXISTS log_level_idx ON log(level);

CREATE INDEX IF NOT EXISTS log_timestamp_idx ON log(timestamp);

CREATE INDEX IF NOT EXISTS log_exception_idx ON log(exception);

CREATE TABLE IF NOT EXISTS log_property(
	id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
	logId TEXT NOT NULL,
	name TEXT NOT NULL,
	value TEXT NULL,
	FOREIGN KEY(logId) REFERENCES log(id)
);

CREATE INDEX IF NOT EXISTS log_property_logId_idx ON log_property(logId);

CREATE INDEX IF NOT EXISTS log_property_name_idx ON log_property(name);

CREATE INDEX IF NOT EXISTS log_property_value_idx ON log_property(value);

CREATE TABLE IF NOT EXISTS log_query(
	id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
	name VARCHAR(255) NOT NULL,
	query TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS log_query_name_idx ON log_query(name);


CREATE OR REPLACE VIEW documentsuggestion AS
  SELECT DISTINCT
    d."id"      AS "document",
    "user"."id" AS "researchEntity"
  FROM "user"
    JOIN "document" d
      ON d."authorsStr" ILIKE '%' :: TEXT || "user"."surname" || '%' :: TEXT
    JOIN (SELECT a.document
          FROM "authorship" a
          WHERE a."researchEntity" IS NOT NULL
          UNION
          SELECT a.document
          FROM "authorshipgroup" a
          WHERE a."researchEntity" IS NOT NULL
         ) AS verified
      ON d.id = verified.document
  WHERE
    d."id" NOT IN (
      SELECT "authorship"."document"
      FROM "authorship"
      WHERE "authorship"."researchEntity" = "user"."id"
    )
    AND
    d."id" NOT IN (
      SELECT "discarded"."document"
      FROM "discarded"
      WHERE "discarded"."researchEntity" = "user"."id"
    )

Document suggestions for the user now proper handle the surname word buondaries when searching.

CREATE OR REPLACE VIEW documentsuggestion AS
  SELECT DISTINCT
    d.id AS "document",
    u.id AS "researchEntity"
  FROM "user" u
    JOIN "document" d
      ON d."authorsStr" ~* ('\y' || u.surname ||'\y')
    JOIN (SELECT a.document
          FROM "authorship" a
          WHERE a."researchEntity" IS NOT NULL
          UNION
          SELECT a.document
          FROM authorshipgroup a
          WHERE a."researchEntity" IS NOT NULL
         ) AS verified
      ON d.id = verified.document
  WHERE
    d."id" NOT IN (
      SELECT "authorship"."document"
      FROM "authorship"
      WHERE "authorship"."researchEntity" = u.id
    )
    AND
    d."id" NOT IN (
      SELECT "discarded"."document"
      FROM "discarded"
      WHERE "discarded"."researchEntity" = u.id
    )


alter table transcriptions ADD needsReview tinyint(1) NOT NULL DEFAULT '1' after user_id;
update transcriptions set needsReview = 0 where user_id is not null;
update transcriptions t left join sentences s on s.id = t.sentence_id set t.needsReview = 0 where s.lang in ('cmn', 'uzb');

Adjust needsReview for furigana in update

alter table transcriptions ADD needsReview tinyint(1) NOT NULL DEFAULT '1' after user_id;
update transcriptions set needsReview = 0 where user_id is not null;
update transcriptions t left join sentences s on s.id = t.sentence_id set t.needsReview = 0 where s.lang in ('cmn', 'uzb');
update transcriptions t join sentences s on s.id = t.sentence_id and s.text = t.text set needsReview = 0 where s.lang = 'jpn';


BEGIN;

-- add userneed state
ALTER TABLE userneed ADD COLUMN state VARCHAR(255) DEFAULT "ACTIVE";

COMMIT;

Fix SQL error in migration script

BEGIN;

-- add userneed state
ALTER TABLE userneed ADD COLUMN state VARCHAR(255) DEFAULT 'ACTIVE';

COMMIT;


/* Create the database */
-- This is to create a schema inside a database instead of creating a blank database.
-- Postgres does not allow to drop a database within the same connection. Also, postgres does not allow switch database within sql statement.
drop schema if exists snomedct;
create schema if not exists snomedct;
set schema 'snomedct';

Add cascade for dropping snomedct schema.

/* Create the database */
-- This is to create a schema inside a database instead of creating a blank database.
-- Postgres does not allow to drop a database within the same connection.
-- Also, postgres does not allow switch database within sql statement. A new connection must be estabilished for changing database target.
drop schema if exists snomedct cascade;
create schema snomedct;
set schema 'snomedct';


COPY (
  SELECT
    json_build_object(
      'id', todo.id,
      'urs_taxid', todo.urs_taxid,
      'publication_count', counts.publication_count
    )
    FROM search_export_urs todo
    JOIN search_export_publication_counts counts
    ON
      todo.urs = counts.urs
    ORDER by todo.id
) TO STDOUT

Change text mining sql to join on urs_taxid rather than urs

COPY (
  SELECT
    json_build_object(
      'id', todo.id,
      'urs_taxid', todo.urs_taxid,
      'publication_count', counts.publication_count
    )
    FROM search_export_urs todo
    JOIN search_export_publication_counts counts
    ON
      todo.urs_taxid = counts.urs
    ORDER by todo.id
) TO STDOUT


CREATE TABLE play (
  id                 VARCHAR(255) NOT NULL,
  theater            VARCHAR(255) NOT NULL,
  date               VARCHAR(255) NOT NULL,
  reservationEndDate VARCHAR(255) NOT NULL,
  show               VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (theater) REFERENCES theater (id),
  FOREIGN KEY (show) REFERENCES show (id)
);

CREATE TABLE play_price (
  id        VARCHAR(255) NOT NULL,
  name      VARCHAR(255) NOT NULL,
  value     NUMERIC      NOT NULL,
  condition TEXT         NULL,
  play      VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (play) REFERENCES play (id)
)

Correct the script creating tables for plays

CREATE TABLE play (
  id                 VARCHAR(255) NOT NULL,
  theater            VARCHAR(255) NOT NULL,
  date               TIMESTAMP NOT NULL,
  reservationEndDate TIMESTAMP NOT NULL,
  show               VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (theater) REFERENCES theater (id),
  FOREIGN KEY (show) REFERENCES show (id)
);

CREATE TABLE play_price (
  id        VARCHAR(255) NOT NULL,
  name      VARCHAR(255) NOT NULL,
  value     NUMERIC      NOT NULL,
  condition TEXT         NULL,
  play      VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (play) REFERENCES play (id)
)


CREATE TABLE FOLLOWUP_DAILY_SYNTHESIS (
  id                   NUMBER       NOT NULL,
  followup_date        DATE         NOT NULL,
  project_id           NUMBER       NOT NULL,
  sum_effort_done      NUMBER(4,2)  NOT NULL,
  sum_effort_backlog   NUMBER(4,2)  NOT NULL,
  CONSTRAINT fds_pk    PRIMARY KEY (id),
  CONSTRAINT fds_project_fk FOREIGN KEY (project_id) REFERENCES project_filter_configuration (id)
);

CREATE INDEX fds_project_date_idx  on followup_daily_synthesis (project_id,followup_date);

Revert "[TOOLS-572] removing wrong sql line"

ALTER TABLE project_filter_configuration ADD constraint project_fc_pk  PRIMARY KEY(id);

CREATE TABLE FOLLOWUP_DAILY_SYNTHESIS (
  id                   NUMBER       NOT NULL,
  followup_date        DATE         NOT NULL,
  project_id           NUMBER       NOT NULL,
  sum_effort_done      NUMBER(4,2)  NOT NULL,
  sum_effort_backlog   NUMBER(4,2)  NOT NULL,
  CONSTRAINT fds_pk    PRIMARY KEY (id),
  CONSTRAINT fds_project_fk FOREIGN KEY (project_id) REFERENCES project_filter_configuration (id)
);

CREATE INDEX fds_project_date_idx  on followup_daily_synthesis (project_id,followup_date);