# We include default installations of WordPress with this Vagrant setup.
# In order for that to respond properly, default databases should be
# available for use.
CREATE DATABASE IF NOT EXISTS `wordpress_default`;
GRANT ALL PRIVILEGES ON `wordpress_default`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
CREATE DATABASE IF NOT EXISTS `wordpress_trunk`;
GRANT ALL PRIVILEGES ON `wordpress_trunk`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
CREATE DATABASE IF NOT EXISTS `wordpress_develop`;
GRANT ALL PRIVILEGES ON `wordpress_develop`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
CREATE DATABASE IF NOT EXISTS `wordpress_unit_tests`;
GRANT ALL PRIVILEGES ON `wordpress_unit_tests`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';

# Create an external user with privileges on all databases in mysql so
# that a connection can be made from the local machine without an SSH tunnel
GRANT ALL PRIVILEGES ON *.* TO 'external'@'%' IDENTIFIED BY 'external';

Allow connections to wordpress_unit_tests database from host

# We include default installations of WordPress with this Vagrant setup.
# In order for that to respond properly, default databases should be
# available for use.
CREATE DATABASE IF NOT EXISTS `wordpress_default`;
GRANT ALL PRIVILEGES ON `wordpress_default`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
CREATE DATABASE IF NOT EXISTS `wordpress_trunk`;
GRANT ALL PRIVILEGES ON `wordpress_trunk`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
CREATE DATABASE IF NOT EXISTS `wordpress_develop`;
GRANT ALL PRIVILEGES ON `wordpress_develop`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
CREATE DATABASE IF NOT EXISTS `wordpress_unit_tests`;
GRANT ALL PRIVILEGES ON `wordpress_unit_tests`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';
GRANT ALL PRIVILEGES ON `wordpress_unit_tests`.* TO 'wp'@'192.168.50.1' IDENTIFIED BY 'wp';

# Create an external user with privileges on all databases in mysql so
# that a connection can be made from the local machine without an SSH tunnel
GRANT ALL PRIVILEGES ON *.* TO 'external'@'%' IDENTIFIED BY 'external';


# We include a default installation of WordPress with this Vagrant setup.
# In order for that to respond properly, a default database should be
# available for it to use.
CREATE DATABASE IF NOT EXISTS `wordpress_default`;
GRANT ALL PRIVILEGES ON `wordpress_default`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';

# Create an external user with privileges on all databases in mysql so
# that a connection can be made from the local machine without an SSH tunnel
#
# We use a stored procedure for this because the mysql.user table is persistent after
# the first `vagrant up` and an error will be thrown if we try to create a
# user that already exists. So... a lot of lines of code to prevent red in our boot. :)
drop procedure if exists createExternalUser;
delimiter $$
create procedure createExternalUser(username varchar(50), pw varchar(50))
begin
IF (SELECT EXISTS(SELECT 1 FROM `mysql`.`user` WHERE `user` = 'external')) = 0 THEN
    begin
    set @sql = CONCAT('CREATE USER ', username, '@\'%\' IDENTIFIED BY \'external\'');
    prepare stmt from @sql;
    execute stmt;
    deallocate prepare stmt;
    end;
END IF;
end $$
delimiter ;

# Use the stored procedure to create our external user
call createExternalUser( 'external', 'external' );
GRANT ALL PRIVILEGES ON *.* TO 'external'@'%';

Remove stored procedure due to wth

# We include a default installation of WordPress with this Vagrant setup.
# In order for that to respond properly, a default database should be
# available for it to use.
CREATE DATABASE IF NOT EXISTS `wordpress_default`;
GRANT ALL PRIVILEGES ON `wordpress_default`.* TO 'wp'@'localhost' IDENTIFIED BY 'wp';

# Create an external user with privileges on all databases in mysql so
# that a connection can be made from the local machine without an SSH tunnel
GRANT ALL PRIVILEGES ON *.* TO 'external'@'%' IDENTIFIED BY 'external';


--DROP FUNCTION insertDataset;

/**
 * This function inserts a row in the datasets table,
 * and returns the value of the id under which it is known.
 * If the dataset name (dsinname) already exists, a new row is added
 * and the rerun value is incremented by 1. If not, it is set
 * to its default value 0.
 *
 * Note: To be able to create a function that modifies data
 * (by insertion) we have to set the global bin log var:
 * mysql> SET GLOBAL log_bin_trust_function_creators = 1;
 *
 */
CREATE FUNCTION insertDataset(idescription VARCHAR(50)) RETURNS INT
BEGIN

  DECLARE idsid INT;
  DECLARE odsid INT;
  DECLARE irerun INT;

  SELECT MAX(rerun)
    INTO irerun
    FROM dataset
   WHERE description = idescription
  ;

  IF irerun IS NULL THEN
    SET irerun = 0;
  ELSE
    SET irerun = irerun + 1;
  END IF;

  SELECT NEXT VALUE FOR seq_dataset INTO idsid;

  INSERT INTO dataset
    (id
    ,rerun
    ,process_ts
    ,description
    )
  VALUES
    (idsid
    ,irerun
    ,NOW()
    ,idescription
    )
  ;

  SET odsid = idsid;
  RETURN odsid;

END;

Increase max length of dataset.description to 100 chars.

--DROP FUNCTION insertDataset;

/**
 * This function inserts a row in the datasets table,
 * and returns the value of the id under which it is known.
 * If the dataset name (description field) already exists, a new row is added
 * and the rerun value is incremented by 1. If not, it is set
 * to its default value 0.
 *
 * Note: To be able to create a function that modifies data
 * (by insertion) we have to set the global bin log var:
 * mysql> SET GLOBAL log_bin_trust_function_creators = 1;
 *
 */
CREATE FUNCTION insertDataset(idescription VARCHAR(100)) RETURNS INT
BEGIN

  DECLARE idsid INT;
  DECLARE odsid INT;
  DECLARE irerun INT;

  SELECT MAX(rerun)
    INTO irerun
    FROM dataset
   WHERE description = idescription
  ;

  IF irerun IS NULL THEN
    SET irerun = 0;
  ELSE
    SET irerun = irerun + 1;
  END IF;

  SELECT NEXT VALUE FOR seq_dataset INTO idsid;

  INSERT INTO dataset
    (id
    ,rerun
    ,process_ts
    ,description
    )
  VALUES
    (idsid
    ,irerun
    ,NOW()
    ,idescription
    )
  ;

  SET odsid = idsid;
  RETURN odsid;

END;


CREATE TABLE `LongTextTest`( entry1 LONGTEXT NOT NULL , entry2 MEDIUMTEXT NOT NULL , entry3 TEXT NOT NULL , entry4 VARCHAR(255) NOT NULL );

CREATE TABLE `table_name`( id INT NOT NULL );

Replace nbsp by proper space in SQL setup script


CREATE TABLE "pogues" (id VARCHAR PRIMARY KEY, data jsonb);
-- Initialize ddi root ids
CREATE TABLE "ddi_group" (id VARCHAR PRIMARY KEY);
INSERT INTO "ddi_group" VALUES ('41c2eca1-8b18-4932-809c-9330ca00fef3');

Set proper root id on docker container

CREATE TABLE "pogues" (id VARCHAR PRIMARY KEY, data jsonb);
-- Initialize ddi root ids
CREATE TABLE "ddi_group" (id VARCHAR PRIMARY KEY);
INSERT INTO "ddi_group" VALUES ('5696739a-751c-4603-a0bc-e4c05bd41c83');


CREATE TABLE SPRING_SESSION (
	PRIMARY_ID CHAR(36) NOT NULL,
	SESSION_ID CHAR(36) NOT NULL,
	CREATION_TIME BIGINT NOT NULL,
	LAST_ACCESS_TIME BIGINT NOT NULL,
	MAX_INACTIVE_INTERVAL INT NOT NULL,
	EXPIRY_TIME BIGINT NOT NULL,
	PRINCIPAL_NAME VARCHAR(100),
	CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
);

CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);

CREATE TABLE SPRING_SESSION_ATTRIBUTES (
	SESSION_PRIMARY_ID CHAR(36) NOT NULL,
	ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
	ATTRIBUTE_BYTES BYTEA NOT NULL,
	CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
	CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
);

CREATE INDEX SPRING_SESSION_ATTRIBUTES_IX1 ON SPRING_SESSION_ATTRIBUTES (SESSION_PRIMARY_ID);

Remove redundant index in JDBC schema scripts


DROP DATABASE IF EXISTS italian_workflow;

CREATE DATABASE italian_workflow;

USE italian_workflow;

CREATE TABLE Users (
    user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    username VARCHAR(40) NOT NULL,
    password CHAR(60) NOT NULL,
    PRIMARY KEY (user_id),
    UNIQUE (username)
);

CREATE TABLE GiftCertificates (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    amount DECIMAL(10, 2) NOT NULL,
    sold_date DATE NOT NULL,
    seller_id INT UNSIGNED NOT NULL,
    redeemed_date DATE,
    PRIMARY KEY (id),
    FOREIGN KEY (seller_id) REFERENCES Users (user_id) ON DELETE NO ACTION ON UPDATE NO ACTION
);

Add fields to Users and GC tables

DROP DATABASE IF EXISTS italian_workflow;

CREATE DATABASE italian_workflow;

USE italian_workflow;

CREATE TABLE Users (
    user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    username VARCHAR(40) NOT NULL,
    password CHAR(60) NOT NULL,
    last_name VARCHAR(20),
    first_name VARCHAR(20),
    PRIMARY KEY (user_id),
    UNIQUE (username)
);

CREATE TABLE GiftCertificates (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    amount DECIMAL(10, 2) NOT NULL,
    sale_price,
    date_sold DATE NOT NULL,
    redeemed_date DATE,
    issuer_id INT UNSIGNED NOT NULL,
    memo VARCHAR(140),
    PRIMARY KEY (id),
    FOREIGN KEY (issuer_id) REFERENCES Users (user_id) ON DELETE NO ACTION ON UPDATE NO ACTION
);


-- This SQL script contains the instruction to create the tables.
-- WARNING: all the tables are dropped and recreated

-- Sentences table
DROP TABLE IF EXISTS sentences CASCADE;
CREATE TABLE sentences (
	docid text,
	sentid text,
	wordidxs int[],
	words text[],
	poses text[],
	ners text[],
	lemmas text[],
	dep_paths text[],
	dep_parents int[],
	bounding_boxes text[]
);

Convert sentid to an int

-- This SQL script contains the instruction to create the tables.
-- WARNING: all the tables are dropped and recreated

-- Sentences table
DROP TABLE IF EXISTS sentences CASCADE;
CREATE TABLE sentences (
	docid text,
	sentid int,
	wordidxs int[],
	words text[],
	poses text[],
	ners text[],
	lemmas text[],
	dep_paths text[],
	dep_parents int[],
	bounding_boxes text[]
);


/* contrib/hstore/hstore--1.1--1.2.sql */

-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION hstore UPDATE TO '1.2'" to load this file. \quit

-- A version of 1.1 was shipped with these objects mistakenly in 9.3.0.
-- Therefore we only add them if we detect that they aren't already there and
-- dependent on the extension.

DO LANGUAGE plpgsql

$$

BEGIN

   PERFORM 1
   FROM pg_proc p
       JOIN  pg_depend d
          ON p.proname = 'hstore_to_json_loose'
            AND d.objid = p.oid
            AND d.refclassid = 'pg_extension'::regclass
       JOIN pg_extension x
          ON d.refobjid = x.oid
            AND  x.extname = 'hstore';

   IF NOT FOUND
   THEN

        CREATE FUNCTION hstore_to_json(hstore)
        RETURNS json
        AS 'MODULE_PATHNAME', 'hstore_to_json'
        LANGUAGE C IMMUTABLE STRICT;

        CREATE CAST (hstore AS json)
          WITH FUNCTION hstore_to_json(hstore);

        CREATE FUNCTION hstore_to_json_loose(hstore)
        RETURNS json
        AS 'MODULE_PATHNAME', 'hstore_to_json_loose'
        LANGUAGE C IMMUTABLE STRICT;

   END IF;

END;

$$;

Add missing condition for pg_depend in hstore migration script.

/* contrib/hstore/hstore--1.1--1.2.sql */

-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION hstore UPDATE TO '1.2'" to load this file. \quit

-- A version of 1.1 was shipped with these objects mistakenly in 9.3.0.
-- Therefore we only add them if we detect that they aren't already there and
-- dependent on the extension.

DO LANGUAGE plpgsql

$$

BEGIN

   PERFORM 1
   FROM pg_proc p
       JOIN  pg_depend d
          ON p.proname = 'hstore_to_json_loose'
            AND d.classid = 'pg_proc'::regclass
            AND d.objid = p.oid
            AND d.refclassid = 'pg_extension'::regclass
       JOIN pg_extension x
          ON d.refobjid = x.oid
            AND  x.extname = 'hstore';

   IF NOT FOUND
   THEN

        CREATE FUNCTION hstore_to_json(hstore)
        RETURNS json
        AS 'MODULE_PATHNAME', 'hstore_to_json'
        LANGUAGE C IMMUTABLE STRICT;

        CREATE CAST (hstore AS json)
          WITH FUNCTION hstore_to_json(hstore);

        CREATE FUNCTION hstore_to_json_loose(hstore)
        RETURNS json
        AS 'MODULE_PATHNAME', 'hstore_to_json_loose'
        LANGUAGE C IMMUTABLE STRICT;

   END IF;

END;

$$;


DROP TABLE IF EXISTS pageviews;
DROP TABLE if exists visitors;
DROP TABLE IF EXISTS pages;
DROP TABLE IF EXISTS sites;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS archive;

Add DROP TABLE statements for new total tables.

DROP TABLE IF EXISTS pageviews;
DROP TABLE if exists visitors;
DROP TABLE IF EXISTS pages;
DROP TABLE IF EXISTS sites;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS total_pageviews;
DROP TABLE IF EXISTS total_visitors;
DROP TABLE IF EXISTS total_browser_languages;
DROP TABLE IF EXISTS total_screens;
DROP TABLE IF EXISTS total_browser_names;
DROP TABLE IF EXISTS total_referrers;
DROP TABLE IF EXISTS options;


DROP TYPE IF EXISTS app;
CREATE TYPE app AS ENUM ('telegram', 'vk');

CREATE TABLE IF NOT EXISTS rooms (
  id SERIAL PRIMARY KEY,
  owner_id BIGINT NOT NULL,
  owner_app app NOT NULL,
  guest_id BIGINT,
  guest_app app,
  active BOOLEAN NOT NULL DEFAULT TRUE,
  created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW()

  CHECK (owner_id <> guest_id OR owner_app <> guest_app)
  CHECK ((guest_id IS NOT NULL AND guest_app IS NOT NULL) OR (guest_id IS NULL AND guest_app IS NULL))
);

CREATE UNIQUE INDEX ON rooms (owner_id) WHERE active = 'true';

Add owner_app to owner_id unique index

DROP TYPE IF EXISTS app;
CREATE TYPE app AS ENUM ('telegram', 'vk');

CREATE TABLE IF NOT EXISTS rooms (
  id SERIAL PRIMARY KEY,
  owner_id BIGINT NOT NULL,
  owner_app app NOT NULL,
  guest_id BIGINT,
  guest_app app,
  active BOOLEAN NOT NULL DEFAULT TRUE,
  created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW()

  CHECK (owner_id <> guest_id OR owner_app <> guest_app)
  CHECK ((guest_id IS NOT NULL AND guest_app IS NOT NULL) OR (guest_id IS NULL AND guest_app IS NULL))
);

CREATE UNIQUE INDEX ON rooms (owner_id, owner_app) WHERE active = 'true';


DROP TABLE IF EXISTS log_tags;

CREATE TABLE log_tags
(
  id serial not null,
  logdate character varying(100),
  logtime character varying(100),
  temperature character varying(100),
    events character varying(100)

)
WITH (
  OIDS=FALSE
);
ALTER TABLE log_tags
  OWNER TO postgres;


CREATE PROCEDURE [dbo].[InsertHistory]
	@entityType NVARCHAR(50) = NULL,
	@entityId BIGINT = NULL,
	@commitmentId BIGINT NULL = NULL,
	@apprenticeshipId BIGINT = NULL,
    @userId NVARCHAR(50),
    @updatedByRole NVARCHAR(50),
    @changeType NVARCHAR(50),
    @updatedByName NVARCHAR(255),
	@providerId BIGINT,
	@employerAccountId BIGINT,
    @originalState NVARCHAR(MAX),
    @updatedState NVARCHAR(MAX),
	@createdOn DATETIME
AS

IF @entityType IS NOT NULL AND @entityId IS NOT NULL
BEGIN
	IF @entityType = 'Commitment'
	BEGIN
		SET @commitmentId = @entityId
		SET @apprenticeshipId = null
	END

	IF @entityType = 'Apprenticeship'
	BEGIN
		SET @commitmentId = null
		SET @apprenticeshipId = @entityId
	END
END

INSERT INTO [dbo].[History]
	(CommitmentId, ApprenticeshipId, UserId, UpdatedByRole, ChangeType, CreatedOn, ProviderId, EmployerAccountId, UpdatedByName, OriginalState, UpdatedState)
VALUES
	(@commitmentId, @apprenticeshipId, @userId, @updatedByRole, @changeType, @createdOn, @providerId, @employerAccountId, @updatedByName, @originalState, @updatedState)

Correct incorrect SQL on table definition

CREATE PROCEDURE [dbo].[InsertHistory]
	@entityType NVARCHAR(50) = NULL,
	@entityId BIGINT = NULL,
	@commitmentId BIGINT = NULL,
	@apprenticeshipId BIGINT = NULL,
    @userId NVARCHAR(50),
    @updatedByRole NVARCHAR(50),
    @changeType NVARCHAR(50),
    @updatedByName NVARCHAR(255),
	@providerId BIGINT,
	@employerAccountId BIGINT,
    @originalState NVARCHAR(MAX),
    @updatedState NVARCHAR(MAX),
	@createdOn DATETIME
AS

IF @entityType IS NOT NULL AND @entityId IS NOT NULL
BEGIN
	IF @entityType = 'Commitment'
	BEGIN
		SET @commitmentId = @entityId
		SET @apprenticeshipId = null
	END

	IF @entityType = 'Apprenticeship'
	BEGIN
		SET @commitmentId = null
		SET @apprenticeshipId = @entityId
	END
END

INSERT INTO [dbo].[History]
	(CommitmentId, ApprenticeshipId, UserId, UpdatedByRole, ChangeType, CreatedOn, ProviderId, EmployerAccountId, UpdatedByName, OriginalState, UpdatedState)
VALUES
	(@commitmentId, @apprenticeshipId, @userId, @updatedByRole, @changeType, @createdOn, @providerId, @employerAccountId, @updatedByName, @originalState, @updatedState)


/********************************************************************************
	component-centric-snapshot-description-unique-term-in-concept

	Assertion:
	For a given concept, all active description terms are unique.

	Implementation is limited to active descriptions of active concepts edited
	in the current prospective release.
********************************************************************************/
/*  violators have the same term twice within a concept */
	insert into qa_result (runid, assertionuuid, concept_id, details)
	select
		<RUNID>,
		'<ASSERTIONUUID>',
		c.conceptid,
		concat('DESC: Id=', c.id, ': non-unique term within concept.')
	from res_concepts_edited a
		join curr_concept_s b
			on a.conceptid = b.id
			and b.active = 1
		join curr_description_s c
			on c.conceptid = a.conceptid
			and c.active = 1
	group by c.conceptid,  binary c.term
	having count(c.conceptid) > 1
	and binary count(c.term) > 1;

Update unique term validation to include the language code

/********************************************************************************
	component-centric-snapshot-description-unique-term-in-concept

	Assertion:
	For a given concept, all active description terms are unique.

	Implementation is limited to active descriptions of active concepts edited
	in the current prospective release.
********************************************************************************/
/*  violators have the same term twice within a concept */
	insert into qa_result (runid, assertionuuid, concept_id, details)
	select
		<RUNID>,
		'<ASSERTIONUUID>',
		c.conceptid,
		concat('DESC: Id=', c.id, ': non-unique term within concept.')
	from res_concepts_edited a
		join curr_concept_s b
			on a.conceptid = b.id
			and b.active = 1
		join curr_description_s c
			on c.conceptid = a.conceptid
			and c.active = 1
	group by c.conceptid, c.languagecode, binary c.term
	having count(c.conceptid) > 1
	and binary count(c.term) > 1;


--
-- Copyright 2022 The Android Open Source Project
--
-- 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
--
--     https://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.
--

DROP VIEW IF EXISTS launch_async_events;
CREATE VIEW launch_async_events AS
SELECT
  ts,
  dur,
  SUBSTR(name, 19) id
FROM slice
WHERE
  name GLOB 'launchingActivity#*'
  AND dur != 0
  AND INSTR(name, ':') = 0;

DROP VIEW IF EXISTS launch_complete_events;
CREATE VIEW launch_complete_events AS
SELECT
  STR_SPLIT(completed, ':completed:', 0) id,
  STR_SPLIT(completed, ':completed:', 1) package_name
FROM (
  SELECT SUBSTR(name, 19) completed
  FROM slice
  WHERE dur = 0 AND name GLOB 'launchingActivity#*:completed:*'
);

INSERT INTO launches(id, ts, ts_end, dur, package)
SELECT
  id,
  ts,
  ts + dur ts_end,
  dur,
  package_name
FROM launch_async_events JOIN launch_complete_events USING (id);

Handle duplicate end slice events in startup traces

--
-- Copyright 2022 The Android Open Source Project
--
-- 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
--
--     https://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.
--

DROP VIEW IF EXISTS launch_async_events;
CREATE VIEW launch_async_events AS
SELECT
  ts,
  dur,
  SUBSTR(name, 19) id
FROM slice
WHERE
  name GLOB 'launchingActivity#*'
  AND dur != 0
  AND INSTR(name, ':') = 0;

DROP VIEW IF EXISTS launch_complete_events;
CREATE VIEW launch_complete_events AS
SELECT
  STR_SPLIT(completed, ':completed:', 0) id,
  STR_SPLIT(completed, ':completed:', 1) package_name,
  MIN(ts)
FROM (
  SELECT ts, SUBSTR(name, 19) completed
  FROM slice
  WHERE dur = 0 AND name GLOB 'launchingActivity#*:completed:*'
)
GROUP BY 1, 2;

INSERT INTO launches(id, ts, ts_end, dur, package)
SELECT
  id,
  ts,
  ts + dur ts_end,
  dur,
  package_name
FROM launch_async_events JOIN launch_complete_events USING (id);


ALTER TABLE "organism_representation" ADD "license" text NOT NULL;
COMMENT ON COLUMN "organism_representation"."license" IS 'License of the obtained representation.';

Fix NOT NULL in license column

ALTER TABLE "organism_representation" ADD "license" text NULL;
COMMENT ON COLUMN "organism_representation"."license" IS 'License of the obtained representation.';


CREATE TABLE users (
    id                 SERIAL          PRIMARY KEY,
    username           VARCHAR( 64 )   UNIQUE,
    encrypted_password VARCHAR( 512 ),
    time_created       TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    openid             VARCHAR( 1024 ) UNIQUE,
    admin              BOOLEAN         NOT NULL DEFAULT FALSE,
    CONSTRAINT identifiable CHECK (
        (
            username IS NOT NULL
            AND encrypted_password IS NOT NULL
        ) OR openid IS NOT NULL
    )
);

Change SQL to leading comma style.

CREATE TABLE users (
      id                 SERIAL          PRIMARY KEY
    , username           VARCHAR( 64 )   UNIQUE
    , encrypted_password VARCHAR( 512 )
    , time_created       TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP
    , openid             VARCHAR( 1024 ) UNIQUE
    , admin              BOOLEAN         NOT NULL DEFAULT FALSE
    , CONSTRAINT identifiable CHECK (
        (
            username IS NOT NULL
            AND encrypted_password IS NOT NULL
        ) OR openid IS NOT NULL
    )
);


-- hash index takes too long to create
CREATE INDEX posts_post_type_id_idx on Posts USING btree (PostTypeId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_score_idx on Posts USING btree (Score)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_creation_date_idx on Posts USING btree (CreationDate)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_owner_user_id_idx on Posts USING hash (OwnerUserId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_answer_count_idx on Posts USING btree (AnswerCount)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_comment_count_idx on Posts USING btree (CommentCount)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_favorite_count_idx on Posts USING btree (FavoriteCount)
       WITH (FILLFACTOR = 100);

-- Composite indexes (optional)
CREATE INDEX posts_id_post_type_id_idx on Posts USING btree (Id, PostTypeId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_id_parent_id_idx on Posts USING btree (Id, ParentId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_id_accepted_answers_id_idx on Posts USING btree (Id, AcceptedAnswerId)
       WITH (FILLFACTOR = 100);

Add an index on ViewCount in Posts table.

-- hash index takes too long to create
CREATE INDEX posts_post_type_id_idx on Posts USING btree (PostTypeId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_score_idx on Posts USING btree (Score)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_creation_date_idx on Posts USING btree (CreationDate)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_owner_user_id_idx on Posts USING hash (OwnerUserId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_answer_count_idx on Posts USING btree (AnswerCount)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_comment_count_idx on Posts USING btree (CommentCount)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_favorite_count_idx on Posts USING btree (FavoriteCount)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_viewcount_idx on Posts USING btree (ViewCount)
       WITH (FILLFACTOR = 100);

-- Composite indexes (optional)
CREATE INDEX posts_id_post_type_id_idx on Posts USING btree (Id, PostTypeId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_id_parent_id_idx on Posts USING btree (Id, ParentId)
       WITH (FILLFACTOR = 100);
CREATE INDEX posts_id_accepted_answers_id_idx on Posts USING btree (Id, AcceptedAnswerId)
       WITH (FILLFACTOR = 100);


create table games (
    id char(48) primary key,
    data jsonb
);

Add db index to improve speed of get_open_games

create table games (
    id char(48) primary key,
    data jsonb
);

create index idx_get_open_games on games((data->>'active'), (data->>'ended_at'), (data->>'created_at'));


alter table `users` add `ls` int not null default 0;

Fix Cote d'Ivoire name in countries table

alter table `users` add `ls` int not null default 0;

set names utf8;
update `countries` set `name`='Кот-д\'Ивуар', `name_en`='Cote d\'Ivoire' where id=385;


use hutmap;
drop table if exists auth_group;
drop table if exists auth_group_permissions;
drop table if exists auth_message;
drop table if exists auth_permission;
drop table if exists auth_user;
drop table if exists auth_user_groups;
drop table if exists auth_user_user_permissions;
drop table if exists django_admin_log;
drop table if exists django_content_type;
drop table if exists django_session;
drop table if exists django_site;

Drop huts tables as well.

use hutmap;
drop table if exists auth_group;
drop table if exists auth_group_permissions;
drop table if exists auth_message;
drop table if exists auth_permission;
drop table if exists auth_user;
drop table if exists auth_user_groups;
drop table if exists auth_user_user_permissions;
drop table if exists django_admin_log;
drop table if exists django_content_type;
drop table if exists django_session;
drop table if exists django_site;
drop table if exists huts_hut;
drop table if exists huts_region;
drop table if exists huts_agency;


CREATE TABLE job(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     description TEXT NOT NULL,
     user_name TEXT NOT NULL,
     calculation_mode TEXT NOT NULL,
     hazard_calculation_id INTEGER REFERENCES job (id) ON DELETE CASCADE,
     status TEXT NOT NULL DEFAULT 'created',
     is_running BOOL NOT NULL DEFAULT false,
     start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     stop_time TIMESTAMP,
     relevant BOOL DEFAULT true,
     ds_calc_dir TEXT NOT NULL);

CREATE TABLE log(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     timestamp TIMESTAMP NOT NULL,
     level TEXT NOT NULL,
     process TEXT NOT NULL,
     message TEXT NOT NULL);

CREATE TABLE output(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     oq_job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     display_name TEXT NOT NULL,
     last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     ds_key TEXT NOT NULL);

CREATE TABLE performance(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     operation TEXT NOT NULL,
     time_sec FLOAT NOT NULL,
     memory_mb FLOAT NOT NULL,
     counts INTEGER NOT NULL);

Set relevant default to 1 instead of true

CREATE TABLE job(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     description TEXT NOT NULL,
     user_name TEXT NOT NULL,
     calculation_mode TEXT NOT NULL,
     hazard_calculation_id INTEGER REFERENCES job (id) ON DELETE CASCADE,
     status TEXT NOT NULL DEFAULT 'created',
     is_running BOOL NOT NULL DEFAULT false,
     start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     stop_time TIMESTAMP,
     relevant BOOL DEFAULT 1,
     ds_calc_dir TEXT NOT NULL);

CREATE TABLE log(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     timestamp TIMESTAMP NOT NULL,
     level TEXT NOT NULL,
     process TEXT NOT NULL,
     message TEXT NOT NULL);

CREATE TABLE output(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     oq_job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     display_name TEXT NOT NULL,
     last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     ds_key TEXT NOT NULL);

CREATE TABLE performance(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     operation TEXT NOT NULL,
     time_sec FLOAT NOT NULL,
     memory_mb FLOAT NOT NULL,
     counts INTEGER NOT NULL);


CREATE DATABASE notes;

USE notes;

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

CREATE TABLE user_preferences (
	PreferenceId INT(4) PRIMARY KEY,
	UserId VARCHAR(60),
    TagColor VARCHAR(6) DEFAULT 'red',
	Pagination TINYINT DEFAULT '1',
	NoteOrder VARCHAR(15) DEFAULT 'oldest',
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);

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

Add the new column for search options

CREATE DATABASE notes;

USE notes;

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

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

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


CREATE TABLE users (
	id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
	name VARCHAR(25) NOT NULL,
	password VARCHAR(65),
	CONSTRAINT users_pk PRIMARY KEY (id)
);

CREATE TABLE documents (
	id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
	category VARCHAR(25) NOT NULL,
	description VARCHAR(1000),
	due_date DATE,
	owner_fk INTEGER,
	CONSTRAINT documents_pk PRIMARY KEY (id)
);

CREATE TABLE attachments (
	id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
	name VARCHAR(250) NOT NULL,
	upload_time TIMESTAMP,
	document_fk INTEGER NOT NULL,
	CONSTRAINT attachments_pk PRIMARY KEY (id)
);

Add constraints to schema creation.

CREATE TABLE users (
	id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
	name VARCHAR(25) NOT NULL,
	password VARCHAR(65),
	CONSTRAINT users_pk PRIMARY KEY (id)
);

CREATE TABLE documents (
	id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
	category VARCHAR(25) NOT NULL,
	description VARCHAR(1000),
	due_date DATE,
	owner_fk INTEGER,
	CONSTRAINT documents_pk PRIMARY KEY (id),
	CONSTRAINT document_owner_fk FOREIGN KEY (owner_fk) REFERENCES users(id)
);

CREATE TABLE attachments (
	id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
	name VARCHAR(250) NOT NULL,
	upload_time TIMESTAMP,
	document_fk INTEGER NOT NULL,
	CONSTRAINT attachments_pk PRIMARY KEY (id),
	CONSTRAINT document_attachments_fk FOREIGN KEY (document_fk) REFERENCES documents(id)
);


create table dependencies (
    project_uuid varchar(255) not null references projects(uuid),
    dependent_project_uuid varchar(255) not null references projects(uuid),
    source_cd smallint not null default 0
);

Add compund index to dependencies table

create table dependencies (
    project_uuid varchar(255) not null references projects(uuid),
    dependent_project_uuid varchar(255) not null references projects(uuid),
    source_cd smallint not null default 0
);

create index index_dependencies_on_project_uuid_and_dependent_project_uuid on (project_uuid, dependent_project_uuid);


CREATE TABLE wof_neighbourhood (
  wof_id BIGINT UNIQUE NOT NULL,
  name TEXT NOT NULL,
  hash TEXT NOT NULL,
  n_photos INTEGER,
  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);

Add placetype column to WOF neighbourhood table

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


\connect airline

CREATE TABLE PNRNum (
  PNR        CHAR(6)     PRIMARY KEY,
  Booking_ID BIGINT      NOT NULL,
  Claimed_At TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE Flight (
  Flight_ID           BIGINT      PRIMARY KEY,
  Departure_Date      DATE        NOT NULL,
  Departure_Location  CHAR(3)     NOT NULL,
  Flight_Code         VARCHAR(7)  NOT NULL,
  Scheduled_Departure TIMESTAMPTZ NOT NULL,
  Arrival_Location    CHAR(3)     NOT NULL,
  Scheduled_Arrival   TIMESTAMPTZ NOT NULL,
  UNIQUE (Departure_Date, Departure_Location, Flight_code),
  CONSTRAINT DEPART_BEFORE_ARRIVE CHECK (Scheduled_Departure < Scheduled_Arrival )
);

GRANT USAGE ON SCHEMA public TO admin;
GRANT USAGE ON SCHEMA public TO clerk;

GRANT SELECT,INSERT ON ALL TABLES IN SCHEMA public TO admin;
GRANT SELECT,INSERT ON ALL TABLES IN SCHEMA public TO clerk;

COPY Flight FROM '/home/vagrant/flights.csv' CSV;

Remove constraint flight depart before arrive

\connect airline

CREATE TABLE PNRNum (
  PNR        CHAR(6)     PRIMARY KEY,
  Booking_ID BIGINT      NOT NULL,
  Claimed_At TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE Flight (
  Flight_ID           BIGINT      PRIMARY KEY,
  Departure_Date      DATE        NOT NULL,
  Departure_Location  CHAR(3)     NOT NULL,
  Flight_Code         VARCHAR(7)  NOT NULL,
  Scheduled_Departure TIMESTAMPTZ NOT NULL,
  Arrival_Location    CHAR(3)     NOT NULL,
  Scheduled_Arrival   TIMESTAMPTZ NOT NULL,
  UNIQUE (Departure_Date, Departure_Location, Flight_code)
);

--  CONSTRAINT DEPART_BEFORE_ARRIVE CHECK (Scheduled_Departure < Scheduled_Arrival )

GRANT USAGE ON SCHEMA public TO admin;
GRANT USAGE ON SCHEMA public TO clerk;

GRANT SELECT,INSERT ON ALL TABLES IN SCHEMA public TO admin;
GRANT SELECT,INSERT ON ALL TABLES IN SCHEMA public TO clerk;

COPY Flight FROM '/mnt/bootstrap/flights.csv' CSV;


-- -------------------
-- Apply changes to rename abac, identity, account portal tables
-- (make inaccessible from existing code, but archive)
-- See geni-portal #299
-- -------------------

alter table abac_assertion rename to abac_assertion_299;
alter table abac rename to abac_299;
alter table account_privilege rename to account_privilege_299;
alter table account rename to account_299;
alter table identity_attribute rename to identity_attribute_299;
alter table identity rename to identity_299;
alter table requested_account rename to requested_account_299;

Add requested_account to list of tables to remove


-- Attach the trigger to the requested table

CREATE TRIGGER "update_log_{db_table}"
    AFTER INSERT OR UPDATE OR DELETE
    ON "{db_table}"
    FOR EACH ROW
    EXECUTE PROCEDURE pubsubpull_change_recorder();

Drop existing trigger before re-creating.

-- Attach the trigger to the requested table

DROP TRIGGER IF EXISTS "update_log_{db_table}" ON "{db_table}";

CREATE TRIGGER "update_log_{db_table}"
    AFTER INSERT OR UPDATE OR DELETE
    ON "{db_table}"
    FOR EACH ROW
    EXECUTE PROCEDURE pubsubpull_change_recorder();


CREATE TABLE user (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(64) NOT NULL,
    issuer VARCHAR(256) NOT NULL,
    subject VARCHAR(256) NOT NULL,
    name TEXT NOT NULL,
    email TEXT NOT NULL,
    UNIQUE issuer_subject_unique (issuer, subject)
);

CREATE TABLE capture (
    id BIGINT NOT NULL AUTO_INCREMENT,
    state INTEGER NOT NULL DEFAULT 0,
    url TEXT,
    started TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    archived TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    status INTEGER,
    reason TEXT,
    size BIGINT,
    user_id BIGINT NOT NULL,

    PRIMARY KEY (id),
    FOREIGN KEY (user_id) REFERENCES user(id)
);

CREATE TABLE session (
    id VARCHAR(32) PRIMARY KEY,
    user_id INT NOT NULL,
    expiry BIGINT NOT NULL,

    FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
);

CREATE INDEX session_expiry ON session (expiry);

Use a shorter issuer/subject key due to mysql limitations

CREATE TABLE user (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(64) NOT NULL,
    issuer VARCHAR(100) NOT NULL,
    subject VARCHAR(100) NOT NULL,
    name TEXT NOT NULL,
    email TEXT NOT NULL,
    UNIQUE issuer_subject_unique (issuer, subject)
);

CREATE TABLE capture (
    id BIGINT NOT NULL AUTO_INCREMENT,
    state INTEGER NOT NULL DEFAULT 0,
    url TEXT,
    started TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    archived TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    status INTEGER,
    reason TEXT,
    size BIGINT,
    user_id BIGINT NOT NULL,

    PRIMARY KEY (id),
    FOREIGN KEY (user_id) REFERENCES user(id)
);

CREATE TABLE session (
    id VARCHAR(32) PRIMARY KEY,
    user_id INT NOT NULL,
    expiry BIGINT NOT NULL,

    FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
);

CREATE INDEX session_expiry ON session (expiry);


select private.create_model(

-- Model name, schema, table

'contact_info', 'public', 'cntct',

-- Columns

E'{
  "cntct.cntct_id as guid",
  "cntct.cntct_name as name",
  "cntct.cntct_active as is_active",
  "cntct.cntct_title as job_title",
  "cntct.cntct_phone as phone",
  "cntct.cntct_phone2 as alternate",
  "cntct.cntct_fax as fax",
  "cntct.cntct_email as primary_email",
  "cntct.cntct_owner_username as owner",
  "cntct.cntct_addr_id as address"}',

-- Rules

E'{"
-- insert rule

create or replace rule \\"_CREATE\\" as on insert to xm.contact_info
  do instead nothing;

","

-- update rule

create or replace rule \\"_UPDATE\\" as on update to xm.contact_info
  do instead nothing;

","

-- delete rules

create or replace rule \\"_DELETE\\" as on delete to xm.contact_info
  do instead nothing;

"}',

-- Conditions, Comment, System, Nested
'{}', 'Contact Info Model', true, true);

Rework conact info as nested.

select private.create_model(

-- Model name, schema, table

'contact_info', 'public', 'cntct',

-- Columns

E'{
  "cntct.cntct_id as guid",
  "cntct.cntct_name as name",
  "cntct.cntct_active as is_active",
  "cntct.cntct_title as job_title",
  "cntct.cntct_phone as phone",
  "cntct.cntct_phone2 as alternate",
  "cntct.cntct_fax as fax",
  "cntct.cntct_email as primary_email",
  "cntct.cntct_webaddr as web_address",
  "(select address_info
    from xm.address_info
    where guid = cntct.cntct_addr_id) as address"}',

-- Rules

E'{"
-- insert rule

create or replace rule \\"_CREATE\\" as on insert to xm.contact_info
  do instead nothing;

","

-- update rule

create or replace rule \\"_UPDATE\\" as on update to xm.contact_info
  do instead nothing;

","

-- delete rules

create or replace rule \\"_DELETE\\" as on delete to xm.contact_info
  do instead nothing;

"}',

-- Conditions, Comment, System, Nested
'{}', 'Contact Info Model', true, true);


CREATE VIEW `page_id_categories` AS (
SELECT p.page_id, c.id, c.name
FROM pages_categories p
LEFT JOIN categories c
ON p.category_id = c.id
);

Remove category id from view

CREATE VIEW `page_id_categories` AS (
SELECT p.page_id, c.name
FROM pages_categories p
LEFT JOIN categories c
ON p.category_id = c.id
);


INSERT INTO calaccess_processed_proposition (
            id,
            name,
            election_id
    )
SELECT
        scraped.scraped_id::INTEGER AS id,
        scraped.name AS name,
        election.id AS election_id
  FROM calaccess_processed_scrapedproposition AS scraped
  JOIN calaccess_processed_propositionscrapedelection AS scrapedelection
    ON scraped.election_id = scrapedelection.id
  JOIN calaccess_processed_election AS election
    ON substring(scrapedelection.name from '\d{4}')::INT = election.year
   AND substring(scrapedelection.name from '\d{4}\s([A-Z])') = election.election_type;

Change join criteria to elections when loading props

INSERT INTO calaccess_processed_proposition (
            id,
            name,
            election_id
    )
SELECT
        scraped.scraped_id::INTEGER AS id,
        scraped.name AS name,
        election.id AS election_id
  FROM calaccess_processed_scrapedproposition AS scraped
  JOIN (
        SELECT id,
               substring(name from '\d{4}\s([A-Z])') AS election_type,
               (regexp_matches(name, '^([A-Z]+\s\d{1,2},\s\d{4})\s.+$'))[1]::DATE as election_date
        FROM calaccess_processed_propositionscrapedelection
       ) AS scrapedelection
    ON scraped.election_id = scrapedelection.id
  JOIN calaccess_processed_election AS election
    ON left(election.election_type, 1) = scrapedelection.election_type
   AND election.election_date = scrapedelection.election_date
ORDER BY id;


CREATE ROLE pass_user PASSWORD 'password' LOGIN;
CREATE ROLE md5_user PASSWORD 'password' LOGIN;

Create hstore extension for travis

CREATE ROLE pass_user PASSWORD 'password' LOGIN;
CREATE ROLE md5_user PASSWORD 'password' LOGIN;
CREATE EXTENSION hstore;


CREATE TABLE `sessions` (
  `id` char(32) CHARSET 'ascii' NOT NULL,
  `timestamp` int unsigned NOT NULL,
  `data` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Switch from utf8 to utf8mb4

CREATE TABLE `sessions` (
  `id` char(32) CHARSET 'ascii' NOT NULL,
  `timestamp` int unsigned NOT NULL,
  `data` longtext CHARSET 'utf8mb4' NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;


DROP TABLE tweets;

CREATE TABLE tweets (
  key text PRIMARY KEY,
  user_user varchar,
  text text,
  createdat timestamp
  ) WITH comment='Tweets'
    AND COMPACT STORAGE
    AND read_repair_chance = 1.0;

Fix nasty timing-dependent Heisenbug in TweetActorsPerformanceSpec by replacing table DROP and re-CREATE by the TRUNCATE CQL command


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

Fix naming error in assessments export file

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


-- tabulate a column

SELECT   col, COUNT(*)
FROM
GROUP BY col
ORDER BY col DESC;

-- get a sample

WHERE random() < 0.05
LIMIT 30

-- truncate, with sequence reset

TRUNCATE TABLE tbl_name RESTART IDENTITY CASCADE;

-- count all tables

SELECT schemaname, relname, n_live_tup
FROM pg_stat_user_tables
ORDER BY schemaname, relname;

-- search_path management

\dn       -- list schemas

SHOW search_path;

SET search_path TO other_schema, public;

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

-- .pgpass @ 600
-- hostname:port:database:username:password

Move part of SQL chunk to memora

-- get a sample

WHERE random() < 0.05
LIMIT 30

-- truncate, with sequence reset

TRUNCATE TABLE tbl_name RESTART IDENTITY CASCADE;

-- count all tables

SELECT schemaname, relname, n_live_tup
FROM pg_stat_user_tables
ORDER BY schemaname, relname;

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

-- .pgpass @ 600
-- hostname:port:database:username:password


# Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
#
# Licensed under The MIT License
# For full copyright and license information, please see the LICENSE.txt
# Redistributions of files must retain the above copyright notice.
# MIT License (https://opensource.org/licenses/mit-license.php)

CREATE TABLE `sessions` (
  `id` char(40) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `created` datetime DEFAULT CURRENT_TIMESTAMP,
  `modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `data` blob DEFAULT NULL,
  `expires` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Change int zero padding length; map to unix TS len

# Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
#
# Licensed under The MIT License
# For full copyright and license information, please see the LICENSE.txt
# Redistributions of files must retain the above copyright notice.
# MIT License (https://opensource.org/licenses/mit-license.php)

CREATE TABLE `sessions` (
  `id` char(40) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  `created` datetime DEFAULT CURRENT_TIMESTAMP,
  `modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `data` blob DEFAULT NULL,
  `expires` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


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

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

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

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

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

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

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

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

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

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

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

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

Set no case collation on log properties names/values

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

Rename column name, tweet_id -> twitter_tweet_id.

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

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

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


IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'charity' AND COLUMN_NAME='name' AND COLUMN_DEFAULT IS NULL)
BEGIN
	ALTER TABLE [CharityData].[charity] ADD DEFAULT '' FOR [name]
END

IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'charity' AND COLUMN_NAME='nhs' AND COLUMN_DEFAULT IS NULL)
BEGIN
	ALTER TABLE [CharityData].[charity] ADD DEFAULT '' FOR [nhs]
END

Update switchign to not-nulls for name and nhs on charity

IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'charity' AND COLUMN_NAME='name' AND COLUMN_DEFAULT IS NULL)
BEGIN
	UPDATE charity
	   SET Name = ''
	 WHERE Name IS NULL;
END

IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'charity' AND COLUMN_NAME='nhs' AND COLUMN_DEFAULT IS NULL)
BEGIN
	UPDATE charity
	   SET nhs = ''
	 WHERE nhs IS NULL;
END


CREATE TABLE accounts (
    account_id int NOT NULL AUTO_INCREMENT,
    account_enabled BOOLEAN NOT NULL,
    email CHAR(254) NOT NULL,
    password CHAR(31) NOT NULL,
    CONSTRAINT pk_AccountID PRIMARY KEY(account_id)
);

CREATE TABLE prices (
    price_id int NOT NULL AUTO_INCREMENT,
    coin_symbol char(4) NOT NULL,
    time datetime NOT NULL,
    usd_value decimal(13,2) NOT NULL,
    CONSTRAINT pk_PriceId PRIMARY KEY(price_id)
);

CREATE TABLE orderbook (
    order_id int NOT NULL AUTO_INCREMENT,
    account_id int NOT NULL,
    num_shares decimal(13,8) NOT NULL,
    order_type char(10) NOT NULL,
    price_id int NOT NULL,
    balance_usd decimal(13,2) NOT NULL,
    balance_coin decimal(13,8) NOT NULL,
    CONSTRAINT pk_OrderId PRIMARY KEY(order_id),
    CONSTRAINT fk_AccountId FOREIGN KEY(account_id) REFERENCES accounts(account_id),
    CONSTRAINT fk_PriceId FOREIGN KEY(price_id) REFERENCES prices(price_id)
);

Increase size of password field

CREATE TABLE accounts (
    account_id int NOT NULL AUTO_INCREMENT,
    account_enabled BOOLEAN NOT NULL,
    email CHAR(254) NOT NULL,
    password CHAR(60) NOT NULL,
    CONSTRAINT pk_AccountID PRIMARY KEY(account_id)
);

CREATE TABLE prices (
    price_id int NOT NULL AUTO_INCREMENT,
    coin_symbol char(4) NOT NULL,
    time datetime NOT NULL,
    usd_value decimal(13,2) NOT NULL,
    CONSTRAINT pk_PriceId PRIMARY KEY(price_id)
);

CREATE TABLE orderbook (
    order_id int NOT NULL AUTO_INCREMENT,
    account_id int NOT NULL,
    num_shares decimal(13,8) NOT NULL,
    order_type char(10) NOT NULL,
    price_id int NOT NULL,
    balance_usd decimal(13,2) NOT NULL,
    balance_coin decimal(13,8) NOT NULL,
    CONSTRAINT pk_OrderId PRIMARY KEY(order_id),
    CONSTRAINT fk_AccountId FOREIGN KEY(account_id) REFERENCES accounts(account_id),
    CONSTRAINT fk_PriceId FOREIGN KEY(price_id) REFERENCES prices(price_id)
);


SET search_path = ref_geo, pg_catalog;

INSERT INTO l_areas (id_type, area_code, area_name, geom)
SELECT 101 AS id_type, insee_com, nom_com, geom FROM temp_fr_municipalities;

TRUNCATE TABLE li_municipalities;
INSERT INTO li_municipalities (id_municipality, id_area, status, insee_com, nom_com, insee_arr, nom_dep, insee_dep, nom_reg, insee_reg, code_epci)
SELECT id,  a.id_area, statut, insee_com, nom_com, insee_arr, nom_dep, insee_dep, nom_reg, insee_reg, code_epci
FROM temp_fr_municipalities t
JOIN l_areas a ON a.area_code = t.insee_com
;
REINDEX INDEX index_l_areas_geom;

INSERT COMMUNE - Ne pas utiliser id_type en dur

SET search_path = ref_geo, pg_catalog;

INSERT INTO l_areas (id_type, area_code, area_name, geom)
SELECT get_id_area_type('COM') AS id_type, insee_com, nom_com, geom FROM temp_fr_municipalities;

TRUNCATE TABLE li_municipalities;
INSERT INTO li_municipalities (id_municipality, id_area, status, insee_com, nom_com, insee_arr, nom_dep, insee_dep, nom_reg, insee_reg, code_epci)
SELECT id,  a.id_area, statut, insee_com, nom_com, insee_arr, nom_dep, insee_dep, nom_reg, insee_reg, code_epci
FROM temp_fr_municipalities t
JOIN l_areas a ON a.area_code = t.insee_com
;
REINDEX INDEX index_l_areas_geom;


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

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

DROP PROCEDURE IF EXISTS add_service_provider_to_service_table;
DELIMITER //

CREATE PROCEDURE add_service_provider_to_service_table()
  BEGIN

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

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

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

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

  END//

DELIMITER ;
CALL add_service_provider_to_service_table();


create table class_group_identifier (
  id int NOT NULL CHECK (id > 0),
  name varchar(1024) NOT NULL,

  PRIMARY KEY (id)
);

create table run_results (
  benchmark_group_id int NOT NULL CHECK (benchmark_group_id > 0),
  class_group_id int NOT NULL CHECK (class_group_id > 0),
  benchmark_group_run_id int NOT NULL CHECK (benchmark_group_run_id > 0),
  run_timestamp timestamp NOT NULL DEFAULT NOW(),
  commit_hash varchar(160) NOT NULL,
  branch_name varchar(1024) DEFAULT NULL,
  benchmark_name varchar(1024) NOT NULL,
  total_executions int NOT NULL,
  duration int NOT NULL CHECK (duration > 0),

  FORIEGN KEY (class_group_id) REFERENCES class_group_identifier (id),
  PRIMARY KEY (benchmark_group_id, benchmark_group_run_id)
);

INSERT INTO class_group_identifier (id, name) VALUES (1, 'JavaUtilBaseline');
INSERT INTO class_group_identifier (id, name) VALUES (2, 'PriorityScheduler');
INSERT INTO class_group_identifier (id, name) VALUES (3, 'SingleThreadScheduler');
INSERT INTO class_group_identifier (id, name) VALUES (4, 'SubmitterSchedulerLimiter');
INSERT INTO class_group_identifier (id, name) VALUES (5, 'KeyedLimiter');
INSERT INTO class_group_identifier (id, name) VALUES (6, 'KeyDistributedScheduler');

Fix typo in sql file

create table class_group_identifier (
  id int NOT NULL CHECK (id > 0),
  name varchar(1024) NOT NULL,

  PRIMARY KEY (id)
);

create table run_results (
  benchmark_group_id int NOT NULL CHECK (benchmark_group_id > 0),
  class_group_id int NOT NULL CHECK (class_group_id > 0),
  benchmark_group_run_id int NOT NULL CHECK (benchmark_group_run_id > 0),
  run_timestamp timestamp NOT NULL DEFAULT NOW(),
  commit_hash varchar(160) NOT NULL,
  branch_name varchar(1024) DEFAULT NULL,
  benchmark_name varchar(1024) NOT NULL,
  total_executions int NOT NULL,
  duration int NOT NULL CHECK (duration > 0),

  FOREIGN KEY (class_group_id) REFERENCES class_group_identifier (id),
  PRIMARY KEY (benchmark_group_id, benchmark_group_run_id)
);

INSERT INTO class_group_identifier (id, name) VALUES (1, 'JavaUtilBaseline');
INSERT INTO class_group_identifier (id, name) VALUES (2, 'PriorityScheduler');
INSERT INTO class_group_identifier (id, name) VALUES (3, 'SingleThreadScheduler');
INSERT INTO class_group_identifier (id, name) VALUES (4, 'SubmitterSchedulerLimiter');
INSERT INTO class_group_identifier (id, name) VALUES (5, 'KeyedLimiter');
INSERT INTO class_group_identifier (id, name) VALUES (6, 'KeyDistributedScheduler');


DROP TABLE IF EXISTS hmhb_user; /* get rid of this in the next PR */

DROP TABLE IF EXISTS requested_program;
DROP TABLE IF EXISTS published_program;

CREATE TABLE published_program (
    id BIGSERIAL PRIMARY KEY,
    organization_name TEXT NOT NULL,
    mission TEXT,
    geo TEXT NOT NULL,
    /* look up geo data in map API */
    /* address info? */
    summary TEXT,
    outcomes TEXT,
    public_email TEXT,
    public_phone TEXT,
    private_email TEXT,
    private_phone TEXT,
    created_on DATE NOT NULL,
    created_by TEXT NOT NULL,
    updated_on DATE,
    updated_by TEXT
);

CREATE TABLE requested_program (
    id BIGSERIAL PRIMARY KEY,
    program_id BIGINT REFERENCES published_program(id),
    organization_name TEXT NOT NULL,
    mission TEXT,
    geo TEXT NOT NULL,
    /* look up geo data in map API */
    /* address info? */
    summary TEXT,
    outcomes TEXT,
    public_email TEXT,
    public_phone TEXT,
    private_email TEXT,
    private_phone TEXT,
    created_on DATE NOT NULL,
    created_by TEXT NOT NULL,
    updated_on DATE,
    updated_by TEXT
);

Remove unnecessary drop tables for hmhb_user


update ACT_GE_PROPERTY set VALUE_ = '5.17.0.0' where NAME_ = 'schema.version';

alter table ACT_RE_PROCEDEF add HAS_GRAPHICAL_NOTATION_ tinyint;

Fix for mssql upgrade (typo in table name)

update ACT_GE_PROPERTY set VALUE_ = '5.17.0.0' where NAME_ = 'schema.version';

alter table ACT_RE_PROCDEF add HAS_GRAPHICAL_NOTATION_ tinyint;


# Select limit = 3 für die besten 3 Länder.

-------
-- Petition metrics
-------
drop table if exists speakeasy_petition_metrics;

create table speakeasy_petition_metrics
(
id int not null auto_increment,
speakout_id int,
campaign_id int,
speakout_name varchar(255), /* TODO: join on campaign instead */
speakout_title varchar (255), /* TODO: join on campaign instead */
language varchar(5), /* TODO: join on campaign instead */
country varchar(255),
npeople int,
activity varchar(255), /* petition signature, share etc. */
status varchar(255), /* completed, scheduled etc. */
is_opt_out tinyint(4),
primary key(id)
);

-------
-- Mailing data
-------
DROP TABLE IF EXISTS data_mailing_counter;
CREATE TABLE data_mailing_counter
   (mailing_id INT(10) UNSIGNED,
    counter VARCHAR(32),
    timebox INT UNSIGNED,
    value INT UNSIGNED,
    last_updated DATETIME NOT NULL,
    PRIMARY KEY (mailing_id, counter, timebox),
    FOREIGN KEY (mailing_id) REFERENCES civicrm_mailing (id));

-- Time boxes in minutes
DROP TABLE IF EXISTS data_timeboxes;
CREATE TABLE data_timeboxes (box INT UNSIGNED, PRIMARY KEY (box));
INSERT INTO data_timeboxes VALUES (60), (180), (360), (720), (1440), (144000);

Add timestamp to speakeasy metrics

# Select limit = 3 für die besten 3 Länder.

-------
-- Petition metrics
-------
drop table if exists speakeasy_petition_metrics;

create table speakeasy_petition_metrics
(
id int not null auto_increment,
speakout_id int,
campaign_id int,
speakout_name varchar(255), /* TODO: join on campaign instead */
speakout_title varchar (255), /* TODO: join on campaign instead */
language varchar(5), /* TODO: join on campaign instead */
country varchar(255),
npeople int,
activity varchar(255), /* petition signature, share etc. */
status varchar(255), /* completed, scheduled etc. */
is_opt_out tinyint(4),
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
primary key(id)
);

-------
-- Mailing data
-------
DROP TABLE IF EXISTS data_mailing_counter;
CREATE TABLE data_mailing_counter
   (mailing_id INT(10) UNSIGNED,
    counter VARCHAR(32),
    timebox INT UNSIGNED,
    value INT UNSIGNED,
    last_updated DATETIME NOT NULL,
    PRIMARY KEY (mailing_id, counter, timebox),
    FOREIGN KEY (mailing_id) REFERENCES civicrm_mailing (id));

-- Time boxes in minutes
DROP TABLE IF EXISTS data_timeboxes;
CREATE TABLE data_timeboxes (box INT UNSIGNED, PRIMARY KEY (box));
INSERT INTO data_timeboxes VALUES (60), (180), (360), (720), (1440), (144000);


-- $Header$

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

CREATE SCHEMA pgpool_catalog;

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

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

ADD "replicate_def" table in "pgpool_catalog" schema

-- $Header$

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

CREATE SCHEMA pgpool_catalog;

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

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

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


DROP TABLE IF EXISTS `obj_load_min_level`;
DROP TABLE IF EXISTS `shopgoldtmp`;
DROP TABLE IF EXISTS `talenhistory`;

Add dropping of commodprices table to SQL migration scripts

DROP TABLE IF EXISTS `obj_load_min_level`;
DROP TABLE IF EXISTS `shopgoldtmp`;
DROP TABLE IF EXISTS `talenhistory`;
DROP TABLE IF EXISTS `commodprices`;


use memegendb;
create table MemeInfo if not exists (
   id bigint auto_increment primary key ,
   filename varchar(256) unique,
   upvotes bigint,
   downvotes bigint,
   createdby varchar(256),
   createdon datetime
);

create table MemeTags if not exists (
   tagid bigint auto_increment primary key,
   memeid bigint,
       index (memeid),
       foreign key (memeid) refenreces MemeInfo(id),
   tag varchar(64)
);

Fix SQL for database setup.

use memegendb;
create table if not exists MemeInfo (
    meme_id bigint auto_increment,
    filename varchar(256) unique,
    upvotes bigint,
    downvotes bigint,
    createdby varchar(256),
    createdon datetime,
    primary key (meme_id)
);

create table if not exists MemeTags (
    tag_id bigint auto_increment primary key,
    meme_id bigint,
    tag varchar(64),
    index using hash (meme_id),
    foreign key (meme_id) references MemeInfo(meme_id) on delete cascade on update cascade
);


CREATE INDEX IN_SB_Pdc_Axis_1 ON SB_Pdc_Axis(AxisType);

CREATE INDEX IN_SB_Pdc_Utilization_1 ON SB_Pdc_Utilization(baseValue);
CREATE INDEX IN_SB_Pdc_Utilization_2 ON SB_Pdc_Utilization(instanceId);

create index IDX_PdcClassification_InstanceId on PdcClassification(instanceId);
create index IDX_PdcClassification_ContentId on PdcClassification(contentId);

Fix the too length of index name for the pdc tables with Oracle

CREATE INDEX IN_SB_Pdc_Axis_1 ON SB_Pdc_Axis(AxisType);

CREATE INDEX IN_SB_Pdc_Utilization_1 ON SB_Pdc_Utilization(baseValue);
CREATE INDEX IN_SB_Pdc_Utilization_2 ON SB_Pdc_Utilization(instanceId);

create index IDX_PdcClass_InstanceId on PdcClassification(instanceId);
create index IDX_PdcClass_ContentId on PdcClassification(contentId);


-- create a schema for our tables
CREATE SCHEMA kite;

-- give usage access to schema for our role
GRANT USAGE ON SCHEMA kite TO kontrol;

-- create the table
CREATE TABLE "kite"."kite" (
    username TEXT NOT NULL,
    environment TEXT NOT NULL,
    kitename TEXT NOT NULL,
    version TEXT NOT NULL,
    region TEXT NOT NULL,
    hostname TEXT NOT NULL,
    id uuid PRIMARY KEY,
    url TEXT NOT NULL,
    created_at timestamptz NOT NULL DEFAULT (NOW() AT TIME ZONE 'UTC'), -- you may set a global timezone
    updated_at timestamptz NOT NULL DEFAULT (NOW() AT TIME ZONE 'UTC')
);

-- add proper permissions for table
GRANT SELECT, INSERT, UPDATE, DELETE ON "kite"."kite" TO "kontrol";

-- create the index
DROP INDEX IF EXISTS kite_updated_at_btree_idx;

CREATE INDEX kite_updated_at_btree_idx ON "kite"."kite" USING BTREE (updated_at DESC);

Convert kite table to an unlogged table

-- create a schema for our tables
CREATE SCHEMA kite;

-- give usage access to schema for our role
GRANT USAGE ON SCHEMA kite TO kontrol;

-- create the table
CREATE UNLOGGED TABLE "kite"."kite" (
    username TEXT NOT NULL,
    environment TEXT NOT NULL,
    kitename TEXT NOT NULL,
    version TEXT NOT NULL,
    region TEXT NOT NULL,
    hostname TEXT NOT NULL,
    id uuid PRIMARY KEY,
    url TEXT NOT NULL,
    created_at timestamptz NOT NULL DEFAULT (NOW() AT TIME ZONE 'UTC'), -- you may set a global timezone
    updated_at timestamptz NOT NULL DEFAULT (NOW() AT TIME ZONE 'UTC')
);

-- add proper permissions for table
GRANT SELECT, INSERT, UPDATE, DELETE ON "kite"."kite" TO "kontrol";

-- create the index
DROP INDEX IF EXISTS kite_updated_at_btree_idx;

CREATE INDEX kite_updated_at_btree_idx ON "kite"."kite" USING BTREE (updated_at DESC);


SELECT
  ps.partkey,
  sum(ps.supplycost*ps.availqty) AS value
FROM
  "${database}"."${schema}"."${prefix}partsupp" ps,
  "${database}"."${schema}"."${prefix}supplier" s,
  "${database}"."${schema}"."${prefix}nation" n
WHERE
  ps.suppkey = s.suppkey
  AND s.nationkey = n.nationkey
  AND n.name = 'GERMANY'
GROUP BY
  ps.partkey
HAVING
  sum(ps.supplycost*ps.availqty) > (
    SELECT
      sum(ps.supplycost*ps.availqty) * 0.0001000000 / ${scale}
    FROM
      "${database}"."${schema}"."${prefix}partsupp" ps,
      "${database}"."${schema}"."${prefix}supplier" s,
      "${database}"."${schema}"."${prefix}nation" n
    WHERE
      ps.suppkey = s.suppkey
      AND s.nationkey = n.nationkey
      AND n.name = 'GERMANY'
  )
ORDER BY
  value DESC
;

Add additional order by columns for tpc-h query 11

SELECT
  ps.partkey,
  sum(ps.supplycost*ps.availqty) AS value
FROM
  "${database}"."${schema}"."${prefix}partsupp" ps,
  "${database}"."${schema}"."${prefix}supplier" s,
  "${database}"."${schema}"."${prefix}nation" n
WHERE
  ps.suppkey = s.suppkey
  AND s.nationkey = n.nationkey
  AND n.name = 'GERMANY'
GROUP BY
  ps.partkey
HAVING
  sum(ps.supplycost*ps.availqty) > (
    SELECT
      sum(ps.supplycost*ps.availqty) * 0.0001000000 / ${scale}
    FROM
      "${database}"."${schema}"."${prefix}partsupp" ps,
      "${database}"."${schema}"."${prefix}supplier" s,
      "${database}"."${schema}"."${prefix}nation" n
    WHERE
      ps.suppkey = s.suppkey
      AND s.nationkey = n.nationkey
      AND n.name = 'GERMANY'
  )
ORDER BY
  value DESC,
  -- additional column to assure results stability for larger scale factors; this is a deviation from TPC-H specification
  ps.partkey ASC
;


-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements.  See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You 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.
;

-- Routines related to the data model;

-- Function to provided an updated C_DATA column for a customer account.
-- Section 2.5.2.2
CREATE FUNCTION BAD_CREDIT_DATA(
    C_DATA VARCHAR(500), W SMALLINT, D SMALLINT,
    CW SMALLINT, CD SMALLINT, C_ID INTEGER,
    AMOUNT DECIMAL(6, 2))
RETURNS VARCHAR(500)
LANGUAGE JAVA PARAMETER STYLE JAVA
NO SQL
EXTERNAL NAME 'org.apache.derbyTesting.system.oe.routines.Data.dataForBadCredit';

Change two SMALLINT routine args to be INT so that they will match the corresponding int args of the target Java method.

-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements.  See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You 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.
;

-- Routines related to the data model;

-- Function to provided an updated C_DATA column for a customer account.
-- Section 2.5.2.2
CREATE FUNCTION BAD_CREDIT_DATA(
    C_DATA VARCHAR(500), W INT, D INT,
    CW SMALLINT, CD SMALLINT, C_ID INTEGER,
    AMOUNT DECIMAL(6, 2))
RETURNS VARCHAR(500)
LANGUAGE JAVA PARAMETER STYLE JAVA
NO SQL
EXTERNAL NAME 'org.apache.derbyTesting.system.oe.routines.Data.dataForBadCredit';


DROP FUNCTION master_update_shard_statistics(shard_id bigint);

CREATE FUNCTION pg_catalog.master_update_shard_statistics(shard_id bigint)
    RETURNS bigint
    LANGUAGE C STRICT
    AS 'MODULE_PATHNAME', $$master_update_shard_statistics$$;
COMMENT ON FUNCTION master_update_shard_statistics(bigint)
    IS 'updates shard statistics and returns the updated shard size';

Drop function from public and create in pg_catalog

DROP FUNCTION IF EXISTS public.master_update_shard_statistics(shard_id bigint);

CREATE OR REPLACE FUNCTION pg_catalog.master_update_shard_statistics(shard_id bigint)
    RETURNS bigint
    LANGUAGE C STRICT
    AS 'MODULE_PATHNAME', $$master_update_shard_statistics$$;
COMMENT ON FUNCTION master_update_shard_statistics(bigint)
    IS 'updates shard statistics and returns the updated shard size';


CREATE TABLE IACUC_SUBMISSION_TYPE (
    SUBMISSION_TYPE_CODE VARCHAR2(3) NOT NULL,
    DESCRIPTION VARCHAR2(200) NOT NULL,
    UPDATE_TIMESTAMP DATE NOT NULL,
    UPDATE_USER VARCHAR2(60) NOT NULL,
    VER_NBR NUMBER(8,0) DEFAULT 1 NOT NULL
    OBJ_ID VARCHAR2(36) NOT NULL)
/
ALTER TABLE IACUC_SUBMISSION_TYPE
ADD CONSTRAINT PK_IACUC_SUBMISSION_TYPE
PRIMARY KEY (SUBMISSION_TYPE_CODE)
/

Create initial tables for IACUC protocol submission (bloody spaces...)

CREATE TABLE IACUC_SUBMISSION_TYPE (
    SUBMISSION_TYPE_CODE VARCHAR2(3) NOT NULL,
    DESCRIPTION VARCHAR2(200) NOT NULL,
    UPDATE_TIMESTAMP DATE NOT NULL,
    UPDATE_USER VARCHAR2(60) NOT NULL,
    VER_NBR NUMBER(8,0) DEFAULT 1 NOT NULL,
    OBJ_ID VARCHAR2(36) NOT NULL)
/
ALTER TABLE IACUC_SUBMISSION_TYPE
ADD CONSTRAINT PK_IACUC_SUBMISSION_TYPE
PRIMARY KEY (SUBMISSION_TYPE_CODE)
/


-- DatasetHolder is no longer unique for MessageEvents
DROP INDEX IF EXISTS IDX_ME_UNIQUE_DATASETHOLDER_ID;
-- add the field for the serialized set of unconfirmed message uris
ALTER TABLE message_container ADD COLUMN unconfirmed bytea NOT NULL;

Apply message refactoring to owner db

ALTER TABLE atom ALTER COLUMN state SET NOT NULL;

-- DatasetHolder is no longer unique for MessageEvents
DROP INDEX IF EXISTS IDX_ME_UNIQUE_DATASETHOLDER_ID;

-- message_event is now only unique with respect to messageuri and parenturi
DROP INDEX IF EXISTS IDX_ME_UNIQUE_MESSAGE_URI;
CREATE UNIQUE INDEX IDX_ME_UNIQUE_MESSAGE_URI_PER_PARENT ON message_event (messageuri, parenturi);

-- drop correspondingremotemessageuri
ALTER TABLE message_event DROP column correspondingremotemessageuri;
DROP INDEX IF EXISTS IDX_ME_UNIQUE_CORRESPONDING_REMOTE_MESSAGE_URI;
DROP INDEX IF EXISTS IDX_ME_INNERMOST_MESSAGE_URI_RECIPIENT_ATOM_URI;

-- drop innermostmessageuri
ALTER TABLE message_event DROP column innermostmessageuri;
CREATE INDEX IDX_ME_RECIPIENT_ATOM_URI on message_event(messageuri, recipientatomuri);

-- add special fields for response information (respondingToURI, responseContainerURI), so we can make sure we
-- store only one response from a container
ALTER TABLE message_event ADD COLUMN respondingtouri VARCHAR(255) default null;
ALTER TABLE message_event ADD COLUMN responsecontaineruri VARCHAR(255) default null;
CREATE UNIQUE INDEX IDX_ME_UNIQUE_RESPONSE_PER_CONTAINER on message_event(parenturi, respondingtouri, responsecontaineruri);

-- make essential fields in connection non-nullable
ALTER TABLE connection ALTER COLUMN state SET NOT NULL;
ALTER TABLE connection ALTER COLUMN atomuri SET NOT NULL;
ALTER TABLE connection ALTER COLUMN targetatomuri SET NOT NULL;
ALTER TABLE connection ALTER COLUMN atomuri SET NOT NULL;
ALTER TABLE connection ALTER COLUMN socketuri SET NOT NULL;
ALTER TABLE connection ALTER COLUMN targetsocketuri SET NOT NULL;
ALTER TABLE connection ALTER COLUMN connectionuri SET NOT NULL;

-- add the field for the serialized set of unconfirmed message uris
ALTER TABLE message_container ADD COLUMN unconfirmed bytea NOT NULL;

-- add the table for pending confirmations
CREATE TABLE public.pendingconfirmation
(
    id bigint NOT NULL,
    confirmedmessageuri oid,
    confirmingmessageuri character varying(255) COLLATE pg_catalog."default" NOT NULL,
    messagecontainer_id bigint NOT NULL,
    CONSTRAINT pendingconfirmation_pkey PRIMARY KEY (id),
    CONSTRAINT idx_pendingconfirmation_to_container_id UNIQUE (messagecontainer_id, confirmingmessageuri),
    CONSTRAINT fk_pendingconfirmation FOREIGN KEY (messagecontainer_id)
        REFERENCES public.message_container (id) MATCH SIMPLE
        ON DELETE CASCADE
)


INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-COIDISCLOSURE', 'Document', 'ANNUAL_DISCLOSURE_ADVANCE_NOTICE', SYS_GUID(), 1, 'CONFG', '30', 'Number of days in advance of Annual Disclosure due date to start displaying message in COI channel', 'A', 'KC')
/

Delete COI Annual Review Due date from table for now

INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-COIDISCLOSURE', 'Document', 'ANNUAL_DISCLOSURE_ADVANCE_NOTICE', SYS_GUID(), 1, 'CONFG', '30', 'Number of days in advance of Annual Disclosure due date to start displaying message in COI channel', 'A', 'KC')
/
INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-COIDISCLOSURE', 'Document', 'ANNUAL_DISCLOSURE_RENEWAL_DATE', SYS_GUID(), 1, 'CONFG', '', 'Date on which Annual Disclosures are due for renewal, in MM/DD/YYYY format.  Or, use 0 if renewal is due on anniversary of last Annual Disclosure Certification', 'A', 'KC')
/


CREATE TABLE job(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     description TEXT NOT NULL,
     user_name TEXT NOT NULL,
     calculation_mode TEXT NOT NULL,
     hazard_calculation_id INTEGER REFERENCES job (id) ON DELETE CASCADE,
     status TEXT NOT NULL DEFAULT 'created',
     is_running BOOL NOT NULL DEFAULT false,
     start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     stop_time TIMESTAMP,
     relevant BOOL DEFAULT true,
     ds_calc_dir TEXT NOT NULL);

CREATE TABLE log(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     timestamp TIMESTAMP NOT NULL,
     level TEXT NOT NULL,
     process TEXT NOT NULL,
     message TEXT NOT NULL);

CREATE TABLE output(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     oq_job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     display_name TEXT NOT NULL,
     last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     ds_key TEXT NOT NULL);

CREATE TABLE performance(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     operation TEXT NOT NULL,
     time_sec FLOAT NOT NULL,
     memory_mb FLOAT NOT NULL,
     counts INTEGER NOT NULL);

Set relevant default to 1 instead of true

CREATE TABLE job(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     description TEXT NOT NULL,
     user_name TEXT NOT NULL,
     calculation_mode TEXT NOT NULL,
     hazard_calculation_id INTEGER REFERENCES job (id) ON DELETE CASCADE,
     status TEXT NOT NULL DEFAULT 'created',
     is_running BOOL NOT NULL DEFAULT false,
     start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     stop_time TIMESTAMP,
     relevant BOOL DEFAULT 1,
     ds_calc_dir TEXT NOT NULL);

CREATE TABLE log(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     timestamp TIMESTAMP NOT NULL,
     level TEXT NOT NULL,
     process TEXT NOT NULL,
     message TEXT NOT NULL);

CREATE TABLE output(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     oq_job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     display_name TEXT NOT NULL,
     last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     ds_key TEXT NOT NULL);

CREATE TABLE performance(
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     job_id INTEGER NOT NULL REFERENCES job (id) ON DELETE CASCADE,
     operation TEXT NOT NULL,
     time_sec FLOAT NOT NULL,
     memory_mb FLOAT NOT NULL,
     counts INTEGER NOT NULL);


ALTER TABLE adverts
  ADD COLUMN maxPlayCount TINYINT UNSIGNED NULL,
  ADD COLUMN maxDailyPlayCount TINYINT UNSIGNED NULL,
  ADD COLUMN assignedShow INT(11) UNSIGNED ZEROFILL NULL,
  ADD COLUMN assignedHour TIME NULL,
  ADD COLUMN backing_song VARCHAR(30) NULL,
  ADD COLUMN backing_artist VARCHAR(30) NULL,
  ADD COLUMN backing_album VARCHAR(30) NULL,
  ADD CONSTRAINT fk_program FOREIGN KEY (assignedShow) REFERENCES program(ProgramID);||
CREATE TABLE radio_show_promos (
  AdId INT(11),
  showName VARCHAR(30) NOT NULL,
  showDay DATE NOT NULL,
  showTime TIME NOT NULL,
  FOREIGN KEY fk_ad_id(AdId) REFERENCES adverts(AdId) ON UPDATE CASCADE ON DELETE RESTRICT
);

Fix adverts software update bug with dumped data

ALTER TABLE adverts ALTER COLUMN last_update SET DEFAULT '1970-01-01 00:00:01';||
UPDATE adverts SET last_update='1970-01-01 00:00:01' WHERE last_update < '1000-01-01';||
ALTER TABLE adverts
  ADD COLUMN maxPlayCount TINYINT UNSIGNED NULL,
  ADD COLUMN maxDailyPlayCount TINYINT UNSIGNED NULL,
  ADD COLUMN assignedShow INT(11) UNSIGNED ZEROFILL NULL,
  ADD COLUMN assignedHour TIME NULL,
  ADD COLUMN backing_song VARCHAR(30) NULL,
  ADD COLUMN backing_artist VARCHAR(30) NULL,
  ADD COLUMN backing_album VARCHAR(30) NULL,
  ADD CONSTRAINT fk_program FOREIGN KEY (assignedShow) REFERENCES program(ProgramID);||
CREATE TABLE IF NOT EXISTS radio_show_promos (
  AdId INT(11),
  showName VARCHAR(30) NOT NULL,
  showDay DATE NOT NULL,
  showTime TIME NOT NULL,
  FOREIGN KEY fk_ad_id(AdId) REFERENCES adverts(AdId) ON UPDATE CASCADE ON DELETE RESTRICT
);


ALTER TABLE master_rnr_columns
ADD calculation_options varchar(200) default 'DEFAULT';

ALTER TABLE program_rnr_columns
ADD calculation_option VARCHAR(200) default 'DEFAULT';

Add migration script for max stock quantity

ALTER TABLE master_rnr_columns
ADD calculationOption varchar(200) default 'DEFAULT';

ALTER TABLE program_rnr_columns
ADD calculationOption VARCHAR(200) default 'DEFAULT';

UPDATE master_rnr_columns
set calculationOption = '[{"name":"Default", "id":"DEFAULT"},{"name":"Consumption x 2","id":"CONSUMPTION_X_2"}]'
where name = 'maxStockQuantity';


DROP TABLE IF EXISTS users cascade;

CREATE TABLE users (
  user_id SERIAL PRIMARY KEY NOT NULL,
  first_name TEXT,
  last_name TEXT,
  birth_date DATE,
  phone BIGINT,
  photo BYTEA
);

INSERT INTO users (first_name, last_name, birth_date, phone) VALUES ('Lucy', 'Monie', '1/1/2000', 07123456789);

SELECT * FROM users;

DROP TABLE IF EXISTS requests cascade;

CREATE TABLE requests (
  request_id SERIAL PRIMARY KEY NOT NULL,
  rental_reference BOOLEAN,
  rental_arrears BOOLEAN,
  rental_history BOOLEAN,
  other_requests TEXT,
  email TEXT UNIQUE,
  address TEXT,
  time_stamp TIMESTAMP WITH TIME ZONE,
  user_id SERIAL REFERENCES users (user_id)
);

INSERT INTO requests (rental_reference, rental_arrears, rental_history, other_requests, email, address, time_stamp, user_id) VALUES (TRUE, FALSE, FALSE, 'also some other report', 'hello@gmail.com', NULL, current_timestamp, 1);

SELECT * FROM requests;

COMMIT;

Adjust initial database, breaking down the address in separate fields

DROP TABLE IF EXISTS users cascade;

CREATE TABLE users (
  user_id SERIAL PRIMARY KEY NOT NULL,
  first_name TEXT,
  last_name TEXT,
  birth_date DATE,
  phone BIGINT,
  photo BYTEA
);

INSERT INTO users (first_name, last_name, birth_date, phone) VALUES ('Lucy', 'Monie', '1/1/2000', 07123456789);

SELECT * FROM users;

DROP TABLE IF EXISTS requests cascade;

CREATE TABLE requests (
  request_id SERIAL PRIMARY KEY NOT NULL,
  rental_reference BOOLEAN,
  rental_arrears BOOLEAN,
  rental_history BOOLEAN,
  other_requests TEXT,
  email TEXT,
  street TEXT,
  town TEXT,
  postcode TEXT,
  time_stamp TIMESTAMP WITH TIME ZONE,
  user_id SERIAL REFERENCES users (user_id)
);

INSERT INTO requests (rental_reference, rental_arrears, rental_history, other_requests, email, street, town, postcode, time_stamp, user_id) VALUES (TRUE, FALSE, FALSE, 'also some other report', 'hello@gmail.com', NULL, NULL, NULL, current_timestamp, 1);

SELECT * FROM requests;

COMMIT;


-- The contents of this file are subject to the MonetDB Public License
-- Version 1.1 (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.monetdb.org/Legal/MonetDBLicense
--
-- Software distributed under the License is distributed on an "AS IS"
-- basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
-- License for the specific language governing rights and limitations
-- under the License.
--
-- The Original Code is the MonetDB Database System.
--
-- The Initial Developer of the Original Code is CWI.
-- Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
-- Copyright August 2008-2014 MonetDB B.V.
-- All Rights Reserved.

create table systemfunctions (function_id)
	as (select id from functions) with data;
grant select on systemfunctions to public;
update _tables
	set system = true
	where name = 'systemfunctions'
		and schema_id = (select id from schemas where name = 'sys');

-- so far only system schemas
update schemas set system = true;

Add an explicit commit statement at the end of the system.sql script. This should force commit the changes applied by the previous scripts, s.t. when these are executed on SQL store init, they are applied.

-- The contents of this file are subject to the MonetDB Public License
-- Version 1.1 (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.monetdb.org/Legal/MonetDBLicense
--
-- Software distributed under the License is distributed on an "AS IS"
-- basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
-- License for the specific language governing rights and limitations
-- under the License.
--
-- The Original Code is the MonetDB Database System.
--
-- The Initial Developer of the Original Code is CWI.
-- Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
-- Copyright August 2008-2014 MonetDB B.V.
-- All Rights Reserved.

create table systemfunctions (function_id)
	as (select id from functions) with data;
grant select on systemfunctions to public;
update _tables
	set system = true
	where name = 'systemfunctions'
		and schema_id = (select id from schemas where name = 'sys');

-- so far only system schemas
update schemas set system = true;

COMMIT;


alter table user
  add username varchar(255) not null after password,
  add unique key(username);

Truncate table in patch to avoid duplicate null values.

truncate table user;
alter table user
  add username varchar(255) not null after password,
  add unique key(username);


-- @Description Test vmem shown in the live view matches the amount in memory accounting
-- @skip Memory accounting failed to capture the memory allocation in UDF. Enable this test once the bug is fixed.
-- @author Zhongxian Gu

-- session1: issue the testing query
1: set explain_memory_verbosity = 'summary';
-- content/segment = 0; size = 100MB; sleep = 20 sec, critical section = false
1&: explain analyze select gp_allocate_palloc_test_all_segs(0, 100 * 1024 * 1024, 20, false);

-- session2: query the live view
2&: select pg_sleep(5);
2<:

2: select vmem_mb from session_state.session_state_memory_entries where segid = 0 and current_query ilike 'explain analyze select gp_allocate_palloc_test_all_segs%' and qe_count > 0;

-- session1: testing query finish
1<:

Remove @skip since test is now functioning correctly

-- @Description Test vmem shown in the live view matches the amount in memory accounting
-- @author Zhongxian Gu

-- session1: issue the testing query
1: set explain_memory_verbosity = 'summary';
-- content/segment = 0; size = 100MB; sleep = 20 sec, critical section = false
1&: explain analyze select gp_allocate_palloc_test_all_segs(0, 100 * 1024 * 1024, 20, false);

-- session2: query the live view
2&: select pg_sleep(5);
2<:

2: select vmem_mb from session_state.session_state_memory_entries where segid = 0 and current_query ilike 'explain analyze select gp_allocate_palloc_test_all_segs%' and qe_count > 0;

-- session1: testing query finish
1<:


CREATE OR REPLACE FUNCTION array_length(ANYARRAY)
  RETURNS INTEGER
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT coalesce(array_length($1, 1), 0) AS array_length;';

-- NULL values are filtered by count()
CREATE OR REPLACE FUNCTION is_unique(ANYARRAY)
  RETURNS BOOLEAN
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT count(val) = count(DISTINCT val)
 FROM unnest($1) AS vals (val);';

DROP TYPE IF EXISTS edge_type;
CREATE TYPE edge_type AS (
  edge_type_id SMALLINT,
  src BIGINT,
  trg BIGINT
);

DROP TYPE IF EXISTS vertex_type;
CREATE TYPE vertex_type AS (
  vertex_table_id SMALLINT,
  id BIGINT
);

CREATE OR REPLACE FUNCTION TO_TIMESTAMP(BIGINT)
  RETURNS TIMESTAMP WITHOUT TIME ZONE
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT TO_TIMESTAMP($1 :: text, ''YYYYMMDDHH24MISSMS'') :: TIMESTAMP WITHOUT TIME ZONE';

CREATE OR REPLACE FUNCTION TO_BIGINT(TIMESTAMP WITHOUT TIME ZONE)
  RETURNS BIGINT
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT to_char($1, ''YYYYMMDDHH24MISSMS'') :: BIGINT';

CREATE OR REPLACE FUNCTION driver_array(ANYARRAY)
  RETURNS JSONB
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT jsonb_build_object(''type'', ''ListValue'',
                           ''value'', jsonb_build_object(''values'', array_to_json($1)));';

Fix strange unnest of composite typed array in is_unique()

CREATE OR REPLACE FUNCTION array_length(ANYARRAY)
  RETURNS INTEGER
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT coalesce(array_length($1, 1), 0) AS array_length;';

-- NULL values are filtered by count()
CREATE OR REPLACE FUNCTION is_unique(ANYARRAY)
  RETURNS BOOLEAN
STRICT
IMMUTABLE
LANGUAGE SQL AS
'WITH q AS (SELECT * FROM unnest(array_remove($1, NULL))),
      distinct_q AS (SELECT DISTINCT * FROM q)
 SELECT (SELECT count(*) FROM distinct_q) = (SELECT count(*) FROM q)';

DROP TYPE IF EXISTS edge_type;
CREATE TYPE edge_type AS (
  edge_type_id SMALLINT,
  src BIGINT,
  trg BIGINT
);

DROP TYPE IF EXISTS vertex_type;
CREATE TYPE vertex_type AS (
  vertex_table_id SMALLINT,
  id BIGINT
);

CREATE OR REPLACE FUNCTION TO_TIMESTAMP(BIGINT)
  RETURNS TIMESTAMP WITHOUT TIME ZONE
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT TO_TIMESTAMP($1 :: text, ''YYYYMMDDHH24MISSMS'') :: TIMESTAMP WITHOUT TIME ZONE';

CREATE OR REPLACE FUNCTION TO_BIGINT(TIMESTAMP WITHOUT TIME ZONE)
  RETURNS BIGINT
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT to_char($1, ''YYYYMMDDHH24MISSMS'') :: BIGINT';

CREATE OR REPLACE FUNCTION driver_array(ANYARRAY)
  RETURNS JSONB
STRICT
IMMUTABLE
LANGUAGE SQL AS
'SELECT jsonb_build_object(''type'', ''ListValue'',
                           ''value'', jsonb_build_object(''values'', array_to_json($1)));';


CREATE TABLE AWARD_REPORT_TERMS (
	AWARD_REPORT_TERMS_ID NUMBER(12,0) NOT NULL,
	AWARD_ID NUMBER(12,0) NOT NULL,
	AWARD_NUMBER VARCHAR2(12) NOT NULL,
	SEQUENCE_NUMBER NUMBER(4,0) NOT NULL,
	REPORT_CLASS_CODE VARCHAR2(3) NOT NULL,
	REPORT_CODE VARCHAR2(3) NOT NULL,
	FREQUENCY_CODE VARCHAR2(3) NOT NULL,
	FREQUENCY_BASE_CODE VARCHAR2(3) NOT NULL,
	OSP_DISTRIBUTION_CODE VARCHAR2(3) NOT NULL,
	DUE_DATE DATE NOT NULL,
	VER_NBR NUMBER(8,0) DEFAULT 1 NOT NULL,
	OBJ_ID VARCHAR2(36) DEFAULT SYS_GUID() NOT NULL,
	UPDATE_TIMESTAMP DATE NOT NULL,
	UPDATE_USER VARCHAR2(60) NOT NULL);

ALTER TABLE AWARD_REPORT_TERMS
ADD CONSTRAINT PK_AWARD_REPORT_TERMS
PRIMARY KEY (AWARD_REPORT_TERMS_ID);

Update Scripts for release 2

CREATE TABLE AWARD_REPORT_TERMS (
	AWARD_REPORT_TERMS_ID NUMBER(12,0) NOT NULL,
	AWARD_ID NUMBER(12,0) NOT NULL,
	AWARD_NUMBER VARCHAR2(12) NOT NULL,
	SEQUENCE_NUMBER NUMBER(4,0) NOT NULL,
	REPORT_CLASS_CODE VARCHAR2(3) NOT NULL,
	REPORT_CODE VARCHAR2(3) NOT NULL,
	FREQUENCY_CODE VARCHAR2(3) NOT NULL,
	FREQUENCY_BASE_CODE VARCHAR2(3) NOT NULL,
	OSP_DISTRIBUTION_CODE VARCHAR2(3) NOT NULL,
	DUE_DATE DATE,
	VER_NBR NUMBER(8,0) DEFAULT 1 NOT NULL,
	OBJ_ID VARCHAR2(36) DEFAULT SYS_GUID() NOT NULL,
	UPDATE_TIMESTAMP DATE NOT NULL,
	UPDATE_USER VARCHAR2(60) NOT NULL);

ALTER TABLE AWARD_REPORT_TERMS
ADD CONSTRAINT PK_AWARD_REPORT_TERMS
PRIMARY KEY (AWARD_REPORT_TERMS_ID);


CREATE PROCEDURE [employer_financial].[GetTransactionLines_ByAccountId]
	@accountId BIGINT
AS
select
    main.*
    ,SUM(Amount) OVER(ORDER BY DateCreated,Transactiontype,ukprn asc
        RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
         AS Balance
from
(
    SELECT
       [AccountId]
      ,[TransactionType]
      ,Sum(Amount) as Amount
      ,UkPrn
      ,DateCreated
  FROM [employer_financial].[TransactionLine]
  WHERE AccountId = @accountId
  GROUP BY DateCreated ,AccountId, UKPRN,[TransactionType]

) as main
order by DateCreated desc, TransactionType desc, ukprn desc

Update store proc to support monthly transaction searches

CREATE PROCEDURE [employer_financial].[GetTransactionLines_ByAccountId]
	@accountId BIGINT,
	@fromDate datetime,
	@toDate datetime
AS
select
    main.*
    ,SUM(Amount) OVER(ORDER BY DateCreated,Transactiontype,ukprn asc
        RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
         AS Balance
from
(
    SELECT
       [AccountId]
      ,[TransactionType]
      ,Sum(Amount) as Amount
      ,UkPrn
      ,DateCreated
  FROM [employer_financial].[TransactionLine]
  WHERE AccountId = @accountId AND DateCreated >= @fromDate AND DateCreated <= @toDate
  GROUP BY DateCreated ,AccountId, UKPRN,[TransactionType]

) as main
order by DateCreated desc, TransactionType desc, ukprn desc


CREATE OR REPLACE FUNCTION cron.Schedule(_Function regprocedure, _ProcessID integer DEFAULT NULL)
RETURNS boolean
LANGUAGE plpgsql
SET search_path TO public, pg_temp
AS $FUNC$
DECLARE
_OK boolean;
BEGIN

IF _ProcessID IS NULL THEN
    SELECT cron.Processes.ProcessID INTO _ProcessID FROM cron.Jobs
    INNER JOIN cron.Processes ON (cron.Processes.JobID = cron.Jobs.JobID)
    WHERE cron.Jobs.Function = _Function::text
    LIMIT 1;
END IF;

UPDATE cron.Processes SET RunAtTime = now() WHERE ProcessID = _ProcessID RETURNING TRUE INTO STRICT _OK;
PERFORM pg_notify('cron.Dispatch()',_ProcessID::text);

RETURN TRUE;
END;
$FUNC$;

ALTER FUNCTION cron.Schedule(_Function regprocedure, _ProcessID integer) OWNER TO pgcronjob;

Fix cron.Schedule() to reset cron.Processes.RunAtTime when a job has been killed

CREATE OR REPLACE FUNCTION cron.Schedule(_Function regprocedure, _ProcessID integer DEFAULT NULL)
RETURNS boolean
LANGUAGE plpgsql
SET search_path TO public, pg_temp
AS $FUNC$
DECLARE
_OK boolean;
BEGIN

IF _ProcessID IS NULL THEN
    SELECT cron.Processes.ProcessID INTO _ProcessID FROM cron.Jobs
    INNER JOIN cron.Processes ON (cron.Processes.JobID = cron.Jobs.JobID)
    WHERE cron.Jobs.Function = _Function::text
    LIMIT 1;
END IF;

UPDATE cron.Processes SET RunAtTime = CASE WHEN RunAtTime > now() THEN now() ELSE NULL END WHERE ProcessID = _ProcessID RETURNING TRUE INTO STRICT _OK;
PERFORM pg_notify('cron.Dispatch()',_ProcessID::text);

RETURN TRUE;
END;
$FUNC$;

ALTER FUNCTION cron.Schedule(_Function regprocedure, _ProcessID integer) OWNER TO pgcronjob;


-- -----------------------------------------------------------------
-- Create the entry for InstaGENI Utah FOAM aggregate:
--
-- Execute as:
--
--    psql -U portal -h localhost -f bin/add-ig-utah-of.sql portal
--
-- -----------------------------------------------------------------

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: zero = aggregate
      0,
      -- URL
     'https://foam.instageni.northwestern.edu:3626/foam/gapi/2',
      -- CERT
     '/usr/share/geni-ch/sr/certs/ig-nw-of.pem',
      -- NAME
     'Northwestern InstaGENI OpenFlow',
      -- DESCRIPTION
     'InstaGENI Northwestern Rack OpenFlow',
      -- URN
     'urn:publicid:IDN+openflow:foam:foam.instageni.northwestern.edu+authority+am'
    );

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: 7 = CA
      7,
      -- URL
     '',
      -- CERT (self signed)
     '/usr/share/geni-ch/sr/certs/ig-nw-of.pem',
      -- NAME
     '',
      -- DESCRIPTION
     'InstaGENI Northwestern FOAM cert signer (self)',
      -- URN
     ''
    );

Fix Northwestern OF aggregate data.

-- -----------------------------------------------------------------
-- Create the entry for InstaGENI Northwestern FOAM aggregate:
--
-- Execute as:
--
--    psql -U portal -h localhost -f add-ig-northwestern-of.sql portal
--
-- -----------------------------------------------------------------

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: zero = aggregate
      0,
      -- URL
     'https://foam.instageni.northwestern.edu:3626/foam/gapi/2',
      -- CERT
     '/usr/share/geni-ch/sr/certs/ig-northwestern-of.pem',
      -- NAME
     'Northwestern InstaGENI OpenFlow',
      -- DESCRIPTION
     'InstaGENI Northwestern Rack OpenFlow',
      -- URN
     'urn:publicid:IDN+openflow:foam:foam.instageni.northwestern.edu+authority+am'
    );

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: 7 = CA
      7,
      -- URL
     '',
      -- CERT (self signed)
     '/usr/share/geni-ch/sr/certs/ig-northwestern-of.pem',
      -- NAME
     '',
      -- DESCRIPTION
     'InstaGENI Northwestern FOAM cert signer (self)',
      -- URN
     ''
    );