COPY (
SELECT
json_build_object(
  'rna_id', anno.rna_id,
  'go_annotations', array_agg(
    json_build_object(
      'go_term_id', anno.ontology_term_id,
      'qualifier', anno.qualifier,
      'go_name', ont.name,
      'assigned_by', anno.assigned_by,
      'pubmed_ids', pubmed.ref_pubmed_id,
      'dois', pubmed.doi
    )
  )
)
FROM go_term_annotations anno
JOIN ontology_terms ont ON ont.ontology_term_id = anno.ontology_term_id
LEFT JOIN go_term_publication_map go_map
ON
  go_map.go_term_annotation_id = anno.go_term_annotation_id
LEFT JOIN ref_pubmed pubmed ON pubmed.ref_pubmed_id = go_map.ref_pubmed_id
GROUP BY anno.rna_id
) TO STDOUT

Drop the go annotation publications

COPY (
SELECT
json_build_object(
  'rna_id', anno.rna_id,
  'go_annotations', array_agg(
    json_build_object(
      'go_term_id', anno.ontology_term_id,
      'qualifier', anno.qualifier,
      'go_name', ont.name,
      'assigned_by', anno.assigned_by
    )
  )
)
FROM go_term_annotations anno
JOIN ontology_terms ont ON ont.ontology_term_id = anno.ontology_term_id
GROUP BY anno.rna_id
) TO STDOUT


-- avoid innocuous NOTICEs about automatic sequence creation
set client_min_messages='WARNING';

-- ----------------------------------------------------------------------
--
-- ----------------------------------------------------------------------
-- Drop the data first, then the type.
DROP TABLE IF EXISTS sa_slice;

create TABLE sa_slice (
  id SERIAL,
  slice_id UUID,
  slice_name VARCHAR,
  project_id VARCHAR,
  expiration TIMESTAMP,
  owner_id UUID,
  slice_urn VARCHAR,
  PRIMARY KEY (id)
);

Add email and certificate to slice table.

-- avoid innocuous NOTICEs about automatic sequence creation
set client_min_messages='WARNING';

-- ----------------------------------------------------------------------
--
-- ----------------------------------------------------------------------
-- Drop the data first, then the type.
DROP TABLE IF EXISTS sa_slice;

create TABLE sa_slice (
  id SERIAL,
  slice_id UUID,
  slice_name VARCHAR,
  project_id VARCHAR,
  expiration TIMESTAMP,
  owner_id UUID,
  slice_urn VARCHAR,
  slice_email VARCHAR,
  certificate VARCHAR,
  PRIMARY KEY (id)
);


-- List of invalid indexes

-- Use it to see invalid indexes list

-- This query doesn't need any additional extensions to be installed
-- (except plpgsql), and doesn't create anything (like views or smth)
-- -- so feel free to use it in your clouds (Heroku, AWS RDS, etc)

-- (Keep in mind, that on replicas, the whole picture of index usage
-- is usually very different from master).

select
    coalesce(nullif(pn.nspname, 'public') || '.', '') || pct.relname as "relation_name",
    pci.relname as index_name,
    pn.nspname as schema_name,
    pct.relname as table_name
from pg_index pidx
join pg_class as pci on pci.oid = pidx.indexrelid
join pg_class as pct on pct.oid = pidx.indrelid
left join pg_namespace pn on pn.oid = pct.relnamespace
where pidx.indisvalid = false;

Drop and revert code added for invalid indexes

-- List of invalid indexes

-- Use it to see invalid indexes list

-- This query doesn't need any additional extensions to be installed
-- (except plpgsql), and doesn't create anything (like views or smth)
-- -- so feel free to use it in your clouds (Heroku, AWS RDS, etc)

-- (Keep in mind, that on replicas, the whole picture of index usage
-- is usually very different from master).

select
    coalesce(nullif(pn.nspname, 'public') || '.', '') || pct.relname as "relation_name",
    pci.relname as index_name,
    pn.nspname as schema_name,
    pct.relname as table_name,
    format('DROP INDEX CONCURRENTLY %s; -- %s, table %s', pidx.indexrelid::regclass::text, 'Invalid index', pct.relname) as drop_code,
    replace(
      format('%s; -- table %s', pg_get_indexdef(pidx.indexrelid), pct.relname),
      'CREATE INDEX',
      'CREATE INDEX CONCURRENTLY'
    ) as revert_code
from pg_index pidx
join pg_class as pci on pci.oid = pidx.indexrelid
join pg_class as pct on pct.oid = pidx.indrelid
left join pg_namespace pn on pn.oid = pct.relnamespace
where pidx.indisvalid = false;


CREATE MATERIALIZED VIEW raw_hashtags_users AS
  SELECT *,
    rank() OVER (ORDER BY edits DESC) edits_rank,
    rank() OVER (ORDER BY buildings DESC) buildings_rank,
    rank() OVER (ORDER BY road_km DESC) road_km_rank,
    rank() OVER (ORDER BY updated_at DESC) updated_at_rank
  FROM (
    SELECT
      raw_changesets_hashtags.hashtag_id,
      raw_changesets.user_id,
      count(id) changesets,
      sum(buildings_added + buildings_modified + roads_added + roads_modified + waterways_added + waterways_modified + pois_added + pois_modified) edits,
      sum(buildings_added + buildings_modified) buildings,
      sum(road_km_added + road_km_modified) road_km,
      max(coalesce(closed_at, created_at)) updated_at
    FROM raw_changesets
    JOIN raw_changesets_hashtags ON raw_changesets_hashtags.changeset_id = raw_changesets.id
    GROUP BY hashtag_id, user_id
  ) AS _;

CREATE UNIQUE INDEX raw_hashtags_users_hashtag_id_user_id ON raw_hashtags_users(hashtag_id, user_id);
CREATE INDEX raw_hashtags_users_hashtag_id ON raw_hashtags_users(hashtag_id);
CREATE INDEX raw_hashtags_users_user_id ON raw_hashtags_users(user_id);

Add roads count for partner leaderboards

CREATE MATERIALIZED VIEW raw_hashtags_users AS
  SELECT *,
    rank() OVER (ORDER BY edits DESC) edits_rank,
    rank() OVER (ORDER BY buildings DESC) buildings_rank,
    rank() OVER (ORDER BY road_km DESC) road_km_rank,
    rank() OVER (ORDER BY updated_at DESC) updated_at_rank
  FROM (
    SELECT
      raw_changesets_hashtags.hashtag_id,
      raw_changesets.user_id,
      count(id) changesets,
      sum(buildings_added + buildings_modified + roads_added + roads_modified + waterways_added + waterways_modified + pois_added + pois_modified) edits,
      sum(buildings_added + buildings_modified) buildings,
      sum(roads_added + roads_modified) roads,
      sum(road_km_added + road_km_modified) road_km,
      max(coalesce(closed_at, created_at)) updated_at
    FROM raw_changesets
    JOIN raw_changesets_hashtags ON raw_changesets_hashtags.changeset_id = raw_changesets.id
    GROUP BY hashtag_id, user_id
  ) AS _;

CREATE UNIQUE INDEX raw_hashtags_users_hashtag_id_user_id ON raw_hashtags_users(hashtag_id, user_id);
CREATE INDEX raw_hashtags_users_hashtag_id ON raw_hashtags_users(hashtag_id);
CREATE INDEX raw_hashtags_users_user_id ON raw_hashtags_users(user_id);


IF OBJECT_ID(N'[Membership].[HasUser]', N'P') IS NULL
	EXEC('CREATE PROCEDURE [Membership].[HasUser] AS SELECT 1')
GO

ALTER PROCEDURE [Membership].[HasUser]
(
	@Email nvarchar(254)
)
AS
BEGIN
	SELECT 1 FROM [Membership].[User]
	WHERE Email = @Email
END
GO

GRANT EXECUTE ON [Membership].[HasUser] TO [ro]
GO

Return user id in has user stored procedure.

IF OBJECT_ID(N'[Membership].[HasUser]', N'P') IS NULL
	EXEC('CREATE PROCEDURE [Membership].[HasUser] AS SELECT 1')
GO

ALTER PROCEDURE [Membership].[HasUser]
(
	@Email nvarchar(254)
)
AS
BEGIN
	SELECT Id FROM [Membership].[User]
	WHERE Email = @Email
END
GO

GRANT EXECUTE ON [Membership].[HasUser] TO [ro]
GO


"CREATE TRIGGER IF NOT EXISTS update_playlist_order_on_insert AFTER INSERT"
" ON PlaylistMediaRelation"
" WHEN new.position IS NOT NULL"
" BEGIN "
    "UPDATE PlaylistMediaRelation SET position = position + 1"
    " WHERE playlist_id = new.playlist_id"
    " AND position >= new.position"
    " AND media_id != new.media_id;"
" END",

"CREATE TRIGGER IF NOT EXISTS update_playlist_order_on_delete AFTER DELETE"
" ON PlaylistMediaRelation"
" BEGIN "
    "UPDATE PlaylistMediaRelation SET position = position - 1"
    " WHERE playlist_id = old.playlist_id"
    " AND position > old.position"
    " AND media_id != old.media_id;"
" END",

"CREATE TRIGGER IF NOT EXISTS insert_playlist_fts AFTER INSERT ON "
+ Playlist::Table::Name +
" BEGIN"
    " INSERT INTO " + Playlist::Table::Name + "Fts(rowid, name) VALUES(new.id_playlist, new.name);"
" END",

"CREATE TRIGGER IF NOT EXISTS update_playlist_fts AFTER UPDATE OF name"
" ON " + Playlist::Table::Name +
" BEGIN"
    " UPDATE " + Playlist::Table::Name + "Fts SET name = new.name WHERE rowid = new.id_playlist;"
" END",

"CREATE TRIGGER IF NOT EXISTS delete_playlist_fts BEFORE DELETE ON "
+ Playlist::Table::Name +
" BEGIN"
" DELETE FROM " + Playlist::Table::Name + "Fts WHERE rowid = old.id_playlist;"
" END",

Fix & simplify insert/delete triggers

"CREATE TRIGGER IF NOT EXISTS update_playlist_order_on_insert AFTER INSERT"
" ON PlaylistMediaRelation"
" WHEN new.position IS NOT NULL"
" BEGIN "
    "UPDATE PlaylistMediaRelation SET position = position + 1"
    " WHERE playlist_id = new.playlist_id"
    " AND position >= new.position"
    " AND rowid != new.rowid;"
" END",

"CREATE TRIGGER IF NOT EXISTS update_playlist_order_on_delete AFTER DELETE"
" ON PlaylistMediaRelation"
" BEGIN "
    "UPDATE PlaylistMediaRelation SET position = position - 1"
    " WHERE playlist_id = old.playlist_id"
    " AND position > old.position;"
" END",

"CREATE TRIGGER IF NOT EXISTS insert_playlist_fts AFTER INSERT ON "
+ Playlist::Table::Name +
" BEGIN"
    " INSERT INTO " + Playlist::Table::Name + "Fts(rowid, name) VALUES(new.id_playlist, new.name);"
" END",

"CREATE TRIGGER IF NOT EXISTS update_playlist_fts AFTER UPDATE OF name"
" ON " + Playlist::Table::Name +
" BEGIN"
    " UPDATE " + Playlist::Table::Name + "Fts SET name = new.name WHERE rowid = new.id_playlist;"
" END",

"CREATE TRIGGER IF NOT EXISTS delete_playlist_fts BEFORE DELETE ON "
+ Playlist::Table::Name +
" BEGIN"
" DELETE FROM " + Playlist::Table::Name + "Fts WHERE rowid = old.id_playlist;"
" END",


create index idx_activity_user_id on activity (user_id);
create index idx_activity_detail_activity_id on activity_detail (activity_id);

delete from activity_detail where activity_id in (select id from activity where user_id in (select id from "user" where name = 'etalabot'));
delete from activity where user_id in (select id from "user" where name = 'etalabot');

drop index idx_activity_user_id;
drop index idx_activity_detail_activity_id;

Comment out the creation and removal of PostgreSQL index, because they require a PostgreSQL vacuum step.

--create index idx_activity_user_id on activity (user_id);
--create index idx_activity_detail_activity_id on activity_detail (activity_id);

delete from activity_detail where activity_id in (select id from activity where user_id in (select id from "user" where name = 'etalabot'));
delete from activity where user_id in (select id from "user" where name = 'etalabot');

--drop index idx_activity_user_id;
--drop index idx_activity_detail_activity_id;


BEGIN;

SELECT plan(2);

SELECT trend_directory.create_view_trend_store(
    'test-source', 'test-type', '900',
    'SELECT 1 x, 2 y'
);

SELECT
    is(x, 1)
FROM trend."test-source_test-type_qtr";

SELECT
    trend_directory.alter_view(view_trend_store, 'SELECT 2 x, 3 y')
FROM trend_directory.view_trend_store
WHERE view_trend_store::text = 'test-source_test-type_qtr';

SELECT
    is(x, 2)
FROM trend."test-source_test-type_qtr";

SELECT * FROM finish();
ROLLBACK;

Use more appropriate cast to type 'name'

BEGIN;

SELECT plan(2);

SELECT trend_directory.create_view_trend_store(
    'test-source', 'test-type', '900',
    'SELECT 1 x, 2 y'
);

SELECT
    is(x, 1)
FROM trend."test-source_test-type_qtr";

SELECT
    trend_directory.alter_view(view_trend_store, 'SELECT 2 x, 3 y')
FROM trend_directory.view_trend_store
WHERE view_trend_store::name = 'test-source_test-type_qtr';

SELECT
    is(x, 2)
FROM trend."test-source_test-type_qtr";

SELECT * FROM finish();
ROLLBACK;


/*
	Copyright 2015 Cisco,

	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 regional_geo_blocking tinyint(1) not null;

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

Support regional geoblocking (Ops), revise copyright header

/*
	Copyright 2015 Cisco Systems, Inc.

	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 regional_geo_blocking tinyint(1) not null;

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


UPDATE TABLE `hero_primaryhero` SET `gradient_color`='#008787' WHERE `gradient_color`='#068989';
UPDATE TABLE `hero_primaryhero` SET `gradient_color`='#C60084' WHERE `gradient_color`='#C60184';
UPDATE TABLE `hero_primaryhero` SET `gradient_color`='#722291' WHERE `gradient_color`='#712290';
UPDATE TABLE `hero_primaryhero` SET `gradient_color`='#592ACB' WHERE `gradient_color`='#582ACB';

Fix typos in migration 1211

UPDATE `hero_primaryhero` SET `gradient_color`='#008787' WHERE `gradient_color`='#068989';
UPDATE `hero_primaryhero` SET `gradient_color`='#C60084' WHERE `gradient_color`='#C60184';
UPDATE `hero_primaryhero` SET `gradient_color`='#722291' WHERE `gradient_color`='#712290';
UPDATE `hero_primaryhero` SET `gradient_color`='#592ACB' WHERE `gradient_color`='#582ACB';


CREATE TABLE IF NOT EXISTS `f_cache` (
`cache_key` int(11) NOT NULL ,
`text_value` MEDIUMTEXT NOT NULL ,
PRIMARY KEY ( `cache_key` )
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;

Add insert_time field on f_cache table

CREATE TABLE IF NOT EXISTS `f_cache` (
`cache_key` int(11) NOT NULL ,
`text_value` MEDIUMTEXT NOT NULL ,
`insert_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ( `cache_key` )
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;


-- custom CSS
ALTER TABLE users ADD COLUMN customcss text NOT NULL DEFAULT '';

-- patch flag
ALTER TABLE releases_rev ADD COLUMN patch BOOLEAN NOT NULL DEFAULT FALSE;

Convert all existing patch media in the DB to the new patch flag

-- custom CSS
ALTER TABLE users ADD COLUMN customcss text NOT NULL DEFAULT '';

-- patch flag
ALTER TABLE releases_rev ADD COLUMN patch BOOLEAN NOT NULL DEFAULT FALSE;
UPDATE releases_rev SET patch = TRUE
  WHERE EXISTS(SELECT 1 FROM releases_media rm WHERE rm.rid = id AND rm.medium = 'pa ');
DELETE FROM releases_media WHERE medium = 'pa ';


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

/**
@header patch_84_85_h.sql - tba
@desc   tba
*/

-- patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_84_85_h.sql|tba.');

Patch to store file types along with the files.

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

/**
@header patch_84_85_h.sql - Store file types.
@desc   Store file types along with the files.
*/

alter table dbfile_registry add column file_type ENUM('BAM','BAMCOV','BIGBED','BIGWIG','VCF','CRAM');
alter table dbfile_registry drop primary key, add primary key(table_id, table_name, file_type);

-- We currently only have bigwig in there.
update dbfile_registry set file_type='BIGWIG';

-- patch identifier
insert into meta (species_id, meta_key, meta_value) values (null, 'patch', 'patch_84_85_h.sql|Store file types along with the files.');


/*
	SQL Server Generic Table Types
		Must be run on the database you're targeting before you use the generic table values
		e.g. before parameters.AddWithValue("@ids", IEnumerable<object> ids, ID_FIELD);
	Authors:
		Josh Keegan 20/09/2016
*/

CREATE TYPE dbo.TableType_Generic_Int AS TABLE
(
	v int NOT NULL
);

CREATE TYPE dbo.TableType_Generic_Int_Nullable AS TABLE
(
	v int NULL
);

CREATE TYPE dbo.TableType_Generic_BigInt AS TABLE
(
	v bigint NOT NULL
);

CREATE TYPE dbo.TableType_Generic_BigInt_Nullable AS TABLE
(
	v bigint NULL
);

CREATE TYPE dbo.TableType_Generic_VarChar_Nullable AS TABLE
(
	v varchar(max) NULL
);

CREATE TYPE dbo.TableType_Generic_VarChar AS TABLE
(
	v varchar(max) NOT NULL
);

Drop existing table types if they already exist when creating

/*
	SQL Server Generic Table Types
		Must be run on the database you're targeting before you use the generic table values
		e.g. before parameters.AddWithValue("@ids", IEnumerable<object> ids, ID_FIELD);
	Authors:
		Josh Keegan 20/09/2016
*/

IF TYPE_ID('dbo.TableType_Generic_Int') IS NOT NULL
	DROP TYPE dbo.TableType_Generic_Int;
CREATE TYPE dbo.TableType_Generic_Int AS TABLE
(
	v int NOT NULL
);

IF TYPE_ID('dbo.TableType_Generic_Int_Nullable') IS NOT NULL
	DROP TYPE dbo.TableType_Generic_Int_Nullable;
CREATE TYPE dbo.TableType_Generic_Int_Nullable AS TABLE
(
	v int NULL
);

IF TYPE_ID('dbo.TableType_Generic_BigInt') IS NOT NULL
	DROP TYPE dbo.TableType_Generic_BigInt;
CREATE TYPE dbo.TableType_Generic_BigInt AS TABLE
(
	v bigint NOT NULL
);

IF TYPE_ID('dbo.TableType_Generic_BigInt_Nullable') IS NOT NULL
	DROP TYPE dbo.TableType_Generic_BigInt_Nullable;
CREATE TYPE dbo.TableType_Generic_BigInt_Nullable AS TABLE
(
	v bigint NULL
);

IF TYPE_ID('dbo.TableType_Generic_VarChar_Nullable') IS NOT NULL
	DROP TYPE dbo.TableType_Generic_VarChar_Nullable;
CREATE TYPE dbo.TableType_Generic_VarChar_Nullable AS TABLE
(
	v varchar(max) NULL
);

IF TYPE_ID('dbo.TableType_Generic_VarChar') IS NOT NULL
	DROP TYPE dbo.TableType_Generic_VarChar;
CREATE TYPE dbo.TableType_Generic_VarChar AS TABLE
(
	v varchar(max) NOT NULL
);


# --- !Ups

ALTER TABLE document ADD md_ark varchar(255);
ALTER TABLE document ADD d_ark varchar(255);
ALTER TABLE document ADD mets_d_ark varchar(255);

# Likely syntax required to make sensible URL fields - Downs needed.
# ALTER TABLE document ALTER COLUMN landing_page_url TYPE text;
# ALTER TABLE document ALTER COLUMN document_url TYPE text;

# --- !Downs

ALTER TABLE document DROP md_ark;
ALTER TABLE document DROP d_ark;
ALTER TABLE document DROP mets_d_ark;

Fix broken syntax by implementing rather than using comments.

# --- !Ups

ALTER TABLE document ADD md_ark varchar(255);
ALTER TABLE document ADD d_ark varchar(255);
ALTER TABLE document ADD mets_d_ark varchar(255);

ALTER TABLE document ALTER COLUMN landing_page_url TYPE text;
ALTER TABLE document ALTER COLUMN document_url TYPE text;

# --- !Downs

ALTER TABLE document DROP md_ark;
ALTER TABLE document DROP d_ark;
ALTER TABLE document DROP mets_d_ark;

ALTER TABLE document ALTER COLUMN landing_page_url TYPE varchar(255);
ALTER TABLE document ALTER COLUMN document_url TYPE varchar(255);


ALTER TABLE `reviews` DROP FOREIGN KEY `client_data_id_refs_id_c0e106c0`, DROP KEY `reviews_1446fb9b`, DROP COLUMN `client_data_id`;
ALTER TABLE `stats_contributions` DROP FOREIGN KEY `client_data_id_refs_id_d3f47e0e`, DROP KEY `stats_contributions_1446fb9b`, DROP COLUMN `client_data_id`;
DROP TABLE IF EXISTS `client_data`;

Fix migration 933 removing client_data: some envs have different FK names

DROP PROCEDURE IF EXISTS drop_client_data_cols;

DELIMITER ';;'

CREATE PROCEDURE drop_client_data_cols() BEGIN
    /* drop the client_data foreign keys - since the fks have 2 possible names each, look them up in information_schema */
    /* reviews */
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema = (SELECT DATABASE()) AND table_name = 'reviews' AND constraint_name = 'client_data_id_refs_id_c0e106c0') THEN
        ALTER TABLE reviews DROP FOREIGN KEY client_data_id_refs_id_c0e106c0;
    END IF;

    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema = (SELECT DATABASE()) AND table_name = 'reviews' AND constraint_name = 'client_data_id_refs_id_d160c5ba') THEN
        ALTER TABLE reviews DROP FOREIGN KEY client_data_id_refs_id_d160c5ba;
    END IF;

    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = (SELECT DATABASE()) AND table_name = 'reviews' AND column_name = 'client_data_id') THEN
        ALTER TABLE reviews DROP COLUMN client_data_id;
    END IF;

    /* stats_contributions */
    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema = (SELECT DATABASE()) AND table_name = 'stats_contributions' AND constraint_name = 'client_data_id_refs_id_d3f47e0e') THEN
        ALTER TABLE stats_contributions DROP FOREIGN KEY client_data_id_refs_id_d3f47e0e;
    END IF;

    IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema = (SELECT DATABASE()) AND table_name = 'stats_contributions' AND constraint_name = 'client_data_id_refs_id_c8ef1728') THEN
        ALTER TABLE stats_contributions DROP FOREIGN KEY client_data_id_refs_id_c8ef1728;
    END IF;

    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = (SELECT DATABASE()) AND table_name = 'stats_contributions' AND column_name = 'client_data_id') THEN
        ALTER TABLE stats_contributions DROP COLUMN client_data_id;
    END IF;
END;;

DELIMITER ';'

CALL drop_client_data_cols();

DROP TABLE IF EXISTS `client_data`;

DROP PROCEDURE IF EXISTS drop_client_data_cols;


/* contrib/json_plperl/jsonb_plperl--1.0.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION jsonb_plperlu" to load this file. \quit

CREATE FUNCTION jsonb_to_plperl(val internal) RETURNS internal
LANGUAGE C STRICT IMMUTABLE
AS 'MODULE_PATHNAME';

CREATE FUNCTION plperl_to_jsonb(val internal) RETURNS jsonb
LANGUAGE C STRICT IMMUTABLE
AS 'MODULE_PATHNAME';

CREATE TRANSFORM FOR jsonb LANGUAGE plperlu (
    FROM SQL WITH FUNCTION jsonb_to_plperl(internal),
    TO SQL WITH FUNCTION plperl_to_jsonb(internal)
);

COMMENT ON TRANSFORM FOR jsonb LANGUAGE plperlu IS 'transform between jsonb and Perl';

Fix clashing function names between jsonb_plperl and jsonb_plperlu

/* contrib/json_plperl/jsonb_plperl--1.0.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION jsonb_plperlu" to load this file. \quit

CREATE FUNCTION jsonb_to_plperlu(val internal) RETURNS internal
LANGUAGE C STRICT IMMUTABLE
AS 'MODULE_PATHNAME', 'jsonb_to_plperl';

CREATE FUNCTION plperlu_to_jsonb(val internal) RETURNS jsonb
LANGUAGE C STRICT IMMUTABLE
AS 'MODULE_PATHNAME', 'plperl_to_jsonb';

CREATE TRANSFORM FOR jsonb LANGUAGE plperlu (
    FROM SQL WITH FUNCTION jsonb_to_plperlu(internal),
    TO SQL WITH FUNCTION plperlu_to_jsonb(internal)
);

COMMENT ON TRANSFORM FOR jsonb LANGUAGE plperlu IS 'transform between jsonb and Perl';


CREATE TABLE IF NOT EXISTS pages(
  id INTEGER PRIMARY KEY,
  name TEXT,
  popularity INTEGER
);

.mode csv
.separator "\t"
.import /dev/stdin pages

CREATE INDEX pages_name_popularity_index ON pages(name, popularity DESC);

Add COLLATE NOCASE to pages name index

CREATE TABLE IF NOT EXISTS pages(
  id INTEGER PRIMARY KEY,
  name TEXT,
  popularity INTEGER
);

.mode csv
.separator "\t"
.import /dev/stdin pages

CREATE INDEX pages_name_popularity_index ON pages(name COLLATE NOCASE, popularity DESC);


CREATE TABLE jobs (
	id CHAR(80) PRIMARY KEY,
	name CHAR(128),
	user CHAR(20),
	status CHAR(20),
	queue CHAR(80),
	submit BIGINT,
	start BIGINT,
	finish BIGINT,

	memory_seconds INTEGER,
	cpu_seconds INTEGER,
	map INTEGER,
	reduce INTEGER,

	changed TIMESTAMP,

	INDEX (user)
);

CREATE TABLE subjobs (
	id CHAR(80) PRIMARY KEY,
	jobid CHAR(80),
	nodeid INTEGER,
	state CHAR(20),
	type CHAR(20),
	start BIGINT,
	finish BIGINT,

	INDEX(id),
	INDEX(jobid)
);

CREATE TABLE jobnodes (
	jobid CHAR(80) NOT NULL,
	nodeid INTEGER,
	elapsed INTEGER,
	map INTEGER,
	reduce INTEGER,

	INDEX (jobid)
);

CREATE TABLE nodes (
	id INTEGER PRIMARY KEY AUTO_INCREMENT,
	host VARCHAR(256),

	INDEX(host)
);

DELIMITER //

CREATE TRIGGER bi_measure BEFORE INSERT ON jobs
FOR EACH ROW BEGIN
  SET NEW.changed = NOW();
END; //

CREATE TRIGGER bu_measure BEFORE UPDATE ON jobs
FOR EACH ROW BEGIN
  SET NEW.changed = NOW();
END; //

DELIMITER ;

Index also time columns in subjobs.

CREATE TABLE jobs (
	id CHAR(80) PRIMARY KEY,
	name CHAR(128),
	user CHAR(20),
	status CHAR(20),
	queue CHAR(80),
	submit BIGINT,
	start BIGINT,
	finish BIGINT,

	memory_seconds INTEGER,
	cpu_seconds INTEGER,
	map INTEGER,
	reduce INTEGER,

	changed TIMESTAMP,

	INDEX (user)
);

CREATE TABLE subjobs (
	id CHAR(80) PRIMARY KEY,
	jobid CHAR(80),
	nodeid INTEGER,
	state CHAR(20),
	type CHAR(20),
	start BIGINT,
	finish BIGINT,

	INDEX(id),
	INDEX(jobid),
	INDEX(start),
	INDEX(finish)
);

CREATE TABLE jobnodes (
	jobid CHAR(80) NOT NULL,
	nodeid INTEGER,
	elapsed INTEGER,
	map INTEGER,
	reduce INTEGER,

	INDEX (jobid)
);

CREATE TABLE nodes (
	id INTEGER PRIMARY KEY AUTO_INCREMENT,
	host VARCHAR(256),

	INDEX(host)
);

DELIMITER //

CREATE TRIGGER bi_measure BEFORE INSERT ON jobs
FOR EACH ROW BEGIN
  SET NEW.changed = NOW();
END; //

CREATE TRIGGER bu_measure BEFORE UPDATE ON jobs
FOR EACH ROW BEGIN
  SET NEW.changed = NOW();
END; //

DELIMITER ;


CREATE TABLE invoice_parent_children (
    record_id serial unique,
    id varchar(36) NOT NULL,
    parent_invoice_id varchar(36) NOT NULL,
    child_invoice_id varchar(36) NOT NULL,
    child_account_id varchar(36) NOT NULL,
    created_by varchar(50) NOT NULL,
    created_date datetime NOT NULL,
    account_record_id bigint /*! unsigned */ not null,
    tenant_record_id bigint /*! unsigned */ not null default 0,
    PRIMARY KEY(record_id)
) /*! CHARACTER SET utf8 COLLATE utf8_bin */;
CREATE UNIQUE INDEX invoice_parent_children_id ON invoice_parent_children(id);
CREATE INDEX invoice_parent_children_invoice_id ON invoice_parent_children(parent_invoice_id);
CREATE INDEX invoice_parent_children_tenant_account_record_id ON invoice_parent_children(tenant_record_id, account_record_id);

alter table invoice_items add column child_account_id varchar(36) after account_id;
alter table invoices add column  parent_invoice bool NOT NULL DEFAULT FALSE after migrated;

Modify migration script for HA to remove NON NULL constraints on start_date and target_date

CREATE TABLE invoice_parent_children (
    record_id serial unique,
    id varchar(36) NOT NULL,
    parent_invoice_id varchar(36) NOT NULL,
    child_invoice_id varchar(36) NOT NULL,
    child_account_id varchar(36) NOT NULL,
    created_by varchar(50) NOT NULL,
    created_date datetime NOT NULL,
    account_record_id bigint /*! unsigned */ not null,
    tenant_record_id bigint /*! unsigned */ not null default 0,
    PRIMARY KEY(record_id)
) /*! CHARACTER SET utf8 COLLATE utf8_bin */;
CREATE UNIQUE INDEX invoice_parent_children_id ON invoice_parent_children(id);
CREATE INDEX invoice_parent_children_invoice_id ON invoice_parent_children(parent_invoice_id);
CREATE INDEX invoice_parent_children_tenant_account_record_id ON invoice_parent_children(tenant_record_id, account_record_id);

alter table invoice_items add column child_account_id varchar(36) after account_id;
alter table invoice_items modify start_date date;

alter table invoices add column  parent_invoice bool NOT NULL DEFAULT FALSE after migrated;
alter table invoices modify target_date date;


-- Convert schema '/root/GADS/share/migrations/_source/deploy/2/001-auto.yml' to '/root/GADS/share/migrations/_source/deploy/3/001-auto.yml':;

;
BEGIN;

;
ALTER TABLE user ADD COLUMN stylesheet text;

;

COMMIT;

Fix quote user table name for PG

-- Convert schema '/root/GADS/share/migrations/_source/deploy/2/001-auto.yml' to '/root/GADS/share/migrations/_source/deploy/3/001-auto.yml':;

;
BEGIN;

;
ALTER TABLE "user" ADD COLUMN stylesheet text;

;

COMMIT;


ALTER TABLE waffle_flag_mkt_users
    ALGORITHM=INPLACE,
    CHANGE COLUMN user_id userprofile_id int(11) unsigned NOT NULL;

Revert "oops, tested this on the wrong mysql version"

ALTER TABLE waffle_flag_mkt_users
    CHANGE COLUMN user_id userprofile_id int(11) unsigned NOT NULL;


-- Run the basic-template against the default tables using two table joins

{@insert_vals = "_value[id], _value[string], _value[int32], _value[float]"}
{@from_tables = "_table, _table"}
{@col_type = "int"}
{@cmp_type = "_value[int:0,100]"}
{@id_col = "ID"}
{@assign_col = "NUM"}
{@assign_type = "_value[int:0,100]"}
{@set_op = "_pick[<options=UNION,INTERSECT,EXCEPT>]"}
{@optional_all = "_pick[<options=,ALL>]"}

<union-template.sql>

Fix the sqlcoverage merge issue between the union and numeric functions changesets.

-- Run the basic-template against the default tables using two table joins

{@insert_vals = "_id, _value[string], _value[int32], _value[float]"}
{@from_tables = "_table, _table"}
{@col_type = "int"}
{@cmp_type = "_value[int:0,100]"}
{@id_col = "ID"}
{@assign_col = "NUM"}
{@assign_type = "_value[int:0,100]"}
{@set_op = "_pick[<options=UNION,INTERSECT,EXCEPT>]"}
{@optional_all = "_pick[<options=,ALL>]"}

<union-template.sql>


CREATE TABLE wof_neighbourhood (
  wof_id BIGINT UNIQUE NOT NULL,
  placetype SMALLINT NOT NULL,
  name TEXT NOT NULL,
  hash TEXT NOT NULL,
  n_photos INTEGER,
  area BIGINT,
  label_position geometry(Point, 900913) NOT NULL,
  geometry geometry(Geometry, 900913) NOT NULL
);

CREATE INDEX wof_neighbourhood_wof_id_index ON wof_neighbourhood(wof_id);
CREATE INDEX wof_neighbourhood_label_position_index ON wof_neighbourhood USING GIST(label_position);
CREATE INDEX wof_neighbourhood_geometry_index ON wof_neighbourhood USING GIST(geometry);

CREATE TABLE wof_neighbourhood_placetype (
  placetype_code SMALLINT UNIQUE NOT NULL,
  placetype_string text NOT NULL
);

INSERT INTO wof_neighbourhood_placetype VALUES
  (1, 'neighbourhood'),
  (2, 'microhood'),
  (3, 'macrohood');

Update WOF tables to use primary keys

CREATE TABLE wof_neighbourhood_placetype (
  placetype_code SMALLINT PRIMARY KEY,
  placetype_string text NOT NULL
);

INSERT INTO wof_neighbourhood_placetype VALUES
  (1, 'neighbourhood'),
  (2, 'microhood'),
  (3, 'macrohood');

CREATE TABLE wof_neighbourhood (
  wof_id BIGINT PRIMARY KEY,
  placetype SMALLINT NOT NULL REFERENCES wof_neighbourhood_placetype(placetype_code),
  name TEXT NOT NULL,
  hash TEXT NOT NULL,
  n_photos INTEGER,
  area BIGINT,
  label_position geometry(Point, 900913) NOT NULL,
  geometry geometry(Geometry, 900913) NOT NULL
);

CREATE INDEX wof_neighbourhood_label_position_index ON wof_neighbourhood USING GIST(label_position);


CREATE SEQUENCE SEQ_IACUC_PROTOCOL_CUSTOM_DATA_ID INCREMENT BY 1 START WITH 1 NOCACHE
/

Implement Custom Data for IACUC

CREATE SEQUENCE SEQ_IACUC_PROT_CUST_DATA_ID INCREMENT BY 1 START WITH 1 NOCACHE
/


-- ALTER TABLE account_user DROP image, DROP nickname, DROP bio, DROP website;

ALTER TABLE account_user ADD COLUMN profile varchar(50);
ALTER TABLE account_user ADD COLUMN institution varchar(256);
ALTER TABLE account_user ADD COLUMN occupation varchar(150);
ALTER TABLE account_user ADD COLUMN birthday DATE;
ALTER TABLE account_user ADD COLUMN uf varchar(2);
ALTER TABLE account_user ADD COLUMN city varchar(256);

Remove comments of the migrations.


# Display all entries that have the issue
SELECT * FROM xcolab.members_Member WHERE members_Member.status IS  NULL ;

# Display entries without a createDate and the replacement createDate (DryRun)
SELECT T1.id_, T1.screenName, T1.createDate, T2.createDate
FROM xcolab.members_Member T1, xcolab.members_Member T2
WHERE T1.id_ = T2.id_ + 1 AND T1.createDate IS NULL;

# Update status fields
UPDATE xcolab.members_Member SET status=0 WHERE status IS NULL;
UPDATE xcolab.members_Member SET autoRegisteredMemberStatus=0 WHERE autoRegisteredMemberStatus IS NULL;

# Update createDate field
UPDATE xcolab.members_Member AS T1
JOIN xcolab.members_Member AS T2
    ON T1.id_-1 = T2.id_
SET T1.createDate = T2.createDate
WHERE T1.createDate IS NULL;

Comment out unnecessary SQL scripts

# Display all entries that have the issue
# SELECT * FROM xcolab.members_Member WHERE members_Member.status IS  NULL ;

# Display entries without a createDate and the replacement createDate (DryRun)
# SELECT T1.id_, T1.screenName, T1.createDate, T2.createDate
# FROM xcolab.members_Member T1, xcolab.members_Member T2
# WHERE T1.id_ = T2.id_ + 1 AND T1.createDate IS NULL;

# Update status fields
UPDATE xcolab.members_Member SET status=0 WHERE status IS NULL;
UPDATE xcolab.members_Member SET autoRegisteredMemberStatus=0 WHERE autoRegisteredMemberStatus IS NULL;

# Update createDate field
UPDATE xcolab.members_Member AS T1
JOIN xcolab.members_Member AS T2
    ON T1.id_-1 = T2.id_
SET T1.createDate = T2.createDate
WHERE T1.createDate IS NULL;


--
-- Public geocoder API function
--
-- These are the only ones with permissions to publicuser role
-- and should also be the only ones with SECURITY DEFINER

CREATE OR REPLACE FUNCTION cdb_geocoder_client.geocode_admin0_polygons(country_name text)
RETURNS Geometry AS $$
DECLARE
  ret Geometry;
BEGIN
  SELECT cdb_geocoder_client._geocode_admin0_polygons(session_user, txid_current(), country_name) INTO ret;
  RETURN ret;
END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;

-- TODO: review all permissions stuff [I'd explicitly grant permissions to the public functions]

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

CREATE OR REPLACE FUNCTION cdb_geocoder_client._geocode_admin0_polygons(user_id name, tx_id bigint, country_name text)
RETURNS Geometry AS $$
  -- TODO check if we can move the config to its own function
  CONNECT cdb_geocoder_client._server_conn_str();
  SELECT cdb_geocoder_server.geocode_admin0_polygons(user_id, tx_id, country_name);
$$ LANGUAGE plproxy;

Remove TODO that no longer applies

--
-- Public geocoder API function
--
-- These are the only ones with permissions to publicuser role
-- and should also be the only ones with SECURITY DEFINER

CREATE OR REPLACE FUNCTION cdb_geocoder_client.geocode_admin0_polygons(country_name text)
RETURNS Geometry AS $$
DECLARE
  ret Geometry;
BEGIN
  SELECT cdb_geocoder_client._geocode_admin0_polygons(session_user, txid_current(), country_name) INTO ret;
  RETURN ret;
END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;

-- TODO: review all permissions stuff [I'd explicitly grant permissions to the public functions]

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

CREATE OR REPLACE FUNCTION cdb_geocoder_client._geocode_admin0_polygons(user_id name, tx_id bigint, country_name text)
RETURNS Geometry AS $$
  CONNECT cdb_geocoder_client._server_conn_str();
  SELECT cdb_geocoder_server.geocode_admin0_polygons(user_id, tx_id, country_name);
$$ LANGUAGE plproxy;


SELECT c.id,
  name,
  iso_3 AS iso,
  COALESCE(percentage_pa_land_cover,0) AS percentage_pa_land_cover,
  COALESCE(percentage_pa_marine_cover,0) AS percentage_pa_marine_cover,
  COALESCE(percentage_well_connected,0) AS percentage_well_connected,
  COALESCE(percentage_importance,0) AS percentage_importance,
  COALESCE(pame_percentage_pa_land_cover,0) AS pame_percentage_pa_land_cover,
  COALESCE(pame_percentage_pa_marine_cover,0) AS pame_percentage_pa_marine_cover,
  'country' AS obj_type
FROM countries c
JOIN country_statistics cs ON cs.country_id = c.id
LEFT JOIN pame_statistics ps ON ps.country_id = c.id

UNION

SELECT region_id AS id,
  r.name,
  r.iso AS iso,
  percentage_pa_land_cover,
  percentage_pa_marine_cover,
  percentage_well_connected,
  percentage_importance,
  pame_percentage_pa_land_cover,
  pame_percentage_pa_marine_cover,
  'region' AS obj_type
FROM regional_statistics_view rs
JOIN regions r ON rs.region_id = r.id

Add region_id to fetch regions and countries from search

SELECT c.id,
  c.region_id AS region_id,
  name,
  iso_3 AS iso,
  COALESCE(percentage_pa_land_cover,0) AS percentage_pa_land_cover,
  COALESCE(percentage_pa_marine_cover,0) AS percentage_pa_marine_cover,
  COALESCE(percentage_well_connected,0) AS percentage_well_connected,
  COALESCE(percentage_importance,0) AS percentage_importance,
  COALESCE(pame_percentage_pa_land_cover,0) AS pame_percentage_pa_land_cover,
  COALESCE(pame_percentage_pa_marine_cover,0) AS pame_percentage_pa_marine_cover,
  'country' AS obj_type
FROM countries c
JOIN country_statistics cs ON cs.country_id = c.id
LEFT JOIN pame_statistics ps ON ps.country_id = c.id

UNION

SELECT region_id AS id,
  region_id AS region_id,
  r.name,
  r.iso AS iso,
  percentage_pa_land_cover,
  percentage_pa_marine_cover,
  percentage_well_connected,
  percentage_importance,
  pame_percentage_pa_land_cover,
  pame_percentage_pa_marine_cover,
  'region' AS obj_type
FROM regional_statistics_view rs
JOIN regions r ON rs.region_id = r.id


CREATE OR REPLACE VIEW ObjectsImagesExport AS
SELECT
    NULLIF(TRIM(o.ItemType), '')                              AS ItemType,
    NULLIF(TRIM(i.Accession_Full_ID), '')                     AS Accession_Full_ID,
    NULLIF(TRIM(o.ItemName), '')                              AS ItemName,
    NULLIF(TRIM(ObjectsImagesID), '')                         AS ObjectsImagesID,
    NULLIF(TRIM(CONCAT_WS('/', REPLACE(
        REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ImageFilePath, '\\', '/'), 'Y:/', ''), 'Y:', ''),
                        '//Foyer/c/Images Mosaic/', ''), '//Foyer/c/Images Mosaic/', ''),
        '//SERVER1/Images Mosaic/', ''), ImageFileName)), '') AS ImagePath,
    NULLIF(TRIM(ImageDescription), '')                        AS ImageDescription,
    NULLIF(TRIM(DisplayImage), '')                            AS DisplayImage,
    NULLIF(TRIM(DefaultImageIndicator), '')                   AS DefaultImageIndicator

FROM ObjectsImages i
    JOIN Objects o ON (i.Accession_Full_ID = o.Accession_Full_ID)
WHERE TRIM(ImageFileName) <> ''
ORDER BY ObjectsImagesID;

Add default image description for images that do not have a caption

CREATE OR REPLACE VIEW ObjectsImagesExport AS
SELECT
    NULLIF(TRIM(o.ItemType), '')                              AS ItemType,
    NULLIF(TRIM(i.Accession_Full_ID), '')                     AS Accession_Full_ID,
    NULLIF(TRIM(o.ItemName), '')                              AS ItemName,
    NULLIF(TRIM(ObjectsImagesID), '')                         AS ObjectsImagesID,
    NULLIF(TRIM(CONCAT_WS('/', REPLACE(
        REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ImageFilePath, '\\', '/'), 'Y:/', ''), 'Y:', ''),
                        '//Foyer/c/Images Mosaic/', ''), '//Foyer/c/Images Mosaic/', ''),
        '//SERVER1/Images Mosaic/', ''), ImageFileName)), '') AS ImagePath,
    IFNULL(NULLIF(TRIM(ImageDescription), ''), TRIM(o.ItemName))                        AS ImageDescription,
    NULLIF(TRIM(DisplayImage), '')                            AS DisplayImage,
    NULLIF(TRIM(DefaultImageIndicator), '')                   AS DefaultImageIndicator

FROM ObjectsImages i
    JOIN Objects o ON (i.Accession_Full_ID = o.Accession_Full_ID)
WHERE TRIM(ImageFileName) <> ''
ORDER BY ObjectsImagesID;


-- ALTER TABLE account_user DROP image, DROP nickname, DROP bio, DROP website;

ALTER TABLE account_user ADD COLUMN profile varchar(50);
ALTER TABLE account_user ADD COLUMN institution varchar(256);
ALTER TABLE account_user ADD COLUMN occupation varchar(150);
ALTER TABLE account_user ADD COLUMN birthday DATE;
ALTER TABLE account_user ADD COLUMN uf varchar(2);
ALTER TABLE account_user ADD COLUMN city varchar(256);

Add column provides in accounts user table.

-- ALTER TABLE account_user DROP image, DROP nickname, DROP bio, DROP website;

ALTER TABLE account_user ADD COLUMN profile varchar(50);
ALTER TABLE account_user ADD COLUMN institution varchar(256);
ALTER TABLE account_user ADD COLUMN occupation varchar(150);
ALTER TABLE account_user ADD COLUMN birthday DATE;
ALTER TABLE account_user ADD COLUMN uf varchar(2);
ALTER TABLE account_user ADD COLUMN city varchar(256);
ALTER TABLE account_user ADD COLUMN provider varchar(50);


CREATE TABLE sessions (
	id SERIAL PRIMARY KEY,
	host VARCHAR(255) NOT NULL,
	port INT NOT NULL,
	session_id VARCHAR(255) NOT NULL,
	protocol VARCHAR(64) NOT NULL,
	title VARCHAR(255) NOT NULL,
	users INT NOT NULL,
	usernames VARCHAR(512) NOT NULL,
	password BOOLEAN NOT NULL,
	nsfm BOOLEAN NOT NULL,
	owner VARCHAR(128) NOT NULL,
	started TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
	last_active TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
	unlisted BOOLEAN NOT NULL DEFAULT FALSE,
	update_key VARCHAR(255) NOT NULL,
	client_ip VARCHAR(64) NOT NULL,
	roomcode VARCHAR(16) DEFAULT NULL CONSTRAINT unique_roomcode UNIQUE,
	private BOOLEA NOT NULL DEFAULT FALSE
);

Fix typo on database creation script

CREATE TABLE sessions (
	id SERIAL PRIMARY KEY,
	host VARCHAR(255) NOT NULL,
	port INT NOT NULL,
	session_id VARCHAR(255) NOT NULL,
	protocol VARCHAR(64) NOT NULL,
	title VARCHAR(255) NOT NULL,
	users INT NOT NULL,
	usernames VARCHAR(512) NOT NULL,
	password BOOLEAN NOT NULL,
	nsfm BOOLEAN NOT NULL,
	owner VARCHAR(128) NOT NULL,
	started TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
	last_active TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
	unlisted BOOLEAN NOT NULL DEFAULT FALSE,
	update_key VARCHAR(255) NOT NULL,
	client_ip VARCHAR(64) NOT NULL,
	roomcode VARCHAR(16) DEFAULT NULL CONSTRAINT unique_roomcode UNIQUE,
	private BOOLEAN NOT NULL DEFAULT FALSE
);


CREATE OR REPLACE FUNCTION dw.f_sha256 (msg VARCHAR)
        returns varchar
STABLE
AS $$
        import hashlib
        return hashlib.sha256(msg).hexdigest()
$$ LANGUAGE plpythonu
;

/* TODO When loading functions, grant to the "default" group from the setup */
GRANT EXECUTE ON FUNCTION dw.f_sha256(msg varchar) TO GROUP analyst_ro
;

Add refs to redshift doc

/* Naming here follows recommenation in https://docs.aws.amazon.com/redshift/latest/dg/udf-naming-udfs.html */
/* See also: https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_FUNCTION.html */
CREATE OR REPLACE FUNCTION dw.f_sha256 (msg VARCHAR)
        returns varchar
STABLE
AS $$
        import hashlib
        return hashlib.sha256(msg).hexdigest()
$$ LANGUAGE plpythonu
;

/* TODO When loading functions, grant to the "default" group from the setup */
GRANT EXECUTE ON FUNCTION dw.f_sha256(msg varchar) TO GROUP analyst_ro
;


/*
Navicat SQL Server Data Transfer

Source Server         : localtest
Source Server Version : 110000
Source Host           : localhost:1433
Source Database       : test
Source Schema         : dbo

Target Server Type    : SQL Server
Target Server Version : 110000
File Encoding         : 65001

Date: 2016-03-12 12:27:00
*/

-- ----------------------------
-- Table structure for People
-- ----------------------------
DROP TABLE [dbo].[People]
GO
CREATE TABLE [dbo].[People] (
[Age] int NOT NULL ,
[Name] nvarchar(255) NOT NULL ,
[PeopleId] int NOT NULL IDENTITY(1,1) ,
[NonIndexA] nchar(255) NULL ,
[NonIndexB] nchar(255) NULL ,
[IndexAPart1] int NULL ,
[IndexAPart2] int NULL ,
[IndexAPart3] int NULL
)

GO
DBCC CHECKIDENT(N'[dbo].[People]', RESEED, 1590)
GO

-- ----------------------------
-- Indexes structure for table People
-- ----------------------------
CREATE INDEX [IX_people_age] ON [dbo].[People]
([Age] ASC)
GO
CREATE UNIQUE INDEX [IX_people_name] ON [dbo].[People]
([Name] ASC)
WITH (IGNORE_DUP_KEY = ON)
GO
CREATE INDEX [IX_People_A] ON [dbo].[People]
([IndexAPart1] ASC, [IndexAPart2] ASC, [IndexAPart3] ASC)
GO

-- ----------------------------
-- Primary Key structure for table People
-- ----------------------------
ALTER TABLE [dbo].[People] ADD PRIMARY KEY ([PeopleId])
GO

Change mssql example data file

USE [test]
GO

/****** Object:  Table [dbo].[People]    Script Date: 2016/3/12 14:27:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[People](
	[Age] [int] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[PeopleId] [int] IDENTITY(1,1) NOT NULL,
	[NonIndexA] [nchar](255) NULL,
	[NonIndexB] [nchar](255) NULL,
	[IndexAPart1] [int] NULL,
	[IndexAPart2] [int] NULL,
	[IndexAPart3] [int] NULL,
 CONSTRAINT [PK__people__3214EC27D30B5FDA] PRIMARY KEY CLUSTERED
(
	[PeopleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


UPDATE `settings` SET `cmumversion`='3.2.0', `dbversion`='3.2.0' WHERE `id`=1;
ALTER TABLE `groups` ADD `enabled` tinyint(1) NULL DEFAULT '1' AFTER `comment`;
ALTER TABLE `settings` DROP `dbversion`;
ALTER TABLE `settings` ADD `autoupdcheck` TINYINT(1) NULL AFTER `soonexpusrorder`;
UPDATE `settings` SET `autoupdcheck`='0' WHERE `id`=1;
ALTER TABLE `log_activity` ADD `data` VARCHAR(50) NULL AFTER `userid`;
TRUNCATE TABLE `log_activity`;
ALTER TABLE `settings` ADD `usrorderby` VARCHAR(12) NULL AFTER `autoupdcheck`, ADD `usrorder` VARCHAR(4) NULL AFTER `usrorderby`;
UPDATE `settings` SET `usrorderby`='user',`usrorder`='asc' WHERE `id`=1;
ALTER TABLE `log_genxmlreq` CHANGE `ip` `ip` VARCHAR(45);
ALTER TABLE `log_login` CHANGE `ip` `ip` VARCHAR(45);

Truncate activity log table before altering it

UPDATE `settings` SET `cmumversion`='3.2.0', `dbversion`='3.2.0' WHERE `id`=1;
ALTER TABLE `groups` ADD `enabled` tinyint(1) NULL DEFAULT '1' AFTER `comment`;
ALTER TABLE `settings` DROP `dbversion`;
ALTER TABLE `settings` ADD `autoupdcheck` TINYINT(1) NULL AFTER `soonexpusrorder`;
UPDATE `settings` SET `autoupdcheck`='0' WHERE `id`=1;
TRUNCATE TABLE `log_activity`;
ALTER TABLE `log_activity` ADD `data` VARCHAR(50) NULL AFTER `userid`;
ALTER TABLE `settings` ADD `usrorderby` VARCHAR(12) NULL AFTER `autoupdcheck`, ADD `usrorder` VARCHAR(4) NULL AFTER `usrorderby`;
UPDATE `settings` SET `usrorderby`='user',`usrorder`='asc' WHERE `id`=1;
ALTER TABLE `log_genxmlreq` CHANGE `ip` `ip` VARCHAR(45);
ALTER TABLE `log_login` CHANGE `ip` `ip` VARCHAR(45);


ALTER TABLE movies ADD COLUMN parent_id INT;
ALTER TABLE movies ADD COLUMN episode_sort_value INT;
UPDATE movies SET parent_id = t.parent_id
       FROM
       (SELECT m.id AS parent_id,e.id AS episode_id FROM movies m
        INNER JOIN movies e ON e.episode_parent_title = m.full_title) t
       WHERE id = t.episode_id;

CREATE OR REPLACE FUNCTION first_release_stamp(movie_id INT)
RETURNS INT
AS
$$
SELECT EXTRACT(epoch FROM MIN(release_stamp))::INT FROM release_dates WHERE movie_id = $1
$$
LANGUAGE SQL;

UPDATE movies SET episode_sort_value = first_release_stamp(id) WHERE is_episode = true;

CREATE INDEX movies_idx_parent_id ON movies (parent_id);

Change type to INT8 to fit all relevant timestamps, change column name, and put stamp as sort value for all items

ALTER TABLE movies ADD COLUMN parent_id INT;
ALTER TABLE movies ADD COLUMN movie_sort_value INT8;
UPDATE movies SET parent_id = t.parent_id
       FROM
       (SELECT m.id AS parent_id,e.id AS episode_id FROM movies m
        INNER JOIN movies e ON e.episode_parent_title = m.full_title) t
       WHERE id = t.episode_id;

CREATE OR REPLACE FUNCTION first_release_stamp(movie_id INT)
RETURNS INT8
AS
$$
SELECT EXTRACT(epoch FROM MIN(release_stamp))::INT8 FROM release_dates WHERE movie_id = $1
$$
LANGUAGE SQL;

UPDATE movies SET movie_sort_value = first_release_stamp(id);

CREATE INDEX movies_idx_parent_id ON movies (parent_id);


#tags: kwc
ALTER TABLE  `cache_component` ADD  `tag` VARCHAR( 255 ) CHARACTER SET ASCII COLLATE ascii_bin NOT NULL AFTER  `value`;

Add index to new tag column in cache_component

#tags: kwc
ALTER TABLE  `cache_component` ADD  `tag` VARCHAR( 255 ) CHARACTER SET ASCII COLLATE ascii_bin NOT NULL AFTER  `value`;
ALTER TABLE  `cache_component` ADD INDEX  `tag` (  `tag` );


--
-- Table structure for table `auth_tokens`
--

CREATE TABLE `auth_tokens` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `series` varchar(255) NOT NULL,
  `token` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `expires` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `auth_tokens`
--
ALTER TABLE `auth_tokens`
  ADD PRIMARY KEY (`id`),
  ADD KEY `user_id` (`user_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `auth_tokens`
--
ALTER TABLE `auth_tokens`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `auth_tokens`
--
ALTER TABLE `auth_tokens`
  ADD CONSTRAINT `auth_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Add indexes to series, token and expires columns for quicker select queries.

--
-- Table structure for table `auth_tokens`
--

CREATE TABLE `auth_tokens` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `series` varchar(255) NOT NULL,
  `token` varchar(255) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `expires` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `auth_tokens`
--
ALTER TABLE `auth_tokens`
  ADD PRIMARY KEY (`id`),
  ADD KEY `user_id` (`user_id`),
  ADD KEY `series` (`series`),
  ADD KEY `token` (`token`),
  ADD KEY `expires` (`expires`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `auth_tokens`
--
ALTER TABLE `auth_tokens`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `auth_tokens`
--
ALTER TABLE `auth_tokens`
  ADD CONSTRAINT `auth_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


--
-- run with psql -U postgres -f dbschema.sql
--
DROP DATABASE IF EXISTS senecatest;
CREATE DATABASE senecatest WITH ENCODING='UTF8' CONNECTION LIMIT=-1;

-- DROP ROLE senecatest;
CREATE ROLE "senecatest" LOGIN PASSWORD 'senecatest';

--# connect to the database (works for psql)
\c senecatest

CREATE TABLE foo
(
  id character varying PRIMARY KEY,
  p1 character varying,
  p2 character varying,
  p3 character varying,
  "fooBar" character varying,
  bar_foo character varying
);

ALTER TABLE foo OWNER TO senecatest;

CREATE TABLE moon_bar
(
  str character varying,
  id character varying PRIMARY KEY,
  "int" integer,
  bol boolean,
  wen timestamp with time zone,
  mark character varying,
  "dec" real,
  arr integer[],
  obj json,
  seneca text
);
ALTER TABLE moon_bar OWNER TO senecatest;

CREATE TABLE product
(
  name character varying,
  id character varying PRIMARY KEY,
  price integer
);
ALTER TABLE product OWNER TO senecatest;

Use the JSON type for storing JSON

--
-- run with psql -U postgres -f dbschema.sql
--
DROP DATABASE IF EXISTS senecatest;
CREATE DATABASE senecatest WITH ENCODING='UTF8' CONNECTION LIMIT=-1;

-- DROP ROLE senecatest;
CREATE ROLE "senecatest" LOGIN PASSWORD 'senecatest';

--# connect to the database (works for psql)
\c senecatest

CREATE TABLE foo
(
  id character varying PRIMARY KEY,
  p1 character varying,
  p2 character varying,
  p3 json,
  "fooBar" character varying,
  bar_foo character varying
);

ALTER TABLE foo OWNER TO senecatest;

CREATE TABLE moon_bar
(
  str character varying,
  id character varying PRIMARY KEY,
  "int" integer,
  bol boolean,
  wen timestamp with time zone,
  mark character varying,
  "dec" real,
  arr integer[],
  obj json,
  seneca text
);
ALTER TABLE moon_bar OWNER TO senecatest;

CREATE TABLE product
(
  name character varying,
  id character varying PRIMARY KEY,
  price integer
);
ALTER TABLE product OWNER TO senecatest;


CREATE TABLE jobs (
  id SERIAL,
  query TEXT NOT NULL,
  user VARCHAR(127) NOT NULL,
  uuid VARCHAR(127) NOT NULL,
  queryStats TEXT,
  state ENUM('QUEUED', 'PLANNING', 'STARTING', 'RUNNING', 'FINISHED_EXECUTION', 'FINISHED', 'CANCELED', 'FAILED') NOT NULL,
  /* Stored as JSON? */
  columns TEXT,
  query_finished TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  query_started TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  error TEXT,

  PRIMARY KEY (id)
);

CREATE INDEX user_jobs_time_index
  ON jobs (user, query_finished, query_started);

CREATE INDEX jobs_time_index
  ON jobs (query_finished, query_started);

Upgrade job.query to be medium text

CREATE TABLE jobs (
  id SERIAL,
  query MEDIUM TEXT NOT NULL,
  user VARCHAR(127) NOT NULL,
  uuid VARCHAR(127) NOT NULL,
  queryStats TEXT,
  state ENUM('QUEUED', 'PLANNING', 'STARTING', 'RUNNING', 'FINISHED_EXECUTION', 'FINISHED', 'CANCELED', 'FAILED') NOT NULL,
  /* Stored as JSON? */
  columns TEXT,
  query_finished TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  query_started TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  error TEXT,

  PRIMARY KEY (id)
);

CREATE INDEX user_jobs_time_index
  ON jobs (user, query_finished, query_started);

CREATE INDEX jobs_time_index
  ON jobs (query_finished, query_started);


RENAME TABLE `journal` TO `oldJournal`;

CREATE TABLE `journal` (
  `journalId` bigint(20) NOT NULL AUTO_INCREMENT,
  `currentIssueId` bigint(20) NOT NULL,
  `journalKey` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `eIssn` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `imageArticleId` bigint(20) DEFAULT NULL,
  `title` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `lastModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`journalId`),
  KEY `journal_ibfk_1_idx` (`currentIssueId`),
  CONSTRAINT `fk_journal_1` FOREIGN KEY (`currentIssueId`) REFERENCES `issue` (`issueId`),
  CONSTRAINT `fk_journal_2` FOREIGN KEY (`imageArticleId`) REFERENCES `article` (`articleId`)
);

Tweak schema so that currentIssueId is nullable

RENAME TABLE `journal` TO `oldJournal`;

CREATE TABLE `journal` (
  `journalId` bigint(20) NOT NULL AUTO_INCREMENT,
  `currentIssueId` bigint(20) DEFAULT NULL,
  `journalKey` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `eIssn` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `imageArticleId` bigint(20) DEFAULT NULL,
  `title` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `lastModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`journalId`),
  KEY `journal_ibfk_1_idx` (`currentIssueId`),
  CONSTRAINT `fk_journal_1` FOREIGN KEY (`currentIssueId`) REFERENCES `issue` (`issueId`),
  CONSTRAINT `fk_journal_2` FOREIGN KEY (`imageArticleId`) REFERENCES `article` (`articleId`)
);


drop database if exists fizzbuzz_twitterbot;
create database fizzbuzz_twitterbot character set = utf8;
use fizzbuzz_twitterbot;

drop table if exists fizzbuzz;
create table fizzbuzz (
  id bigint not null auto_increment,
  tweet_id bigint not null,
  num bigint not null unique,
  updated_at datetime not null default current_timestamp,
  created_at datetime not null,
  primary key (id)
) engine InnoDB charset utf8;

Fix upper case for SQL reserved word.

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

DROP TABLE IF EXISTS fizzbuzz;
CREATE TABLE fizzbuzz (
  id BIGINT NOT NULL AUTO_INCREMENT,
  tweet_id BIGINT NOT NULL,
  num BIGINT NOT NULL UNIQUE,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id)
) ENGINE InnoDB CHARSET utf8;


ALTER TABLE `kwc_basic_downloadtag` CHANGE `open_type` `open_type` ENUM('self','blank') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'self';
ALTER TABLE `kwc_basic_downloadtag` DROP `width`;
ALTER TABLE `kwc_basic_downloadtag` DROP `height`;
ALTER TABLE `kwc_basic_downloadtag` DROP `menubar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `toolbar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `locationbar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `statusbar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `scrollbars`;
ALTER TABLE `kwc_basic_downloadtag` DROP `resizable`;
ALTER TABLE `kwc_basic_downloadtag` DROP `rel_nofollow`;
ALTER TABLE `kwc_basic_downloadtag` DROP `rel_noopener`;
ALTER TABLE `kwc_basic_downloadtag` DROP `rel_noreferrer`;
ALTER TABLE `kwc_basic_downloadtag` ADD `rel_noindex` TINYINT NOT NULL ;

Change all open types from popup to blank for download tag

UPDATE `kwc_basic_downloadtag` SET `open_type` = 'blank' WHERE `open_type` = 'popup';
ALTER TABLE `kwc_basic_downloadtag` CHANGE `open_type` `open_type` ENUM('self','blank') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'self';
ALTER TABLE `kwc_basic_downloadtag` DROP `width`;
ALTER TABLE `kwc_basic_downloadtag` DROP `height`;
ALTER TABLE `kwc_basic_downloadtag` DROP `menubar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `toolbar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `locationbar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `statusbar`;
ALTER TABLE `kwc_basic_downloadtag` DROP `scrollbars`;
ALTER TABLE `kwc_basic_downloadtag` DROP `resizable`;
ALTER TABLE `kwc_basic_downloadtag` DROP `rel_nofollow`;
ALTER TABLE `kwc_basic_downloadtag` DROP `rel_noopener`;
ALTER TABLE `kwc_basic_downloadtag` DROP `rel_noreferrer`;
ALTER TABLE `kwc_basic_downloadtag` ADD `rel_noindex` TINYINT NOT NULL ;


CREATE USER liberator WITH ENCRYPTED PASSWORD 'imessage';
CREATE DATABASE liberator OWNER liberator;

\connect liberator

CREATE TABLE contact (
  id SERIAL PRIMARY KEY,
  imessage_id VARCHAR NOT NULL UNIQUE,
  full_name VARCHAR DEFAULT NULL
);
ALTER TABLE contact OWNER TO liberator;
CREATE INDEX ON contact (imessage_id);

CREATE TABLE contact_handle (
  contact_id INT NOT NULL REFERENCES contact (id),
  handle VARCHAR DEFAULT NULL,
  CONSTRAINT contact_id_handle_constraint UNIQUE (contact_id, handle)
);
ALTER TABLE contact_handle OWNER TO liberator;
CREATE INDEX ON contact_handle (handle);

CREATE TABLE message_channel (
  id INT PRIMARY KEY,
  name VARCHAR DEFAULT NULL
);
ALTER TABLE message_channel OWNER TO liberator;
INSERT INTO message_channel (id, name) VALUES (
  0, 'SLACK'
);

CREATE TABLE contact_channel_mapping (
  id SERIAL PRIMARY KEY,
  message_channel_id INT NOT NULL REFERENCES message_channel (id),
  contact_id INT NOT NULL REFERENCES contact (id),
  channel_key VARCHAR DEFAULT NULL,
  channel_name VARCHAR DEFAULT NULL,
  CONSTRAINT contact_id_message_channel_id_constraint UNIQUE (contact_id, message_channel_id)
);
ALTER TABLE contact_channel_mapping OWNER TO liberator;

Add disabled column to handle

CREATE USER liberator WITH ENCRYPTED PASSWORD 'imessage';
CREATE DATABASE liberator OWNER liberator;

\connect liberator

CREATE TABLE contact (
  id SERIAL PRIMARY KEY,
  imessage_id VARCHAR NOT NULL UNIQUE,
  full_name VARCHAR DEFAULT NULL
);
ALTER TABLE contact OWNER TO liberator;
CREATE INDEX ON contact (imessage_id);

CREATE TABLE contact_handle (
  contact_id INT NOT NULL REFERENCES contact (id),
  handle VARCHAR DEFAULT NULL,
  disabled BOOLEAN DEFAULT FALSE,
  CONSTRAINT contact_id_handle_constraint UNIQUE (contact_id, handle)
);
ALTER TABLE contact_handle OWNER TO liberator;
CREATE INDEX ON contact_handle (handle);

CREATE TABLE message_channel (
  id INT PRIMARY KEY,
  name VARCHAR DEFAULT NULL
);
ALTER TABLE message_channel OWNER TO liberator;
INSERT INTO message_channel (id, name) VALUES (
  0, 'SLACK'
);

CREATE TABLE contact_channel_mapping (
  id SERIAL PRIMARY KEY,
  message_channel_id INT NOT NULL REFERENCES message_channel (id),
  contact_id INT NOT NULL REFERENCES contact (id),
  channel_key VARCHAR DEFAULT NULL,
  channel_name VARCHAR DEFAULT NULL,
  CONSTRAINT contact_id_message_channel_id_constraint UNIQUE (contact_id, message_channel_id)
);
ALTER TABLE contact_channel_mapping OWNER TO liberator;


CREATE TABLE `SENT_MESSAGES` (
  `CHANGE_NUM` BIGINT DEFAULT NULL,
  `TIME_STAMP` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `OBJECT_ID` BIGINT NOT NULL,
  `OBJECT_VERSION` BIGINT NOT NULL,
  `OBJECT_TYPE` enum('JSON_SCHEMA_DEPENDANT','JSON_SCHEMA','ENTITY','ENTITY_CONTAINER','PRINCIPAL','ACTIVITY','EVALUATION','SUBMISSION','EVALUATION_SUBMISSIONS','FILE','MESSAGE','WIKI','FAVORITE','ACCESS_REQUIREMENT','ACCESS_APPROVAL','TEAM','TABLE','ACCESS_CONTROL_LIST','PROJECT_SETTING','VERIFICATION_SUBMISSION','CERTIFIED_USER_PASSING_RECORD','FORUM','THREAD','REPLY','ENTITY_VIEW','USER_PROFILE','DATA_ACCESS_REQUEST','DATA_ACCESS_SUBMISSION','DATA_ACCESS_SUBMISSION_STATUS','MEMBERSHIP_INVITATION', 'THREAD_VIEW') NOT NULL,
  PRIMARY KEY (`OBJECT_ID`,`OBJECT_VERSION`,`OBJECT_TYPE`),
  UNIQUE KEY `SENT_UNIQUE_CHANG_NUM` (`CHANGE_NUM`),
  CONSTRAINT `SENT_MESS_CH_NUM_FK` FOREIGN KEY (`OBJECT_ID`,`OBJECT_VERSION`,`OBJECT_TYPE`) REFERENCES `CHANGES` (`OBJECT_ID`,`OBJECT_VERSION`,`OBJECT_TYPE`) ON DELETE CASCADE)

Remove DB enum value that does not existing in the referenced table

CREATE TABLE `SENT_MESSAGES` (
  `CHANGE_NUM` BIGINT DEFAULT NULL,
  `TIME_STAMP` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `OBJECT_ID` BIGINT NOT NULL,
  `OBJECT_VERSION` BIGINT NOT NULL,
  `OBJECT_TYPE` enum('JSON_SCHEMA_DEPENDANT','JSON_SCHEMA','ENTITY','ENTITY_CONTAINER','PRINCIPAL','ACTIVITY','EVALUATION','SUBMISSION','EVALUATION_SUBMISSIONS','FILE','MESSAGE','WIKI','FAVORITE','ACCESS_REQUIREMENT','ACCESS_APPROVAL','TEAM','TABLE','ACCESS_CONTROL_LIST','PROJECT_SETTING','VERIFICATION_SUBMISSION','CERTIFIED_USER_PASSING_RECORD','FORUM','THREAD','REPLY','ENTITY_VIEW','USER_PROFILE','DATA_ACCESS_REQUEST','DATA_ACCESS_SUBMISSION','DATA_ACCESS_SUBMISSION_STATUS', 'THREAD_VIEW') NOT NULL,
  PRIMARY KEY (`OBJECT_ID`,`OBJECT_VERSION`,`OBJECT_TYPE`),
  UNIQUE KEY `SENT_UNIQUE_CHANG_NUM` (`CHANGE_NUM`),
  CONSTRAINT `SENT_MESS_CH_NUM_FK` FOREIGN KEY (`OBJECT_ID`,`OBJECT_VERSION`,`OBJECT_TYPE`) REFERENCES `CHANGES` (`OBJECT_ID`,`OBJECT_VERSION`,`OBJECT_TYPE`) ON DELETE CASCADE)


-- Water types

insert into band_type (band_type_id, band_type_name)
values (50, 'WATER');

insert into public.band (band_id, sensor_id, band_name, band_type_id,
                         file_number,
                         resolution, min_wavelength, max_wavelength, file_pattern, satellite_id, band_tag, band_number)
values (34, null, 'WATER', 50,
        10, -- ??
        null, null, null, '.*_WATER_.*', null, 'WATER', 1);

insert into processing_level (level_id, level_name, nodata_value, resampling_method, level_description)
values (6, 'WATER', 0, 'near', 'Water');

insert into band_source (tile_type_id, band_id, level_id, tile_layer)
values (1,
        34, 6,
        1 -- ??
);

Use existing sequence for new DB type ids.

-- Water types

insert into band_type (band_type_name) values ('WATER');

insert into public.band (sensor_id, band_name, band_type_id,
                         file_number,
                         resolution, min_wavelength, max_wavelength, file_pattern, satellite_id, band_tag, band_number)
values (null, 'WATER',
        (select band_type_id from band_type where band_type_name = 'WATER'),
        10, -- ??
        null, null, null, '.*_WATER_.*', null, 'WATER', 1);

insert into processing_level (level_name, nodata_value, resampling_method, level_description)
values ('WATER', 0, 'near', 'Water');

insert into band_source (tile_type_id, band_id, level_id, tile_layer)
values (1,
        (select band_type_id from band_type where band_type_name = 'WATER'),
        (select level_id from processing_level where level_name='WATER'),
        1 -- ??
);


/* Copyright 2018 New Vector Ltd
 *
 * 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.
 */

ALTER TABLE groups ADD COLUMN is_joinable BOOLEAN NOT NULL DEFAULT 0;

Make column definition that works on both dbs

/* Copyright 2018 New Vector Ltd
 *
 * 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.
 */

ALTER TABLE groups ADD COLUMN is_joinable BOOLEAN NOT NULL DEFAULT (CAST(0 AS BOOLEAN));


-- Aggregate bird_migration_altitude_profile data:
-- - Aggregate on time frame (e.g. 20 min)
-- - Aggregate on altitude band (e.g. 200-1600m, 1600+ m)
-- - Average values

WITH aggregated_data AS (
    SELECT
        radar_id,
        date_trunc('hour', start_time) + date_part('minute', start_time)::int / 20 * interval '20 min' AS interval_start_time,
        CASE
            WHEN altitude >= 0.2 AND altitude < 1.6 THEN 'low'
            WHEN altitude >= 1.6 THEN 'high'
        END AS altitude_band,
        count(*) AS number_of_measurements,
        avg(u_speed) AS avg_u_speed,
        avg(v_speed) AS avg_v_speed,
        avg(bird_density) AS avg_bird_density
    FROM
        bird_migration_altitude_profiles
    WHERE
        radial_velocity_std >= 2
        AND bird_density >= 10
        AND altitude >= 0.2
    GROUP BY
        radar_id,
        interval_start_time,
        altitude_band
    ORDER BY
        radar_id,
        interval_start_time,
        altitude_band DESC
)

SELECT
    *
FROM
    aggregated_data

Add radar latitude / longitude to query results

-- Aggregate bird_migration_altitude_profile data:
-- - Aggregate on time frame (e.g. 20 min)
-- - Aggregate on altitude band (e.g. 200-1600m, 1600+ m)
-- - Average values

WITH aggregated_data AS (
    SELECT
        b.radar_id,
        ST_X(r.the_geom) AS longitude,
  		ST_Y(r.the_geom) AS latitude,
  		date_trunc('hour', b.start_time) + date_part('minute', b.start_time)::int / 20 * interval '20 min' AS interval_start_time,
        CASE
            WHEN b.altitude >= 0.2 AND b.altitude < 1.6 THEN 'low'
            WHEN b.altitude >= 1.6 THEN 'high'
        END AS altitude_band,
        count(*) AS number_of_measurements,
        avg(b.u_speed) AS avg_u_speed,
        avg(b.v_speed) AS avg_v_speed,
        avg(b.bird_density) AS avg_bird_density
    FROM
        bird_migration_altitude_profiles b
    LEFT JOIN
  		radars r
    ON
  		b.radar_id = r.cartodb_id
    WHERE
        b.radial_velocity_std >= 2
        AND b.bird_density >= 10
        AND b.altitude >= 0.2
    GROUP BY
        b.radar_id,
  		r.the_geom,
        interval_start_time,
        altitude_band
    ORDER BY
        b.radar_id,
        interval_start_time,
        altitude_band DESC
)

SELECT
    *
FROM
    aggregated_data


CREATE EXTERNAL TABLE presto_test_s3 (
  t_bigint BIGINT,
  t_string STRING
)
COMMENT 'Presto test S3 table'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 's3://presto-test-hive/presto_test_s3'
TBLPROPERTIES ('RETENTION'='-1')
;

Simplify test table creation for S3

CREATE EXTERNAL TABLE presto_test_s3 (
  t_bigint BIGINT,
  t_string STRING
)
COMMENT 'Presto test S3 table'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE
TBLPROPERTIES ('RETENTION'='-1')
;

ALTER TABLE presto_test_s3
SET LOCATION 's3://presto-test-hive/presto_test_s3'
;


/* Copyright 2015 OpenMarket Ltd
 *
 * 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.
 */

/*
 * Keeps track of what rooms users have left and don't want to be able to
 * access again.
 *
 * If all users on this server have left a room, we can delete the room
 * entirely.
 */

 ALTER TABLE room_memberships ADD COLUMN forgotten INTEGER(1) DEFAULT 0;

Remove size specifier for database column

/* Copyright 2015 OpenMarket Ltd
 *
 * 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.
 */

/*
 * Keeps track of what rooms users have left and don't want to be able to
 * access again.
 *
 * If all users on this server have left a room, we can delete the room
 * entirely.
 *
 * This column should always contain either 0 or 1.
 */

 ALTER TABLE room_memberships ADD COLUMN forgotten INTEGER DEFAULT 0;


-- Add a table for human-in-the-loop jobs

CREATE TYPE public.hitl_job_status AS ENUM (
    'NOTRUN',
    'TORUN',
    'RUNNING',
    'RAN',
    'FAILED'
);

ALTER TYPE public.hitl_job_status OWNER TO rasterfoundry;

CREATE TABLE public.hitl_jobs (
    id UUID PRIMARY KEY,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    created_by CHARACTER VARYING(255) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
    modified_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    owner CHARACTER VARYING(255) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
    campaign_id UUID NOT NULL REFERENCES public.campaigns(id) ON DELETE CASCADE,
    project_id UUID NOT NULL REFERENCES public.annotation_projects(id) ON DELETE CASCADE,
    status public.hitl_job_status NOT NULL,
    version int default 0 NOT NULL
);

CREATE INDEX hitl_jobs_owner_idx ON public.hitl_jobs USING btree(owner);

CREATE INDEX hitl_jobs_created_by_idx ON public.hitl_jobs USING btree(created_by);

CREATE INDEX hitl_jobs_status_idx ON public.hitl_jobs USING btree(status);

CREATE INDEX hitl_jobs_campaign_id_idx ON public.hitl_jobs USING btree(campaign_id);

CREATE INDEX hitl_jobs_project_id_idx ON public.hitl_jobs USING btree(project_id);

CREATE INDEX hitl_jobs_version_idx ON public.hitl_jobs USING btree(version);

Fix bug in migration to add hitl job status type

-- Add a table for human-in-the-loop jobs

CREATE TYPE public.hitl_job_status AS ENUM (
    'NOTRUN',
    'TORUN',
    'RUNNING',
    'RAN',
    'FAILED'
);

CREATE TABLE public.hitl_jobs (
    id UUID PRIMARY KEY,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    created_by CHARACTER VARYING(255) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
    modified_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    owner CHARACTER VARYING(255) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
    campaign_id UUID NOT NULL REFERENCES public.campaigns(id) ON DELETE CASCADE,
    project_id UUID NOT NULL REFERENCES public.annotation_projects(id) ON DELETE CASCADE,
    status public.hitl_job_status NOT NULL,
    version int default 0 NOT NULL
);

CREATE INDEX hitl_jobs_owner_idx ON public.hitl_jobs USING btree(owner);

CREATE INDEX hitl_jobs_created_by_idx ON public.hitl_jobs USING btree(created_by);

CREATE INDEX hitl_jobs_status_idx ON public.hitl_jobs USING btree(status);

CREATE INDEX hitl_jobs_campaign_id_idx ON public.hitl_jobs USING btree(campaign_id);

CREATE INDEX hitl_jobs_project_id_idx ON public.hitl_jobs USING btree(project_id);

CREATE INDEX hitl_jobs_version_idx ON public.hitl_jobs USING btree(version);


# Create new sms_alerts table

CREATE TABLE sms_alerts
(
	id 						INTEGER 		NOT NULL AUTO_INCREMENT PRIMARY KEY,
    citation_number 		VARCHAR(25),
    court_date 				DATETIME,
    phone_number			VARCHAR(25),
    date_of_birth 			DATE     						NULL
)ENGINE=InnoDB;

# Add auto_increment to violations
ALTER TABLE violations MODIFY COLUMN INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;

# Add auto_increment to citations
ALTER TABLE citations MODIFY COLUMN INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;

Fix changelog issues with mysql and existing state

# Create new sms_alerts table

CREATE TABLE sms_alerts
(
  id 						      INTEGER 		        NOT NULL AUTO_INCREMENT PRIMARY KEY,
  citation_number 		VARCHAR(25),
  court_date 				  DATETIME,
  phone_number			  VARCHAR(25),
  date_of_birth 			DATE     						NULL
)ENGINE=InnoDB;

# Add auto_increment to violations
ALTER TABLE violations MODIFY id INTEGER NOT NULL AUTO_INCREMENT;

# Add auto_increment to citations
ALTER TABLE citations MODIFY id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;


CREATE TABLE dglusers(
       id SERIAL PRIMARY KEY,
       username TEXT UNIQUE NOT NULL,
       email TEXT UNIQUE NOT NULL,
       env TEXT,
       password TEXT NOT NULL,
       flags INT NOT NULL DEFAULT 0
);

CREATE TABLE xlogfile(
       id SERIAL PRIMARY KEY,
       version TEXT,
       points BIGINT,
       deathdnum BIGINT,
       deathdname TEXT,
       deathlev BIGINT,
       maxlvl BIGINT,
       dlev_name TEXT,
       hp BIGINT,
       maxhp BIGINT,
       deaths BIGINT,
       deathdate DATE,
       birthdate DATE,
       uid BIGINT,
       "role" TEXT,
       race TEXT,
       gender TEXT,
       align TEXT,
       "name" TEXT,
       death TEXT,
       death_uniq TEXT,
       conduct TEXT,
       turns BIGINT,
       achieve TEXT,
       realtime BIGINT,
       starttime TIMESTAMP,
       endtime TIMESTAMP,
       gender0 TEXT,
       align0 TEXT,
       flags TEXT,
       region TEXT,
       event TEXT,
       carried TEXT,
       elbereths BIGINT,
       xplevel BIGINT,
       exp BIGINT,
       mode TEXT,
       gold BIGINT,
       fpos BIGINT
);

Remove unique from email in schema

CREATE TABLE dglusers(
       id SERIAL PRIMARY KEY,
       username TEXT UNIQUE NOT NULL,
       email TEXT NOT NULL,
       env TEXT,
       password TEXT NOT NULL,
       flags INT NOT NULL DEFAULT 0
);

CREATE TABLE xlogfile(
       id SERIAL PRIMARY KEY,
       version TEXT,
       points BIGINT,
       deathdnum BIGINT,
       deathdname TEXT,
       deathlev BIGINT,
       maxlvl BIGINT,
       dlev_name TEXT,
       hp BIGINT,
       maxhp BIGINT,
       deaths BIGINT,
       deathdate DATE,
       birthdate DATE,
       uid BIGINT,
       "role" TEXT,
       race TEXT,
       gender TEXT,
       align TEXT,
       "name" TEXT,
       death TEXT,
       death_uniq TEXT,
       conduct TEXT,
       turns BIGINT,
       achieve TEXT,
       realtime BIGINT,
       starttime TIMESTAMP,
       endtime TIMESTAMP,
       gender0 TEXT,
       align0 TEXT,
       flags TEXT,
       region TEXT,
       event TEXT,
       carried TEXT,
       elbereths BIGINT,
       xplevel BIGINT,
       exp BIGINT,
       mode TEXT,
       gold BIGINT,
       fpos BIGINT
);


CREATE OR REPLACE
FUNCTION geocode_admin0(search text, tx_id bigint, user_id name)
  RETURNS Geometry AS
$$
  import logging
  from sys import path
  path.append( '/home/ubuntu/www/cartodb-geocoder/server/lib/python/cartodb_geocoder' )
  import quota_service

  LOG_FILENAME = '/tmp/plpython.log'
  logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

  if user_id in SD and tx_id in SD[user_id] and 'redis_connection' in SD[user_id][tx_id]:
    logging.debug("Using redis cached connection...")
    qs = quota_service.QuotaService(logging, user_id, tx_id, redis_connection=SD[user_id][tx_id]['redis_connection'])
  else:
    qs = quota_service.QuotaService(logging, user_id, tx_id, redis_host='localhost', redis_port=6379, redis_db=5)

  if qs.check_user_quota():
    result = plpy.execute("SELECT geom FROM geocode_admin0_polygons(Array[\'{0}\']::text[])".format(search))
    if result.status() == 5 and result.nrows() == 1:
      qs.increment_georeference_use()
      SD[user_id] = {tx_id: {'redis_connection': qs.get_redis_connection()}}
      return result[0]["geom"]
    else:
      raise Exception('Something wrong with the georefence operation')
  else:
    raise Exception('Not enough quota for this user')

$$ LANGUAGE plpythonu;

Change to use the installed package

CREATE OR REPLACE
FUNCTION geocode_admin0(search text, tx_id bigint, user_id name)
  RETURNS Geometry AS
$$
  import logging
  from cartodb_geocoder import quota_service

  LOG_FILENAME = '/tmp/plpython.log'
  logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

  if user_id in SD and tx_id in SD[user_id] and 'redis_connection' in SD[user_id][tx_id]:
    logging.debug("Using redis cached connection...")
    qs = quota_service.QuotaService(logging, user_id, tx_id, redis_connection=SD[user_id][tx_id]['redis_connection'])
  else:
    qs = quota_service.QuotaService(logging, user_id, tx_id, redis_host='localhost', redis_port=6379, redis_db=5)

  if qs.check_user_quota():
    result = plpy.execute("SELECT geom FROM geocode_admin0_polygons(Array[\'{0}\']::text[])".format(search))
    if result.status() == 5 and result.nrows() == 1:
      qs.increment_georeference_use()
      SD[user_id] = {tx_id: {'redis_connection': qs.get_redis_connection()}}
      return result[0]["geom"]
    else:
      raise Exception('Something wrong with the georefence operation')
  else:
    raise Exception('Not enough quota for this user')

$$ LANGUAGE plpythonu;


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

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

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


/**
 * This table stores representations of regions of sky.
 * centre_ra, centre_decl, and xtr_radius should all be in degrees.
 * x,y,z are the cartesian representation of the central ra,decl.
 */

{% ifdb monetdb %}
CREATE SEQUENCE seq_skyregion AS INTEGER;
CREATE TABLE skyregion
  (id INTEGER NOT NULL DEFAULT NEXT VALUE FOR seq_skyregion
{% endifdb %}

{% ifdb postgresql %}
CREATE TABLE skyregion
  (id SERIAL
{% endifdb %}

  ,dataset INTEGER NOT NULL
  ,centre_ra DOUBLE PRECISION NOT NULL
  ,centre_decl DOUBLE PRECISION NOT NULL
  ,xtr_radius DOUBLE PRECISION NOT NULL
  {% ifdb postgresql %}
  ,CHECK (xtr_radius >= 0)
  {% endifdb %}
  ,x DOUBLE PRECISION NOT NULL
  ,y DOUBLE PRECISION NOT NULL
  ,z DOUBLE PRECISION NOT NULL
  ,PRIMARY KEY (id)
  ,FOREIGN KEY (dataset) REFERENCES dataset (id)
  )
;

{% ifdb postgresql %}
CREATE INDEX "skyregion_dataset" ON "skyregion" ("dataset");
{% endifdb %}

Remove database constraint for simplicity.

/**
 * This table stores representations of regions of sky.
 * centre_ra, centre_decl, and xtr_radius should all be in degrees.
 * x,y,z are the cartesian representation of the central ra,decl.
 */

{% ifdb monetdb %}
CREATE SEQUENCE seq_skyregion AS INTEGER;
CREATE TABLE skyregion
  (id INTEGER NOT NULL DEFAULT NEXT VALUE FOR seq_skyregion
{% endifdb %}

{% ifdb postgresql %}
CREATE TABLE skyregion
  (id SERIAL
{% endifdb %}

  ,dataset INTEGER NOT NULL
  ,centre_ra DOUBLE PRECISION NOT NULL
  ,centre_decl DOUBLE PRECISION NOT NULL
  ,xtr_radius DOUBLE PRECISION NOT NULL
  ,x DOUBLE PRECISION NOT NULL
  ,y DOUBLE PRECISION NOT NULL
  ,z DOUBLE PRECISION NOT NULL
  ,PRIMARY KEY (id)
  ,FOREIGN KEY (dataset) REFERENCES dataset (id)
  )
;

{% ifdb postgresql %}
CREATE INDEX "skyregion_dataset" ON "skyregion" ("dataset");
{% endifdb %}


START TRANSACTION;
CREATE TABLE bug3512 (a VARCHAR(100) ) ;

INSERT INTO bug3512 (a) VALUES ('9999') ;
INSERT INTO bug3512 (a) VALUES ('32767') ;
INSERT INTO bug3512 (a) VALUES ('32768') ;
INSERT INTO bug3512 (a) VALUES ('327670') ;
-- this fails because values > 32767 cannot be represented as short
-- and MonetDB tries to cast the col to short
SELECT * FROM bug3512 WHERE a > 8888;

INSERT INTO bug3512 (a) VALUES ('0.0');
INSERT INTO bug3512 (a) VALUES ('0.10');
INSERT INTO bug3512 (a) VALUES ('9999.00') ;

-- this fails because decimal numbers cannot be cast to short
SELECT * FROM bug3512 WHERE a = 9999;
ROLLBACK;

Add newline at end of file.


/* Ensure project short names are unique. */

UPDATE project SET projectShortName = NULL WHERE projectShortName = '';
ALTER TABLE project add unique (projectShortName);

Change comment in patch file.

-- Ensure project short names are unique.
UPDATE project SET projectShortName = NULL WHERE projectShortName = '';
ALTER TABLE project add unique (projectShortName);


ALTER TABLE transfer_properties
    ADD transferType VARCHAR(50);

UPDATE transfer_properties
    SET transferType = 'ORDER';

ALTER TABLE transfer_properties
  ALTER COLUMN transferType SET NOT NULL;

ALTER TABLE transfer_properties
  DROP CONSTRAINT IF EXISTS uk_sprkvmtubsjd58jc0afdycmiy;

CREATE UNIQUE INDEX
  uk_tp_by_facility_and_transfer_type
  ON transfer_properties (transferType, facilityId);

Remove constraint that was renamed, rename new constraint to be more descriptive.

ALTER TABLE transfer_properties
    ADD transferType VARCHAR(50);

UPDATE transfer_properties
    SET transferType = 'ORDER';

ALTER TABLE transfer_properties
  ALTER COLUMN transferType SET NOT NULL;

ALTER TABLE transfer_properties
  DROP CONSTRAINT IF EXISTS transfer_properties_facility_id_unique;

CREATE UNIQUE INDEX
  transfer_properties_facility_id_transfer_type_unique
  ON transfer_properties (transferType, facilityId);


ALTER TABLE SC_Classifieds_Classifieds
ADD description	varchar(4000)	NULL,
ADD price		int 			NULL
;

Fix the bad sql instruction for MSSQL

ALTER TABLE SC_Classifieds_Classifieds
ADD (description	varchar(4000)	NULL,
  price		int 			NULL)
;


CREATE INDEX CONCURRENTLY relative_file_paths_to_verify on downloads( relative_file_path ) where file_status = 'tbd' and relative_file_path <> 'tbd' and relative_file_path <> 'error';

Remove concurrent from create index.

CREATE INDEX relative_file_paths_to_verify on downloads( relative_file_path ) where file_status = 'tbd' and relative_file_path <> 'tbd' and relative_file_path <> 'error';


INSERT INTO system_role (`name`,`description`) VALUES ('ROLE_USER','A basic user in the system');

Add a test user to allow authentication to succeed.

INSERT INTO system_role (`name`,`description`) VALUES ('ROLE_USER','A basic user in the system');
INSERT INTO user (`createdDate`, `email`, `firstName`, `lastName`, `password`, `phoneNumber`, `username`, `valid`, `system_role`) VALUES (now(), 'fbristow@phac-aspc.gc.ca', 'Franklin', 'Bristow', '$2a$10$yvzFLxWA9m2wNQmHpJtWT.MRZv8qV8Mo3EMB6HTkDnUbi9aBrbWWW', '7029', 'fbristow', 1, 1);


IF NOT EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name ='KI_TransformationNames')
    CREATE TYPE dbo.KI_TransformationNames AS TABLE(
        TransformationName VARCHAR(MAX)
    )

SELECT TransformationID, TransformationName, TransformationCode
	FROM CMS_Transformation
	WHERE TransformationName IN (SELECT * FROM @ListOfNames)

DROP TYPE [dbo].[KI_TransformationNames]

Remove extra SQL that was separated to another script


CREATE TABLE divisions (
    id bigint auto_increment,
    name varchar(8));

CREATE TABLE teams (
    id bigint auto_increment,
    division_id bigint,
    name varchar(128),
);

CREATE TABLE games (
    id bigint auto_increment,
    home_team_id bigint,
    away_team_id bigint,
    played_at date,

    -- score
    full_time_home_team_goals tinyint,
    full_time_away_team_goals tinyint,
    full_time_result varchar(1),
    half_time_home_team_goals tinyint,
    half_time_away_team_goals tinyint,
    half_time_result varchar(1),

    -- stats
    attendance bigint,
    referee varchar(100),
    home_team_shots tinyint,
    away_team_shots tinyint,
    home_team_shots_on_target tinyint,
    away_team_shots_on_target tinyint,
    home_team_hit_woodwork tinyint,
    away_team_hit_woodwork tinyint,
    home_team_corners tinyint,
    away_team_corners tinyint,
    home_team_fouls_committed tinyint,
    away_team_fouls_committed tinyint,
    home_team_offsides tinyint,
    away_team_offsides tinyint,
    home_team_yellow_cards tinyint,
    away_team_yellow_cards tinyint,
    home_team_red_cards tinyint,
    away_team_red_cards tinyint
);

Create index to speed up games loading

CREATE TABLE divisions (
    id bigint auto_increment,
    name varchar(8));

CREATE TABLE teams (
    id bigint auto_increment,
    division_id bigint,
    name varchar(128),
);

CREATE TABLE games (
    id bigint auto_increment,
    home_team_id bigint,
    away_team_id bigint,
    played_at date,

    -- score
    full_time_home_team_goals tinyint,
    full_time_away_team_goals tinyint,
    full_time_result varchar(1),
    half_time_home_team_goals tinyint,
    half_time_away_team_goals tinyint,
    half_time_result varchar(1),

    -- stats
    attendance bigint,
    referee varchar(100),
    home_team_shots tinyint,
    away_team_shots tinyint,
    home_team_shots_on_target tinyint,
    away_team_shots_on_target tinyint,
    home_team_hit_woodwork tinyint,
    away_team_hit_woodwork tinyint,
    home_team_corners tinyint,
    away_team_corners tinyint,
    home_team_fouls_committed tinyint,
    away_team_fouls_committed tinyint,
    home_team_offsides tinyint,
    away_team_offsides tinyint,
    home_team_yellow_cards tinyint,
    away_team_yellow_cards tinyint,
    home_team_red_cards tinyint,
    away_team_red_cards tinyint
);

CREATE INDEX games_load_index ON games (home_team_id, away_team_id, played_at);


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

Implement COI event type of "IACUC Protocol"

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


CREATE DATABASE [activerecord_unittest];
CREATE DATABASE [activerecord_unittest2];
GO
CREATE LOGIN [rails] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [activerecord_unittest];
EXEC sp_addrolemember N'sysadmin', N'rails';
GO
USE [activerecord_unittest];
CREATE USER [rails] FOR LOGIN [rails];
GO
EXEC sp_addrolemember N'db_owner', N'rails';
GO

Create DB user before giving roles.

CREATE DATABASE [activerecord_unittest];
CREATE DATABASE [activerecord_unittest2];
GO
CREATE LOGIN [rails] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [activerecord_unittest];
GO
EXEC sp_addrolemember N'sysadmin', N'rails';
GO
USE [activerecord_unittest];
CREATE USER [rails] FOR LOGIN [rails];
GO
EXEC sp_addrolemember N'db_owner', N'rails';
GO


-- Store source for movies
ALTER TABLE `movie`
ADD `dateSearch` DATETIME NULL DEFAULT NULL,
ADD `searchCount` TINYINT UNSIGNED NOT NULL,
ADD `identity` TINYINT UNSIGNED NOT NULL,
ADD `quality` TINYINT UNSIGNED NOT NULL,
ADD `score` TINYINT UNSIGNED NOT NULL,
ADD `source` VARCHAR( 1024 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

Use SMALLINT to store high scores

-- Store source for movies
ALTER TABLE `movie`
ADD `dateSearch` DATETIME NULL DEFAULT NULL,
ADD `searchCount` SMALLINT UNSIGNED NOT NULL,
ADD `identity` SMALLINT UNSIGNED NOT NULL,
ADD `quality` SMALLINT UNSIGNED NOT NULL,
ADD `score` SMALLINT UNSIGNED NOT NULL,
ADD `source` VARCHAR( 1024 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;


INSERT INTO oauth_client_details (
  client_id,
  client_secret,
  scope,
  resource_ids,
  authorized_grant_types,
  authorities
)
VALUES (
  'testClient',
  'testClientSecret',
  'read,write',
  'dataPoint',
  'authorization_code,password,refresh_token',
  'ROLE_CLIENT'
);

Add implicit grant type to sample client.

INSERT INTO oauth_client_details (
  client_id,
  client_secret,
  scope,
  resource_ids,
  authorized_grant_types,
  authorities
)
VALUES (
  'testClient',
  'testClientSecret',
  'read,write',
  'dataPoint',
  'authorization_code,implicit,password,refresh_token',
  'ROLE_CLIENT'
);


--
-- resto triggers
--

--
-- On INSERT resto.feature THEN
--
--     1. Split resto.feature._geometry into part
--     2. Store parts within resto.geometry_part
--
CREATE OR REPLACE FUNCTION resto.store_geometry_part()
RETURNS TRIGGER AS $$
DECLARE
    geo_type TEXT;
BEGIN

    -- First get geometry type
    geo_type := GeometryType(NEW._geometry);

    -- Do not touch (MULTI)POINT
    IF geo_type = 'POINT' OR geo_type = 'MULTIPOINT' THEN
        INSERT INTO resto.geometry_part (id, collection, part_num, geom) VALUES (NEW.id, NEW.collection, 1, NEW._geometry);
    ELSE
        INSERT INTO resto.geometry_part
            SELECT NEW.id, NEW.collection, ordinality AS part_num, sub AS geom
            FROM ST_SubDivide(NEW._geometry, 32) WITH ordinality AS sub;
    END IF;

    RETURN NEW;

END
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_geometry_part AFTER INSERT ON resto.feature FOR EACH ROW EXECUTE PROCEDURE resto.store_geometry_part();

Update store_geometry_trigger to support null geometry

--
-- resto triggers
--

--
-- On INSERT resto.feature THEN
--
--     1. Split resto.feature._geometry into part
--     2. Store parts within resto.geometry_part
--
CREATE OR REPLACE FUNCTION resto.store_geometry_part()
RETURNS TRIGGER AS $$
DECLARE
    geo_type TEXT;
BEGIN

    -- Do nothing if _geometry is not set
    IF NEW._geometry IS NULL THEN
        RETURN NEW;
    END IF;

    -- First get geometry type
    geo_type := GeometryType(NEW._geometry);

    -- Do not touch (MULTI)POINT
    IF geo_type = 'POINT' OR geo_type = 'MULTIPOINT' THEN
        INSERT INTO resto.geometry_part (id, collection, part_num, geom) VALUES (NEW.id, NEW.collection, 1, NEW._geometry);
    ELSE
        INSERT INTO resto.geometry_part
            SELECT NEW.id, NEW.collection, ordinality AS part_num, sub AS geom
            FROM ST_SubDivide(NEW._geometry, 32) WITH ordinality AS sub;
    END IF;

    RETURN NEW;

END
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_geometry_part AFTER INSERT ON resto.feature FOR EACH ROW EXECUTE PROCEDURE resto.store_geometry_part();


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

--------------------------------------------------------------------------------------
-- Fix EmployerAgreement to have account id
--------------------------------------------------------------------------------------
DECLARE @AccountEntity TABLE
(
	LegalEntityId bigint,
	AccountId bigint
)
DECLARE @TemplateId int

INSERT INTO @AccountEntity
SELECT ea.LegalEntityId, aea.AccountId
FROM [employer_account].[EmployerAgreement] ea
INNER JOIN [employer_account].[AccountEmployerAgreement] aea
	ON ea.Id = aea.EmployerAgreementId

SET @TemplateId = (SELECT MAX([id]) FROM [employer_account].[EmployerAgreementTemplate])

DELETE FROM [employer_account].[AccountEmployerAgreement]
DELETE FROM [employer_account].[EmployerAgreement]

INSERT INTO [employer_account].[EmployerAgreement] (LegalEntityId, AccountId, TemplateId, StatusId)
SELECT LegalEntityId, AccountId, @TemplateId, 1 FROM @AccountEntity

INSERT INTO [employer_account].[AccountEmployerAgreement]
SELECT AccountId, Id FROM [employer_account].[EmployerAgreement]

ALTER TABLE [employer_account].[EmployerAgreement]
ADD CONSTRAINT [FK_EmployerAgreement_Template] FOREIGN KEY ([TemplateId]) REFERENCES [employer_account].[EmployerAgreementTemplate]([Id])

Remove post deployment to re-add constraint


CREATE INDEX HPA_IDENT_SEARCH ON IIS.IIS_IDENTIFIER
(
    PERSON_IDENT_TYPE_CODE,
    PERSON_IDENTIFIER_VALUE,
    FK_PERSON_IDENTIFIER
) WITH(ONLINE=ON)

CREATE INDEX HPA_IDENT_LOOKUP ON IIS.IIS_IDENTIFIER
(
    FK_PERSON_IDENTIFIER,
    PERSON_IDENT_TYPE_CODE
)INCLUDE(
    PERSON_IDENTIFIER_VALUE
) WITH(ONLINE=ON)

CREATE INDEX HPA_LOL_SEARCH ON IIS.LOSS_OF_LIBERTY
(
    FK_PERSON_IDENTIFIER,
    INMATE_SURNAME,
    INMATE_FORENAME_1,
    DATE_1ST_RECEP
) INCLUDE (
    INMATE_FORENAME_2,
    INMATE_BIRTH_DATE
) WITH(ONLINE=ON)

CREATE INDEX HPA_KA_SEARCH ON IIS.KNOWN_AS
(
    PERSON_SURNAME,
    PERSON_FORENAME_1,
    PERSON_FORENAME_2,
    PERSON_BIRTH_DATE,
    PERSON_SEX,
    FK_PERSON_IDENTIFIER
) WITH(ONLINE=ON)

Update indexes for new search routine

CREATE INDEX HPA_IDENT_SEARCH ON IIS.IIS_IDENTIFIER
(
    PERSON_IDENT_TYPE_CODE,
    PERSON_IDENTIFIER_VALUE,
    FK_PERSON_IDENTIFIER
) WITH(ONLINE=ON)

CREATE INDEX HPA_IDENT_LOOKUP ON IIS.IIS_IDENTIFIER
(
    FK_PERSON_IDENTIFIER,
    PERSON_IDENT_TYPE_CODE
)INCLUDE(
    PERSON_IDENTIFIER_VALUE
) WITH(ONLINE=ON)

CREATE INDEX HPA_LOL_SEARCH ON IIS.LOSS_OF_LIBERTY
(
    PK_PRISON_NUMBER,
    FK_PERSON_IDENTIFIER
)INCLUDE(
    DATE_1ST_RECEP,
    INMATE_SURNAME, INMATE_FORENAME_1, INMATE_FORENAME_2,
    INMATE_BIRTH_DATE
)

CREATE INDEX HPA_KA_SEARCH ON IIS.KNOWN_AS
(
    FK_PERSON_IDENTIFIER,
    PERSON_FORENAME_1,
    PERSON_FORENAME_2,
    PERSON_SURNAME,
    PERSON_BIRTH_DATE,
    PERSON_SEX
)


USE [Footprints]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[cusip_UnhandledTickets]
AS
BEGIN

	SELECT
		COUNT(*) AS Tickets
	FROM
		MASTER4 m
	INNER JOIN
		MASTER4_ABDATA ma
	ON
		m.mrid=ma.mrID
	WHERE
		m.mrSTATUS NOT IN ('Escalated__b__u__bTier__b2','Escalated__b__u__bDevelopment','Escalated__b__u__bCBSW__bDevelopment','Assigned','In__bProgress','Closed','Resolved', '_DELETED_', 'Client__bAcceptance', 'Contracted__bWork', 'Development', 'Pending', 'Deployment')
	AND --First filter all non-support tickets
		m.mrASSIGNEES LIKE 'Support%'
	AND --Check for Support as the only assignee after stripping CCs (which always come at the end of the assignee string)
		RTRIM(LEFT(m.mrASSIGNEES,(
					CASE
						WHEN CHARINDEX('cc',m.mrAssignees) > 0 THEN CHARINDEX('cc',m.mrAssignees) - 1
						ELSE LEN(m.mrAssignees)
					END
		))) = 'Support'
	AND --Empty scheduled call time or the scheduled call time is before tomorrow.
	(
			Scheduled__bCall IS NULL
		OR
			Scheduled__bCall < DATEADD(day,1,CAST(GETDATE() AS DATE))
	)

END

Change Support team assignee handling

USE [Footprints]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[cusip_UnhandledTickets]
AS
BEGIN

	SELECT
		COUNT(*) AS Tickets
	FROM
		MASTER4 m
	INNER JOIN
		MASTER4_ABDATA ma
	ON
		m.mrid=ma.mrID
	WHERE
		m.mrSTATUS NOT IN ('Escalated__b__u__bTier__b2','Escalated__b__u__bDevelopment','Escalated__b__u__bCBSW__bDevelopment','Assigned','In__bProgress','Closed','Resolved', '_DELETED_', 'Client__bAcceptance', 'Contracted__bWork', 'Development', 'Pending', 'Deployment')
	AND
		(m.mrASSIGNEES LIKE 'Support%' OR m.mrASSIGNEES LIKE ' Support%')
	AND --Empty scheduled call time or the scheduled call time is before tomorrow.
	(
			Scheduled__bCall IS NULL
		OR
			Scheduled__bCall < DATEADD(day,1,CAST(GETDATE() AS DATE))
	)

END


CREATE PROCEDURE [employer_account].[GetEmployerAgreementsToRemove_ByAccountId]
	@accountId BIGINT
AS
BEGIN

	SET NOCOUNT ON;

	SELECT	ISNULL(Signed.Id, Pending.Id) AS Id,
			ALE.Name,
			ISNULL(Signed.StatusId, Pending.StatusId) AS Status,
			a.HashedId,
			le.Code
	FROM	employer_account.AccountLegalEntity AS ALE
			JOIN employer_account.Account AS A
				ON A.Id = ALE.AccountId
			JOIN employer_account.LegalEntity AS LE
				ON LE.Id = ALE.LegalEntityId
			LEFT JOIN employer_account.EmployerAgreement AS Pending
				ON Pending.AccountLegalEntityId = ALE.ID
					AND Pending.StatusId = 1
			LEFT JOIN employer_account.EmployerAgreement AS Signed
				ON Signed.AccountLegalEntityId = ALE.ID
					AND Signed.StatusId = 2
	WHERE	ALE.AccountId = 6;

END;

Fix bug in remove organisation

CREATE PROCEDURE [employer_account].[GetEmployerAgreementsToRemove_ByAccountId]
	@accountId BIGINT
AS
BEGIN

	SET NOCOUNT ON;

	SELECT	ISNULL(Signed.Id, Pending.Id) AS Id,
			ALE.Name,
			ISNULL(Signed.StatusId, Pending.StatusId) AS Status,
			a.HashedId,
			le.Code
	FROM	employer_account.AccountLegalEntity AS ALE
			JOIN employer_account.Account AS A
				ON A.Id = ALE.AccountId
			JOIN employer_account.LegalEntity AS LE
				ON LE.Id = ALE.LegalEntityId
			LEFT JOIN employer_account.EmployerAgreement AS Pending
				ON Pending.AccountLegalEntityId = ALE.ID
					AND Pending.StatusId = 1
			LEFT JOIN employer_account.EmployerAgreement AS Signed
				ON Signed.AccountLegalEntityId = ALE.ID
					AND Signed.StatusId = 2
	WHERE	ALE.Deleted IS NULL
			AND ALE.AccountId = @accountId;

END;