/*
Sample grant for PostgreSQL

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

*/

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

DROP TABLE IF EXISTS queue;

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

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

DROP TABLE IF EXISTS message;

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

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

/*
Sample grant for PostgreSQL

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

*/

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

DROP TABLE IF EXISTS queue;

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

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

DROP TABLE IF EXISTS message;

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


insert into users(user_id, username, password, fullname, email, reg_date)
    values(1, 'admin', 'nimda', 'System Administrator', 'postmaster@localhost',
    now());

Insert registration log type in database.

insert into users(user_id, username, password, fullname, email, reg_date)
    values(1, 'admin', 'nimda', 'System Administrator', 'postmaster@localhost',
    now());

insert into log_types(log_type) values('Registration');


select * from transactions
  join transaction_types on transaction_types.transaction_type_id=transactions.transaction_type_id
 where transaction_id in
 (select transaction_id from transactions
  join transaction_types on transaction_types.transaction_type_id=transactions.transaction_type_id
  where account_id=? and transaction_types.transaction_is_wage_payment order by transaction_id desc fetch next %d rows only)
 order by transaction_id

Add dummy "paid_out" column to the payments SQL query.

select t.*, tt.*, false as paid_out from transactions t
  join transaction_types tt on tt.transaction_type_id=t.transaction_type_id
 where t.transaction_id in
 (select transaction_id from transactions
  join transaction_types on transaction_types.transaction_type_id=transactions.transaction_type_id
  where account_id=? and transaction_types.transaction_is_wage_payment order by transaction_id desc fetch next %d rows only)
 order by t.transaction_id


CREATE TABLE users(
  id INTEGER PRIMARY KEY,
  username TEXT UNIQUE
);

CREATE TABLE modmails(
  id INTEGER PRIMARY KEY,
  author INTEGER,
  time DATETIME,
  body TEXT,
  subreddit INTEGER,
  FOREIGN KEY(author) REFERENCES users(id),
  FOREIGN KEY(subreddit) REFERENCES subreddits(id)
);

CREATE TABLE actions(
  target INTEGER,
  target_type INTEGER,
  author INTEGER,
  moderator INTEGER,
  time DATETIME DEFAULT CURRENT_TIMESTAMP,
  subreddit INTEGER,
  FOREIGN KEY(author) REFERENCES users(id),
  FOREIGN KEY(moderator) REFERENCES moderators(id),
  FOREIGN KEY(subreddit) REFERENCES subreddits(id)
);

CREATE TABLE moderators(
  id INTEGER PRIMARY KEY,
  username TEXT UNIQUE
);

CREATE TABLE subreddits(
  id INTEGER PRIMARY KEY,
  display_name TEXT UNIQUE
);

CREATE TABLE subreddit_moderator(
  subreddit_id INTEGER,
  moderator_id INTEGER,
  FOREIGN KEY(subreddit_id) REFERENCES subreddits(id),
  FOREIGN KEY(moderator_id) REFERENCES moderators(id)
);

Add removal and notifications tables

CREATE TABLE users(
  id INTEGER PRIMARY KEY,
  username TEXT UNIQUE
);

CREATE TABLE modmails(
  id INTEGER PRIMARY KEY,
  author INTEGER,
  time DATETIME,
  body TEXT,
  subreddit INTEGER,
  FOREIGN KEY(author) REFERENCES users(id),
  FOREIGN KEY(subreddit) REFERENCES subreddits(id)
);

CREATE TABLE actions(
  id INTEGER PRIMARY KEY,
  target_type INTEGER,
  target_id INTEGER,
  action_summary TEXT,
  action_details TEXT,
  author INTEGER,
  moderator INTEGER,
  time DATETIME DEFAULT CURRENT_TIMESTAMP,
  subreddit INTEGER,
  FOREIGN KEY(author) REFERENCES users(id),
  FOREIGN KEY(moderator) REFERENCES moderators(id),
  FOREIGN KEY(subreddit) REFERENCES subreddits(id)
);

CREATE TABLE moderators(
  id INTEGER PRIMARY KEY,
  username TEXT UNIQUE
);

CREATE TABLE subreddits(
  id INTEGER PRIMARY KEY,
  display_name TEXT UNIQUE
);

CREATE TABLE subreddit_moderator(
  subreddit_id INTEGER,
  moderator_id INTEGER,
  FOREIGN KEY(subreddit_id) REFERENCES subreddits(id),
  FOREIGN KEY(moderator_id) REFERENCES moderators(id)
);

CREATE TABLE removals(
  action_id INTEGER,
  reinstated INTEGER DEFAULT 0,
  FOREIGN KEY(action_id) REFERENCES actions(id)
);

CREATE TABLE notifications(
  comment_id INTEGER PRIMARY KEY,
  action_id INTEGER,
  FOREIGN KEY(action_id) REFERENCES actions(id)
);


-- Function by Stuart Lynn for a simple interpolation of a value
-- from a polygon table over an arbitrary polygon
-- (weighted by the area proportion overlapped)
CREATE OR REPLACE
FUNCTION cdb_overlap_sum(geom geometry, target_table_name text, target_column text)
  RETURNS numeric AS
$$
DECLARE
	result numeric;
BEGIN
  EXECUTE Format('
    SELECT sum(%I*ST_Area(St_Intersection($1, a.the_geom))/ST_Area(a.the_geom))
    FROM %I AS a
    WHERE $1 && a.the_geom
  ', target_column, target_table_name)
  USING geom
  INTO result;
  RETURN result;
END;
$$ LANGUAGE plpgsql;

Add optional schema name to cdb_overlap_sum

-- Function by Stuart Lynn for a simple interpolation of a value
-- from a polygon table over an arbitrary polygon
-- (weighted by the area proportion overlapped)
-- Aereal weighting is a very simple form of aereal interpolation.
--
-- Parameters:
--   * geom a Polygon geometry which defines the area where a value will be
--     estimated as the area-weighted sum of a given table/column
--   * target_table_name table name of the table that provides the values
--   * target_column column name of the column that provides the values
--   * schema_name optional parameter to defina the schema the target table
--     belongs to, which is necessary if its not in the search_path.
--     Note that target_table_name should never include the schema in it.
-- Return value:
--   Aereal-weighted interpolation of the column values over the geometry
CREATE OR REPLACE
FUNCTION cdb_overlap_sum(geom geometry, target_table_name text, target_column text, schema_name text DEFAULT NULL)
  RETURNS numeric AS
$$
DECLARE
	result numeric;
  qualified_name text;
BEGIN
  IF schema_name IS NULL THEN
    qualified_name := Format('%I', target_table_name);
  ELSE
    qualified_name := Format('%I.%s', schema_name, target_table_name);
  END IF;
  EXECUTE Format('
    SELECT sum(%I*ST_Area(St_Intersection($1, a.the_geom))/ST_Area(a.the_geom))
    FROM %s AS a
    WHERE $1 && a.the_geom
  ', target_column, qualified_name)
  USING geom
  INTO result;
  RETURN result;
END;
$$ LANGUAGE plpgsql;


DROP TABLE DASHBOARD_RESOURCE;
CREATE TABLE IF NOT EXISTS DASHBOARD_RESOURCE (
  ID                INTEGER      NOT NULL AUTO_INCREMENT,
  URL               VARCHAR(100) NOT NULL,
  OWNER             VARCHAR(100) NOT NULL,
  NAME              VARCHAR(256) NOT NULL,
  DESCRIPTION       VARCHAR(1000),
  PARENT_ID         INTEGER      NOT NULL,
  LANDING_PAGE      VARCHAR(100),
  CONTENT           LONGBLOB,
  CONSTRAINT PK_DASHBOARD_RESOURCE PRIMARY KEY (URL,OWNER),
  UNIQUE (ID)
);

DROP TABLE WIDGET_RESOURCE;
CREATE TABLE IF NOT EXISTS WIDGET_RESOURCE (
  WIDGET_ID                   VARCHAR(255) NOT NULL,
  WIDGET_NAME                 VARCHAR(255) NOT NULL,
  WIDGET_CONFIGS              LONGBLOB,
  CONSTRAINT PK_WIDGET_RESOURCE PRIMARY KEY (WIDGET_ID, WIDGET_NAME),
  UNIQUE (WIDGET_NAME, WIDGET_NAME)
);

Fix MySQL script for Dashboard table creation

DROP TABLE IF EXISTS DASHBOARD_RESOURCE;
CREATE TABLE IF NOT EXISTS DASHBOARD_RESOURCE (
  ID                INTEGER      NOT NULL AUTO_INCREMENT,
  URL               VARCHAR(100) NOT NULL,
  OWNER             VARCHAR(100) NOT NULL,
  NAME              VARCHAR(256) NOT NULL,
  DESCRIPTION       VARCHAR(1000),
  PARENT_ID         INTEGER      NOT NULL,
  LANDING_PAGE      VARCHAR(100),
  CONTENT           LONGBLOB,
  CONSTRAINT PK_DASHBOARD_RESOURCE PRIMARY KEY (URL,OWNER),
  UNIQUE (ID)
);

DROP TABLE IF EXISTS WIDGET_RESOURCE;
CREATE TABLE IF NOT EXISTS WIDGET_RESOURCE (
  WIDGET_ID                   VARCHAR(255) NOT NULL,
  WIDGET_NAME                 VARCHAR(255) NOT NULL,
  WIDGET_CONFIGS              LONGBLOB,
  CONSTRAINT PK_WIDGET_RESOURCE PRIMARY KEY (WIDGET_ID, WIDGET_NAME),
  UNIQUE (WIDGET_ID, WIDGET_NAME)
);


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
-- Move the relationship between campaigns and smtp to campaigns
ALTER TABLE campaigns ADD COLUMN "smtp_id" bigint;
UPDATE campaigns
	SET smtp_id = (
		SELECT smtp.smtp_id
		FROM smtp,campaigns
		WHERE campaigns.id=smtp.campaign_id
	)
;
-- Add the appropriate user_id to each smtp record
ALTER TABLE smtp ADD COLUMN "user_id" bigint;
UPDATE smtp
        SET user_id = (
		SELECT campaigns.user_id
		FROM smtp,campaigns
		WHERE smtp.smtp_id=campaigns.smtp_id
	)
;
-- Create a new table to store smtp records
ALTER TABLE smtp RENAME TO smtp_old;
CREATE TABLE smtp(
	id integer primary key autoincrement,
	user_id bigint,
	interface_type varchar(255),
	name varchar(255),
	host varchar(255),
	username varchar(255),
	password varchar(255),
	from_address varchar(255),
	modified_date datetime default CURRENT_TIMESTAMP,
	ignore_cert_errors BOOLEAN
);
-- Import existing smtp records into new format and drop the old table
INSERT INTO smtp (id,user_id,interface_type,name,host,username,from_address,ignore_cert_errors)
	SELECT smtp_id,user_id,'SMTP',
	'Imported campaign via ' || COALESCE(host,'') || ' from ' || COALESCE(from_address,''),
	host,username,from_address,ignore_cert_errors
	FROM smtp_old
	-- Prevent insertion of duplicate records
	WHERE smtp_id IN (
		SELECT smtp_id
		FROM smtp_old
		GROUP BY user_id,host,from_address
	)
;
DROP TABLE smtp_old;
-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

Remove attempt to migrate from existing SMTP since it is unsafe in multi user environments and confusing + breaks relationships when entries are de-duped.

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
-- Move the relationship between campaigns and smtp to campaigns
ALTER TABLE campaigns ADD COLUMN "smtp_id" bigint;
-- Create a new table to store smtp records
DROP TABLE smtp;
CREATE TABLE smtp(
	id integer primary key autoincrement,
	user_id bigint,
	interface_type varchar(255),
	name varchar(255),
	host varchar(255),
	username varchar(255),
	password varchar(255),
	from_address varchar(255),
	modified_date datetime default CURRENT_TIMESTAMP,
	ignore_cert_errors BOOLEAN
);
-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back


CREATE TABLE equipment_status_line_items(
  id                        SERIAL PRIMARY KEY,
  facilityId                INT NOT NULL REFERENCES facilities(id),
  programId                 INT NOT NULL REFERENCES programs(id),
  periodId                  INT NOT NULL REFERENCES processing_periods(id),
  operationalStatusId       INT NOT NULL REFERENCES equipment_operational_status(id),
  testCount                 INT NULL,
  totalCount                INT NULL,
  daysOutOfUse              INT NOT NULL,
  remarks                   VARCHAR (200) NULL,

  createdBy                 INTEGER,
  createdDate               TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  modifiedBy                INTEGER,
  modifiedDate              TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Update equipment status line item table schema

DROP TABLE IF EXISTS equipment_status_line_items;

CREATE TABLE equipment_status_line_items(
  id                        SERIAL PRIMARY KEY,

  rnrId                     INT NOT NULL REFERENCES requisitions(id),
  code                      VARCHAR(200) NOT NULL,
  equipmentName             VARCHAR(200) NOT NULL,
  equipmentCategory         VARCHAR(200) NOT NULL,
  equipmentModel            VARCHAR(200) NULL,
  equipmentSerial           VARCHAR(200) NULL,
  equipmentInventoryId      INT NOT NULL,

  operationalStatusId       INT NOT NULL REFERENCES equipment_operational_status(id),
  testCount                 INT NULL,
  totalCount                INT NULL,
  daysOutOfUse              INT NOT NULL,
  remarks                   VARCHAR (2000) NULL,

  createdBy                 INTEGER,
  createdDate               TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  modifiedBy                INTEGER,
  modifiedDate              TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE events (
	id INTEGER PRIMARY KEY,
	slug TEXT,

	/* Event data */
	title TEXT,
	startdt DATETIME,
	enddt DATETIME,
	location TEXT,
	blurb TEXT,
	url TEXT,
	type TEXT,		/* ATM this can only be "film" */
	cost TEXT,		/* NULL = free */
	host TEXT,

	/* State crap */
	state TEXT,		/* "submitted", "approved" */
	email TEXT,		/* Delete after submitted? */
	key TEXT,			/* key needed for validation */ /* XXX remove */
	importid TEXT
);

CREATE TABLE users (
	email TEXT PRIMARY KEY,
	salt TEXT,
	digest TEXT,

	pwreset TEXT,
	notify BOOLEAN,
	rights TEXT		/* "admin", "editor" */
);

CREATE TABLE posts (
	id TEXT PRIMARY KEY,
	link TEXT,
	title TEXT,
	date DATETIME,
	feed_id TEXT,
	hidden INTEGER
);

CREATE TABLE feeds (
	id TEXT PRIMARY KEY,
	site_url TEXT,
	title TEXT,
	errors TEXT
);

Add SQL for test@example.com / test

CREATE TABLE events (
	id INTEGER PRIMARY KEY,
	slug TEXT,

	/* Event data */
	title TEXT,
	startdt DATETIME,
	enddt DATETIME,
	location TEXT,
	blurb TEXT,
	url TEXT,
	type TEXT,		/* ATM this can only be "film" */
	cost TEXT,		/* NULL = free */
	host TEXT,

	/* State crap */
	state TEXT,		/* "submitted", "approved" */
	email TEXT,		/* Delete after submitted? */
	key TEXT,			/* key needed for validation */ /* XXX remove */
	importid TEXT
);

CREATE TABLE users (
	email TEXT PRIMARY KEY,
	salt TEXT,
	digest TEXT,

	pwreset TEXT,
	notify BOOLEAN,
	rights TEXT		/* "admin", "editor" */
);
INSERT INTO "users" VALUES('test@example.com',NULL,'aaa','e3MjCoP7Oui8bQ+BK+2wWIRmq/OApZ8nXNuv6Kt6qiw=',NULL,NULL,'admin');

CREATE TABLE posts (
	id TEXT PRIMARY KEY,
	link TEXT,
	title TEXT,
	date DATETIME,
	feed_id TEXT,
	hidden INTEGER
);

CREATE TABLE feeds (
	id TEXT PRIMARY KEY,
	site_url TEXT,
	title TEXT,
	errors TEXT
);


-- @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<:


-- should ever only have one row for each service
ALTER TABLE oskari_capabilities_cache
  ADD CONSTRAINT oskari_capabilities_cache__unique_service UNIQUE (layertype, url);

Delete duplicate capabilities from db

-- drop duplicates, those who have same layertype + url
DELETE FROM oskari_capabilities_cache
WHERE id IN (SELECT id
             FROM (SELECT id,
                     ROW_NUMBER() OVER (partition BY layertype, url ORDER BY id) AS rnum
                   FROM oskari_capabilities_cache) t
             WHERE t.rnum > 1);

-- should ever only have one row for each service
ALTER TABLE oskari_capabilities_cache
  ADD CONSTRAINT oskari_capabilities_cache__unique_service UNIQUE (layertype, url);


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

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

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

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

Drop tables so that we know we are starting with a clean DB

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

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

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

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

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


-- All Trac tickets to convert.
select
    id,
    type,
    owner,
    reporter,
    milestone,
    status,
    resolution,
    summary,
    description
from
    ticket
order
    by id

Add created and modification times

-- All Trac tickets to convert.
select
    id,
    type,
    owner,
    reporter,
    milestone,
    status,
    resolution,
    summary,
    description,
    time / 1000000 as PosixTime,
    modified / 1000000 as ModifiedTime
from
    ticket
order
    by id


CREATE TABLE productions (
  id INTEGER PRIMARY KEY,
  title TEXT,
  creator TEXT,
  subject TEXT,
  description TEXT,
  publisher TEXT,
  date TEXT,
  type TEXT,
  format TEXT,
  identifier TEXT,
  source TEXT,
  language TEXT,
  rights TEXT,
  sourceDate TEXT,
  sourceEdition TEXT,
  sourcePublisher TEXT,
  sourceRights TEXT,
  multimediaType TEXT,
  multimediaContent TEXT,
  narrator TEXT,
  producer TEXT,
  producedDate TEXT,
  revision TEXT,
  revisionDate TEXT,
  revisionDescription TEXT,
  totalTime TEXT,
  audioFormat TEXT,
  -- SBS specific columns
  state TEXT,
  productNumber TEXT
);

INSERT INTO productions (title, creator, source, language, sourcePublisher) VALUES
("Unter dem Deich", "Hart, Maarten", "978-3-492-05573-4", "de", "Piper"),
("Aus dem Berliner Journal", "Frisch, Max", "978-3-518-42352-3", "de", "Suhrkamp"),
("Info-Express, Februar 2014", "SZB Taubblinden-Beratung", "", "de", "");

Add a note about the origin of the meta data schema

-- Meta data for a production
-- see http://www.daisy.org/z3986/2005/Z3986-2005.html#PubMed
CREATE TABLE productions (
  id INTEGER PRIMARY KEY,
  title TEXT,
  creator TEXT,
  subject TEXT,
  description TEXT,
  publisher TEXT,
  date TEXT,
  type TEXT,
  format TEXT,
  identifier TEXT,
  source TEXT,
  language TEXT,
  rights TEXT,
  sourceDate TEXT,
  sourceEdition TEXT,
  sourcePublisher TEXT,
  sourceRights TEXT,
  multimediaType TEXT,
  multimediaContent TEXT,
  narrator TEXT,
  producer TEXT,
  producedDate TEXT,
  revision TEXT,
  revisionDate TEXT,
  revisionDescription TEXT,
  totalTime TEXT,
  audioFormat TEXT,
  -- SBS specific columns
  state TEXT,
  productNumber TEXT
);

INSERT INTO productions (title, creator, source, language, sourcePublisher) VALUES
("Unter dem Deich", "Hart, Maarten", "978-3-492-05573-4", "de", "Piper"),
("Aus dem Berliner Journal", "Frisch, Max", "978-3-518-42352-3", "de", "Suhrkamp"),
("Info-Express, Februar 2014", "SZB Taubblinden-Beratung", "", "de", "");


CREATE TABLE phabricator_conduit.conduit_certificatetoken (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  userPHID VARCHAR(64) BINARY NOT NULL,
  token VARCHAR(64),
  dateCreated INT UNSIGNED NOT NULL,
  dateModified INT UNSIGNED NOT NULL,
);

ALTER TABLE phabricator_conduit.conduit_certificatetoken
  ADD UNIQUE KEY (userPHID);
ALTER TABLE phabricator_conduit.conduit_certificatetoken
  ADD UNIQUE KEY (token);

Fix syntax clowning in patch 046.

CREATE TABLE phabricator_conduit.conduit_certificatetoken (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  userPHID VARCHAR(64) BINARY NOT NULL,
  token VARCHAR(64),
  dateCreated INT UNSIGNED NOT NULL,
  dateModified INT UNSIGNED NOT NULL
);

ALTER TABLE phabricator_conduit.conduit_certificatetoken
  ADD UNIQUE KEY (userPHID);
ALTER TABLE phabricator_conduit.conduit_certificatetoken
  ADD UNIQUE KEY (token);


CREATE DATABASE `ssbo_ticketing` CHARACTER SET utf8 COLLATE utf8_general_ci;
USE ssbo_ticketing;

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE ON `ssbo_ticketing`.* TO 'admin'@'localhost';

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

Update SQL table to work with special characters

CREATE DATABASE `ssbo_ticketing` CHARACTER SET utf8 COLLATE utf8_general_ci;
USE ssbo_ticketing;

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE ON `ssbo_ticketing`.* TO 'admin'@'localhost';

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


TRUNCATE scat_item;

INSERT INTO scat_item
       (code, retail_price, discount_type, discount, stock)
SELECT code, retail_price, discount_type, discount,
       IFNULL((SELECT SUM(allocated)
                 FROM scat.txn_line
                WHERE item = scat.item.id), 0) stock
  FROM scat.item;

Set stock quantity to NULL for inactive products

TRUNCATE scat_item;

INSERT INTO scat_item
       (code, retail_price, discount_type, discount, stock)
SELECT code, retail_price, discount_type, discount,
       IF(active,
          (SELECT SUM(allocated) FROM scat.txn_line WHERE item = scat.item.id),
          NULL) stock
  FROM scat.item;


ALTER TABLE  `company` ADD  `CopyFakturaMail` VARCHAR( 255 ) NULL AFTER `Email`;

INSERT INTO `confdbfields` (`ConfDBFieldID`, `TableField`, `Active`, `TS`, `ValidFrom`, `ValidTo`, `TableName`, `PrimaryKey`, `FormHeight`, `FormWidth`, `DefaultValue`, `FormType`, `FieldType`, `FieldNull`, `FieldExtra`, `DefaultLink`, `InputValidation`, `OutputValidation`, `Required`, `FormTypeEdit`, `FieldExtraEdit`, `MD5Created`, `MD5Updated`, `CreatedByPersonID`, `UpdatedByPersonID`, `CreatedDateTime`) VALUES (NULL, 'CopyFakturaMail', '1', CURRENT_TIMESTAMP, '2001-01-01 00:00:00', '9999-12-31 00:00:00', 'company', '0', '1', '0', '0', 'text', 'varchar(255)', '', '', NULL, 'Email', 'Email', '0', '', '', '', '', '0', '0', '2014-09-15 00:00:00');

Remove a line to new migration because into AB db


--- System Setup
SET application_name="container_setup";

CREATE EXTENSION IF NOT EXISTS pgaudit;

CREATE USER PGHA_USER LOGIN;
ALTER USER PGHA_USER PASSWORD 'PGHA_USER_PASSWORD';

CREATE DATABASE PGHA_DATABASE;
GRANT ALL PRIVILEGES ON DATABASE PGHA_DATABASE TO PGHA_USER;

CREATE USER testuser2 LOGIN;
ALTER USER testuser2 PASSWORD 'customconfpass';

CREATE DATABASE PGHA_DATABASE;
GRANT ALL PRIVILEGES ON DATABASE PGHA_DATABASE TO testuser2;

--- PGHA_DATABASE Setup

\c PGHA_DATABASE

CREATE EXTENSION IF NOT EXISTS pgaudit;

CREATE SCHEMA IF NOT EXISTS PGHA_USER;

/* The following has been customized for the custom-config example */

\c PGHA_DATABASE PGHA_USER;

CREATE TABLE custom_config_table (
	KEY VARCHAR(30) PRIMARY KEY,
	VALUE VARCHAR(50) NOT NULL,
	UPDATEDT TIMESTAMP NOT NULL
);

INSERT INTO custom_config_table (KEY, VALUE, UPDATEDT) VALUES ('CPU', '256', now());

GRANT ALL ON custom_config_table TO testuser2;

Modify how the user connects in the custom config example

--- System Setup
SET application_name="container_setup";

CREATE EXTENSION IF NOT EXISTS pgaudit;

CREATE USER PGHA_USER LOGIN;
ALTER USER PGHA_USER PASSWORD 'PGHA_USER_PASSWORD';

CREATE DATABASE PGHA_DATABASE;
GRANT ALL PRIVILEGES ON DATABASE PGHA_DATABASE TO PGHA_USER;

CREATE USER testuser2 LOGIN;
ALTER USER testuser2 PASSWORD 'customconfpass';

CREATE DATABASE PGHA_DATABASE;
GRANT ALL PRIVILEGES ON DATABASE PGHA_DATABASE TO testuser2;

--- PGHA_DATABASE Setup

\c PGHA_DATABASE

CREATE EXTENSION IF NOT EXISTS pgaudit;

CREATE SCHEMA IF NOT EXISTS PGHA_USER;

/* The following has been customized for the custom-config example */

SET SESSION AUTHORIZATION PGHA_USER;

CREATE TABLE custom_config_table (
	KEY VARCHAR(30) PRIMARY KEY,
	VALUE VARCHAR(50) NOT NULL,
	UPDATEDT TIMESTAMP NOT NULL
);

INSERT INTO custom_config_table (KEY, VALUE, UPDATEDT) VALUES ('CPU', '256', now());

GRANT ALL ON custom_config_table TO testuser2;


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

Add composite indexes to avoid touching the table while filtering for Q/A.

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


insert into PROPOSAL_LOG (FISCAL_MONTH,FISCAL_YEAR,PROPOSAL_TYPE_CODE,LOG_STATUS,PI_ID,PROPOSAL_LOG_TYPE_CODE,PROPOSAL_NUMBER,TITLE,LEAD_UNIT,UPDATE_TIMESTAMP,UPDATE_USER)
values ('3','2010','1','1','000000014','1','1','test','000001',SYSDATE,USER);

Update sql to match new db schema


# patch_54_55_a.sql
#
# title: update schema version
#
# description:
# Update schema_version in meta table to 56

UPDATE meta SET meta_value='56' WHERE meta_key='schema_version';

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_55_56_a.sql|schema_version');

Correct patch name in comment.

# patch_55_56_a.sql
#
# title: update schema version
#
# description:
# Update schema_version in meta table to 56

UPDATE meta SET meta_value='56' WHERE meta_key='schema_version';

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_55_56_a.sql|schema_version');


CREATE INDEX "road_wkb_geometry_geom_idx" ON "osmm_itn"."road" USING GIST ("wkb_geometry");
CREATE INDEX "roadlink_wkb_geometry_geom_idx" ON "osmm_itn"."roadlink" USING GIST ("wkb_geometry");
CREATE INDEX "roadnode_wkb_geometry_geom_idx" ON "osmm_itn"."roadnode" USING GIST ("wkb_geometry");
CREATE INDEX "ferrynode_wkb_geometry_geom_idx" ON "osmm_itn"."ferrynode" USING GIST ("wkb_geometry");
CREATE INDEX "informationpoint_wkb_geometry_geom_idx" ON "osmm_itn"."informationpoint" USING GIST ("wkb_geometry");
CREATE INDEX "roadlinkinformation_wkb_geometry_geom_idx" ON "osmm_itn"."roadlinkinformation" USING GIST ("wkb_geometry");
CREATE INDEX "roadrouteinformation_wkb_geometry_geom_idx" ON "osmm_itn"."roadrouteinformation" USING GIST ("wkb_geometry");

Remove wkb_geometry index for osmm_itn.road


drop table if exists minsk_polygons;

create table minsk_polygons as (
    with segments as (
        select
            (ST_Dump(
                ST_Transform(
                    ST_VoronoiPolygons(
                        ST_Collect(
                            ST_SetSRID(
                                ST_MakePoint(
                                    ST_X(
                                        ST_Transform(point, 3857)
                                    )::bigint,
                                    ST_Y(
                                        ST_Transform(point, 3857)
                                    )::bigint
                                ),
                                3857
                            )
                        )
                ),
                3857
                )
            )).geom as geom
        from
            minsk_points
        )
    select
        ST_Transform(geom, 4326) as geom,
        greatest(ceil(duration/300.0), 1) as duration
    from
        segments
        left join minsk_points on ST_Intersects(geom, point)
);

drop table if exists minsk_areas;

create table minsk_areas as (
    select
        duration,
        ST_Union(geom) as geom
    from
        minsk_polygons
    group by 1
);

update minsk_areas set geom = (select ST_Union(geom) from minsk_areas where duration >2) where duration = 3;
delete from minsk_areas where duration>3;

Add new layer to polygons

drop table if exists minsk_polygons;

create table minsk_polygons as (
    with segments as (
        select
            (ST_Dump(
                ST_Transform(
                    ST_VoronoiPolygons(
                        ST_Collect(
                            ST_SetSRID(
                                ST_MakePoint(
                                    ST_X(
                                        ST_Transform(point, 3857)
                                    )::bigint,
                                    ST_Y(
                                        ST_Transform(point, 3857)
                                    )::bigint
                                ),
                                3857
                            )
                        )
                ),
                3857
                )
            )).geom as geom
        from
            minsk_points
        )
    select
        ST_Transform(geom, 4326) as geom,
        greatest(ceil(duration/300.0), 1) as duration
    from
        segments
        left join minsk_points on ST_Intersects(geom, point)
);

drop table if exists minsk_areas;

create table minsk_areas as (
    select
        duration,
        ST_Union(geom) as geom
    from
        minsk_polygons
    group by 1
);

update minsk_areas set geom = (select ST_Union(geom) from minsk_areas where duration >3) where duration = 4;
delete from minsk_areas where duration>4;


-- Add field for municipality ID in company table.
ALTER TABLE company
ADD CompanyMunicipalityID INT(11);

-- Populate this field with values from kommune table, based on current CompanyMunicipality number in company table.
UPDATE company AS c INNER JOIN kommune AS k
ON k.KommuneNumber = c.CompanyMunicipality
SET c.CompanyMunicipalityID = k.KommuneID;

-- Remove CompanyMunicipality and CompanyMunicipalityName from company table. These are replaced now with ID field.
ALTER TABLE company
DROP CompanyMunicipality,
DROP CompanyMunicipalityName;

Fix migration to retain the previous municipality

-- Add field for municipality ID in company table.
ALTER TABLE company
ADD CompanyMunicipalityID INT(11);

-- Add municipality if not existing in the kommune table
REPLACE INTO kommune (KommuneNumber, KommuneName, Sone)
SELECT CompanyMunicipality, CompanyMunicipalityName, 1
FROM company
WHERE CompanyMunicipality IS NOT NULL OR CompanyMunicipalityName IS NOT NULL OR CompanyMunicipality != '' OR CompanyMunicipalityName != '';

-- Populate this field with values from kommune table, based on current CompanyMunicipality number in company table.
UPDATE company AS c INNER JOIN kommune AS k
ON k.KommuneNumber = c.CompanyMunicipality
SET c.CompanyMunicipalityID = k.KommuneID;

-- Remove CompanyMunicipality and CompanyMunicipalityName from company table. These are replaced now with ID field.
ALTER TABLE company
DROP CompanyMunicipality,
DROP CompanyMunicipalityName;


CREATE EXTENSION IF NOT EXISTS CITEXT;

CREATE TABLE IF NOT EXISTS users (
  id       SERIAL PRIMARY KEY,
  nickname CITEXT NOT NULL UNIQUE,
  fullname TEXT   NOT NULL,
  email    CITEXT NOT NULL UNIQUE,
  about    TEXT
);

CREATE INDEX IF NOT EXISTS idx_users_nickname ON users (nickname);

Add Collate for user nickname

CREATE EXTENSION IF NOT EXISTS CITEXT;

CREATE TABLE IF NOT EXISTS users (
  id       SERIAL PRIMARY KEY,
  nickname CITEXT NOT NULL UNIQUE COLLATE UCS_BASIC,
  fullname TEXT   NOT NULL,
  email    CITEXT NOT NULL UNIQUE,
  about    TEXT
);

CREATE INDEX IF NOT EXISTS idx_users_nickname ON users (nickname);


Update Data import to include default issue labels

--
-- Dumping data for table issue_label
--

INSERT INTO issue_label (id, title, hex_color, all_projects, project_id) VALUES
(1, 'New Feature', '4171f6', 1, NULL),
(2, 'Bug', 'ff0000', 1, NULL),
(3, 'Enhancement', 'd900ff', 1, NULL),
(4, 'Maintenance', 'ff7070', 1, NULL),
(5, 'Dublicate', 'c9c9c9', 1, NULL),
(6, 'Help wanted', '008000', 1, NULL),
(7, 'Invalid', 'b0b0b0', 1, NULL),
(8, 'Question', 'bf4051', 1, NULL),
(9, 'Wontfix', 'ff6600', 1, NULL);


CREATE TABLE sessions (
    id INTEGER PRIMARY KEY NOT NULL,
    token TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    expired_at TEXT NOT NULL
);

CREATE UNIQUE INDEX index_sessions_on_token ON sessions (token);

CREATE INDEX index_sessions_on_user_id ON sessions (user_id);

Add foreign key constraint on User/Session relationship

CREATE TABLE sessions (
    id INTEGER PRIMARY KEY NOT NULL,
    token TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    expired_at TEXT NOT NULL,
    FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX index_sessions_on_token ON sessions (token);

CREATE INDEX index_sessions_on_user_id ON sessions (user_id);


CREATE TABLE IF NOT EXISTS `DateExamJam` (
    `start` DATETIME NOT NULL,
    `end` DATETIME NOT NULL,
PRIMARY KEY (`start`, `end`));

Implement courses in Exam Jam dates

CREATE TABLE IF NOT EXISTS `DateExamJam` (
    `start` DATETIME NOT NULL,
    `end` DATETIME NOT NULL,
    `course` INT(8) NOT NULL,
PRIMARY KEY (`start`, `end`, `course`));


DROP DATABASE IF EXISTS openchpl;

CREATE DATABASE openchpl
  WITH OWNER = openchpl
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       CONNECTION LIMIT = -1;

DROP DATABASE IF EXISTS openchpl_test;

CREATE DATABASE openchpl_test
  WITH OWNER = openchpl
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       CONNECTION LIMIT = -1;

Reset script more robust wrt owners

DROP DATABASE IF EXISTS openchpl;

CREATE DATABASE openchpl
  WITH OWNER = openchpl
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       CONNECTION LIMIT = -1;

DROP DATABASE IF EXISTS openchpl_test;

CREATE DATABASE openchpl_test
  WITH OWNER = openchpl
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       CONNECTION LIMIT = -1;

alter database openchpl owner to openchpl;
alter database openchpl_test owner to openchpl;


# Retrieve heterozygous haplotype calls on chromosomes X and Y.
SELECT
  call.call_set_name,
  GROUP_CONCAT(STRING(call.genotype)) WITHIN call AS genotype,
  reference_name,
  start,
  end,
  reference_bases,
  GROUP_CONCAT(alternate_bases) WITHIN RECORD AS alternate_bases,
FROM
  [_THE_TABLE_]
WHERE
  reference_name IN ('chrX', 'chrY')
OMIT
  call if (2 > COUNT(call.genotype))
  OR EVERY(call.genotype <= 0)
  OR EVERY(call.genotype = 1)
HAVING call.call_set_name IN (_MALE_SAMPLE_IDS_)
# Optionally add a clause here to sort and limit the results.
#_ORDER_BY_

Update query to allow for more diverse datasets.

# Retrieve heterozygous haplotype calls on chromosomes X and Y.
SELECT
  call.call_set_name,
  GROUP_CONCAT(STRING(call.genotype)) WITHIN call AS genotype,
  reference_name,
  start,
  end,
  reference_bases,
  GROUP_CONCAT(alternate_bases) WITHIN RECORD AS alternate_bases,
FROM
  [_THE_TABLE_]
WHERE
  reference_name CONTAINS 'X' OR reference_name CONTAINS 'Y'
OMIT
  call if (2 > COUNT(call.genotype))
  OR EVERY(call.genotype <= 0)
  OR EVERY(call.genotype = 1)
HAVING call.call_set_name IN (_MALE_SAMPLE_IDS_)
# Optionally add a clause here to sort and limit the results.
#_ORDER_BY_


ALTER TABLE users MODIFY COLUMN name varchar(64), MODIFY COLUMN username varchar(64), MODIFY COLUMN organization_id varchar(64);
ALTER TABLE tokens MODIFY COLUMN username varchar(64), MODIFY COLUMN organization_id varchar(64);
ALTER TABLE roles_users MODIFY COLUMN username varchar(64);

Increase varchar lenght in DB.

ALTER TABLE users MODIFY COLUMN name varchar(64), MODIFY COLUMN username varchar(64), MODIFY COLUMN organization_id varchar(64);
ALTER TABLE tokens MODIFY COLUMN username varchar(64), MODIFY COLUMN organization_id varchar(64);
ALTER TABLE roles_users MODIFY COLUMN username varchar(64);
ALTER TABLE permissions MODIFY COLUMN name varchar(128);


# Table structure for healthcheck output database

# Healthcheck running sessions.

CREATE TABLE session (

  session_id					INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  start_time					DATETIME,
  end_time					DATETIME,
  release					INT,

  PRIMARY KEY (session_id),
  KEY release_idx(release)

);

# Individual healthcheck reports

CREATE TABLE report (

  report_id					INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  session_id					INT(10) UNSIGNED NOT NULL,
  species					VARCHAR(255),
  testcase					VARCHAR(255),
  result						ENUM("pass", "fail"),
  text						VARCHAR(255),

  PRIMARY KEY (report_id),
  KEY session_idx(session_id)

);

# Store annotations about healthcheck results

CREATE TABLE annotation (

  report_id					INT(10) UNSIGNED NOT NULL,
  person						VARCHAR(255),
  action						ENUM("ignore", "normal", "flag"),
  reason						ENUM("not relevant", "will be fixed", "healthcheck bug"),
  comment					VARCHAR(255)

);

Make result enum match ReportLine level strings.

# Table structure for healthcheck output database

# Healthcheck running sessions.

CREATE TABLE session (

  session_id					INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  start_time					DATETIME,
  end_time					DATETIME,
  release					INT,

  PRIMARY KEY (session_id),
  KEY release_idx(release)

);

# Individual healthcheck reports

CREATE TABLE report (

  report_id					INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  session_id					INT(10) UNSIGNED NOT NULL,
  species					VARCHAR(255),
  testcase					VARCHAR(255),
  result						ENUM("PROBLEM", "CORRECT", "WARNING", "INFO"),
  text						VARCHAR(255),

  PRIMARY KEY (report_id),
  KEY session_idx(session_id)

);

# Store annotations about healthcheck results

CREATE TABLE annotation (

  report_id					INT(10) UNSIGNED NOT NULL,
  person						VARCHAR(255),
  action						ENUM("ignore", "normal", "flag"),
  reason						ENUM("not relevant", "will be fixed", "healthcheck bug"),
  comment					VARCHAR(255)

);


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

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

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

Revert "Revert "Revert "Fix DDL on timestamp"""

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

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

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


-- version 0

CREATE CACHED TABLE series (
       series_id INTEGER IDENTITY,
       series_name VARCHAR(128) NOT NULL
);

CREATE CACHED TABLE sample (
       series_id INTEGER NOT NULL REFERENCES series(series_id),
       t TIMESTAMP NOT NULL,
       val DOUBLE NOT NULL
);

CREATE INDEX idx_sample_t ON sample(t);

CREATE CACHED TABLE dual (
       x INTEGER
);

INSERT INTO dual(x) values(1);

-- version 1

CREATE CACHED TABLE dashboard (
       name VARCHAR(64) NOT NULL,
       definition VARCHAR(4096) NOT NULL
);

Remove the schema v0 def from the v1, now that sql-file can properly handle it.


USE infrastructure;

-- Views

DROP VIEW IF EXISTS pid_skid;
CREATE VIEW pid_skid
AS
  SELECT DISTINCT
    p.id AS pid,
    csk.skid AS skid
  FROM
    customer_ssh_key csk
    INNER JOIN customer c ON csk.cid = c.id
    INNER JOIN project p ON p.customer = c.id
  UNION DISTINCT
  SELECT
    psk.pid AS pid,
    psk.skid AS skid
  FROM project_ssh_key psk;

DROP VIEW IF EXISTS permission;
CREATE VIEW permission
AS
  SELECT
    sk.id AS key_id,
    CONCAT(sk.key_type, ' ', sk.key_value) AS ssh_key,
    (
      SELECT GROUP_CONCAT(DISTINCT csk.cid SEPARATOR ',')
      FROM customer_ssh_key csk
      WHERE csk.skid = sk.id
    ) AS customers,
    (
      SELECT GROUP_CONCAT(DISTINCT r.pid SEPARATOR ',')
      FROM pid_skid AS r
      WHERE r.skid = sk.id
    ) AS projects
  FROM ssh_key sk;

Document permissions view and example data

USE infrastructure;

-- Views

DROP VIEW IF EXISTS pid_skid;
CREATE VIEW pid_skid
AS
  SELECT DISTINCT
    p.id AS pid,
    csk.skid AS skid
  FROM
    customer_ssh_key csk
    INNER JOIN customer c ON csk.cid = c.id
    INNER JOIN project p ON p.customer = c.id
  UNION DISTINCT
  SELECT
    psk.pid AS pid,
    psk.skid AS skid
  FROM project_ssh_key psk;

-- Permissions view for calculating what each SSH Key is allowed to view.
-- Example:
-- +--------+-------------------+-----------+------------------------------------+
-- | key_id | ssh_key           | customers | projects                           |
-- +--------+-------------------+-----------+------------------------------------+
-- |      1 | ssh-rsa <key>     | 1,2       | 2,1                                |
-- |      2 | ssh-rsa <key>     | NULL      | NULL                               |
-- |      3 | ssh-rsa <key>     | 1         | 1                                  |
-- |      4 | ssh-rsa <key>     | 3         | 17,16,15,14,12,11,10,9,8,7,6,5,4,3 |
-- |      5 | ssh-ed25519 <key> | 3         | 17,16,15,14,12,11,10,9,8,7,6,5,4,3 |
-- +--------+-------------------+-----------+------------------------------------+
DROP VIEW IF EXISTS permission;
CREATE VIEW permission
AS
  SELECT
    sk.id AS key_id,
    CONCAT(sk.key_type, ' ', sk.key_value) AS ssh_key,
    (
      SELECT GROUP_CONCAT(DISTINCT csk.cid SEPARATOR ',')
      FROM customer_ssh_key csk
      WHERE csk.skid = sk.id
    ) AS customers,
    (
      SELECT GROUP_CONCAT(DISTINCT r.pid SEPARATOR ',')
      FROM pid_skid AS r
      WHERE r.skid = sk.id
    ) AS projects
  FROM ssh_key sk;


-- Create schema, tables, indexes, and constraints used for WebUtils module here
-- All SQL VIEW definitions should be created in webutils-create.sql and dropped in webutils-drop.sql
DROP SCHEMA webutils CASCADE;
CREATE SCHEMA webutils;

Fix issue on new installs.

-- Create schema, tables, indexes, and constraints used for WebUtils module here
-- All SQL VIEW definitions should be created in webutils-create.sql and dropped in webutils-drop.sql
DROP SCHEMA IF EXISTS webutils CASCADE;
CREATE SCHEMA webutils;


ALTER TABLE workspace RENAME TO project;
ALTER TABLE space RENAME COLUMN workspace_id TO project_id;
ALTER TABLE space RENAME TO pairing_board;
ALTER TABLE person RENAME COLUMN workspace_id TO project_id;
ALTER TABLE person RENAME COLUMN space_id TO pairing_board_id;
ALTER TABLE pairing_history RENAME COLUMN workspace_id TO project_id;

Update mifgration to use correct foreign key names

ALTER TABLE workspace RENAME TO project;

ALTER TABLE space RENAME CONSTRAINT fk_workspace_id TO fk_project_id;
ALTER TABLE space RENAME COLUMN workspace_id TO project_id;
ALTER TABLE space RENAME TO pairing_board;

ALTER TABLE person RENAME CONSTRAINT fk_workspace_id TO fk_project_id;
ALTER TABLE person RENAME COLUMN workspace_id TO project_id;
ALTER TABLE person RENAME CONSTRAINT fk_space_id TO fk_pairing_board_id;
ALTER TABLE person RENAME COLUMN space_id TO pairing_board_id;

ALTER TABLE pairing_history RENAME CONSTRAINT fk_workspace_id TO fk_project_id;
ALTER TABLE pairing_history RENAME COLUMN workspace_id TO project_id;


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

SET client_min_messages TO WARNING;

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

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

-- Enable PostGIS
CREATE EXTENSION postgis;

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

Set database character set to UTF-8

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

SET client_min_messages TO WARNING;

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

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

-- Enable PostGIS
CREATE EXTENSION postgis;

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


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

Add missing field for owner db

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


CREATE TABLE IF NOT EXISTS `sgf_sessions` (
  `session_id` varchar(40) collate utf8_bin NOT NULL default '0',
  `ip_address` varchar(16) collate utf8_bin NOT NULL default '0',
  `user_agent` varchar(150) collate utf8_bin NOT NULL,
  `last_activity` int(10) unsigned NOT NULL default '0',
  `user_data` text collate utf8_bin DEFAULT '',
  PRIMARY KEY  (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `sgf_user_session` (
	`session_id` VARCHAR(36) NOT NULL,
	`user_id` VARCHAR(36) NOT NULL,
	PRIMARY KEY ( `session_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `sgf_user_settings` (
	`user_id` VARCHAR(36) NOT NULL,
	`style` VARCHAR(16) NOT NULL,
	PRIMARY KEY ( `user_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `sgf_user_validation` (
	`user_id` VARCHAR(36) NOT NULL,
	`validation_code` VARCHAR(36) NOT NULL,
	`validation_type` VARCHAR(16) NOT NULL,
	PRIMARY KEY ( `user_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Revert "A test of the assumption that perhaps it was wrong to eliminate the default value in resolving the conflicting sql"

CREATE TABLE IF NOT EXISTS `sgf_sessions` (
  `session_id` varchar(40) collate utf8_bin NOT NULL default '0',
  `ip_address` varchar(16) collate utf8_bin NOT NULL default '0',
  `user_agent` varchar(150) collate utf8_bin NOT NULL,
  `last_activity` int(10) unsigned NOT NULL default '0',
  `user_data` text collate utf8_bin NOT NULL,
  PRIMARY KEY  (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `sgf_user_session` (
	`session_id` VARCHAR(36) NOT NULL,
	`user_id` VARCHAR(36) NOT NULL,
	PRIMARY KEY ( `session_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `sgf_user_settings` (
	`user_id` VARCHAR(36) NOT NULL,
	`style` VARCHAR(16) NOT NULL,
	PRIMARY KEY ( `user_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `sgf_user_validation` (
	`user_id` VARCHAR(36) NOT NULL,
	`validation_code` VARCHAR(36) NOT NULL,
	`validation_type` VARCHAR(16) NOT NULL,
	PRIMARY KEY ( `user_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


SET ROLE social;

CREATE SCHEMA sitemap;

GRANT usage ON SCHEMA sitemap to socialapplication;

-- append sitemap schema
SELECT set_config('search_path', current_setting('search_path') || ',sitemap', false);

Disable set role line in schema.sql

-- SET ROLE social;

CREATE SCHEMA sitemap;

GRANT usage ON SCHEMA sitemap to socialapplication;

-- append sitemap schema
SELECT set_config('search_path', current_setting('search_path') || ',sitemap', false);


-- Geocodes a street address given a searchtext and a state and/or country
CREATE OR REPLACE FUNCTION cdb_geocoder_server.geocode_street(searchtext TEXT, state TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
  RETURNS Geometry
AS $$
  from heremaps import heremapsgeocoder

  geocoder = heremapsgeocoder.Geocoder(app_id, app_code)

  results = geocoder.geocode_address(searchtext=searchtext, state=state, country=country)
  coordinates = geocoder.extract_lng_lat_from_result(results[0])

  plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"])
  point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0]

  return point['st_setsrid']
$$ LANGUAGE plpythonu VOLATILE;

Remove schema from function definition

-- Geocodes a street address given a searchtext and a state and/or country
CREATE OR REPLACE FUNCTION geocode_street(searchtext TEXT, state TEXT DEFAULT NULL, country TEXT DEFAULT NULL)
  RETURNS Geometry
AS $$
  from heremaps import heremapsgeocoder

  geocoder = heremapsgeocoder.Geocoder(app_id, app_code)

  results = geocoder.geocode_address(searchtext=searchtext, state=state, country=country)
  coordinates = geocoder.extract_lng_lat_from_result(results[0])

  plan = plpy.prepare("SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326); ", ["double precision", "double precision"])
  point = plpy.execute(plan, [coordinates[0], coordinates[1]], 1)[0]

  return point['st_setsrid']
$$ LANGUAGE plpythonu VOLATILE;


UPDATE court
SET website="http://www.stlouisco.com/LawandPublicSafety/Municipalcourt"
WHERE id = 35 OR id = 51;

Fix changelog for court "id" to "court_id"

UPDATE court
SET website="http://www.stlouisco.com/LawandPublicSafety/Municipalcourt"
WHERE court_id = 35 OR court_id = 51;


-- We are declaring the return type to be queue_classic_jobs.
-- This is ok since I am assuming that all of the users added queues will
-- have identical columns to queue_classic_jobs.
-- When QC supports queues with columns other than the default, we will have to change this.

CREATE OR REPLACE FUNCTION lock_head(q_name varchar, top_boundary integer)
RETURNS SETOF queue_classic_jobs AS $$
DECLARE
  unlocked bigint;
  relative_top integer;
  job_count integer;
BEGIN
  -- The purpose is to release contention for the first spot in the table.
  -- The select count(*) is going to slow down dequeue performance but allow
  -- for more workers. Would love to see some optimization here...

  EXECUTE 'SELECT count(*) FROM '
    || '(SELECT * FROM queue_classic_jobs '
    || ' WHERE locked_at IS NULL'
    || ' AND q_name = '
    || quote_literal(q_name)
    || ' AND scheduled_at <= '
    || quote_literal(now())
    || ' LIMIT '
    || quote_literal(top_boundary)
    || ') limited'
  INTO job_count;

  SELECT TRUNC(random() * (top_boundary - 1))
  INTO relative_top;

  IF job_count < top_boundary THEN
    relative_top = 0;
  END IF;

  LOOP
    BEGIN
      EXECUTE 'SELECT id FROM queue_classic_jobs '
        || ' WHERE locked_at IS NULL'
        || ' AND q_name = '
        || quote_literal(q_name)
        || ' AND scheduled_at <= '
        || quote_literal(now())
        || ' ORDER BY id ASC'
        || ' LIMIT 1'
        || ' OFFSET ' || quote_literal(relative_top)
        || ' FOR UPDATE NOWAIT'
      INTO unlocked;
      EXIT;
    EXCEPTION
      WHEN lock_not_available THEN
        -- do nothing. loop again and hope we get a lock
    END;
  END LOOP;

  RETURN QUERY EXECUTE 'UPDATE queue_classic_jobs '
    || ' SET locked_at = (CURRENT_TIMESTAMP),'
    || ' locked_by = (select pg_backend_pid())'
    || ' WHERE id = $1'
    || ' AND locked_at is NULL'
    || ' RETURNING *'
  USING unlocked;

  RETURN;
END $$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION lock_head(tname varchar) RETURNS SETOF queue_classic_jobs AS $$ BEGIN
  RETURN QUERY EXECUTE 'SELECT * FROM lock_head($1,10)' USING tname;
END $$ LANGUAGE plpgsql;

-- queue_classic_notify function and trigger
CREATE FUNCTION queue_classic_notify() RETURNS TRIGGER AS $$ BEGIN
  perform pg_notify(new.q_name, ''); RETURN NULL;
END $$ LANGUAGE plpgsql;

CREATE TRIGGER queue_classic_notify
AFTER INSERT ON queue_classic_jobs FOR EACH ROW
EXECUTE PROCEDURE queue_classic_notify();

Remove lock_head as it is no longer used


INSERT INTO version (`name`, `release`, `author`, `license`, `created`) VALUES ("Present IT", "1.0.1", "Icepick & Strikeflare", "MIT",  CURRENT_TIMESTAMP());

Change Strikeflare to Winnefoo in TestSuite

INSERT INTO version (`name`, `release`, `author`, `license`, `created`) VALUES ("Present IT", "1.0.1", "Icepick & Winnefoo", "MIT",  CURRENT_TIMESTAMP());


-- WARNING! Speak to Joe or Matt first. CASCADE is very very dangerous.

\connect test

DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE users (
  ID SERIAL PRIMARY KEY,
  TOKEN VARCHAR(255) NOT NULL,
  NAME VARCHAR(255) NOT NULL,
  EMAIL VARCHAR(255) NOT NULL,
  PHONE VARCHAR(255) NOT NULL
);

DROP TABLE IF EXISTS saved_queries CASCADE;
CREATE TABLE saved_queries (
  ID SERIAL PRIMARY KEY,
  API_ID INT REFERENCES apis (ID),
  ARGUMENTS TEXT NOT NULL,
  USER_ID INT REFERENCES users (ID),
  CREATED_AT TIMESTAMP NOT NULL
);

DROP TABLE IF EXISTS apis CASCADE;
CREATE TABLE apis (
  ID SERIAL PRIMARY KEY,
  NAME varchar(255)
);

DROP TABLE IF EXISTS words CASCADE;
CREATE TABLE words (
  ID SERIAL PRIMARY KEY,
  API_ID INT REFERENCES apis (ID),
  WORD TEXT NOT NULL
);

Change column names from upper to lower case

-- WARNING! Speak to Joe or Matt first. CASCADE is very very dangerous.

\connect test

DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  token VARCHAR(255) NOT NULL,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  phone VARCHAR(255) NOT NULL
);

DROP TABLE IF EXISTS saved_queries CASCADE;
CREATE TABLE saved_queries (
  id SERIAL PRIMARY KEY,
  api_id INT REFERENCES apis (id),
  arguments TEXT NOT NULL,
  user_id INT REFERENCES users (id),
  created_at TIMESTAMP NOT NULL
);

DROP TABLE IF EXISTS apis CASCADE;
CREATE TABLE apis (
  id SERIAL PRIMARY KEY,
  name varchar(255)
);

DROP TABLE IF EXISTS words CASCADE;
CREATE TABLE words (
  id SERIAL PRIMARY KEY,
  api_id INT REFERENCES apis (id),
  word TEXT NOT NULL
);


delete from PROPOSAL_LOG;
delete from PROPOSAL_LOG_STATUS;
insert into PROPOSAL_LOG_STATUS (PROPOSAL_LOG_STATUS_CODE, DESCRIPTION, UPDATE_TIMESTAMP, UPDATE_USER)
values ('1', 'PENDING', SYSDATE, USER);

Change proposal log status to submitted after promoting it to an institutional proposal

delete from PROPOSAL_LOG;
delete from PROPOSAL_LOG_STATUS;
insert into PROPOSAL_LOG_STATUS (PROPOSAL_LOG_STATUS_CODE, DESCRIPTION, UPDATE_TIMESTAMP, UPDATE_USER)
values ('1', 'PENDING', SYSDATE, USER);
insert into PROPOSAL_LOG_STATUS (PROPOSAL_LOG_STATUS_CODE, DESCRIPTION, UPDATE_TIMESTAMP, UPDATE_USER)
values ('3', 'SUBMITTED', SYSDATE, USER);


COPY (SELECT
id,
created,
modified,
reference,
category_id,
you_id,
partner_id,
disputed_savings_id,
'[deleted]' AS your_problem_notes,
'[deleted]' AS notes,
state,
dependants_young,
dependants_old,
on_passported_benefits,
on_nass_benefits,
specific_benefits,
is_you_or_your_partner_over_60,
has_partner,
calculations
FROM legalaid_eligibilitycheck
WHERE (modified >= %(from_date)s::timestamp AND modified <= %(to_date)s::timestamp)
OR (created >= %(from_date)s::timestamp AND created <= %(to_date)s::timestamp))
TO STDOUT CSV HEADER;

Add disregards in the eligibility report

COPY (SELECT
id,
created,
modified,
reference,
category_id,
you_id,
partner_id,
disputed_savings_id,
'[deleted]' AS your_problem_notes,
'[deleted]' AS notes,
state,
dependants_young,
dependants_old,
on_passported_benefits,
on_nass_benefits,
specific_benefits,
CASE WHEN lower(disregards::text) LIKE '%%:true%%' THEN 'Yes' ELSE 'No' end as "Disregarded Payment",
is_you_or_your_partner_over_60,
has_partner,
calculations
FROM legalaid_eligibilitycheck
WHERE (modified >= %(from_date)s::timestamp AND modified <= %(to_date)s::timestamp)
OR (created >= %(from_date)s::timestamp AND created <= %(to_date)s::timestamp))
TO STDOUT CSV HEADER;


CREATE TABLE [security].[User](
  [Id] INT NOT NULL PRIMARY KEY IDENTITY,
  [UniqueId] UNIQUEIDENTIFIER NOT NULL DEFAULT newid(),
  [Login] NVARCHAR(100) NOT NULL,
  [Password] NVARCHAR(1000) NOT NULL,
  [Salt] NVARCHAR(1000) NOT NULL,
  [FirstName] NVARCHAR(50) NOT NULL,
  [LastName] NVARCHAR(50) NOT NULL,
  [LastNamePrefix] NVARCHAR(50) NULL
) ON [PRIMARY];

Add correlation columns in the user database table.

CREATE TABLE [security].[User](
  [Id] INT NOT NULL PRIMARY KEY IDENTITY,
  [UniqueId] UNIQUEIDENTIFIER NOT NULL DEFAULT newid(),
  [Login] NVARCHAR(100) NOT NULL,
  [Password] NVARCHAR(1000) NOT NULL,
  [Salt] NVARCHAR(1000) NOT NULL,
  [FirstName] NVARCHAR(50) NOT NULL,
  [LastName] NVARCHAR(50) NOT NULL,
  [LastNamePrefix] NVARCHAR(50) NULL,
  [ExternalType] NCHAR(1) NULL,
  [ExternalCorrelationId] NVARCHAR(50) NULL
) ON [PRIMARY];


TRUNCATE TABLE HumanResources.JobCandidate;
GO
TRUNCATE TABLE PartitionTable1
WITH (PARTITIONS (2, 4, 6 TO 8));
GO

Add test for truncate in different database

TRUNCATE TABLE HumanResources.JobCandidate;
GO
TRUNCATE TABLE PartitionTable1
WITH (PARTITIONS (2, 4, 6 TO 8));
GO
TRUNCATE TABLE DifferentDB..JobCandidate;
GO


update tree_element ute
set synonyms = synonyms_as_jsonb(ute.instance_id, 'id.biodiversity.org.au'),
    synonyms_html = coalesce(synonyms_as_html(ute.instance_id), '<synonyms></synonyms>')
where ute.id in (select distinct(te.id)
                 from tree_element te
                        join tree_version_element tve on te.id = tve.tree_element_id
                        join tree t on (t.current_tree_version_id = tve.tree_version_id) OR
                                       (t.default_draft_tree_version_id = tve.tree_version_id)
                 where te.synonyms <> synonyms_as_jsonb(te.instance_id, 'id.biodiversity.org.au'));

-- version
UPDATE db_version
SET version = 30
WHERE id = 1;

Fix bung upgrade in 29 where an extra "synonyms_html =" got through un-noticed until staging.

-- fix buggered synonyms_html from update 29
update tree_element ute
set synonyms_html = coalesce(synonyms_as_html(ute.instance_id), '<synonyms></synonyms>')
where ute.synonyms_html = 'false';

update tree_element ute
set synonyms_html = coalesce(synonyms_as_html(ute.instance_id), '<synonyms></synonyms>')
where ute.synonyms_html = 'true';

-- version
UPDATE db_version
SET version = 30
WHERE id = 1;


-- run as a cronjob: psql packages -f tools/daily.sql -o /dev/null
delete from cookies where last_seen < now()-INTERVAL'1day';
delete from openid_sessions where expires < now();
delete from openid_nonces where created < now()-INTERVAL'1day';
delete from users where name in (select name from rego_otk where date < now()-INTERVAL'7days');

Delete openids of expired users.

-- run as a cronjob: psql packages -f tools/daily.sql -o /dev/null
delete from cookies where last_seen < now()-INTERVAL'1day';
delete from openid_sessions where expires < now();
delete from openid_nonces where created < now()-INTERVAL'1day';
delete from openids where name in (select name from rego_otk where date < now()-INTERVAL'7days');
delete from users where name in (select name from rego_otk where date < now()-INTERVAL'7days');


ALTER TABLE `files`
 DROP COLUMN `is_packaged`, `jetpack_version`, `requires_chrome`, `is_multi_package`;

Fix syntax of broken migration 1086

/* If this migration fails locally, comment lines for columns that don't exist */
ALTER TABLE `files`
 DROP COLUMN `is_packaged`,
 DROP COLUMN `jetpack_version`,
 DROP COLUMN `requires_chrome`,
 DROP COLUMN `is_multi_package`;


begin;
alter table journals drop constraint "$1";
alter table journals add foreign key (submitted_by) references users (name) on update cascade;
end;

Support renaming users for roles.

begin;
alter table journals drop constraint "$1";
alter table journals add foreign key (submitted_by) references users (name) on update cascade;
end;
begin;
alter table roles drop constraint "$1";
alter table roles add foreign key (user_name) references users (name) on update cascade;
end;


SELECT t1.name AS nameOne, t2.name AS nameTwo, t1.spotId AS spotId, t1.id AS resultId, t1.nameNumber AS nameNumberOne,
       t2.nameNumber AS nameNumberTwo, t1.comments AS firstComments, t2.comments AS secondComments, t1.winnerId,
       t1.performanceId, t1.performanceName
FROM
  (
    SELECT u1.name, r1.id, r1.spotId, u1.nameNumber, r1.firstComments AS comments, r1.winnerId, r1.pending,
    p.id AS performanceId, p.name AS performanceName
    FROM results AS r1, users AS u1, performances AS p
    WHERE r1.firstNameNumber = u1.nameNumber AND r1.needsApproval AND r1.performanceId = p.id AND NOT r1.pending
    ORDER BY p.id
  ) t1
LEFT JOIN
  (
    SELECT u2.name, r2.id, u2.nameNumber, r2.secondComments AS comments
    FROM results AS r2, users AS u2
    WHERE r2.secondNameNumber = u2.nameNumber
  ) t2
ON
  t1.id = t2.id;

Correct order by performance id

SELECT t1.name AS nameOne, t2.name AS nameTwo, t1.spotId AS spotId, t1.id AS resultId, t1.nameNumber AS nameNumberOne,
       t2.nameNumber AS nameNumberTwo, t1.comments AS firstComments, t2.comments AS secondComments, t1.winnerId,
       t1.performanceId, t1.performanceName
FROM
  (
    SELECT u1.name, r1.id, r1.spotId, u1.nameNumber, r1.firstComments AS comments, r1.winnerId, r1.pending,
    p.id AS performanceId, p.name AS performanceName
    FROM results AS r1, users AS u1, performances AS p
    WHERE r1.firstNameNumber = u1.nameNumber AND r1.needsApproval AND r1.performanceId = p.id AND NOT r1.pending
  ) t1
LEFT JOIN
  (
    SELECT u2.name, r2.id, u2.nameNumber, r2.secondComments AS comments
    FROM results AS r2, users AS u2
    WHERE r2.secondNameNumber = u2.nameNumber
  ) t2
ON
  t1.id = t2.id
ORDER BY t1.performanceId;


/*
    Select all tweets and match with their authors.

    For replies, show the GUID of the target tweet and the screen name of the target profile.

    GUIDs are formatted as strings so they read as text rather than integers when parsed as a CSV file.

    DATETIME is used to remove microseconds from posted time.

*/

SELECT
    'ID_' || Profile.guid AS author_guid,
    '@' || Profile.screen_name AS author_screen_name,
    Profile.image_url AS author_image_url,
    Profile.followers_count AS author_follower_count,
    Profile.location AS author_bio_location,
    'ID_' || Tweet.guid AS tweet_guid,
    DATE(Tweet.created_at) AS posted_date,
    DATETIME(Tweet.created_at) AS posted_datetime,
    Tweet.favorite_count AS favs,
    Tweet.retweet_count AS RTs,
    Tweet.message,
    Tweet.in_reply_to_tweet_guid AS replied_to_tweet_guid,
    '@' || target_prof.screen_name AS replied_to_prof_screen_name,
    target_prof.image_url AS replied_to_prof_image_url
FROM Tweet
INNER JOIN Profile ON Tweet.profile_id = Profile.id
LEFT OUTER JOIN Profile AS target_prof ON target_prof.guid = Tweet.in_reply_to_profile_guid
ORDER BY posted_datetime DESC;

Update in reply to tweet GUID in SQL.

/*
    Select all tweets and match with their authors.

    For replies, show the GUID of the target tweet and the screen name of the target profile.

    GUIDs are formatted as strings so they read as text rather than integers when parsed as a CSV file.

    DATETIME is used to remove microseconds from posted time.

*/

SELECT
    'ID_' || Profile.guid AS author_guid,
    '@' || Profile.screen_name AS author_screen_name,
    Profile.image_url AS author_image_url,
    Profile.followers_count AS author_follower_count,
    Profile.location AS author_bio_location,
    'ID_' || Tweet.guid AS tweet_guid,
    DATE(Tweet.created_at) AS posted_date,
    DATETIME(Tweet.created_at) AS posted_datetime,
    Tweet.favorite_count AS favs,
    Tweet.retweet_count AS RTs,
    Tweet.message,
    'ID_' || Tweet.in_reply_to_tweet_guid AS replied_to_tweet_guid,
    '@' || target_prof.screen_name AS replied_to_prof_screen_name,
    target_prof.image_url AS replied_to_prof_image_url
FROM Tweet
INNER JOIN Profile ON Tweet.profile_id = Profile.id
LEFT OUTER JOIN Profile AS target_prof ON target_prof.guid = Tweet.in_reply_to_profile_guid
ORDER BY posted_datetime DESC;


CREATE OR REPLACE FUNCTION cdb_dataservices_client.cdb_isochrone(center GEOMETRY, kind TEXT, range INTEGER[], OUT center geometry, OUT data_range integer, OUT the_geom geometry)
AS $$
  SELECT
    $1 center,
    _t.radius data_range,
    ST_Buffer($1::geography, _t.radius)::geometry as the_geom
  FROM (
    SELECT unnest($3) as radius
  ) _t
$$ LANGUAGE 'sql' IMMUTABLE STRICT;

Add mapzen isochrone function to fixtures

CREATE OR REPLACE FUNCTION cdb_dataservices_client.cdb_isochrone(center GEOMETRY, kind TEXT, range INTEGER[], OUT center geometry, OUT data_range integer, OUT the_geom geometry)
AS $$
  SELECT
    $1 center,
    _t.radius data_range,
    ST_Buffer($1::geography, _t.radius)::geometry as the_geom
  FROM (
    SELECT unnest($3) as radius
  ) _t
$$ LANGUAGE 'sql' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION cdb_dataservices_client.cdb_mapzen_isochrone(center GEOMETRY, kind TEXT, range INTEGER[], OUT center geometry, OUT data_range integer, OUT the_geom geometry)
AS $$
  SELECT
    $1 center,
    _t.radius data_range,
    ST_Buffer($1::geography, _t.radius)::geometry as the_geom
  FROM (
    SELECT unnest($3) as radius
  ) _t
$$ LANGUAGE 'sql' IMMUTABLE STRICT;


-- Change version of Centreon
UPDATE `informations` SET `value` = '2.8.3' WHERE CONVERT( `informations`.`key` USING utf8 ) = 'version' AND CONVERT ( `informations`.`value` USING utf8 ) = '2.8.2' LIMIT 1;

-- broker option
DELETE FROM `options` WHERE `key` = 'broker';
INSERT INTO `options` (`key`, `value`) VALUES ('broker', 'broker');

Remove relations between contact templates and contactgroups

-- Change version of Centreon
UPDATE `informations` SET `value` = '2.8.3' WHERE CONVERT( `informations`.`key` USING utf8 ) = 'version' AND CONVERT ( `informations`.`value` USING utf8 ) = '2.8.2' LIMIT 1;

-- broker option
DELETE FROM `options` WHERE `key` = 'broker';
INSERT INTO `options` (`key`, `value`) VALUES ('broker', 'broker');

-- Remove relations between contact templates and contactgroups
DELETE FROM contactgroup_contact_relation
WHERE contact_contact_id IN (
    SELECT contact_id FROM contact WHERE contact_register = '0'
);


CREATE TABLE meta(
       schema_version INTEGER NOT NULL DEFAULT 1
);
CREATE TABLE objects(
       id INTEGER PRIMARY KEY NOT NULL,
       name TEXT NOT NULL UNIQUE,
       type INTEGER NOT NULL
);
CREATE TABLE raw_data(
       id INTEGER NOT NULL,
       raw_value BLOB NOT NULL,
       FOREIGN KEY(id) REFERENCES objects(id)
       ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE TABLE kv_data(
       id INTEGER NOT NULL,
       kv_key BLOB NOT NULL,
       kv_value BLOB NOT NULL,
       FOREIGN KEY(id) REFERENCES objects(id)
       ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE TABLE array_data(
       id INTEGER NOT NULL,
       array_index INTEGER NOT NULL,
       array_value BLOB NOT NULL,
       FOREIGN KEY(id) REFERENCES objects(id)
       ON DELETE CASCADE ON UPDATE RESTRICT
);

Add 'IF NOT EXISTS' specifier to schema

CREATE TABLE IF NOT EXISTS meta(
       schema_version INTEGER NOT NULL DEFAULT 1
);
CREATE TABLE IF NOT EXISTS objects(
       id INTEGER PRIMARY KEY NOT NULL,
       name TEXT NOT NULL UNIQUE,
       type INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS raw_data(
       id INTEGER NOT NULL,
       raw_value BLOB NOT NULL,
       FOREIGN KEY(id) REFERENCES objects(id)
       ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE TABLE IF NOT EXISTS kv_data(
       id INTEGER NOT NULL,
       kv_key BLOB NOT NULL,
       kv_value BLOB NOT NULL,
       FOREIGN KEY(id) REFERENCES objects(id)
       ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE TABLE IF NOT EXISTS array_data(
       id INTEGER NOT NULL,
       array_index INTEGER NOT NULL,
       array_value BLOB NOT NULL,
       FOREIGN KEY(id) REFERENCES objects(id)
       ON DELETE CASCADE ON UPDATE RESTRICT
);


create table entities (
    id serial primary key,
    jid text not null unique
);

create table nodes (
    id serial primary key,
    node text not null unique,
    persistent boolean not null default true,
    deliver_payload boolean not null default true
    send_last_published_item text not null default 'on_sub'
        check (send_last_published_item in ('never', 'on_sub')),
);

create table affiliations (
    id serial primary key,
    entity_id integer not null references entities on delete cascade,
    node_id integer not null references nodes on delete cascade,
    affiliation text not null
        check (affiliation in ('outcast', 'publisher', 'owner')),
    unique (entity_id, node_id)
);

create table subscriptions (
    id serial primary key,
    entity_id integer not null references entities on delete cascade,
    resource text,
    node_id integer not null references nodes on delete cascade,
    subscription text not null default 'subscribed' check
        (subscription in ('subscribed', 'pending', 'unconfigured')),
    unique (entity_id, resource, node_id)
);

create table items (
    id serial primary key,
    node_id integer not null references nodes on delete cascade,
    item text not null,
    publisher text not null,
    data text,
    date timestamp with time zone not null default now(),
    unique (node_id, item)
);

Fix syntax errors in database schema.

create table entities (
    id serial primary key,
    jid text not null unique
);

create table nodes (
    id serial primary key,
    node text not null unique,
    persistent boolean not null default true,
    deliver_payload boolean not null default true,
    send_last_published_item text not null default 'on_sub'
        check (send_last_published_item in ('never', 'on_sub'))
);

create table affiliations (
    id serial primary key,
    entity_id integer not null references entities on delete cascade,
    node_id integer not null references nodes on delete cascade,
    affiliation text not null
        check (affiliation in ('outcast', 'publisher', 'owner')),
    unique (entity_id, node_id)
);

create table subscriptions (
    id serial primary key,
    entity_id integer not null references entities on delete cascade,
    resource text,
    node_id integer not null references nodes on delete cascade,
    subscription text not null default 'subscribed' check
        (subscription in ('subscribed', 'pending', 'unconfigured')),
    unique (entity_id, resource, node_id)
);

create table items (
    id serial primary key,
    node_id integer not null references nodes on delete cascade,
    item text not null,
    publisher text not null,
    data text,
    date timestamp with time zone not null default now(),
    unique (node_id, item)
);


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

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

Add schema v16 update script to schema install

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

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


CREATE DATABASE notes;

CREATE TABLE note (
    NoteId INT(11) AUTO_INCREMENT PRIMARY KEY,
    NoteText VARCHAR(255),
    NoteTags VARCHAR(512),
	NoteTitle VARCHAR(50),
	NoteComplete TINYINT,
	UserId VARCHAR(60),
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);

CREATE TABLE note_users (
    UserId VARCHAR(60) AUTO_INCREMENT PRIMARY KEY,
    UserEmail VARCHAR(255),
    UserPassword VARCHAR(512)
);

Fix the invalid SQL. Add a new field in the user table.

CREATE DATABASE notes;

CREATE TABLE note (
    NoteId INT(11) AUTO_INCREMENT PRIMARY KEY,
    NoteText VARCHAR(255),
    NoteTags VARCHAR(512),
	NoteTitle VARCHAR(50),
	NoteComplete TINYINT,
	UserId VARCHAR(60),
	FOREIGN KEY (UserId) REFERENCES note_users(UserId) ON DELETE CASCADE
);

CREATE TABLE note_users (
    UserId VARCHAR(60) PRIMARY KEY,
    UserEmail VARCHAR(255),
    UserPassword VARCHAR(512),
	TagColor VARCHAR(6)
);


/*-------------------------------------------
-- Description: Add concept search; now all search
  terms are concepts and query text can be removed

-- Previous Version: 11.1.0.4
-- Target Version: 11.2.0.0
--------------------------------------------*/

SET SCHEMA find;

-- Add quotes to old related concepts, as we no longer want to add quotes to all concepts when generating query text
UPDATE search_concept_cluster_phrases
SET search_concept_cluster_phrases.phrase = CONCAT('"', search_concept_cluster_phrases.phrase, '"');

-- Treat query text as context
-- it will always be primary because there is no associated cluster
-- we can safely use -1 for the cluster id as we generate it in the code from 0 incrementing upwards
-- we no longer store '*' as a concept, so we want to exclude that from the initial migration step
INSERT INTO search_concept_cluster_phrases (search_id, phrase, primary_phrase, cluster_id)
SELECT search_id, query_text, TRUE, -1
FROM searches
WHERE query_text <> '*';

ALTER TABLE searches DROP COLUMN query_text;

Correct mysql migration script (FIND-588)

/*-------------------------------------------
-- Description: Add concept search; now all search
  terms are concepts and query text can be removed

-- Previous Version: 11.1.0.4
-- Target Version: 11.2.0.0
--------------------------------------------*/

USE find;

-- Add quotes to old related concepts, as we no longer want to add quotes to all concepts when generating query text
UPDATE search_concept_cluster_phrases
SET search_concept_cluster_phrases.phrase = CONCAT('"', search_concept_cluster_phrases.phrase, '"');

-- Treat query text as context
-- it will always be primary because there is no associated cluster
-- we can safely use -1 for the cluster id as we generate it in the code from 0 incrementing upwards
-- we no longer store '*' as a concept, so we want to exclude that from the initial migration step
INSERT INTO search_concept_cluster_phrases (search_id, phrase, primary_phrase, cluster_id)
SELECT search_id, query_text, TRUE, -1
FROM searches
WHERE query_text <> '*';

ALTER TABLE searches DROP COLUMN query_text;


select c.id, c.name, count(v.id) as votes from "choice" c
left outer join
(  select v.* from vote v
	join "round" r on v.round_id = r.id
	where r.id = $1
) as v on v.choice_id = c.id
where (c.added_in is null or c.added_in <= $1)
and (c.removed_in is null or c.removed_in >= $1)
group by c.id
order by c.id asc;

Fix up the votes db query so that it gets the winner correctly

select c.id, c.name, r.winning_choice_id = c.id as winner, count(v.id) as votes from choice c
join round r on r.id = $1
left outer join vote v on v.choice_id = c.id and v.round_id = $1
where (c.added_in is null or c.added_in <= $1)
and (c.removed_in is null or c.removed_in >= $1)
group by c.id, r.winning_choice_id
order by c.id asc;


CREATE PROCEDURE [dbo].[ProcessFullyApprovedCohort]
    @cohortId BIGINT,
    @accountId BIGINT,
    @apprenticeshipEmployerType INT
AS
BEGIN
    UPDATE [dbo].[Commitment]
    SET ApprenticeshipEmployerTypeOnApproval = @apprenticeshipEmployerType, IsFullApprovalProcessed = 1
    WHERE Id = @cohortId

    UPDATE [dbo].[Apprenticeship]
    SET PaymentStatus = 1
    WHERE CommitmentId = @cohortId

    INSERT INTO [dbo].[PriceHistory] (ApprenticeshipId, Cost, FromDate)
    SELECT Id, Cost, StartDate
    FROM [dbo].[Apprenticeship]
    WHERE CommitmentId = @cohortId

END

Use actual start date if available.

CREATE PROCEDURE [dbo].[ProcessFullyApprovedCohort]
    @cohortId BIGINT,
    @accountId BIGINT,
    @apprenticeshipEmployerType INT
AS
BEGIN
    UPDATE [dbo].[Commitment]
    SET ApprenticeshipEmployerTypeOnApproval = @apprenticeshipEmployerType, IsFullApprovalProcessed = 1
    WHERE Id = @cohortId

    UPDATE [dbo].[Apprenticeship]
    SET PaymentStatus = 1
    WHERE CommitmentId = @cohortId

    INSERT INTO [dbo].[PriceHistory] (ApprenticeshipId, Cost, FromDate)
    SELECT Id, Cost, ISNULL(ActualStartDate, StartDate)
    FROM [dbo].[Apprenticeship]
    WHERE CommitmentId = @cohortId

END


-- The Postgres trigger function

CREATE OR REPLACE FUNCTION pubsubpull_change_recorder() RETURNS TRIGGER AS $body$
BEGIN
  IF (TG_OP = 'DELETE') THEN
    INSERT INTO pubsubpull_updatelog ("table", "when", old, new)
    SELECT TG_TABLE_NAME, now(), row_to_json(OLD)::jsonb, NULL;
  ELSEIF (TG_OP = 'UPDATE') THEN
    INSERT INTO pubsubpull_updatelog ("table", "when", old, new)
    SELECT TG_TABLE_NAME, now(), row_to_json(OLD)::jsonb, row_to_json(NEW)::jsonb;
  ELSEIF (TG_OP = 'INSERT') THEN
    INSERT INTO pubsubpull_updatelog ("table", "when", old, new)
    SELECT TG_TABLE_NAME, now(), NULL, row_to_json(NEW)::jsonb;
  END IF;
  RETURN NULL;
END;
$body$ LANGUAGE plpgsql;

Fix the trigger function to record the instruction type.

-- The Postgres trigger function

CREATE OR REPLACE FUNCTION pubsubpull_change_recorder() RETURNS TRIGGER AS $body$
BEGIN
  IF (TG_OP = 'DELETE') THEN
    INSERT INTO pubsubpull_updatelog ("table", type, "when", old, new)
    SELECT TG_TABLE_NAME, 'D', now(), row_to_json(OLD)::jsonb, NULL;
  ELSEIF (TG_OP = 'UPDATE') THEN
    INSERT INTO pubsubpull_updatelog ("table", type, "when", old, new)
    SELECT TG_TABLE_NAME, 'U', now(), row_to_json(OLD)::jsonb, row_to_json(NEW)::jsonb;
  ELSEIF (TG_OP = 'INSERT') THEN
    INSERT INTO pubsubpull_updatelog ("table", type, "when", old, new)
    SELECT TG_TABLE_NAME, 'I', now(), NULL, row_to_json(NEW)::jsonb;
  END IF;
  RETURN NULL;
END;
$body$ LANGUAGE plpgsql;


insert into Coursework(title,date_submitted,file,student_user_id,class_id)
values (?,now(),?,?,?);

Modify to match new column data for Coursework table

insert into Coursework(title,date_submitted,file,file_extension,student_user_id,class_id)
values (?,now(),?,?,?,?);


SELECT code, description, coalesce(fl.local_description, flp.local_description) as local_description
FROM foods_categories
         INNER JOIN foods_local_lists ON foods_categories.food_code = foods_local_lists.food_code AND foods_local_lists.locale_id = {locale_id}
         LEFT JOIN foods ON foods.code = foods_local_lists.food_code
         LEFT JOIN foods_local as fl ON fl.food_code = foods_local_lists.food_code AND fl.locale_id = {locale_id}
         LEFT JOIN foods_local as flp ON flp.food_code = foods_local_lists.food_code AND flp.locale_id IN
                                                                                (SELECT prototype_locale_id AS l FROM locales WHERE id = {locale_id})
WHERE foods_categories.category_code = {category_code} AND coalesce(fl.local_description, flp.local_description) IS NOT NULL
ORDER BY coalesce(fl.local_description, flp.local_description) DESC
LIMIT 30

Fix sorting order for food browsing

SELECT code, description, coalesce(fl.local_description, flp.local_description) as local_description
FROM foods_categories
         INNER JOIN foods_local_lists ON foods_categories.food_code = foods_local_lists.food_code AND foods_local_lists.locale_id = {locale_id}
         LEFT JOIN foods ON foods.code = foods_local_lists.food_code
         LEFT JOIN foods_local as fl ON fl.food_code = foods_local_lists.food_code AND fl.locale_id = {locale_id}
         LEFT JOIN foods_local as flp ON flp.food_code = foods_local_lists.food_code AND flp.locale_id IN
                                                                                (SELECT prototype_locale_id AS l FROM locales WHERE id = {locale_id})
WHERE foods_categories.category_code = {category_code} AND coalesce(fl.local_description, flp.local_description) IS NOT NULL
ORDER BY coalesce(fl.local_description, flp.local_description)
LIMIT 30


ALTER TABLE `previews`
    DROP INDEX `previews_caption_f5d9791a_fk_translations_id` (`caption`),
    DROP INDEX `previews_addon_id_320f2325_fk_addons_id` (`addon_id`);

Fix syntax error in migration 1206

ALTER TABLE `previews`
    DROP INDEX `previews_caption_f5d9791a_fk_translations_id`,
    DROP INDEX `previews_addon_id_320f2325_fk_addons_id`;


ALTER TABLE ${ohdsiSchema}.sec_permission ADD COLUMN for_role_id INTEGER;

INSERT INTO ${ohdsiSchema}.sec_permission (id, value, for_role_id)
SELECT nextval('${ohdsiSchema}.sec_permission_id_seq'),
  REPLACE('vocabulary:%s:concept:*:ancestorAndDescendant:get', '%s', REPLACE(REPLACE(value, 'source:', ''), ':access', '')),
  role_id
FROM ${ohdsiSchema}.sec_permission sp
  JOIN ${ohdsiSchema}.sec_role_permission srp on sp.id = srp.permission_id
WHERE sp.value LIKE 'source:%:access';

INSERT INTO ${ohdsiSchema}.sec_role_permission (id, role_id, permission_id)
SELECT nextval('${ohdsiSchema}.sec_role_permission_sequence'), sp.for_role_id, sp.id
FROM ${ohdsiSchema}.sec_permission sp
WHERE sp.for_role_id IS NOT NULL;

ALTER TABLE ${ohdsiSchema}.sec_permission DROP COLUMN for_role_id;

Use temp tables for migration.

CREATE TEMP TABLE temp_migration (
  from_perm_id int,
  new_value character varying(255)
);

INSERT INTO temp_migration (from_perm_id, new_value)
SELECT sp.id as from_id,
  REPLACE('vocabulary:%s:concept:*:ancestorAndDescendant:get', '%s', REPLACE(REPLACE(value, 'source:', ''), ':access', '')) as new_value
FROM ${ohdsiSchema}.sec_permission sp
WHERE sp.value LIKE 'source:%:access';

INSERT INTO ${ohdsiSchema}.sec_permission (id, value)
SELECT nextval('${ohdsiSchema}.sec_permission_id_seq'), new_value
FROM temp_migration;

INSERT INTO ${ohdsiSchema}.sec_role_permission (id,role_id, permission_id)
SELECT nextval('${ohdsiSchema}.sec_role_permission_sequence'),
  srp.role_id,
  sp.id as permission_id
FROM temp_migration m
JOIN ${ohdsiSchema}.sec_permission sp on m.new_value = sp.value
JOIN ${ohdsiSchema}.sec_role_permission srp on m.from_perm_id = srp.permission_id;


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

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

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

CREATE TABLE earners
(
  id INTEGER PRIMARY KEY,
  identity TEXT NOT NULL UNIQUE,
  hashed INTEGER NOT NULL DEFAULT 1,
  type TEXT NOT NULL
);

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

Change database schema for earned badges from TEXT to BLOB

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

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

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

CREATE TABLE earners
(
  id INTEGER PRIMARY KEY,
  identity TEXT NOT NULL UNIQUE,
  hashed INTEGER NOT NULL DEFAULT 1,
  type TEXT NOT NULL
);

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


-- partitioned table
CREATE TABLE partitioned
(
  txnid bigint             NOT NULL
, ts    bigint             NOT NULL
, cid   tinyint            NOT NULL
, rid   bigint             NOT NULL
, value varbinary(1048576) NOT NULL
, CONSTRAINT PK_txnid PRIMARY KEY
  (
    txnid
  )
, UNIQUE ( cid, rid )
);
PARTITION TABLE partitioned ON COLUMN cid;

-- replicated table
CREATE TABLE replicated
(
  txnid  bigint NOT NULL
, ts     bigint NOT NULL
, rid    bigint NOT NULL
, cnt    bigint NOT NULL
, CONSTRAINT PK_id PRIMARY KEY
  (
    txnid
  )
, UNIQUE ( rid )
, UNIQUE ( cnt )
);

-- procedures
CREATE PROCEDURE FROM CLASS txnIdSelfCheck.procedures.Initialize;
CREATE PROCEDURE FROM CLASS txnIdSelfCheck.procedures.doTxn;
PARTITION PROCEDURE doTxn ON TABLE partitioned COLUMN cid;
CREATE PROCEDURE FROM CLASS txnIdSelfCheck.procedures.updateReplicated;
CREATE PROCEDURE txnIdSelfCheck.procedures.getLastRow AS
       SELECT cid, MAX(rid) AS last_rid FROM partitioned GROUP BY cid;
CREATE PROCEDURE txnIdSelfCheck.procedures.getLastReplicatedRow AS
       SELECT rid FROM replicated;

Fix a bug in a query in txnid-selfcheck test app.

-- partitioned table
CREATE TABLE partitioned
(
  txnid bigint             NOT NULL
, ts    bigint             NOT NULL
, cid   tinyint            NOT NULL
, rid   bigint             NOT NULL
, value varbinary(1048576) NOT NULL
, CONSTRAINT PK_txnid PRIMARY KEY
  (
    txnid
  )
, UNIQUE ( cid, rid )
);
PARTITION TABLE partitioned ON COLUMN cid;

-- replicated table
CREATE TABLE replicated
(
  txnid  bigint NOT NULL
, ts     bigint NOT NULL
, rid    bigint NOT NULL
, cnt    bigint NOT NULL
, CONSTRAINT PK_id PRIMARY KEY
  (
    txnid
  )
, UNIQUE ( rid )
, UNIQUE ( cnt )
);

-- procedures
CREATE PROCEDURE FROM CLASS txnIdSelfCheck.procedures.Initialize;
CREATE PROCEDURE FROM CLASS txnIdSelfCheck.procedures.doTxn;
PARTITION PROCEDURE doTxn ON TABLE partitioned COLUMN cid;
CREATE PROCEDURE FROM CLASS txnIdSelfCheck.procedures.updateReplicated;
CREATE PROCEDURE txnIdSelfCheck.procedures.getLastRow AS
       SELECT cid, MAX(-1 * rid) AS last_rid FROM partitioned GROUP BY cid;
CREATE PROCEDURE txnIdSelfCheck.procedures.getLastReplicatedRow AS
       SELECT rid FROM replicated;


CREATE DATABASE weather;
USE weather;

CREATE TABLE `results` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `latitude` decimal(7,4) NOT NULL,
  `longitude` decimal(7,4) NOT NULL,
  `summary` varchar(50) NOT NULL,
  `temperature` decimal(5,2) NOT NULL,
  `units` enum('auto','ca','uk2','us','si') NOT NULL,
  `icon` enum('clear-day','clear-night','rain','snow','sleet','wind','fog','cloudy','partly-cloudy-day','partly-cloudy-night') NOT NULL,
  `created_on` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Add compound index to results table

CREATE DATABASE weather;
USE weather;

CREATE TABLE `results` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `latitude` decimal(7,4) NOT NULL,
  `longitude` decimal(7,4) NOT NULL,
  `summary` varchar(50) NOT NULL,
  `temperature` decimal(5,2) NOT NULL,
  `units` enum('auto','ca','uk2','us','si') NOT NULL,
  `icon` enum('clear-day','clear-night','rain','snow','sleet','wind','fog','cloudy','partly-cloudy-day','partly-cloudy-night') NOT NULL,
  `created_on` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `latitude` (`latitude`,`longitude`,`created_on`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS artist (id INTEGER PRIMARY KEY, name TEXT NOT NULL, playcount INTEGER, UNIQUE (name));
CREATE TABLE IF NOT EXISTS venue (id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT NOT NULL, country TEXT NOT NULL, UNIQUE (name, city, country));
CREATE TABLE IF NOT EXISTS event (id INTEGER PRIMARY KEY, name TEXT NOT NULL, venueid INTEGER NOT NULL, date TEXT NOT NULL, price TEXT, FOREIGN KEY (venueid) REFERENCES venue(id), UNIQUE (name, date, venueid));

Use write-ahead logging with SQLite.

CREATE TABLE IF NOT EXISTS artist (id INTEGER PRIMARY KEY, name TEXT NOT NULL, playcount INTEGER, UNIQUE (name));
CREATE TABLE IF NOT EXISTS venue (id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT NOT NULL, country TEXT NOT NULL, UNIQUE (name, city, country));
CREATE TABLE IF NOT EXISTS event (id INTEGER PRIMARY KEY, name TEXT NOT NULL, venueid INTEGER NOT NULL, date TEXT NOT NULL, price TEXT, FOREIGN KEY (venueid) REFERENCES venue(id), UNIQUE (name, date, venueid));
PRAGMA journal_mode=WAL;


--
-- Copyright 2020 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 {{table_name}}_span;
CREATE VIEW {{table_name}}_span AS
SELECT
  ts,
  LEAD(ts, 1, (SELECT end_ts + 1 FROM trace_bounds))
      OVER(ORDER BY ts) - ts AS dur,
  CAST(value AS INT) AS {{table_name}}_val
FROM (
    SELECT ts, value, LAG(value) OVER (ORDER BY ts) as lag_value
    FROM counter c JOIN counter_track t
      ON t.id = c.track_id
    WHERE t.type = 'counter_track'
      AND name = '{{counter_name}}'
)
WHERE value != lag_value;

Fix bug with null equivalence that led us to lose the first span.

--
-- Copyright 2020 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 {{table_name}}_span;
CREATE VIEW {{table_name}}_span AS
SELECT
  ts,
  LEAD(ts, 1, (SELECT end_ts + 1 FROM trace_bounds))
      OVER(ORDER BY ts) - ts AS dur,
  CAST(value AS INT) AS {{table_name}}_val
FROM (
    SELECT ts, value, LAG(value) OVER (ORDER BY ts) as lag_value
    FROM counter c JOIN counter_track t
      ON t.id = c.track_id
    WHERE t.type = 'counter_track'
      AND name = '{{counter_name}}'
)
WHERE value != lag_value OR lag_value IS NULL;


create table Account (
	id            int not null identity,

	instance      int null references Instance(id),
	fullname      nvarchar(255) null,
	email         nvarchar(255) null,
	password      nvarchar(255) null,
	password_salt varchar(50) null,
	password_tag  varchar(255) null,
	createdate    datetimeoffset(0) null,
	last_login    datetimeoffset(0) null,

	primary key (id)
);

CREATE INDEX Account_instance_index ON Account(instance);
CREATE INDEX Account_email_index ON Account(email);

Change date datatype to one without TZ offset information. Dates are stored in UTC.

create table Account (
	id            int not null identity,

	instance      int null references Instance(id),
	fullname      nvarchar(255) null,
	email         nvarchar(255) null,
	password      nvarchar(255) null,
	password_salt varchar(50) null,
	password_tag  varchar(255) null,
	createdate    datetime2 null,
	last_login    datetime2 null,

	primary key (id)
);

CREATE INDEX Account_instance_index ON Account(instance);
CREATE INDEX Account_email_index ON Account(email);


DROP TABLE IF EXISTS `irclog`;
CREATE TABLE `irclog` (
        id INT auto_increment,
        channel VARCHAR(30),
        nick VARCHAR(40),
        opcode VARCHAR(20),
        timestamp INT,
        line TEXT,
        PRIMARY KEY(`id`)
) CHARSET=utf8 ENGINE=MyISAM;

CREATE INDEX `irclog_channel_timestamp_index` ON `irclog` (channel, timestamp);
CREATE INDEX `irclog_channel_opcode_index` ON `irclog` (channel, opcode);
CREATE FULLTEXT INDEX `irclog_fulltext_index` ON `irclog` (nick, line);

Add index on (channel, nick) for faster nick: searches.

DROP TABLE IF EXISTS `irclog`;
CREATE TABLE `irclog` (
        id INT auto_increment,
        channel VARCHAR(30),
        nick VARCHAR(40),
        opcode VARCHAR(20),
        timestamp INT,
        line TEXT,
        PRIMARY KEY(`id`)
) CHARSET=utf8 ENGINE=MyISAM;

CREATE INDEX `irclog_channel_timestamp_index` ON `irclog` (channel, timestamp);
CREATE INDEX `irclog_channel_opcode_index` ON `irclog` (channel, opcode);
CREATE INDEX `irclog_channel_nick_index` ON `irclog` (channel, nick);
CREATE FULLTEXT INDEX `irclog_fulltext_index` ON `irclog` (nick, line);