Add sample data to fill out the reservation table.

\set title 'Mr. & Mrs. Robert Shaw'
\set source 'Title|AddressLineOne|AddressLineTwo|AddressCity|AddressState|AddressZipCode'
\set lineone '12345 Street One'
\set linetwo 'Apt 111'
\set city 'Some City'
\set state 'IN'
\set  zip '554433'
\set code 'd18cb7'
\set email 'test@test.com'

\set notes null
\set diet null
\set bridegroomnotes null

\set isinviteddinner true
\set isattendingbigday true
\set isattendingdinner false

INSERT INTO reservation (reservation_Title, rsvp_Code_Source, address_Line_One, address_Line_Two,
address_City, address_State, address_Zip_Code, rsvp_Code, email_Address, reservation_Notes,
dietary_Restrictions, notes_For_Bride_Groom, is_Invited_To_Rehearsal_Dinner, is_Attending_Big_Day, is_Attending_Rehearsal_Dinner
, create_date, modified_date)
VALUES
(':title', ':source', ':lineone', ':linetwo',
':city', ':state', ':zip', ':code', ':email', :notes,
:diet, :bridegroomnotes, :isinviteddinner, :isattendingbigday, :isattendingdinner,
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
) RETURNING id;


-- //

alter table page_v add published boolean default false;
create index page_v_published_rid on page_v(publisheh, rid);
update page_v inner join page on page_v.id = page.published_vid set published = true;

-- //@UNDO

alter table page_v drop published;

-- //


Add number of new members later active to campaign metrics

-- Number of new members who are active at the moment
INSERT INTO speakeasy_petition_metrics (activity, campaign_id, npeople)
  SELECT
    'new_active_now', a.campaign_id, COUNT(DISTINCT ac.contact_id)
  FROM civicrm_activity a
  JOIN civicrm_activity_contact ac ON ac.activity_id=a.id
  JOIN civicrm_activity_contact ac_act ON ac.contact_id=ac_act.contact_id
  JOIN civicrm_activity a_act ON a_act.id=ac_act.activity_id
  WHERE a.status_id=9
    AND a_act.activity_type_id IN (6, 28, 32)
    AND a_act.activity_date_time >= DATE_ADD(a.activity_date_time, INTERVAL 1 DAY)
    AND a_act.activity_date_time >= DATE_ADD(NOW(), INTERVAL -1 MONTH)
  GROUP BY a.campaign_id;

-- Number of new people who were active 2 months later
INSERT INTO speakeasy_petition_metrics (activity, campaign_id, npeople)
  SELECT
    'new_active_2mlater', a.campaign_id, COUNT(DISTINCT ac.contact_id)
  FROM civicrm_activity a
  JOIN civicrm_activity_contact ac ON ac.activity_id=a.id
  JOIN civicrm_activity_contact ac_act ON ac.contact_id=ac_act.contact_id
  JOIN civicrm_activity a_act ON a_act.id=ac_act.activity_id
  WHERE a.status_id=9
    AND a_act.activity_type_id IN (6, 28, 32)
    AND (a_act.activity_date_time
      BETWEEN DATE_ADD(a.activity_date_time, INTERVAL 1 MONTH)
          AND DATE_ADD(a.activity_date_time, INTERVAL 2 MONTH))
  GROUP BY a.campaign_id;


Store sent notifications for tracking purposes

CREATE TABLE IACUC_PROTOCOL_NOTIFICATION
(
      NOTIFICATION_ID NUMBER(20)
        , NOTIFICATION_TYPE_ID NUMBER(6) NOT NULL
        , DOCUMENT_NUMBER VARCHAR2(40) NOT NULL
        , ACTION_ID_FK NUMBER(12) NOT NULL
        , SUBJECT VARCHAR2(1000)
        , MESSAGE VARCHAR2(4000)
        , UPDATE_USER VARCHAR2(60) NOT NULL
        , UPDATE_TIMESTAMP DATE NOT NULL
        , VER_NBR NUMBER(8) default 1 NOT NULL
        , OBJ_ID VARCHAR2(36) NOT NULL
)
/
ALTER TABLE IACUC_PROTOCOL_NOTIFICATION
    ADD CONSTRAINT IACUC_NOTIFICATIONP1
PRIMARY KEY (NOTIFICATION_ID)
/

CREATE TABLE PROTOCOL_NOTIFICATION
(
      NOTIFICATION_ID NUMBER(20)
        , NOTIFICATION_TYPE_ID NUMBER(6) NOT NULL
        , DOCUMENT_NUMBER VARCHAR2(40) NOT NULL
        , ACTION_ID_FK NUMBER(12) NOT NULL
        , SUBJECT VARCHAR2(1000)
        , MESSAGE VARCHAR2(4000)
        , UPDATE_USER VARCHAR2(60) NOT NULL
        , UPDATE_TIMESTAMP DATE NOT NULL
        , VER_NBR NUMBER(8) default 1 NOT NULL
        , OBJ_ID VARCHAR2(36) NOT NULL
)
/
ALTER TABLE PROTOCOL_NOTIFICATION
    ADD CONSTRAINT IRB_NOTIFICATIONP1
PRIMARY KEY (NOTIFICATION_ID)
/


SELECT
  w1.id AS Id
FROM
  Weather AS w1
  INNER JOIN Weather AS w2
    ON TO_DAYS(w1.date) - TO_DAYS(w2.date) = 1
WHERE
  w1.temperature > w2.temperature
;


update users
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and email like '%@test.apidoc.me';

update organizations
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and (key like 'a-public-%'
        or key like 'z-test-%');

update memberships
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and organization_guid in (select guid from organizations where deleted_at is not null);

update membership_requests
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and organization_guid in (select guid from organizations where deleted_at is not null);

update organization_domains
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and organization_guid in (select guid from organizations where deleted_at is not null);

update subscriptions
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and organization_guid in (select guid from organizations where deleted_at is not null);

update applications
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and organization_guid in (select guid from organizations where deleted_at is not null);

update application_moves
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and application_guid in (select guid from applications where deleted_at is not null);

update changes
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and application_guid in (select guid from applications where deleted_at is not null);

delete from search.items
 where application_guid in (select guid from applications where deleted_at is not null);

update versions
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and application_guid in (select guid from applications where deleted_at is not null);

update watches
   set deleted_at=now(), deleted_by_guid = created_by_guid
 where deleted_at is null
   and application_guid in (select guid from applications where deleted_at is not null);


-- Function: public.get_walk_paths(text)
--
-- Sample function call:
-- SELECT * FROM get_walk_paths();

-- DROP FUNCTION public.get_walk_paths();

CREATE OR REPLACE FUNCTION public.get_walk_paths()
  RETURNS TABLE(WalkPathId integer, StartLatitude text, StartLongitude text, EndLatitude text, EndLongitude text, DepartureTime integer, user text) AS
$BODY$

SELECT
	wp."WalkPathId",
	wp."StartLatitude",
	wp."StartLongitude",
	wp."EndLatitude",
	wp."EndLongitude",
	wp."DepartureTime",
	g."UserId"
FROM
	"WalkPath" wp,
	"Group" g
WHERE
	wp."WalkPathId" = g."WalkPathId"
	and wp."DepartureTime" > now()
ORDER BY
	wp."DepartureTime"

$BODY$
  LANGUAGE sql STABLE STRICT


CREATE TABLE "lmv_bright"."wetlands" (
    gid int primary key,
    type varchar(16),
    the_geom geometry
);

CREATE INDEX ON lmv_bright.wetlands USING GIST (the_geom);

insert into lmv_bright.wetlands
    select gid,
        type,
        the_geom
    FROM lmv_bright.landuse_terrang
    WHERE type = 'swamp'
    UNION
    SELECT gid,
        type,
        the_geom
    FROM lmv_bright.landuse_inre_norrland
    WHERE type = 'swamp';


Implement IACUC notification for Admin Incomplete

INSERT INTO NOTIFICATION_TYPE (NOTIFICATION_TYPE_ID, MODULE_CODE, ACTION_CODE, DESCRIPTION, SUBJECT, MESSAGE, PROMPT_USER, SEND_NOTIFICATION, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL, (SELECT MODULE_CODE FROM COEUS_MODULE WHERE DESCRIPTION='IACUC Protocol'),'216','IACUC Protocol Admin Incomplete','IACUC Protocol {PROTOCOL_NUMBER} Admin Incomplete Action','The IACUC protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Admin Incomplete" performed on it.
The action was executed by {USER_FULLNAME}.  Additional information and further actions can be accessed through the Kuali Coeus system.','Y','Y','admin',SYSDATE,1,SYS_GUID())
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Admin Incomplete'),'KC-IACUC:PI','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Admin Incomplete'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/


Create procedure to load erikoistumiskoulutukset fact table

USE [ANTERO]
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dw].[p_lataa_f_virta_otp_erikoistumiskoulutukset]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dw].[p_lataa_f_virta_otp_erikoistumiskoulutukset] AS'
END
GO

ALTER PROCEDURE dw.p_lataa_f_virta_otp_erikoistumiskoulutukset as

TRUNCATE TABLE dw.f_virta_otp_erikoistumiskoulutukset

INSERT INTO [dw].[f_virta_otp_erikoistumiskoulutukset]
           ([d_erikoistumiskoulutuskoodi_id]
           ,[d_ika_id]
           ,[d_ohjauksenala_id]
           ,[opiskeluoikeus_alku_pvm]
           ,[d_organisaatioluokitus_id]
           ,[d_sukupuoli_id]
		   ,[source])
     select
            coalesce(d1.id,-1) as d_erikoistumiskoulutuskoodi_id,
		    coalesce(d2.id,-1) as d_ika_id,
			coalesce(d3.id,-1) as d_ohjauksenala_id,
			convert(date,dateadd(s, convert(bigint, sa.opiskeluoikeudenalkamispaivamaara) / 1000, convert(datetime, '1-1-1970'))) as opiskeluoikeus_alku_pvm,
			coalesce(d5.id,-1) as d_organisaatioluokitus_id,
			coalesce(d4.id,-1) as d_sukupuoli_id,
			'p_lataa_f_virta_otp_erikoistumiskoulutukset' as [source]

		    	FROM 	ANTERO.sa.sa_virta_otp_erikoistumiskoulutukset sa
	          LEFT JOIN ANTERO.dw.d_erikoistumiskoulutus d1 on d1.erikoistumiskoulutus_koodi = sa.erikoistumiskoulutuskoodi
		      LEFT JOIN ANTERO.dw.d_ika d2 on d2.ika_avain	=	sa.ika
		      LEFT JOIN ANTERO.dw.d_ohjauksenala d3 on d3.ohjauksenala_koodi	=	sa.ohjauksenalakoodi
              LEFT JOIN ANTERO.dw.d_sukupuoli d4 on d4.sukupuoli_koodi	=	sa.sukupuolikoodi
			  LEFT JOIN ANTERO.dw.d_organisaatioluokitus d5 on d5.organisaatio_koodi = sa.oppilaitoskoodi

GO


Revert "Modified schema, changed sql reation script, moved all old scripts to db/old"

INSERT INTO "MACHINE_MODEL"."RUNS" ("RUN_SOURCE_CHK", "MODEL_MODES_ID") VALUES ('EXTANT', 5);
INSERT INTO "MACHINE_MODEL"."RUNS" ("RUN_SOURCE_CHK", "MODEL_MODES_ID") VALUES ('DESIGN', 5);


Add new function for creating admin superusers

-- Makes a user a super administrator

CREATE OR REPLACE FUNCTION admin.make_user_superadmin(_user_id bigint)
    RETURNS void
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
    BEGIN
        -- Creates a new organization for NDR
        UPDATE webui.users SET superadmin = 't' WHERE id=_user_id;
        IF NOT FOUND THEN
            RAISE EXCEPTION 'UserID % not found!', _user_id;
        END IF;

        RETURN;
    END;
$$;


Add TF "International Journal of Pavement Engineering"

Title	ISSN	EISSN	Field	Parent	Format	Documentation
International Journal of Pavement Engineering	1029-8436	1477-268X	engineering	taylor-and-francis-harvard-x	author-date	http://www.tandfonline.com/action/authorSubmission?journalCode=gpav20&page=instructions#mp_style


Add migration file for rename from fat to lowfat

UPDATE django_content_type SET app_label="lowfat" WHERE app_label="fat";
ALTER TABLE fat_blog RENAME TO lowfat_blog;
ALTER TABLE fat_blogsentmail RENAME TO lowfat_blogsentmail;
ALTER TABLE fat_claimant RENAME TO lowfat_claimant;
ALTER TABLE fat_expense RENAME TO lowfat_expense;
ALTER TABLE fat_expensesentmail RENAME TO lowfat_expensesentmail;
ALTER TABLE fat_fund RENAME TO lowfat_fund;
ALTER TABLE fat_fundsentmail RENAME TO lowfat_fundsentmail;
ALTER TABLE fat_generalsentmail RENAME TO lowfat_generalsentmail;
UPDATE django_migrations SET app='lowfat' WHERE app='fat';
.quit


Add a simple SQL script to insert some test data.

SELECT @admin_id:=`id` FROM users WHERE `username`='admin';
SELECT @human_id:=`id` FROM species WHERE `name` = 'Human';
SELECT @dodge_id:=`id` FROM talents WHERE name='Dodge';
SELECT @blaster_id:=`id` FROM weapons WHERE name='Blaster Carbine';
SELECT @laminate_id:=`id` FROM armour WHERE name='Laminate';

DELETE FROM characters WHERE `name` = 'Default Test User' AND `user_id` = @admin_id;

INSERT INTO characters (`name`, `user_id`, `species_id`, `created`, `modified`) VALUES ('Default Test User', @admin_id, @human_id, NOW(), NOW());
SET @character_id:=LAST_INSERT_ID();

INSERT INTO characters_talents (`character_id`, `talent_id`, `created`, `modified`) VALUES (@character_id, @dodge_id, NOW(), NOW());
INSERT INTO characters_weapons (`character_id`, `weapon_id`, `created`, `modified`) VALUES (@character_id, @blaster_id, NOW(), NOW());
INSERT INTO characters_armour (`character_id`, `armour_id`, `created`, `modified`) VALUES (@character_id, @laminate_id, NOW(), NOW());

SELECT * FROM characters WHERE id=@character_id;
SELECT * FROM characters_talents WHERE character_id=@character_id;
SELECT * FROM characters_armour WHERE character_id=@character_id;
SELECT * FROM characters_weapons WHERE character_id=@character_id;


Add update script to remove duplicate entries from cache_component_includes

CREATE TABLE `cache_component_includes_temp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `component_id` varchar(255) NOT NULL,
  `target_id` varchar(255) NOT NULL,
  `type` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),   KEY `source` (`component_id`,`type`),
   KEY `target_id` (`target_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO cache_component_includes_temp(component_id, target_id, type)
    SELECT DISTINCT component_id, target_id, type
    FROM cache_component_includes;

DROP TABLE `cache_component_includes`;
RENAME TABLE `cache_component_includes_temp` TO `cache_component_includes`;


Add query for getting flight operator statistics

DECLARE @ClubKey nvarchar(10) = 'FGZO';

SELECT      CONCAT(Persons.Lastname, ' ', Persons.Firstname) AS Person, PlanningDays.Day
, (SELECT COUNT(*) AS NumberOfReservations FROM AircraftReservations WHERE (AircraftReservations.LocationId = PlanningDays.LocationId) AND (CAST(Start AS Date) = CAST(PlanningDays.Day AS Date))
	AND AircraftReservations.IsDeleted = 0) AS 'Anzahl Reservationen'
, (SELECT COUNT(*) AS NumberOfFlights FROM Flights WHERE (StartLocationId = PlanningDays.LocationId) AND (CAST(StartDateTime AS Date) = CAST(PlanningDays.Day AS Date))
	AND (Flights.FlightAircraftType = 1 OR Flights.FlightAircraftType = 3)
	AND Flights.IsDeleted = 0) AS 'Anzahl Flge'

FROM            PlanningDays
INNER JOIN PlanningDayAssignments ON PlanningDayAssignments.AssignedPlanningDayId = PlanningDays.PlanningDayId
INNER JOIN Persons ON PlanningDayAssignments.AssignedPersonId = Persons.PersonId

where PlanningDays.ClubId = (SELECT ClubId FROM Clubs WHERE ClubKey = @ClubKey)
and PlanningDayAssignments.AssignmentTypeId = (SELECT PlanningDayAssignmentTypeId FROM PlanningDayAssignmentTypes where PlanningDayAssignmentTypes.AssignmentTypeName = 'Segelflugleiter' and PlanningDayAssignmentTypes.ClubId = (SELECT ClubId FROM Clubs WHERE ClubKey = @ClubKey))
and PlanningDayAssignments.IsDeleted = 0
and PlanningDays.IsDeleted = 0
and YEAR(PlanningDays.Day) = YEAR(SYSDATETIME())
GROUP BY  CONCAT(Persons.Lastname, ' ', Persons.Firstname), PlanningDays.LocationId, PlanningDays.Day


Add migration to remove duplicate emails

--------------------------------------------------------------------------------
-- For each relation, find all records tied to a learn user that also have a
-- github account with the same email, re-associate the records with the github
-- account
--------------------------------------------------------------------------------
UPDATE membership
  SET "user" = guser.id
  FROM "user" luser
  JOIN "user" guser
    ON guser.email = luser.email
  WHERE luser.id = membership.user
    AND guser.plugin = 'github'
    AND luser.plugin = 'learn';

UPDATE comment
  SET "user" = guser.id
  FROM "user" luser
  JOIN "user" guser
    ON guser.email = luser.email
  WHERE luser.id = comment.user
    AND guser.plugin = 'github'
    AND luser.plugin = 'learn';

UPDATE subscription
  SET "user" = guser.id
  FROM "user" luser
  JOIN "user" guser
    ON guser.email = luser.email
  WHERE luser.id = subscription.user
    AND guser.plugin = 'github'
    AND luser.plugin = 'learn';

--------------------------------------------------------------------------------
-- Delete all learn accounts that have no related data. All that should remain
-- are learn accounts with data, but no associated github account.
--------------------------------------------------------------------------------
DELETE
  FROM "user"
  WHERE "user"."id" IN (
    SELECT "user"."id"
    FROM "user"
    LEFT OUTER JOIN membership
      ON membership.user = "user".id
    LEFT OUTER JOIN comment
      ON comment.user = "user".id
    LEFT OUTER JOIN subscription
      ON subscription.user = "user".id
    WHERE membership.id IS NULL
      AND comment.id IS NULL
      AND subscription.id IS NULL
  );


Correct new primary key sequence in the database.

# Calendar schema

# --- !Ups

DROP SEQUENCE "s_game_id";
CREATE SEQUENCE s_game_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 552
  CACHE 1;

# --- !Downs


Add basic version of SQL script to update winners page

/* insert new contentarticle version into table, created by cwoebker */

INSERT INTO `xcolab_ContentArticleVersion` (`contentArticleId`, `folderId`, `authorId`, `createDate`, `title`, `content`, `lang`)
VALUES (1214, 38, 2675595, '2018-04-20 18:00:00', 'Climate CoLab Contest Winners', '', 'en')

/* add new version to contentArticle */

UPDATE `xcolab_ContentArticle` t
SET t.`maxVersionId` = /* TODO: fix this */
WHERE t.`contentArticleId` = 1214


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

CREATE INDEX idx_pipeline_name_id ON pipelines( name, id );

--//@UNDO

DROP INDEX idx_pipeline_name_id;


Add Generate table definition to match query script

/*
Author: SQL Undercover
Source link: https://sqlundercover.com/2017/08/21/undercover-toolbox-generate-a-temporary-table-definition-to-match-the-resultset-of-a-query/
*/

SET NOCOUNT ON;

DECLARE @Query VARCHAR(MAX) = 'select * from sys.databases;';
DECLARE @TempTableName VARCHAR(128) = '#temptable';
DECLARE @ColumnList VARCHAR(MAX);

SELECT @ColumnList = STUFF((SELECT ',' + name + ' ' + system_type_name + ' ' +
CASE is_nullable WHEN 0 THEN 'NOT NULL' ELSE 'NULL' END
+ CHAR(10)
FROM sys.dm_exec_describe_first_result_set(@Query, NULL, 0)
FOR XML PATH('')) ,1,1,'');

PRINT 'CREATE TABLE ' + @TempTableName + '('
PRINT @ColumnList;
PRINT(')');


Add recently appeared column emojulkaisunNimi.

IF NOT EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='sa' and TABLE_NAME='sa_virta_jtp_julkaisut'
  and COLUMN_NAME='emojulkaisunNimi'
) BEGIN

ALTER TABLE sa.sa_virta_jtp_julkaisut ADD emojulkaisunNimi nvarchar(500) NULL

END
/* revert
ALTER TABLE sa.sa_virta_jtp_julkaisut DROP COLUMN emojulkaisunNimi
--*/


Prepare the migration from AWA 0.3 to 0.4

ALTER TABLE awa_access_key CHANGE `expire_date` `expire_date` DATE NOT NULL;
ALTER TABLE awa_store_local CHANGE `expire_date` `expire_date` DATE NOT NULL;

/* File generated automatically by dynamo */
/* The tag definition. */
CREATE TABLE awa_tag (
  /* the tag identifier */
  `id` BIGINT NOT NULL,
  /* the tag name */
  `name` VARCHAR(255) BINARY NOT NULL,
  PRIMARY KEY (`id`)
);
/*  */
CREATE TABLE awa_tagged_entity (
  /* the tag entity identifier */
  `id` BIGINT NOT NULL,
  /* Title: Tag model
Date: 2013-02-23the database entity to which the tag is associated */
  `for_entity_id` BIGINT NOT NULL,
  /* the entity type */
  `entity_type` INTEGER NOT NULL,
  /*  */
  `tag_id` BIGINT NOT NULL,
  PRIMARY KEY (`id`)
);
INSERT INTO entity_type (name) VALUES
("awa_tag")
,("awa_tagged_entity")
;


CREATE OR REPLACE FUNCTION ts.updatesampleage(_sampleageid integer,
_age double precision DEFAULT NULL::double precision,
_ageyounger double precision DEFAULT NULL::double precision,
_ageolder double precision DEFAULT NULL::double precision)
 RETURNS void
 LANGUAGE sql
AS $function$

  UPDATE ndb.sampleages
  SET      age = _age,
      ageyounger = _ageyounger,
  	    ageolder = _ageolder
  WHERE sampleageid = _sampleageid;

  INSERT INTO ti.stewardupdates(contactid, tablename, pk1, operation)
  VALUES (_contactid, 'sampleages', _sampleageid, 'update')

$function$


Add script to copy mapping to sequence regions

insert into rnc_sequence_regions (
	urs_taxid,
	region_name,
	chromosome,
	strand,
	region_start,
	region_stop,
	assembly_id,
	was_mapped,
	identity,
	providing_databases
) (
select
	max(rna_id),
	region_id,
	max(chromosome),
	max(strand::int),
	min("start"),
	max(stop),
	assembly_id,
	true,
	max(mapping."identity"),
	'{}'
from rnc_genome_mapping mapping
group by mapping.region_id, mapping.assembly_id
) on conflict (region_name, assembly_id) do nothing
;


ALTER TABLE "as".as_languages  RENAME TO as_locale_languages;

ALTER TABLE "as"."as_locale_languages"  DROP CONSTRAINT "as_languages_pkey" CASCADE;

--$ scan_tables locale t
--$ scan_tables lang t
--$ scan_tables user t
--$ scan_tables template t

INSERT INTO as_locale_domains_list (locale_domain_id, locale_domain_name) VALUES (1, 'as');
UPDATE as_locale_languages SET lang_key = lang_key || '_1';

--Pas de langue "Espagnol" (/!\: locale_domain_id==1 -> AS)
INSERT INTO as_locale_languages (lang_key, country_code, lang_code, lang_fallback, lang_name, locale_domain_id) values('es-es_1', 'esp', 'spa', 'en-us_1','Spanish',1)

--Pas de projet "Distrib" (/!\: parent_id==4 -> Web)
INSERT INTO as_projects_list (project_order, project_name, parent_id) values(NULL,'Distrib','4')

UPDATE "as".as_locale_languages SET locale_domain_id = 1;

-- Auto fallback
INSERT INTO public.prod_locale_languages
(lang_key, country_code, lang_code, lang_fallback, lang_name, locale_domain_id)
SELECT replace(lang_key,'_1',''), country_code, lang_code, lang_key, lang_name, null
FROM public.prod_locale_languages WHERE locale_domain_id = 1

--$ scan_tables locale t
--$ v v v v v v


Patch to update 68 schema to support DNAMethylationFeature in result_set, feature_type and input_set tables

/**
@header patch_68_69_b.sql - result_set.feature_class
@desc   add a feature_class field to result_set
*/

-- Move currently misplaced fields from experiment table
ALTER TABLE result_set ADD `feature_class` varchar(30) enum('result','DNAMethylation') DEFAULT NULL;
ALTER TABLE feature_type MODIFY class enum('Insulator','DNA','Regulatory Feature','Histone','RNA','Polymerase','Transcription Factor','Transcription Factor Complex','Regulatory Motif','Enhancer','Expression','Pseudo','Open Chromatin','Search Region','Association Locus','Segmentation State', 'DNAMethylation') DEFAULT NULL;
ALTER TABLE input_set MODIFY type  enum('annotated','result','segmentation','DNAMethylation') DEFAULT NULL;

# UPDATE result_set set feature_class = "result";

OPTIMIZE TABLE result_set;
ANALYZE TABLE result_set;

OPTIMIZE TABLE feature_type;
ANALYZE TABLE feature_type;

OPTIMIZE TABLE input_set;
ANALYZE TABLE input_set;

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_68_69_b.sql|result_set.feature_class');


Create SQL migration to support supply partners

CREATE TABLE  supply_partners
(
  id SERIAL PRIMARY KEY,
  code varchar(10) NOT NULL UNIQUE,
  name varchar(50) NOT NULL UNIQUE,
  isActive BOOLEAN NOT NULL DEFAULT TRUE,

  createdBy INTEGER,
  createdDate TIMESTAMP DEFAULT NOW(),
  modifiedBy INTEGER,
  modifiedDate TIMESTAMP
);

CREATE TABLE supply_partner_programs
(
  id SERIAL PRIMARY KEY,
  supplyPartnerId INTEGER NOT NULL REFERENCES supply_partners(id),
  sourceProgramId INTEGER NOT NULL REFERENCES programs(id),
  destinationProgramId INTEGER NOT NULL REFERENCES programs(id),
  destinationSupervisoryNode INTEGER NOT NULL REFERENCES supervisory_nodes(id),

  createdBy INTEGER,
  createdDate TIMESTAMP DEFAULT NOW(),
  modifiedBy INTEGER,
  modifiedDate TIMESTAMP
);

CREATE TABLE supply_partner_program_products(
  id SERIAL PRIMARY KEY,
  supplyPartnerProgramId INTEGER REFERENCES supply_partner_programs(id),
  productId INTEGER REFERENCES products(id),
  percentageSupported INTEGER NOT NULL DEFAULT (100),

  createdBy INTEGER,
  createdDate TIMESTAMP DEFAULT NOW(),
  modifiedBy INTEGER,
  modifiedDate TIMESTAMP
);

CREATE TABLE supply_partner_program_facilities(
  id SERIAL PRIMARY KEY,
  supplyPartnerProgramId INTEGER REFERENCES supply_partner_programs(id),
  facilityId INTEGER REFERENCES facilities(id),

  createdBy INTEGER,
  createdDate TIMESTAMP DEFAULT NOW(),
  modifiedBy INTEGER,
  modifiedDate TIMESTAMP
);


Add missing sql script for the last commit

ALTER TABLE shop_rates ADD CONSTRAINT fk_shop_rates_shops1_idx FOREIGN KEY (shop_id) REFERENCES shops(id);
ALTER TABLE shop_rates ADD CONSTRAINT fk_shop_rates_orders1_idx FOREIGN KEY (order_id) REFERENCES orders(id);
ALTER TABLE shop_rates ADD CONSTRAINT fk_shop_rates_customers1_idx FOREIGN KEY (customer_id) REFERENCES customers(id);


# create new table sample
CREATE TABLE IF NOT EXISTS sample (
  sample_id int(10) unsigned NOT NULL AUTO_INCREMENT,
  individual_id int(10) unsigned NOT NULL,
  name varchar(255) DEFAULT NULL,
  description text,
  study_id int(10) unsigned DEFAULT NULL,
  display enum('REFERENCE','DEFAULT','DISPLAYABLE','UNDISPLAYABLE','LD','MARTDISPLAYABLE') DEFAULT 'UNDISPLAYABLE',
  has_coverage tinyint(1) unsigned NOT NULL DEFAULT '0',
  variation_set_id set('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59','60','61','62','63','64') DEFAULT NULL,
  PRIMARY KEY (sample_id),
  KEY individual_idx (individual_id)
);

RENAME TABLE individual TO individual_old;

# create new individual table
CREATE TABLE individual (
  individual_id int(10) unsigned NOT NULL AUTO_INCREMENT,
  name varchar(255) DEFAULT NULL,
  description text,
  gender enum('Male','Female','Unknown') NOT NULL DEFAULT 'Unknown',
  father_individual_id int(10) unsigned DEFAULT NULL,
  mother_individual_id int(10) unsigned DEFAULT NULL,
  individual_type_id int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (individual_id),
  KEY father_individual_idx (father_individual_id),
  KEY mother_individual_idx (mother_individual_id)
);

INSERT INTO sample(sample_id, individual_id, name, description, display, has_coverage, variation_set_id) SELECT individual_id, individual_id, name, description, display, has_coverage, variation_set_id FROM individual_old;
INSERT INTO individual(individual_id, name, description, gender, father_individual_id, mother_individual_id, individual_type_id) SELECT individual_id, name, description, gender, father_individual_id, mother_individual_id, individual_type_id FROM individual_old;

DROP TABLE individual_old;

INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL,'patch', 'patch_80_81_b.sql|Create new sample table and update individual table. Copy individual data into new sample table.');


Add migration for missing fields in invoiceout for locked_at and locked_by

ALTER TABLE invoiceout
ADD COLUMN UpdatedAt datetime DEFAULT NULL,
ADD COLUMN LockedAt datetime DEFAULT NULL,
ADD COLUMN LockedBy int(11) DEFAULT NULL;
UPDATE invoiceout
SET UpdatedByPersonID = CreatedByPersonID, UpdatedAt = TS;


Patch to apply a numeric primary key to caldav_data / calendar_item in order to attempt to resolve performance issues in non-POSIX collation.

-- Add a numeric primary key link between caldav_data and calendar_item to
-- provide more efficient linking when the db has been initialised with a
-- non POSIX collation.

BEGIN;
SELECT check_db_revision(1,1,11);

-- Add a column to the collection table to allow us to mark collections
-- as publicly readable
ALTER TABLE collection ADD COLUMN publicly_readable BOOLEAN DEFAULT FALSE;

-- Add a numeric dav_id to link the caldav_data and calendar_item tables
ALTER TABLE caldav_data ADD COLUMN dav_id INT8;
ALTER TABLE calendar_item ADD COLUMN dav_id INT8;
CREATE SEQUENCE caldav_data_dav_id_seq;

CREATE or REPLACE FUNCTION sync_dav_id ( ) RETURNS TRIGGER AS '
  DECLARE
  BEGIN

    IF TG_OP = ''DELETE'' THEN
      -- Just let the ON DELETE CASCADE handle this case
      RETURN OLD;
    END IF;

    IF NEW.dav_id IS NULL THEN
      NEW.dav_id = nextval(''caldav_data_dav_id_seq'');
    END IF;

    IF OLD.dav_id = NEW.dav_id THEN
      -- Nothing to do
      RETURN NEW;
    END IF;

    IF TG_RELNAME = ''caldav_data'' THEN
      UPDATE calendar_item SET dav_id = NEW.dav_id WHERE user_no = NEW.user_no AND dav_name = NEW.dav_name;
    ELSE
      UPDATE caldav_data SET dav_id = NEW.dav_id WHERE user_no = NEW.user_no AND dav_name = NEW.dav_name;
    END IF;

    RETURN NEW;

  END
' LANGUAGE 'plpgsql';

CREATE TRIGGER caldav_data_sync_dav_id AFTER INSERT OR UPDATE ON caldav_data
    FOR EACH ROW EXECUTE PROCEDURE sync_dav_id();

CREATE TRIGGER calendar_item_sync_dav_id AFTER INSERT OR UPDATE ON calendar_item
    FOR EACH ROW EXECUTE PROCEDURE sync_dav_id();

-- Now, using the trigger, magically assign dav_id to all rows in caldav_data and calendar_item
UPDATE caldav_data SET dav_id = dav_id;

-- Finally, what we are really after, create the foreign key constraints
ALTER TABLE caldav_data DROP CONSTRAINT caldav_data_pkey CASCADE;
ALTER TABLE caldav_data ADD PRIMARY KEY ( dav_id );
ALTER TABLE calendar_item DROP CONSTRAINT calendar_item_pkey CASCADE;
ALTER TABLE calendar_item ADD PRIMARY KEY ( dav_id );
ALTER TABLE calendar_item ADD CONSTRAINT "calendar_item_dav_id_fkey" FOREIGN KEY (dav_id) REFERENCES caldav_data(dav_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

SELECT new_db_revision(1,1,12, 'December' );

COMMIT;
ROLLBACK;


BEGIN;

DROP TABLE IF EXISTS `timeside_results`;
DROP TABLE IF EXISTS `timeside_selections_items`;
DROP TABLE IF EXISTS `timeside_items`;
DROP TABLE IF EXISTS `timeside_tasks`;
DROP TABLE IF EXISTS `timeside_selections_selections`;
DROP TABLE IF EXISTS `timeside_selections`;
DROP TABLE IF EXISTS `timeside_experiences_presets`;
DROP TABLE IF EXISTS `timeside_presets`;
DROP TABLE IF EXISTS `timeside_experiences_experiences`;
DROP TABLE IF EXISTS `timeside_experiences`;
DROP TABLE IF EXISTS `timeside_processors`;

COMMIT;


SELECT run_migration(6, $$
	INSERT INTO choice (id, name, added_in, removed_in)
	VALUES (17, 'Kebabs', 33, NULL);
$$);

-- SELECT rollback_migration(6, $$
-- 	DELETE FROM "choice" WHERE id = 17;
-- $$);


Upgrade Patch to show Modules menu item when tiki_p_admin_modules permission is activated

SET @pcant=0;
SELECT (@pcant:=count(*)) FROM tiki_menu_options WHERE `position` = 1085;
INSERT INTO tiki_menu_options (`perm`) VALUES ('tiki_p_admin_modules');

INSERT INTO `tiki_menu_options` (`menuId`, `type`, `name`, `url`, `position`, `section`, `perm`, `groupname`, `userlevel`) VALUES (42,'r','Admin','tiki-admin.php',1050,'','tiki_p_admin_modules','',0);


Delete old versions that are no longer migratable but for which we have a latest good version

update versions
   set deleted_at=now(),deleted_by_guid = (select guid from users where deleted_at is null and email='admin@apidoc.me')
 where deleted_at is null
   and version != (select v2.version from versions v2 where v2.deleted_at is null and v2.application_guid = versions.application_guid order by version_sort_key desc limit 1)
   and guid not in (select version_guid from cache.services where deleted_at is null and version = '0.8.18');


Add missing collection scientist changes

set search_path=darwin2;
alter table collections add column staff_ref integer;
alter table collections add constraint fk_collections_staff foreign key (staff_ref) references users(id) on delete set null;
comment on column collections.staff_ref is 'Reference of staff member, scientist responsible - id field of users table';


Add a schema for DB migration of Grafana

CREATE DATABASE grafana_database;
USE grafana_database;

CREATE TABLE `session` (
    `key`       CHAR(16) NOT NULL,
    `data`      BLOB,
    `expiry`    INT(11) UNSIGNED NOT NULL,
    PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Add read only permissions to ipsative survey.

GRANT SELECT ON mappa.ipsative_surveys to anonymous;
GRANT SELECT ON "1".ipsative_surveys to anonymous;

GRANT SELECT ON mappa.ipsative_questions to anonymous;
GRANT SELECT ON "1".ipsative_questions to anonymous;

GRANT SELECT ON mappa.ipsative_categories to anonymous;
GRANT SELECT ON "1".ipsative_categories to anonymous;

GRANT SELECT ON mappa.ipsative_answers to anonymous;
GRANT SELECT ON "1".ipsative_answers to anonymous;


Add PostgreSQL function example with feature split bigtable by month

DROP FUNCTION if exists split_bigtable("s" timestamp);
CREATE OR REPLACE FUNCTION split_bigtable("s" timestamp)
  RETURNS void AS
$$
DECLARE
    "e" timestamp :="s" + interval '1 months';
    "sId" integer;
    "eId" integer;
    t_name varchar(40) :='bigtable_'||to_char("e", 'YYYYMMDD');

BEGIN
    EXECUTE 'SELECT min(id), max(id) FROM "Collection" WHERE time >= $1 AND time <= $2'
        INTO "sId","eId"
    USING "s", "e";
    raise notice 'start Id %', "sId";
    raise notice 'end Id %', "eId";
    raise notice 't_name %', t_name;
    EXECUTE 'CREATE table '||t_name||' as select * from "bigtable"';
    TRUNCATE "bigtable";
    EXECUTE 'insert into "bigtable" (select * from bigtable_id where "collectionId" >= $1 and "collectionId" <= $2 )'
    USING "sId", "eId";
end;
$$ LANGUAGE plpgsql;


Add a n option for Beginning balance column

UPDATE master_rnr_columns
set calculationOption = '[{"name":"Default (Users not allowed to override beginning balance) ", "id":"DEFAULT"},{"name":"Allow users to override beginning balance","id":"FORCE_DATA_ENTRY"}]'
where name = 'beginningBalance';


Add table definition to store (failed) token attempts

CREATE TABLE if not exists gems__token_attempts (
        gta_id_attempt bigint unsigned not null auto_increment,
        gta_id_token varchar(9) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null,
        gta_ip_address int(11) unsigned not null,
        gta_datetime timestamp not null,

        PRIMARY KEY (gta_id_attempt)
    )
    ENGINE=InnoDB
	AUTO_INCREMENT = 10000
    CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';


DROP VIEW invusedtypes;

CREATE OR REPLACE VIEW invships AS
 SELECT invtypes.typeid, invtypes.typename, invtypes.groupid, invgroups.groupname, invtypes.marketgroupid, invmarketgroups.marketgroupname
   FROM eve.invtypes
   JOIN eve.invgroups ON invtypes.groupid = invgroups.groupid
   LEFT JOIN eve.invmarketgroups ON invtypes.marketgroupid = invmarketgroups.marketgroupid
  WHERE invgroups.categoryid = 6 AND invgroups.published = 1 AND invtypes.published = 1;

CREATE OR REPLACE VIEW invusedtypes AS
        (        (        (         SELECT invships.typeid
                                   FROM invships
                        UNION
                                 SELECT invmodules.typeid
                                   FROM invmodules)
                UNION
                         SELECT invcharges.chargeid AS typeid
                           FROM invcharges)
        UNION
                 SELECT invdrones.typeid
                   FROM invdrones)
UNION
         SELECT invskills.typeid
           FROM invskills;


Test proper setting of the global optimizer variable.

-- show behavior of optimizer plans

select optimizer;
set optimizer='off';
select optimizer;
set optimizer='on';
select optimizer;
set optimizer='off';
set optimizer='default';
select optimizer;
set optimizer='default,costModel';


INSERT INTO NOTIFICATION_TYPE (NOTIFICATION_TYPE_ID, MODULE_CODE, ACTION_CODE, DESCRIPTION, SUBJECT, MESSAGE, PROMPT_USER, SEND_NOTIFICATION, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL, (SELECT MODULE_CODE FROM COEUS_MODULE WHERE DESCRIPTION='IACUC Protocol'),'203','IACUC Protocol Table','IACUC Protocol {PROTOCOL_NUMBER} Table Action','The IACUC protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Table" performed on it.
The action was executed by {USER_FULLNAME}.  Additional information and further actions can be accessed through the Kuali Coeus system.','Y','Y','admin',SYSDATE,1,SYS_GUID())
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Table'),'KC-IACUC:PI','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Table'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Table'),'KC-IACUC:Active Committee Member On Iacuc Protocol','admin',SYSDATE,SYS_GUID())
/


BEGIN;
    ALTER TABLE exchange_routes ADD COLUMN is_deleted bool NOT NULL DEFAULT FALSE;

    DROP CAST (current_exchange_routes AS exchange_routes);
    DROP VIEW current_exchange_routes;

    CREATE VIEW current_exchange_routes AS
        SELECT DISTINCT ON (participant, network) *
          FROM exchange_routes
         WHERE NOT is_deleted
      ORDER BY participant, network, id DESC;

    CREATE CAST (current_exchange_routes AS exchange_routes) WITH INOUT;

    UPDATE exchange_routes SET is_deleted = true, error = '' WHERE error = 'invalidated';
END;


Delete old service versions on past api.json format

update users
   set email = 'admin@apidoc.me'
 where email = 'admin@apidoc.com'
   and deleted_at is null;

update services
   set deleted_at = now(),
       deleted_by_guid = (select guid from users where deleted_at is null and email='admin@apidoc.me')
 where deleted_at is null
   and created_at < '2014-05-21 10:27:07.047495+00';

update versions
   set deleted_at = now(),
       deleted_by_guid = (select guid from users where deleted_at is null and email='admin@apidoc.me')
 where deleted_at is null
   and created_at < '2014-05-21 10:27:07.047495+00';


Add the first migration, to create the tasks table in H2 database

CREATE TABLE tasks (
  id INT (11) PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(200) NOT NULL,
  description TEXT DEFAULT '',
  status TINYINT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT NOW()
);


Add tables for federated device messages

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

CREATE TABLE device_federation_outbox (
    destination TEXT NOT NULL,
    stream_id BIGINT NOT NULL,
    sender TEXT NOT NULL,
    message_id TEXT NOT NULL,
    sent_ts BIGINT NOT NULL,
    messages_json TEXT NOT NULL
);

CREATE INDEX device_federation_outbox_destination_id
    ON device_federation_outbox(destination, stream_id);

CREATE TABLE device_federation_inbox (
    origin TEXT NOT NULL,
    message_id TEXT NOT NULL,
    received_ts BIGINT NOT NULL
);

CREATE INDEX device_federation_inbox_sender_id
    ON device_federation_inbox(origin, message_id);


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

-- --------------------------------------------------------
-- Biling feature db changes
-- --------------------------------------------------------

-- -----------------------------------------------------
-- Table `AppCloudDB`.`AC_TENANT_SUBSCRIPTION_PLANS`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `AppCloudDB`.`AC_TENANT_SUBSCRIPTION_PLANS` (
  `tenant_id` INT NOT NULL,
  `plan_id` INT NOT NULL,
  CONSTRAINT `fk_Tenant_UsagePlans`
    FOREIGN KEY (`plan_id`)
    REFERENCES `AppCloudDB`.`AC_SUBSCRIPTION_PLANS` (`plan_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE AC_SUBSCRIPTION_PLANS ADD CUMULATIVE_CPU int(11);
ALTER TABLE AC_SUBSCRIPTION_PLANS ADD CUMULATIVE_RAM int(11);

-- ALTER TABLE AC_SUBSCRIPTION_PLANS ADD MAX_CPU int(11);
-- ALTER TABLE AC_SUBSCRIPTION_PLANS ADD MAX_RAM int(11);


CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE traces(
trace_uuid uuid PRIMARY KEY,
transaction_uuid uuid,
origin_uuid uuid,
trace_name text,
start_time timestamp(6),
end_time timestamp(6),
meta jsonb
)

CREATE TABLE spans(
trace_uuid uuid,
span_name text,
start_time timestamp(6),
end_time timestamp(6),
meta jsonb,
FOREIGN KEY (trace_uuid) REFERENCES traces(trace_uuid)
)


-- Create a nonclustered index on a table or view
CREATE INDEX i1 ON t1 (col1);

--Create a clustered index on a table and use a 3-part name for the table
CREATE CLUSTERED INDEX i1 ON d1.s1.t1 (col1);

-- Create a nonclustered index with a unique constraint on 3 columns and specify the sort order for each column
CREATE UNIQUE INDEX i1 ON t1 (col1 DESC, col2 ASC, col3 DESC);

-- Create a nonclustered index with a unique constraint on 3 columns and specify the sort order for each column
CREATE UNIQUE INDEX i1 ON t1 (col1 DESC, col2 ASC, col3 DESC);

CREATE NONCLUSTERED INDEX IX_Address_PostalCode
    ON Person.Address (PostalCode)
    INCLUDE (AddressLine1, AddressLine2, City, StateProvinceID);

-- filtered index
CREATE NONCLUSTERED INDEX IX_BillOfMaterials_ComponentID
    ON Production.BillOfMaterials (ComponentID, StartDate)
    WHERE EndDate IS NOT NULL ;


Increase size of kuvaus in varda_koodistot

USE [ANTERO]
GO

ALTER TABLE [sa].[sa_varda_koodistot] DROP CONSTRAINT [DF__sa_varda_koodistot__username]
GO

ALTER TABLE [sa].[sa_varda_koodistot] DROP CONSTRAINT [DF__sa_varda_koodistot__loadtime]
GO

/****** Object:  Table [sa].[sa_varda_koodistot]    Script Date: 13.3.2020 15:10:14 ******/
DROP TABLE [sa].[sa_varda_koodistot]
GO

/****** Object:  Table [sa].[sa_varda_koodistot]    Script Date: 13.3.2020 15:10:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [sa].[sa_varda_koodistot](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[koodisto] [nvarchar](100) NOT NULL,
	[koodi] [nvarchar](100) NOT NULL,
	[nimi_fi] [nvarchar](200) NULL,
	[nimi_sv] [nvarchar](200) NULL,
	[nimi_en] [nvarchar](200) NULL,
	[kuvaus_fi] [nvarchar](300) NULL,
	[kuvaus_sv] [nvarchar](300) NULL,
	[kuvaus_en] [nvarchar](300) NULL,
	[alkupvm] [date] NULL,
	[loppupvm] [date] NULL,
	[loadtime] [datetime2](4) NOT NULL,
	[source] [nvarchar](255) NULL,
	[username] [nvarchar](128) NOT NULL,
 CONSTRAINT [PK__sa_varda_koodistot] PRIMARY KEY CLUSTERED
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [sa].[sa_varda_koodistot] ADD  CONSTRAINT [DF__sa_varda_koodistot__loadtime]  DEFAULT (getdate()) FOR [loadtime]
GO

ALTER TABLE [sa].[sa_varda_koodistot] ADD  CONSTRAINT [DF__sa_varda_koodistot__username]  DEFAULT (suser_name()) FOR [username]
GO


INSERT INTO NOTIFICATION_TYPE (NOTIFICATION_TYPE_ID, MODULE_CODE, ACTION_CODE, DESCRIPTION, SUBJECT, MESSAGE, PROMPT_USER, SEND_NOTIFICATION, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL, (SELECT MODULE_CODE FROM COEUS_MODULE WHERE DESCRIPTION='IACUC Protocol'),'305','IACUC Protocol Hold','IACUC Protocol {PROTOCOL_NUMBER} Hold Action','The IACUC protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Hold" performed on it.
The action was executed by {USER_FULLNAME}.  Additional information and further actions can be accessed through the Kuali Coeus system.','Y','Y','admin',SYSDATE,1,SYS_GUID())
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Hold'),'KC-IACUC:PI','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE DESCRIPTION='IACUC Protocol Hold'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/


USE sdrs;

DROP PROCEDURE IF EXISTS modify_sdrs_add_rule_composite_key;

DELIMITER //

CREATE PROCEDURE modify_sdrs_add_rule_composite_key ( )
BEGIN
  DECLARE indexExists INT;
  SELECT COUNT(*) INTO indexExists
  FROM INFORMATION_SCHEMA.STATISTICS
  WHERE table_schema=DATABASE()
    AND table_name='retention_rule'
    AND index_name='unique_dataset_storage_project';

  IF indexExists = 0 THEN
    ALTER TABLE retention_rule
      ADD UNIQUE KEY `unique_dataset_storage_project` (`dataset_name`, `data_storage_name`, `project_id`);
  END IF;
END//

DELIMITER ;

CALL modify_sdrs_add_rule_composite_key;

DROP PROCEDURE modify_sdrs_add_rule_composite_key;


--drop and recreate the address table to allow new columns for organisation_name and locality

set schema 'caseframe';

DELETE FROM questionnaire;
DELETE FROM "case";
DELETE FROM address;

DROP TABLE address CASCADE;

CREATE TABLE address
(
  uprn numeric(12,0) NOT NULL,
  addresstype character varying(6),
  estabtype character varying(6),
  organisation_name character varying(60),
  address_line1 character varying(60),
  address_line2 character varying(60),
  locality character varying(35),
  town_name character varying(30),
  postcode character varying(8),
  oa11cd character varying(9),
  lsoa11cd character varying(9),
  msoa11cd character varying(9),
  lad12cd character varying(9),
  region11cd character varying(9),
  eastings numeric(8,0),
  northings numeric(8,0),
  htc numeric(8,0),
  latitude double precision,
  longitude double precision
);

ALTER TABLE caseframe.address
  OWNER TO postgres;

ALTER TABLE ONLY caseframe.address
    ADD CONSTRAINT address_pkey PRIMARY KEY (uprn);

CREATE INDEX address_lad12cd_idx ON caseframe.address USING btree (lad12cd);

CREATE INDEX address_msoa11cd_idx ON caseframe.address USING btree (msoa11cd);

--case table has foreign key constraint to address table

ALTER TABLE ONLY caseframe."case"
    ADD CONSTRAINT case_uprn_fkey FOREIGN KEY (uprn) REFERENCES caseframe.address(uprn);

Address owner changed to role_connect

--drop and recreate the address table to allow new columns for organisation_name and locality

set schema 'caseframe';

DELETE FROM questionnaire;
DELETE FROM "case";
DELETE FROM address;

DROP TABLE address CASCADE;

CREATE TABLE address
(
  uprn numeric(12,0) NOT NULL,
  addresstype character varying(6),
  estabtype character varying(6),
  organisation_name character varying(60),
  address_line1 character varying(60),
  address_line2 character varying(60),
  locality character varying(35),
  town_name character varying(30),
  postcode character varying(8),
  oa11cd character varying(9),
  lsoa11cd character varying(9),
  msoa11cd character varying(9),
  lad12cd character varying(9),
  region11cd character varying(9),
  eastings numeric(8,0),
  northings numeric(8,0),
  htc numeric(8,0),
  latitude double precision,
  longitude double precision
);

ALTER TABLE caseframe.address
  OWNER TO role_connect;

ALTER TABLE ONLY caseframe.address
    ADD CONSTRAINT address_pkey PRIMARY KEY (uprn);

CREATE INDEX address_lad12cd_idx ON caseframe.address USING btree (lad12cd);

CREATE INDEX address_msoa11cd_idx ON caseframe.address USING btree (msoa11cd);

--case table has foreign key constraint to address table

ALTER TABLE ONLY caseframe."case"
    ADD CONSTRAINT case_uprn_fkey FOREIGN KEY (uprn) REFERENCES caseframe.address(uprn);


Add script to migrate duplicated study groups

CREATE OR REPLACE FUNCTION migrate_study_group(target_study_group int, duplicated_study_group int)
    RETURNS VOID
    LANGUAGE plpgsql
AS
$$
DECLARE

BEGIN
    IF target_study_group = duplicated_study_group THEN
        RETURN;
    END IF;

    UPDATE community_solution_contributions SET study_group_id = target_study_group WHERE study_group_id = duplicated_study_group;
    UPDATE remote_evaluation_mappings SET study_group_id = target_study_group WHERE study_group_id = duplicated_study_group;
    UPDATE authentication_tokens SET study_group_id = target_study_group WHERE study_group_id = duplicated_study_group;
    UPDATE subscriptions SET study_group_id = target_study_group WHERE study_group_id = duplicated_study_group;
    UPDATE submissions SET study_group_id = target_study_group WHERE study_group_id = duplicated_study_group;

    -- Preventing duplicated entries in exercises_proxy_exercises
    -- The same proxy exercise should not have two entries for the same exercise it proxies.
    DELETE
    FROM study_group_memberships
    WHERE study_group_id = duplicated_study_group
      AND id IN (SELECT duplicated.id
                                FROM study_group_memberships AS target,
                                     study_group_memberships AS duplicated
                                WHERE target.study_group_id = target_study_group
                                  AND duplicated.study_group_id = duplicated_study_group
                                  AND target.user_id = duplicated.user_id
                                  AND target.user_type = duplicated.user_type);
    UPDATE study_group_memberships SET study_group_id = target_study_group where study_group_id = duplicated_study_group;

    DELETE FROM study_groups WHERE id = duplicated_study_group;
END;
$$;

/* Execute migration
do $$
begin
	perform migrate_study_group(target_study_group := 237, duplicated_study_group := 695);
end
$$;
*/


/*
Author: Bert Wagner
Source link: https://blog.bertwagner.com/warning-are-your-queries-vulnerable-to-sql-injection-db914fb39668
*/

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

SELECT
      ROUTINE_CATALOG
    , ROUTINE_SCHEMA
    , ROUTINE_NAME
    , ROUTINE_TYPE
    , ROUTINE_DEFINITION
FROM
    INFORMATION_SCHEMA.ROUTINES
WHERE
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ROUTINE_DEFINITION,CHAR(0),''),CHAR(9),''),CHAR(10),''),CHAR(11),''),CHAR(12),''),CHAR(13),''),CHAR(14),''),CHAR(160),''),' ','')
        LIKE '%+@%'
    AND
    ( -- Only if executes a dynamic string
        ROUTINE_DEFINITION LIKE '%EXEC(%'
        OR ROUTINE_DEFINITION LIKE '%EXECUTE%'
        OR ROUTINE_DEFINITION LIKE '%sp_executesql%'
    );


-- Locate user defined point on network
-- Author: Christopher Fricke
-- License: MIT

-- DROP FUNCTION routing.get_waypoint(geometry);
CREATE OR REPLACE FUNCTION routing.get_waypoint(test_geom geometry) RETURNS int AS $$
    SELECT id::integer FROM routing.ways_vertices_pgr ORDER BY the_geom <-> test_geom LIMIT 1;;
$$ LANGUAGE SQL;


Add the fields for the eye candy and sale featires to the prims table. No user functionality yet. Run prebuild. Contains a Migration. May contain nuts.

BEGIN;

ALTER TABLE prims ADD COLUMN PayPrice integer not null default 0;
ALTER TABLE prims ADD COLUMN PayButton1 integer not null default 0;
ALTER TABLE prims ADD COLUMN PayButton2 integer not null default 0;
ALTER TABLE prims ADD COLUMN PayButton3 integer not null default 0;
ALTER TABLE prims ADD COLUMN PayButton4 integer not null default 0;
ALTER TABLE prims ADD COLUMN LoopedSound char(36) not null default '00000000-0000-0000-0000-000000000000';
ALTER TABLE prims ADD COLUMN LoopedSoundGain float not null default 0.0;
ALTER TABLE prims ADD COLUMN TextureAnimation blob;
ALTER TABLE prims ADD COLUMN OmegaX float not null default 0.0;
ALTER TABLE prims ADD COLUMN OmegaY float not null default 0.0;
ALTER TABLE prims ADD COLUMN OmegaZ float not null default 0.0;
ALTER TABLE prims ADD COLUMN CameraEyeOffsetX float not null default 0.0;
ALTER TABLE prims ADD COLUMN CameraEyeOffsetY float not null default 0.0;
ALTER TABLE prims ADD COLUMN CameraEyeOffsetZ float not null default 0.0;
ALTER TABLE prims ADD COLUMN CameraAtOffsetX float not null default 0.0;
ALTER TABLE prims ADD COLUMN CameraAtOffsetY float not null default 0.0;
ALTER TABLE prims ADD COLUMN CameraAtOffsetZ float not null default 0.0;
ALTER TABLE prims ADD COLUMN ForceMouselook tinyint not null default 0;
ALTER TABLE prims ADD COLUMN ScriptAccessPin integer not null default 0;
ALTER TABLE prims ADD COLUMN AllowedDrop tinyint not null default 0;
ALTER TABLE prims ADD COLUMN DieAtEdge tinyint not null default 0;
ALTER TABLE prims ADD COLUMN SalePrice integer not null default 10;
ALTER TABLE prims ADD COLUMN SaleType tinyint not null default 0;

COMMIT;


Add function to refresh all materialized views.

CREATE OR REPLACE FUNCTION refresh_nist_materialized_views()
RETURNS void LANGUAGE plpgsql
AS $$
BEGIN
    REFRESH MATERIALIZED VIEW nist.dept_incidents;
    REFRESH MATERIALIZED VIEW nist.coded_fires;
    REFRESH MATERIALIZED VIEW nist.casualties_fire;
    REFRESH MATERIALIZED VIEW nist.tract_years;
    REFRESH MATERIALIZED VIEW nist.final_query;
END $$;


Prepare for switch to Django-generated db schema.

-- o/` Leave the past behind, just walk away o/`

DROP TABLE IF EXISTS auth_user_groups;
DROP TABLE IF EXISTS auth_user_user_permissions;
DROP TABLE IF EXISTS django_admin_log;
DROP TABLE IF EXISTS auth_user;
DROP TABLE IF EXISTS addons_categories;
DROP TABLE IF EXISTS categories;

ALTER TABLE files DROP COLUMN is_packaged;
ALTER TABLE addon_payment_account DROP COLUMN set_price;


Implement IACUC notification for Review Type Determination

INSERT INTO IACUC_PROTOCOL_ACTION_TYPE ( PROTOCOL_ACTION_TYPE_CODE, DESCRIPTION, TRIGGER_SUBMISSION, TRIGGER_CORRESPONDENCE, UPDATE_TIMESTAMP, UPDATE_USER, OBJ_ID )
       VALUES ( '908', 'Review Type Determination', 'N', 'N', SYSDATE, 'admin', SYS_GUID() )
/
INSERT INTO NOTIFICATION_TYPE (NOTIFICATION_TYPE_ID, MODULE_CODE, ACTION_CODE, DESCRIPTION, SUBJECT, MESSAGE, PROMPT_USER, SEND_NOTIFICATION, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL, (SELECT MODULE_CODE FROM COEUS_MODULE WHERE DESCRIPTION='IACUC Protocol'),'908','IACUC Protocol Review Type Determination','IACUC Protocol {PROTOCOL_NUMBER} Review Type Determination Action','The protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Review Type Determination" performed on it.
The action was executed by {USER_FULLNAME}.  Determination is due by {DUE_DATE}. Additional information and further actions can be accessed through the Kuali Coeus system.','Y','N','admin',SYSDATE,1,SYS_GUID())
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND ACTION_CODE='908'),'KC-IACUC:PI','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND ACTION_CODE='908'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(),null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND ACTION_CODE='908'),'KC-IACUC:Active Committee Member On Iacuc Protocol','admin',SYSDATE,SYS_GUID(),null)
/


Add Identifying In-Memory Objects script

/*
Author: Frank Gill
Original link: https://skreebydba.com/2016/07/18/identifying-in-memory-objects-when-querying-the-transaction-log/
*/

SELECT
f.[Current LSN],
f.Operation,
f.Context,
f.[Transaction ID],
f.operation_desc,
f.tx_end_timestamp,
f.total_size,
OBJECT_NAME(m.object_id) AS ObjectName,
f.newrow_identity,
f.newrow_data,
f.newrow_datasize,
f.oldrow_begin_timestamp,
f.oldrow_identity,
f.oldrow_key_data,
f.oldrow_key_datasize,
f.xtp_description,
f.[Log Record Fixed Length],
f.[Log Record Length],
f.[Flag Bits],
f.[Log Reserve],
f.[Log Record]
FROM fn_dblog_xtp(NULL,NULL) AS f
INNER JOIN sys.memory_optimized_tables_internal_attributes m
ON (f.xtp_object_id - 2147483648) = (m.xtp_object_id + 2147483648)
WHERE Operation LIKE '%HK%';
GO


Create indexes for tables with a FK to job.id

CREATE INDEX log_job_id on log (job_id);

CREATE INDEX performance_job_id on performance (job_id);

CREATE INDEX output_job_id on output (oq_job_id);


Update / migration SQL script to convert local patient ids in the default document repository to unique patient ids

-- CONNECT 3.2
--
-- Default Document Repository - Migrate Patient Ids
-- Created on: 2011-06-09
-- Author: richard.ettema
-- ------------------------------------------------------

-- Update all SourcePatientId values with Local Assigning Authority Id where SourcePatientId equials PatientID
-- PatientId column stores all local patient ids
--
UPDATE `docrepository`.`document` SET SourcePatientId = CONCAT(PatientId,'^^^\&[LOCAL AA ID]\&ISO') WHERE SourcePatientId = PatientId;

UPDATE `docrepository`.`document` SET PatientId = CONCAT(PatientId,'^^^\&[LOCAL AA ID]\&ISO');


Add db patch for deprovision warning email

UPDATE `emails` SET `email_text` = 'Dear {user},

This mail is to inform you that you will be deprovisioned at {deprovision_time}. If you have any questions regarding this mail please contact help@surfconext.nl.

You will be deleted as a member of all the groups. Currently you are member of the following groups:
{groups}
In the groups displayed below you are the only administrator. Please make another member administrator before you are deprovisioned, for these teams will be without an administrator otherwise.
{onlyAdminGroups}
Best regards,
Surfconext' WHERE `email_type` = 'deprovisioning_warning_email';


Fix column position between fresh install and upgrade - please use the AFTER parameter

ALTER TABLE `tiki_tracker_fields` MODIFY `validationMessage` varchar(255) default '' AFTER `validationParam`;
ALTER TABLE `tiki_blogs` MODIFY `show_related` show_related` char(1) default NULL AFTER `always_owner`;


Add migration (new compute_timestamp column)

-- Target versions: pre v2.18
-- SHOULD be executed when upgrading to SlipStream v2.18

alter table "usage_summaries" add column "compute_timestamp" varchar(30);


Set default academic grant percentage on competition templates

-- Update the value of the academic grant percentage for both the template competitions
SET @sector_template_name = "Template for the Sector competition type";
SET @programme_template_name = "Template for the Programme competition type";

UPDATE competition
SET academic_grant_percentage=100
WHERE name=@sector_template_name OR name=@programme_template_name AND non_ifs=0;


INSERT INTO version_db_world (`sql_rev`) VALUES ('1530734106945512205');

UPDATE `creature_template` SET `unit_flags` = 0 WHERE `entry` = 7856;
UPDATE `smart_scripts` SET `event_type` = 9, `event_param1` = 2, `event_param2` = 30, `comment` = 'Southsea Freebooter - Within 2-30 Range - Shoot' WHERE `entryorguid` = 7856 AND `id` = 0;


Make sure everyone is automatically on the free plan

-- +goose Up
ALTER TABLE accounts ALTER COLUMN plan SET NOT NULL;
ALTER TABLE accounts ALTER COLUMN plan SET DEFAULT 'free';

-- +goose Down
ALTER TABLE accounts ALTER COLUMN plan DROP DEFAULT;
ALTER TABLE accounts ALTER COLUMN plan DROP NOT NULL;


INSERT INTO permission (type, name, friendly_name, description, partner_id, status, created_at, updated_at, custom_data) VALUES
(1, 'MEDIA_SERVER_BASE', 'Media server system permission', 'Media server system permission', -5, 1, NOW(), NOW(), NULL),
(4, 'PARTNER_-5_GROUP_*_PERMISSION', 'Partner -5 permission for group *', 'Partner -5 permission for group *', -5, 1, NOW(), NOW(), 'a:1:{s:13:"partner_group";s:1:"*";}');


Update the schema version and the synteny_region/dnafrag_region schema tables transferring the existing data

# Updating the schema version

DELETE FROM meta WHERE meta_key="schema_version";
INSERT INTO meta (meta_key,meta_value) VALUES ("schema_version",37);

# Renaming the old schema tables

RENAME TABLE synteny_region TO synteny_region_old, dnafrag_region TO dnafrag_region_old;

# Creating the new schema tables

CREATE TABLE synteny_region (
  synteny_region_id           int(10) unsigned NOT NULL auto_increment,
  method_link_species_set_id  int(10) unsigned NOT NULL,

  PRIMARY KEY (synteny_region_id),
  KEY (method_link_species_set_id)
) COLLATE=latin1_swedish_ci;

CREATE TABLE dnafrag_region (
  synteny_region_id           int(10) unsigned DEFAULT '0' NOT NULL,
  dnafrag_id                  int(10) unsigned DEFAULT '0' NOT NULL,
  dnafrag_start               int(10) unsigned DEFAULT '0' NOT NULL,
  dnafrag_end                 int(10) unsigned DEFAULT '0' NOT NULL,
  dnafrag_strand              tinyint(4) DEFAULT '0' NOT NULL,

  FOREIGN KEY (dnafrag_id) REFERENCES dnafrag(dnafrag_id),

  UNIQUE unique_synteny (synteny_region_id,dnafrag_id),
  UNIQUE unique_synteny_reversed (dnafrag_id,synteny_region_id)
) COLLATE=latin1_swedish_ci;

# Transferring the data from the old schema tables to the new schema tables

INSERT INTO dnafrag_region SELECT dfr1.*,"1" FROM synteny_region_old sr, dnafrag_region_old dfr1, dnafrag_region_old dfr2 WHERE sr.synteny_region_id=dfr1.synteny_region_id AND sr.synteny_region_id=dfr2.synteny_region_id AND dfr1.dnafrag_id<dfr2.dnafrag_id;

INSERT INTO dnafrag_region SELECT dfr2.*,sr.rel_orientation FROM synteny_region_old sr, dnafrag_region_old dfr1, dnafrag_region_old dfr2 WHERE sr.synteny_region_id=dfr1.synteny_region_id AND sr.synteny_region_id=dfr2.synteny_region_id AND dfr1.dnafrag_id<dfr2.dnafrag_id;

INSERT INTO synteny_region SELECT synteny_region_id,method_link_species_set_id from synteny_region_old;

# Dropping the old schema tables

DROP TABLE synteny_region_old;
DROP TABLE dnafrag_region_old;


Create honor for 7,16,17,30 destroyer squadrons


Add sql script to alter table and add index

--
-- JBoss, Home of Professional Open Source.
-- Copyright 2014-2020 Red Hat, Inc., and individual contributors
-- as indicated by the @author tags.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

-- [NCL-6652] Add to the Artifacts model a new field purl
BEGIN transaction;
    ALTER TABLE artifact ADD COLUMN purl varchar(1024);
COMMIT;

BEGIN transaction;
    CREATE INDEX idx_artifact_purl ON artifact (purl text_pattern_ops);
COMMIT;


Drop obsolete tables from the database, if they exist

DROP TABLE IF EXISTS
    addon_payment_account,
    addons_excluded_regions,
    addons_trending,
    api_access,
    app_collection_membership,
    app_collections,
    app_collections_curators,
    auth_message,
    auth_user_groups,
    auth_user_user_permissions,
    collection_recommendations,
    django_session_old,
    django_site,
    favorites,
    log_activity_app,
    oauth_nonce,
    oauth_token,
    payment_accounts,
    payment_assets,
    payments_seller,
    perf_appversions,
    perf_osversions,
    preload_test_plans,
    prices,
    submission_checklist_apps,
    tag_stat,
    user_inapp_keys,
    users_install,
    users_versioncomments;


Fix Death Knight quest 12711 "Abandoned Mail": Send letter "News From The North" after completing the quest.

INSERT INTO version_db_world (`sql_rev`) VALUES ('1526717821898752673');

UPDATE `quest_template_addon` SET `RewardMailTemplateID` = 236 WHERE `id` = 12711;

DELETE FROM `mail_loot_template` WHERE `entry` = 236;
INSERT INTO `mail_loot_template` (`entry`,`item`,`ChanceOrQuestChance`,`lootmode`,`groupid`,`mincountOrRef`,`maxcount`) VALUES (236,39317,100,1,0,1,1);


-- Upgrade script for disks.
UPDATE disks
SET disk_alias = vm_name || '_DISK' || disks.internal_drive_mapping
FROM vm_static, images, vm_device
WHERE vm_device.vm_id = vm_static.vm_guid
  AND vm_device.device_id = images.image_group_id
  AND disks.disk_id = images.image_group_id
  AND disks.disk_alias IS NULL;


Create notification for action "Removed from Agenda"

INSERT INTO NOTIFICATION_TYPE (NOTIFICATION_TYPE_ID, MODULE_CODE, ACTION_CODE, DESCRIPTION, SUBJECT, MESSAGE, PROMPT_USER, SEND_NOTIFICATION, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL, (SELECT MODULE_CODE FROM COEUS_MODULE WHERE DESCRIPTION='IACUC Protocol'),'201','IACUC Protocol Removed from Agenda','IACUC Protocol {PROTOCOL_NUMBER} Removed from Agenda Action','The protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Removed from Agenda" performed on it.
The action was executed by {USER_FULLNAME}.  Additional information and further actions can be accessed through the Kuali Coeus system.','Y','N','admin',SYSDATE,1,SYS_GUID())
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND DESCRIPTION='IACUC Protocol Removed from Agenda'),'KC-IACUC:PI','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND DESCRIPTION='IACUC Protocol Removed from Agenda'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/


Implement notification for IACUC Review Not Required

INSERT INTO IACUC_PROTOCOL_ACTION_TYPE ( PROTOCOL_ACTION_TYPE_CODE, DESCRIPTION, TRIGGER_SUBMISSION, TRIGGER_CORRESPONDENCE, UPDATE_TIMESTAMP, UPDATE_USER, OBJ_ID )
       VALUES ( '207', 'IACUC Protocol Review Not Required', 'N', 'N', SYSDATE, 'admin', SYS_GUID() )
/
INSERT INTO NOTIFICATION_TYPE (NOTIFICATION_TYPE_ID, MODULE_CODE, ACTION_CODE, DESCRIPTION, SUBJECT, MESSAGE, PROMPT_USER, SEND_NOTIFICATION, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL, (SELECT MODULE_CODE FROM COEUS_MODULE WHERE DESCRIPTION='IACUC Protocol'),'207','IACUC Protocol Review Not Required','IACUC Protocol {PROTOCOL_NUMBER} Review Not Required Action','The protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Review Not Required" performed on it.
The action was executed by {USER_FULLNAME}.  Determination is due by {DUE_DATE}. Additional information and further actions can be accessed through the Kuali Coeus system.','Y','N','admin',SYSDATE,1,SYS_GUID())
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND ACTION_CODE='207'),'KC-IACUC:PI','admin',SYSDATE,1,SYS_GUID(), null)
/
INSERT INTO NOTIFICATION_TYPE_RECIPIENT (NOTIFICATION_TYPE_RECIPIENT_ID, NOTIFICATION_TYPE_ID, ROLE_NAME, UPDATE_USER, UPDATE_TIMESTAMP, VER_NBR, OBJ_ID, ROLE_SUB_QUALIFIER)
       VALUES (SEQ_NOTIFICATION_TYPE_ID.NEXTVAL,(SELECT NOTIFICATION_TYPE_ID FROM NOTIFICATION_TYPE WHERE MODULE_CODE=9 AND ACTION_CODE='207'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(),null)
/


Remove not updated from Ensembl and unused array designs; Removed bad quality micrRNA array designs


-- Archive anything older than 1 month that is read.
--
-- This is to keep the table used for active interaction smaller and quicker.

--INSERT INTO rss_item_archive
--(id, title, description, link, publication_date, rss_feed_id, create_time,
--  update_time, guid)
--SELECT ri.id, ri.title, ri.description, ri.link, ri.publication_date,
--  ri.rss_feed_id, ri.create_time, ri.update_time, ri.guid
--FROM rss_item ri
--LEFT JOIN rss_item_state ris ON ris.item_id = ri.id
--WHERE ri.publication_date < NOW() - '1 months'::INTERVAL AND
--COALESCE(ris.state, 'unread') = 'read'
--;

DELETE FROM rss_item ri
WHERE
ri.id IN (
  SELECT ris.item_id
  FROM rss_item_state ris
  WHERE ris.item_id = ri.id AND
  ris.state = 'read'
) AND
ri.publication_date < NOW() - '1 months'::INTERVAL
;


Add rollback sql for migration v 7 - second edition

-- ALTER TABLE bemyndigelse.bemyndigelse ADD COLUMN rettighed_id  BIGINT;
-- ALTER TABLE bemyndigelse.bemyndigelse ADD COLUMN arbejdsfunktion_id BIGINT;
-- ALTER TABLE bemyndigelse.bemyndigelse ADD COLUMN linked_system_id BIGINT;

UPDATE bemyndigelse.bemyndigelse SET linked_system_id = (SELECT id FROM bemyndigelse.linked_system WHERE kode = linked_system_kode);
UPDATE bemyndigelse.bemyndigelse AS b SET arbejdsfunktion_id = (SELECT id FROM bemyndigelse.arbejdsfunktion AS a WHERE kode = arbejdsfunktion_kode AND a.linked_system_id=b.linked_system_id);
UPDATE bemyndigelse.bemyndigelse AS b SET rettighed_id = (SELECT id FROM bemyndigelse.rettighed AS a WHERE kode = rettighed_kode AND a.linked_system_id=b.linked_system_id);

ALTER TABLE bemyndigelse.bemyndigelse MODIFY linked_system_id BIGINT NOT NULL;
ALTER TABLE bemyndigelse.bemyndigelse MODIFY arbejdsfunktion_id BIGINT NOT NULL;
ALTER TABLE bemyndigelse.bemyndigelse MODIFY rettighed_id BIGINT NOT NULL;

ALTER TABLE bemyndigelse.bemyndigelse
  ADD CONSTRAINT `rettighed_type`
      FOREIGN KEY (`rettighed_id` )
      REFERENCES `rettighed` (`id` )
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
ALTER TABLE bemyndigelse.bemyndigelse
  ADD CONSTRAINT `arbejdsfunktion_type`
      FOREIGN KEY (`arbejdsfunktion_id` )
      REFERENCES `arbejdsfunktion` (`id` )
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
ALTER TABLE bemyndigelse.bemyndigelse
  ADD CONSTRAINT `bemyndigelse_system_type`
      FOREIGN KEY (`linked_system_id` )
      REFERENCES `linked_system` (`id` )
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;

ALTER TABLE bemyndigelse.bemyndigelse DROP COLUMN linked_system_kode;
ALTER TABLE bemyndigelse.bemyndigelse DROP COLUMN arbejdsfunktion_kode;
ALTER TABLE bemyndigelse.bemyndigelse DROP COLUMN rettighed_kode;

DELETE FROM bemyndigelse.schema_version WHERE version=7;


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

--Update for PAYE
alter table account.paye drop constraint FK_Paye_LegalEntity
GO
alter table account.paye drop column legalentityid
GO

Update to pre-deployment script for PAYE

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

--Update for PAYE

IF EXISTS(select 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_NAME = 'FK_Paye_LegalEntity')
BEGIN
	alter table account.paye drop constraint FK_Paye_LegalEntity
END
GO

IF EXISTS(select 1 from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'PAYE' and COLUMN_NAME = 'legalentityid')
BEGIN
	alter table account.paye drop column legalentityid
END
GO


DO $$
BEGIN

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

ALTER TABLE planet_osm_line
    ADD COLUMN mz_id TEXT,
    ADD COLUMN mz_road_level SMALLINT,
    ADD COLUMN mz_road_sort_key FLOAT;

UPDATE planet_osm_line AS line SET
    mz_id = mz_normalize_id(road.osm_id, road.way),
    mz_road_level = road.mz_road_level,
    mz_road_sort_key = (CASE WHEN road.mz_road_level IS NULL
        THEN NULL
        ELSE mz_calculate_road_sort_key(road.layer, road.bridge, road.tunnel, road.highway, road.railway)
    END)

    FROM (
        SELECT osm_id, way, layer, bridge, tunnel, highway, railway, mz_calculate_road_level(highway, railway) AS mz_road_level
        FROM planet_osm_line
    ) road

    WHERE line.osm_id = road.osm_id;

PERFORM mz_create_partial_index_if_not_exists('planet_osm_line_mz_road_level_index', 'planet_osm_line', 'mz_road_level', 'mz_road_level IS NOT NULL');

END $$;

Add index on waterway lines

DO $$
BEGIN

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

ALTER TABLE planet_osm_line
    ADD COLUMN mz_id TEXT,
    ADD COLUMN mz_road_level SMALLINT,
    ADD COLUMN mz_road_sort_key FLOAT;

UPDATE planet_osm_line AS line SET
    mz_id = mz_normalize_id(road.osm_id, road.way),
    mz_road_level = road.mz_road_level,
    mz_road_sort_key = (CASE WHEN road.mz_road_level IS NULL
        THEN NULL
        ELSE mz_calculate_road_sort_key(road.layer, road.bridge, road.tunnel, road.highway, road.railway)
    END)

    FROM (
        SELECT osm_id, way, layer, bridge, tunnel, highway, railway, mz_calculate_road_level(highway, railway) AS mz_road_level
        FROM planet_osm_line
    ) road

    WHERE line.osm_id = road.osm_id;

PERFORM mz_create_partial_index_if_not_exists('planet_osm_line_mz_road_level_index', 'planet_osm_line', 'mz_road_level', 'mz_road_level IS NOT NULL');
PERFORM mz_create_partial_index_if_not_exists('planet_osm_line_waterway', 'planet_osm_line', 'waterway', 'waterway IS NOT NULL');

END $$;


CREATE SEQUENCE SEQ_IACUC_PROTOCOL_CUSTOM_DATA_ID INCREMENT BY 1 START WITH 1 NOCACHE
/

Implement Custom Data for IACUC

CREATE SEQUENCE SEQ_IACUC_PROT_CUST_DATA_ID INCREMENT BY 1 START WITH 1 NOCACHE
/


CREATE USER 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON * . * TO  'root'@'%' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

Correct a SQL error when happen after 2nd boot by 'vagrant up' command

GRANT ALL PRIVILEGES ON * . * TO  'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;


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

  DROP INDEX ix_usr_oguser_name;

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

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

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

  CREATE INDEX ix_usr_oguser_userid ON usr_oguser(userid);

COMMIT;

Fix DROP INDEX ix_usr_oguser_name, which was missing ON usr_oguser(name)

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

  DROP INDEX ix_usr_oguser_name ON usr_oguser(name);

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

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

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

  CREATE INDEX ix_usr_oguser_userid ON usr_oguser(userid);

COMMIT;


-- Stop NOTE level warnings about objects (not)? existing
SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;

-- Some handy views for admins viewing the doc store, NOT used by the app

create or replace view v_fs_volume as
select
    fv.id fs_volume_id,
    fv.path,
    case fv.status -- FsVolume.VolumeUseState
        when 0 then 'Active'
        when 1 then 'Inactive'
        when 3 then 'Closed'
        else concat('ERROR: UNKNOWN STATE (', fv.status, ')')
        end status,
    fv.byte_limit,
    from_unixtime(fv.create_time_ms / 1000) create_time,
    fv.update_user create_user,
    from_unixtime(fv.update_time_ms / 1000) update_time,
    fv.update_user update_user,
    fvs.id,
    fvs.bytes_used,
    fvs.bytes_free,
    fvs.bytes_total
from fs_volume fv
inner join fs_volume_state fvs on fv.fk_fs_volume_state_id = fvs.id;

-- vim: set tabstop=4 shiftwidth=4 expandtab:

Add missing SET SQL_NOTES line

-- Stop NOTE level warnings about objects (not)? existing
SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;

-- Some handy views for admins viewing the doc store, NOT used by the app

create or replace view v_fs_volume as
select
    fv.id fs_volume_id,
    fv.path,
    case fv.status -- FsVolume.VolumeUseState
        when 0 then 'Active'
        when 1 then 'Inactive'
        when 3 then 'Closed'
        else concat('ERROR: UNKNOWN STATE (', fv.status, ')')
        end status,
    fv.byte_limit,
    from_unixtime(fv.create_time_ms / 1000) create_time,
    fv.update_user create_user,
    from_unixtime(fv.update_time_ms / 1000) update_time,
    fv.update_user update_user,
    fvs.id,
    fvs.bytes_used,
    fvs.bytes_free,
    fvs.bytes_total
from fs_volume fv
inner join fs_volume_state fvs on fv.fk_fs_volume_state_id = fvs.id;

SET SQL_NOTES=@OLD_SQL_NOTES;

-- vim: set tabstop=4 shiftwidth=4 expandtab:


-- Revises: V1
-- Creation Date: 2022-05-06 14:33:59.784020 UTC
-- Reason: automod changes

ALTER TABLE guild_mod_config RENAME COLUMN safe_mention_channel_ids TO safe_automod_channel_ids;
ALTER TABLE guild_mod_config RENAME COLUMN raid_mode TO automod_flags;
ALTER TABLE guild_mod_config ALTER COLUMN automod_flags SET DEFAULT 0;
ALTER TABLE guild_mod_config ADD COLUMN broadcast_webhook_url TEXT;

-- Previous versions of raid_mod = 2 implied raid_mode = 1
-- Due to this now being interpreted as bit flags this will need to be 3 (1 | 2)
-- This also removes the NULL flag case and defaults it to zero straight up
UPDATE guild_mod_config
    SET
        automod_flags = CASE COALESCE(automod_flags, -1)
                        WHEN 2 THEN 3
                        WHEN -1 THEN 0
                        END;

-- Change the flags to be not null now that there are no null values
ALTER TABLE guild_mod_config ALTER COLUMN automod_flags SET NOT NULL;

Fix automod upgrade query to explicitly change nulls

-- Revises: V1
-- Creation Date: 2022-05-06 14:33:59.784020 UTC
-- Reason: automod changes

ALTER TABLE guild_mod_config RENAME COLUMN safe_mention_channel_ids TO safe_automod_channel_ids;
ALTER TABLE guild_mod_config RENAME COLUMN raid_mode TO automod_flags;
ALTER TABLE guild_mod_config ALTER COLUMN automod_flags SET DEFAULT 0;
ALTER TABLE guild_mod_config ADD COLUMN broadcast_webhook_url TEXT;

-- Previous versions of raid_mod = 2 implied raid_mode = 1
-- Due to this now being interpreted as bit flags this will need to be 3 (1 | 2)
UPDATE guild_mod_config SET automod_flags = 3 WHERE automod_flags = 2;

-- Remove all nulls and set them to zero
UPDATE guild_mod_config SET automod_flags = 0 WHERE automod_flags IS NULL;

-- Change the flags to be not null now that there are no null values
ALTER TABLE guild_mod_config ALTER COLUMN automod_flags SET NOT NULL;


CREATE TABLE IF NOT EXISTS `salesforce_cache` (
	`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
	`sfid` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
	`type` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL,
	`data` text COLLATE utf8mb4_unicode_ci NOT NULL,
	`sf_created_at` datetime NULL,
	`sf_updated_at` datetime NULL,
	`created_at` datetime NOT NULL,
	`updated_at` datetime NOT NULL,
	PRIMARY KEY (`id`),
	UNIQUE KEY `idx_sfid_type` (`sfid`,`type`),
	KEY `type` (`type`),
	KEY `sfid` (`sfid`),
	FULLTEXT KEY `data` (`data`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Use MyISAM for FULLTEXT support on older MySQL versions

CREATE TABLE IF NOT EXISTS `salesforce_cache` (
	`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
	`sfid` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
	`type` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL,
	`data` text COLLATE utf8mb4_unicode_ci NOT NULL,
	`sf_created_at` datetime NULL,
	`sf_updated_at` datetime NULL,
	`created_at` datetime NOT NULL,
	`updated_at` datetime NOT NULL,
	PRIMARY KEY (`id`),
	UNIQUE KEY `idx_sfid_type` (`sfid`,`type`),
	KEY `type` (`type`),
	KEY `sfid` (`sfid`),
	FULLTEXT KEY `data` (`data`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


ALTER table court
ADD COLUMN citation_expires_after_days SMALLINT;

UPDATE court
SET citation_expires_after_days=-1
WHERE court_id IS NOT NULL;

UPDATE court
set citation_expires_after_days=8
WHERE court_id=35 OR court_id=51 OR court_id=79 OR court_id=80;

Fix changelog UPDATE to satisfy mysql safe mode

ALTER table court
ADD COLUMN citation_expires_after_days SMALLINT;

UPDATE court
SET citation_expires_after_days=-1
WHERE court_id > 0;

UPDATE court
set citation_expires_after_days=8
WHERE court_id=35 OR court_id=51 OR court_id=79 OR court_id=80;


-- Test Script for Database Migration
CREATE TABLE ACCOUNT (
  USER_ID INT PRIMARY KEY,
  USER_NAME VARCHAR(100) NOT NULL
);

CREATE TABLE PROJECT (
  PROJECT_ID INT PRIMARY KEY,
  PROJECT_NAME VARCHAR(100) NOT NULL
);

Update SQL file to create schema from ERD.

CREATE TABLE ACCOUNT(
		USER_ID IDENTITY NOT NULL,
		USER_NAME VARCHAR(100) NOT NULL,
		MAIL_ADDRESS VARCHAR(100) NOT NULL,
		PASSWORD VARCHAR(20) NOT NULL,
		USER_TYPE INT DEFAULT 0 NOT NULL,
		URL VARCHAR(200),
		REGISTERED_DATE TIMESTAMP NOT NULL,
		UPDATED_DATE TIMESTAMP NOT NULL,
		LAST_LOGIN_DATE TIMESTAMP
);

CREATE TABLE PROJECT(
		PROJECT_ID IDENTITY NOT NULL,
		PROJECT_NAME VARCHAR(100) NOT NULL,
		USER_ID INT NOT NULL,
		PROJECT_TYPE INT DEFAULT 0 NOT NULL,
		DESCRIPTION TEXT(10),
		REGISTERED_DATE TIMESTAMP NOT NULL,
		UPDATED_DATE TIMESTAMP NOT NULL,
		LAST_ACTIVITY_DATE TIMESTAMP NOT NULL
);

CREATE TABLE PROJECT_ACCOUNT(
		PROJECT_ID INT NOT NULL,
		USER_ID INT NOT NULL
);

ALTER TABLE ACCOUNT ADD CONSTRAINT IDX_ACCOUNT_PK PRIMARY KEY (USER_ID);
ALTER TABLE ACCOUNT ADD CONSTRAINT IDX_ACCOUNT_1 UNIQUE (MAIL_ADDRESS);

ALTER TABLE PROJECT ADD CONSTRAINT IDX_PROJECT_PK PRIMARY KEY (PROJECT_ID);
ALTER TABLE PROJECT ADD CONSTRAINT IDX_PROJECT_1 UNIQUE (PROJECT_NAME, USER_ID);

ALTER TABLE PROJECT_ACCOUNT ADD CONSTRAINT IDX_PROJECT_ACCOUNT_PK PRIMARY KEY (PROJECT_ID, USER_ID);
ALTER TABLE PROJECT_ACCOUNT ADD CONSTRAINT IDX_PROJECT_ACCOUNT_FK0 FOREIGN KEY (PROJECT_ID) REFERENCES PROJECT (PROJECT_ID);
ALTER TABLE PROJECT_ACCOUNT ADD CONSTRAINT IDX_PROJECT_ACCOUNT_FK1 FOREIGN KEY (USER_ID) REFERENCES ACCOUNT (USER_ID);


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

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

Revert update to default clause

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

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


CREATE KEYSPACE ccfp WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

use ccfp;

CREATE TABLE abstracts (
    id           uuid,
	title        text,
    body         text,
    created      timestamp,
    authors      map<text,text>,
    tags         set<text>,
	attributes   map<text,text>,
	comments     map<timeuuid,text>,
	scores_a     map<text,float>,
	scores_b     map<text,float>,
	scores_c     map<text,float>,
	scores_d     map<text,float>,
	scores_e     map<text,float>,
	scores_f     map<text,float>,
	scores_g     map<text,float>,
	scores_names map<text,text>,
    PRIMARY KEY(id)
);

CREATE TABLE abstract_email_index (
    email text,
	abstracts set<uuid>,
	comments set<uuid>,
	PRIMARY KEY(email)
);

CREATE TABLE abstract_tag_index (
    tag text,
	abstracts set<uuid>,
	comments set<uuid>,
	PRIMARY KEY(tag)
);

CREATE TABLE sessions (
	id       uuid,
	data     blob,
	created  timestamp,
	modified timestamp,
	PRIMARY KEY(id)
);

Change data blob to email text.

CREATE KEYSPACE ccfp WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

use ccfp;

CREATE TABLE abstracts (
    id           uuid,
	title        text,
    body         text,
    created      timestamp,
    authors      map<text,text>,
    tags         set<text>,
	attributes   map<text,text>,
	comments     map<timeuuid,text>,
	scores_a     map<text,float>,
	scores_b     map<text,float>,
	scores_c     map<text,float>,
	scores_d     map<text,float>,
	scores_e     map<text,float>,
	scores_f     map<text,float>,
	scores_g     map<text,float>,
	scores_names map<text,text>,
    PRIMARY KEY(id)
);

CREATE TABLE abstract_email_index (
    email text,
	abstracts set<uuid>,
	comments set<uuid>,
	PRIMARY KEY(email)
);

CREATE TABLE abstract_tag_index (
    tag text,
	abstracts set<uuid>,
	comments set<uuid>,
	PRIMARY KEY(tag)
);

CREATE TABLE sessions (
	id       uuid,
	email    text,
	created  timestamp,
	modified timestamp,
	PRIMARY KEY(id)
);


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

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

Remove extra ; to fix test

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

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


ALTER table `sentences` ADD COLUMN `hash` BINARY(16) NOT NULL;

UPDATE sentences SET hash=LOWER(CONV(murmur_hash_v3(CONCAT(lang, text), 0), 10, 32));

# Must update users_vocabulary before updating the vocabulary table!
UPDATE IGNORE users_vocabulary
    INNER JOIN vocabulary ON users_vocabulary.vocabulary_id = vocabulary.id
    SET users_vocabulary.vocabulary_id=LOWER(CONV(murmur_hash_v3(CONCAT(vocabulary.lang, vocabulary.text), 0), 10, 32));

UPDATE IGNORE vocabulary SET id=LOWER(CONV(murmur_hash_v3(CONCAT(lang, text), 0), 10, 32));

Add function murmur_hash_v3.sql in database update script.

ALTER table `sentences` ADD COLUMN `hash` BINARY(16) NOT NULL;

source docs/database/procedures/murmur_hash_v3.sql;

UPDATE sentences SET hash=LOWER(CONV(murmur_hash_v3(CONCAT(lang, text), 0), 10, 32));

# Must update users_vocabulary before updating the vocabulary table!
UPDATE IGNORE users_vocabulary
    INNER JOIN vocabulary ON users_vocabulary.vocabulary_id = vocabulary.id
    SET users_vocabulary.vocabulary_id=LOWER(CONV(murmur_hash_v3(CONCAT(vocabulary.lang, vocabulary.text), 0), 10, 32));

UPDATE IGNORE vocabulary SET id=LOWER(CONV(murmur_hash_v3(CONCAT(lang, text), 0), 10, 32));


create database if not exists device_mgmt;
create database if not exists test;

use device_mgmt;
CREATE TABLE if not exists users (id int(10) PRIMARY KEY AUTO_INCREMENT, username VARCHAR(100), password VARCHAR(100), permission VARCHAR(500), UNIQUE(username));

Create a table to store unique device ID

create database if not exists device_mgmt;
create database if not exists test;

use device_mgmt;
CREATE TABLE if not exists users (id int(10) PRIMARY KEY AUTO_INCREMENT, username VARCHAR(100), password VARCHAR(100), permission VARCHAR(500), UNIQUE(username));
CREATE TABLE if not exists device (dev_id VARCHAR(100), UNIQUE(dev_id));


-- vds_groups table has been renamed to cluster and should not co-exist
SELECT
    COUNT(1)
FROM
    information_schema.tables
WHERE
    table_name in ('cluster', 'vds_groups');

Use IN instead of in for SQL cmds

-- vds_groups table has been renamed to cluster and should not co-exist
SELECT
    COUNT(1)
FROM
    information_schema.tables
WHERE
    table_name IN ('cluster', 'vds_groups');


-- WARNING
-- This file is executed against the postgres database, as that is known to
-- exist at the time of running upgrades. If objects are to be manipulated
-- in other databases, make sure to change to the correct database first.

DROP DATABASE regression;
DROP DATABASE dsp3;
DROP DATABASE isolation2test;

CREATE DATABASE pgutest;
\c pgutest;

CREATE TABLE t (a integer, b numeric);
CREATE TALBE tt (a integer, b numeric) WITH (appendonly = true);

Fix DDL syntax in pg_upgrade test

-- WARNING
-- This file is executed against the postgres database, as that is known to
-- exist at the time of running upgrades. If objects are to be manipulated
-- in other databases, make sure to change to the correct database first.

DROP DATABASE regression;
DROP DATABASE dsp3;
DROP DATABASE isolation2test;

CREATE DATABASE pgutest;
\c pgutest;

CREATE TABLE t (a integer, b numeric);
CREATE TABLE tt (a integer, b numeric) WITH (appendonly = true);


SELECT JsonQuery('{ "foo": "bar baz" }', 'foo');
SELECT JsonQueryString('{ "foo": "bar baz" }', 'foo');

SELECT JsonQuery('{ "foo": true }', 'foo')::boolean;
SELECT JsonQueryString('{ "foo": "true" }', 'foo')::boolean;

Extend the string examples with weakly-typed queries.

SELECT JsonQuery('{ "foo": "bar baz" }', 'foo');
SELECT JsonQueryString('{ "foo": "bar baz" }', 'foo');
SELECT JsonQueryUnquoted('{ "foo": "bar baz" }', 'foo');

SELECT JsonQueryString('{ "foo": 10 }', 'foo');
SELECT JsonQueryUnquoted('{ "foo": 10 }', 'foo');

SELECT JsonQuery('{ "foo": true }', 'foo')::boolean;
SELECT JsonQueryString('{ "foo": "true" }', 'foo')::boolean;
SELECT JsonQueryUnquoted('{ "foo": true }', 'foo')::boolean;
SELECT JsonQueryUnquoted('{ "foo": "true" }', 'foo')::boolean;


-- Deploy rams-core:add-watch-list to pg

BEGIN;

CREATE TABLE watch_list (
        id uuid NOT NULL,
        first_names character varying DEFAULT ''::character varying NOT NULL,
        last_name character varying DEFAULT ''::character varying NOT NULL,
        email character varying DEFAULT ''::character varying NOT NULL,
        birthdate date,
        reason character varying DEFAULT ''::character varying NOT NULL,
        "action" character varying DEFAULT ''::character varying NOT NULL,
        active boolean DEFAULT true NOT NULL
);

ALTER TABLE attendee
        ADD COLUMN watchlist_id uuid,
        ADD COLUMN extra_donation integer DEFAULT 0 NOT NULL;

ALTER TABLE watch_list
        ADD CONSTRAINT watch_list_pkey PRIMARY KEY (id);

ALTER TABLE attendee
        ADD CONSTRAINT attendee_watchlist_id_key UNIQUE (watchlist_id);

ALTER TABLE attendee
        ADD CONSTRAINT attendee_watchlist_id_fkey FOREIGN KEY (watchlist_id) REFERENCES watch_list(id) ON DELETE SET NULL;

COMMIT;

Stop trying to add extra_donation

-- Deploy rams-core:add-watch-list to pg

BEGIN;

CREATE TABLE watch_list (
        id uuid NOT NULL,
        first_names character varying DEFAULT ''::character varying NOT NULL,
        last_name character varying DEFAULT ''::character varying NOT NULL,
        email character varying DEFAULT ''::character varying NOT NULL,
        birthdate date,
        reason character varying DEFAULT ''::character varying NOT NULL,
        "action" character varying DEFAULT ''::character varying NOT NULL,
        active boolean DEFAULT true NOT NULL
);

ALTER TABLE attendee
        ADD COLUMN watchlist_id uuid;

ALTER TABLE watch_list
        ADD CONSTRAINT watch_list_pkey PRIMARY KEY (id);

ALTER TABLE attendee
        ADD CONSTRAINT attendee_watchlist_id_key UNIQUE (watchlist_id);

ALTER TABLE attendee
        ADD CONSTRAINT attendee_watchlist_id_fkey FOREIGN KEY (watchlist_id) REFERENCES watch_list(id) ON DELETE SET NULL;

COMMIT;


# Move current id to hash column
ALTER TABLE `vocabulary` ADD hash BINARY(16) NOT NULL;
UPDATE `vocabulary` SET hash = id;

# Change id to auto-increment int
ALTER TABLE `vocabulary` DROP PRIMARY KEY;
UPDATE vocabulary SET id = 0;
ALTER TABLE `vocabulary` MODIFY id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT;

# Update users_vocabulary vocabulary_id column
UPDATE `users_vocabulary`
    LEFT OUTER JOIN `vocabulary` ON(vocabulary.hash=users_vocabulary.vocabulary_id)
    SET vocabulary_id = vocabulary.id;

Change users_vocabulary vocabulary_id data type.

# Move current id to hash column
ALTER TABLE `vocabulary` ADD hash BINARY(16) NOT NULL;
UPDATE `vocabulary` SET hash = id;

# Change id to auto-increment int
ALTER TABLE `vocabulary` DROP PRIMARY KEY;
UPDATE vocabulary SET id = 0;
ALTER TABLE `vocabulary` MODIFY id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT;

# Update users_vocabulary vocabulary_id column
ALTER TABLE `users_vocabulary` MODIFY vocabulary_id int(11) NOT NULL;
UPDATE `users_vocabulary`
    LEFT OUTER JOIN `vocabulary` ON(vocabulary.hash=users_vocabulary.vocabulary_id)
    SET vocabulary_id = vocabulary.id;