Modify spelling mistake in drug_allergies table

/*
MySQL Data Transfer
Source Host: localhost
Source Database: cs673
Target Host: localhost
Target Database: cs673
Date: 2013/11/11 21:15:15
*/
USE `cs673`;

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `drug_allergies`;
-- ----------------------------
-- Table structure for drug_allergies
-- ----------------------------
CREATE TABLE `drug_allergies` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `patient_id` char(36) collate utf8_unicode_ci NOT NULL,
  `met` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `dpp_4i` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `glp_1ra` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `tzd` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `agi` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `colsvl` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `bcr_or` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `su_gln` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `insulin` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `sglt_2` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  `praml` enum('yes','unknow','no') collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `patient_id` (`patient_id`),
  CONSTRAINT `drug_allergies_ibfk_1` FOREIGN KEY (`patient_id`) REFERENCES `patients` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `drug_allergies` VALUES ('1', '2013-11-08 01:54:21', '2013-11-08 01:54:21', '01', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no');


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE IF NOT EXISTS m_country (
  `id` SMALLINT unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `name_ja` varchar(255) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

ALTER TABLE teacher
  ADD COLUMN `country_id` SMALLINT NOT NULL DEFAULT 0 AFTER `name`,
  ADD COLUMN `gender` ENUM('female', 'male', 'other') NOT NULL DEFAULT 'other' AFTER `country_id`,
  ADD COLUMN `birthday` DATETIME NOT NULL AFTER `gender`,
  ADD COLUMN `years_of_experience` TINYINT UNSIGNED NOT NULL DEFAULT 0 AFTER `birthday`;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE IF EXISTS m_country;
ALTER TABLE teacher
  DROP COLUMN `country_id`,
  DROP COLUMN `gender`,
  DROP COLUMN `birthday`,
  DROP COLUMN ``years_of_experience;


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


Add function to return NFPA region based on the state.

CREATE OR REPLACE function nfpa_region(state varchar) returns varchar(2) AS $$
BEGIN
  IF upper(state) in ('AL', 'AR', 'DC', 'DE', 'FL', 'GA', 'KY', 'LA', 'MD', 'MS', 'NC', 'OK', 'SC', 'TN', 'TX', 'VA', 'WV') THEN
    RETURN 'South';
  ELSIF upper(state) in ('AK', 'AZ', 'CA', 'CO', 'HI', 'ID', 'MT', 'NM', 'NV', 'OR', 'UT', 'WA', 'WY') THEN
    RETURN 'West';
  ELSIF upper(state) in ('CT', 'MA', 'ME', 'NH', 'NJ', 'NY', 'PA', 'RI', 'VT') THEN
    RETURN 'Northeast';
  ELSIF upper(state) in ('IA', 'IL', 'IN', 'KS', 'MI', 'MN', 'MO', 'ND', 'NE', 'OH', 'SD', 'WI') THEN
    RETURN 'Midwest';
  ELSE RETURN '';
  END IF;
END;
$$ LANGUAGE plpgsql;


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE `configs` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `total_reward_threshold` INT(11) NOT NULL COMMENT 'threshold that indicates reward_rate_type',
  `referer_reward_rate` FLOAT NOT NULL COMMENT 'referer reward rate means the percentage get from reward of referee',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE `configs`;


Add mysql schema for dummy data and test

--
-- Sample Database in MySql for unit testing demo
--

DROP TABLE IF EXISTS users CASCADE;

CREATE TABLE users (
     id INT NOT NULL AUTO_INCREMENT,
     name VARCHAR (30) NOT NULL,
     PRIMARY KEY (id)
);
--
-- Insert few dummy records into the users table
INSERT INTO users (id, name)
VALUES
(1, 'Person 1'),
(2, 'Person 1'),
(3, 'Person 1'),
(4, 'Person 1'),
(5, 'Foo Bar');

--
-- Session Table
DROP TABLE IF EXISTS sessions CASCADE;

CREATE TABLE sessions (
     id INT NOT NULL AUTO_INCREMENT,
     name VARCHAR (30) NOT NULL,
     value VARCHAR (30) NOT NULL,
     PRIMARY KEY (id)
);

INSERT INTO sessions (id, name, value)
VALUES
(1, 'user_id', '1');


SELECT exp.line_num,
        exp.dist_line_num,
        row_number () OVER (
            PARTITION BY pol.po_id, exp.dist_line_num
            ORDER BY exp.pymnt_dt, exp.line_num, exp.invoice_id, exp.monetary_amount ) AS line_num,
        pol.po_id,
        'PAY' AS cost_code,
        exp.pymnt_dt AS payment_date,
        exp.invoice_id,
        round (
            CASE
                WHEN pol.doc_type IN ( 'BPA', 'BPC', 'CRO' ) THEN exp.merchandise_amt
                ELSE exp.monetary_amount
                END, 2 ) AS total_approved_amt,
        bcat.category_code
    FROM schema1.po_line pol
    JOIN schema1.expenditure exp
        ON ( exp.po_id = pol.po_id
            AND exp.line_num = pol.line_num )
    JOIN schema2.budget_category bcat
        ON ( bcat.category_desc = pol.category_desc )
    WHERE exp.monetary_amount != 0 ;


Add a sql procedure for finding duplicate ids

WITH non_distinct_ac AS (
	SELECT
		count(*) as num,
		ac
	FROM load_rnacentral_all
	GROUP BY ac
	ORDER BY count(*) DESC
)
SELECT *
FROM load_rnacentral_all t, non_distinct_ac l
WHERE t.ac = l.ac AND l.num > 1
ORDER BY t.ac;


IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'api.valos_suoritteet'))
DROP VIEW api.valos_suoritteet;
GO

IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'api.valos_koulutuksenkustannukset'))
DROP VIEW api.valos_koulutuksenkustannukset;
GO


Remove some hard-coded latin collation settings (corresponding change to tiki.sql etc went in with last commit, apologies)

ALTER TABLE `tiki_user_reports`
CHANGE `user` `user` VARCHAR( 200 ) NOT NULL ,
CHANGE `interval` `interval` VARCHAR( 20 ) NOT NULL ,
CHANGE `view` `view` VARCHAR( 8 ) NOT NULL ,
CHANGE `type` `type` VARCHAR( 5 ) NOT NULL;
ALTER TABLE `tiki_user_reports_cache`
CHANGE `user` `user` VARCHAR( 200 ) NOT NULL ,
CHANGE `event` `event` VARCHAR( 200 ) NOT NULL ,
CHANGE `data` `data` TEXT NOT NULL


Add procedure occurrence dimension with ICD-9-CM

/* Build procedure occurrence dimension table. This uses the ICD-9-CM
 * vocabulary. */
INSERT INTO #dim
SELECT
    cp.person_id,
    SUBSTRING(REPLACE(sm.source_code, '.', '') FROM 2 FOR 3) AS covariate_id,
    COUNT(SUBSTRING(REPLACE(sm.source_code, '.', '') FROM 2 FOR 3))
        AS covariate_count
FROM
    #cohort_person cp INNER JOIN @cdm_schema.dbo.procedure_occurrence po
        ON cp.person_id = po.person_id
    INNER JOIN vocabulary.source_to_concept_map sm
        ON po.procedure_concept_id = sm.target_concept_id
    INNER JOIN vocabulary.vocabulary v
        ON sm.source_vocabulary_id = v.vocabulary_id
WHERE
    cp.cohort_start_date < po.procedure_date
    AND po.procedure_date <= cp.cohort_end_date
    AND v.vocabulary_name ~* 'ICD-9-C.*'
    AND sm.source_code ~ '^V[0-9\.]*$'
GROUP BY
    cp.person_id,
    covariate_id
;


Update Dual Talent cost (10g from now on, as in the 4.x.x revision should be)

-- "Dual talent specialization" cost has been reduced since 4.x.x, from the original 1.000g to the new amount of 10g
UPDATE gossip_menu_option SET box_money=100000 WHERE option_id=18 AND npc_option_npcflag=16;


Migrate db add total income column to users

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE `users` ADD COLUMN `total_income` DECIMAL(32, 8) NOT NULL DEFAULT 0 COMMENT 'user total income';
ALTER TABLE `users` ADD COLUMN `referer_total_income` DECIMAL(32, 8) NOT NULL DEFAULT 0 COMMENT 'referer total income';

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

ALTER TABLE `users` DROP COLUMN `total_income`;
ALTER TABLE `users` DROP COLUMN `referer_total_income`;


update queues_opening_hours set day_id = 1 where day_id = 2 and queue_id = 1;
update queues_opening_hours set day_id = 2 where day_id = 3 and queue_id = 1;
update queues_opening_hours set day_id = 3 where day_id = 4 and queue_id = 1;
update queues_opening_hours set day_id = 4 where day_id = 5 and queue_id = 1;
update queues_opening_hours set day_id = 5 where day_id = 6 and queue_id = 1;

update queues_opening_hours set day_id = 40 where day_id = 5 and queue_id = 1;
update queues_opening_hours set day_id = 5 where day_id = 4 and queue_id = 1;
update queues_opening_hours set day_id = 4 where day_id = 40 and queue_id = 1;


Add script for reversing incorrectly-set project lat/long coordinates

-- Reverses project lat/long coordinates, as they were set in reversed order initially

UPDATE
    civictechprojects_project
SET
    project_location_coords = st_geometryfromtext('POINT('|| ST_Y(project_location_coords) ||' '|| ST_X(project_location_coords) ||')', 4326);


Change index to speed up queries on language pairs

-- Add composite index on languages to speed up queries on language pairs
CREATE INDEX sentence_lang_translation_lang_idx
ON sentences_translations(sentence_lang, translation_lang);

-- Delete the single-column index on sentence_lang that has become useless
DROP INDEX sentence_lang
ON sentences_translations;


-- Fix min lvl and Min, Max players on the TP and BG
DELETE FROM `battleground_template` WHERE `id` IN (120,108);
INSERT INTO `battleground_template` VALUES
(108,10,10,81,85,1726,0,1727,0,1,'','Twin Peaks'),
(120,10,10,85,85,1740,0,1739,0,1,'','The Battle for Gilneas');


Change max planet construction, double exp

UPDATE planet_construction
SET max_construction = 25, exp_gain = 90 WHERE construction_id = 1;
UPDATE planet_construction
SET max_construction = 85, exp_gain = 180 WHERE construction_id = 2;
UPDATE planet_construction
SET exp_gain = 540 WHERE construction_id = 3;


Fix not null requirement in membership message column.

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE memberships ALTER COLUMN message DROP NOT NULL;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
ALTER TABLE memberships ALTER COLUMN message SET NOT NULL;


Revert "Issue IMCMS-335: New design to super admin page: categories tab - Revert"

SET @schema_version__major_new = 6;
SET @schema_version__minor_new = 65;

ALTER TABLE category_types
  DROP COLUMN is_image_archive;
ALTER TABLE categories
  DROP COLUMN image;

ALTER TABLE category_types
  MODIFY max_choices tinyint(1);
ALTER TABLE category_types
  CHANGE max_choices is_multi_select tinyint(1);

UPDATE database_version
SET major = @schema_version__major_new,
    minor = @schema_version__minor_new;


ALTER VIEW dw.v_virta_otp_55_suppea AS
SELECT d1.organisaatio_koodi AS 'Oppilaitos Koodi'
  ,d1.organisaatio_fi AS 'Oppilaitos'

  ,d2.okmohjauksenala_koodi AS 'OKM Ohjauksen ala Koodi'
  ,d2.okmohjauksenala_fi AS 'OKM Ohjauksen ala'
  ,d2.koulutusastetaso2_koodi AS 'Koulutusaste (taso 2) Koodi'
  ,d2.koulutusastetaso2_fi AS 'Koulutusaste (taso 2)'
  ,d2.koulutusalataso1_koodi AS 'Koulutusala (taso 1) Koodi'
  ,d2.koulutusalataso1_fi AS 'Koulutusala (taso 1)'
  ,d2.koulutusalataso2_koodi AS 'Koulutusala (taso 2) Koodi'
  ,d2.koulutusalataso2_fi AS 'Koulutusala (taso 2)'
  ,d2.koulutusalataso3_koodi AS 'Koulutusala (taso 3) Koodi'
  ,d2.koulutusalataso3_fi AS 'Koulutusala (taso 3)'
  ,d2.koulutusluokitus_koodi AS 'Koulutus Koodi'
  ,d2.koulutusluokitus_fi AS 'Koulutusnimike'

  ,f.vuosi
  ,f.suorittanut55

  FROM dw.f_virta_otp_55_suppea f
  LEFT JOIN dw.d_organisaatio d1 ON d1.id=f.d_organisaatio_id
  LEFT JOIN dw.d_koulutusluokitus d2 ON d2.id=f.d_koulutusluokitus_id


Add migration for seo fields for links (extern, download)

ALTER TABLE `kwc_basic_downloadtag` ADD `open_type` ENUM('self', 'popup', 'blank') NOT NULL DEFAULT 'self' ;
ALTER TABLE `kwc_basic_downloadtag` ADD `width` MEDIUMINT NULL DEFAULT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `height` MEDIUMINT NULL DEFAULT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `menubar` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `toolbar` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `locationbar` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `statusbar` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `scrollbars` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `resizable` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `rel_nofollow` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `rel_noopener` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `rel_noreferrer` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_downloadtag` ADD `content_disposition` VARCHAR(255) NOT NULL DEFAULT 'attachment' AFTER `open_type`;
ALTER TABLE `kwc_basic_link_extern` ADD `rel_nofollow` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_link_extern` ADD `rel_noopener` TINYINT NOT NULL ;
ALTER TABLE `kwc_basic_link_extern` ADD `rel_noreferrer` TINYINT NOT NULL ;


Add an argument for XML based tabular cubes to be processed.

-- DO NOTICE WE USE dbo SCHEMA HERE IN PURPOSE BECAUSE THIS
-- PROCEDURE IS NOT DIRECTLY PART OF sa OR dw SCHEMAS

ALTER PROCEDURE [dbo].[run_tabular_process]
  @tabularName nvarchar(128) = NULL,
  @processType varchar(20) = 'calculate',
  @XMLvsJSON varchar(10) = 'json'
AS
IF @tabularName IS NULL BEGIN
  print '@tabularName parameter is mandatory. Exiting'
  RETURN
END
--
-- constant-like
DECLARE @jobName nvarchar(128) = N'Tabular process'
-- make catenation ready variables for procedure call
DECLARE @commandStr nvarchar(max) = N'{"refresh": {"type": "'+@processType+'", "objects": [{"database": "'+@tabularName+'"}]}}'
if @XMLvsJSON='xml' begin
  if @processType='calculate' set @processType='ProcessRecalc'
  if @processType='full' set @processType='ProcessFull'
  set @commandStr = N'<Process xmlns="http://schemas.microsoft.com/analysisservices/2003/engine"><Type>'+@processType+'</Type><Object><DatabaseID>'+@tabularName+'</DatabaseID></Object></Process>'
end
DECLARE @updateStr nvarchar(max) = N'update tabularprocess set ready=1 where tabular='''+@tabularName+''' '
DECLARE @updateFailStr nvarchar(max) = N'update tabularprocess set ready=-1 where tabular='''+@tabularName+''' '
DECLARE @stepName nvarchar(max) = @tabularName+'; '+@processType

DECLARE @jobId BINARY(16)
select @jobId=job_id from msdb.dbo.sysjobs_view where name=@jobName

-- only if agent job exists (precaution)
IF @jobId IS NOT NULL BEGIN

  -- update step 1 (tab process)
  BEGIN TRAN
    EXEC msdb.dbo.sp_update_jobstep @job_id=@jobId, @step_id=1, @command=@commandStr, @step_name=@stepName
  COMMIT
  -- update step 2 (mark ready)
  BEGIN TRAN
    EXEC msdb.dbo.sp_update_jobstep @job_id=@jobId, @step_id=2, @command=@updateStr
  COMMIT
  -- update step 3 (mark failed)
  BEGIN TRAN
    EXEC msdb.dbo.sp_update_jobstep @job_id=@jobId, @step_id=3, @command=@updateFailStr
  COMMIT

  EXEC msdb.dbo.sp_start_job @job_name=@jobName

END ELSE BEGIN
  RAISERROR (N'Agent ''%s'' missing.', 11, 1, @jobName)
END -- agent job exists


Create notification for action "Designated Review Approval"

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'),'212','IACUC Protocol Designated Review Approval','IACUC Protocol {PROTOCOL_NUMBER} Designated Review Approval Action','The protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Designated Review Approval" 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 Designated Review Approval'),'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 Designated Review Approval'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/


Add Create JOIN query between multiple tables dynamically script

/*
<documentation>
  <author>Konstantin Taranov</author>
  <summary>Create JOIN query between multiple tables dynamically.</summary>
  <returns>SELECT statement from input table with INNER JOINS for all tables having foreign consttraints with input table.</returns>
  <created>2019-04-22</created>
  <modified>2019-04-22</modified>
  <version>1.0</version>
  <sourceLink>https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Create_JOIN_Query_Between_Multiple_Tables_Dynamically.sql</sourceLink>
</documentation>
*/

DECLARE @table AS VARCHAR(100)= 'YouTableName';

WITH AllColumns
     AS (SELECT
                pObj.name AS ParentTable
               ,pCol.COLUMN_NAME AS ParentColumn
               ,fkObj.name AS ReferecedTable
               ,fkCol.COLUMN_NAME AS ReferencedColumn
         FROM sys.foreign_key_columns AS fkc
              INNER JOIN sys.objects AS pObj ON pObj.object_id = fkc.parent_object_id
              INNER JOIN INFORMATION_SCHEMA.COLUMNS AS pCol ON pCol.TABLE_NAME = pObj.name
                                                               AND pCol.ORDINAL_POSITION = fkc.parent_column_id
              INNER JOIN sys.objects AS fkObj ON fkObj.object_id = fkc.referenced_object_id
              INNER JOIN INFORMATION_SCHEMA.COLUMNS AS fkCol ON fkCol.TABLE_NAME = fkObj.name
                                                                AND fkCol.ORDINAL_POSITION = fkc.referenced_column_id
         WHERE fkc.constraint_object_id IN
         (
             SELECT
                    object_id
             FROM sys.objects AS o
             WHERE o.name IN
             (
                 SELECT
                        tc.CONSTRAINT_NAME
                 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
                 WHERE tc.CONSTRAINT_TYPE = 'FOREIGN KEY'
                       AND tc.TABLE_NAME = @table
             )
         ))
     SELECT
            'SELECT *
        FROM ' + @table +
     (
         SELECT
                ' INNER JOIN ' + ac.ReferecedTable + ' ON ' + ac.ParentTable + '.' + ac.ParentColumn + '=' + ac.ReferecedTable + '.' + ac.ReferencedColumn + CHAR(10)
         FROM AllColumns AS ac FOR
         XML PATH('')
     );


Drop the GIN index on @graph,data and add a template for database migrations.

BEGIN;

DO $$DECLARE
   -- THESE MUST BE CHANGED WHEN YOU COPY THE SCRIPT!

   -- The version you expect the database to have _before_ the migration
   old_version numeric := 0;
   -- The version the database should have _after_ the migration
   new_version numeric := 1;

   -- hands off
   existing_version numeric;

BEGIN

   -- Set up database version tracking (only this first time, DON'T COPY this so subsequent scripts)
   CREATE TABLE lddb__schema (
      version numeric
   );
   INSERT INTO lddb__schema values (0);
   --

   -- Check existing version
   SELECT version from lddb__schema INTO existing_version;
   IF ( existing_version <> old_version) THEN
      RAISE EXCEPTION 'ASKED TO MIGRATE FROM INCORRECT EXISTING VERSION!';
      ROLLBACK;
   END IF;
   UPDATE lddb__schema SET version = new_version;

   -- ACTUAL SCHEMA CHANGES HERE:
   DROP INDEX idx_lddb_graph;

END$$;

COMMIT;


Add selectors in search questions table.

ALTER TABLE search_question ADD selectors VARCHAR(400) AFTER profile_id;

UPDATE search_question SET selectors = 'cnae,bra' WHERE id = '1';
UPDATE search_question SET selectors = 'cnae,bra' WHERE id = '2';
UPDATE search_question SET selectors = 'cnae,bra' WHERE id = '3';
UPDATE search_question SET selectors = 'hs,bra' WHERE id = '4';
UPDATE search_question SET selectors = 'cnae' WHERE id = '5';
UPDATE search_question SET selectors = 'hs' WHERE id = '6';
UPDATE search_question SET selectors = 'hs' WHERE id = '7';
UPDATE search_question SET selectors = 'bra' WHERE id = '8';
UPDATE search_question SET selectors = 'bra' WHERE id = '9';
UPDATE search_question SET selectors = 'bra' WHERE id = '10';
UPDATE search_question SET selectors = 'bra' WHERE id = '11';
UPDATE search_question SET selectors = 'bra' WHERE id = '12';
UPDATE search_question SET selectors = 'bra' WHERE id = '13';
UPDATE search_question SET selectors = 'bra' WHERE id = '14';
UPDATE search_question SET selectors = 'bra' WHERE id = '15';
UPDATE search_question SET selectors = 'bra' WHERE id = '16';
UPDATE search_question SET selectors = 'cnae' WHERE id = '17';
UPDATE search_question SET selectors = 'bra' WHERE id = '18';
UPDATE search_question SET selectors = 'bra' WHERE id = '19';
UPDATE search_question SET selectors = 'cbo,bra' WHERE id = '20';
UPDATE search_question SET selectors = 'cbo' WHERE id = '21';
UPDATE search_question SET selectors = 'cbo' WHERE id = '22';
UPDATE search_question SET selectors = 'bra' WHERE id = '23';
UPDATE search_question SET selectors = 'bra' WHERE id = '24';
UPDATE search_question SET selectors = 'bra,cnae,cbo' WHERE id = '25';


Create script to copy SQ grid in NEP2035

/*
Copy SQ grid to NEP2035 scenario

__copyright__ 	= "Flensburg University of Applied Sciences, Centre for Sustainable Energy Systems"
__license__ 	= "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ 	= "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ 	= "IlkaCu"
*/

----------------
-- Create a new scenario 'NEP 2035' for the interconnected electrical grid in Germany
----------------

-- Use buses from the scenario 'Status Quo' for the new scenario

DELETE FROM calc_ego_hv_powerflow.bus WHERE scn_name = 'NEP 2035';

INSERT INTO calc_ego_hv_powerflow.bus
SELECT 'NEP 2035', a.bus_id, a.v_nom, a.current_type, a.v_mag_pu_min, a.v_mag_pu_max, a.geom
FROM 	calc_ego_hv_powerflow.bus a
WHERE scn_name= 'Status Quo';

-- Include all lines from SQ scenario in the NEP 2035 scenario

DELETE FROM calc_ego_hv_powerflow.line WHERE scn_name = 'NEP 2035';

INSERT INTO 	calc_ego_hv_powerflow.line
SELECT 'NEP 2035', a.line_id, a.bus0, a.bus1, a.x, a.r, a.g, a.b, a.s_nom, a.s_nom_extendable, a.s_nom_min,
	a.s_nom_max, a.capital_cost, a.length, a.cables, a.frequency, a.terrain_factor, a.geom, a.topo
FROM 	calc_ego_hv_powerflow.line a
WHERE 	scn_name= 'Status Quo';

-- Include transformers from Status Quo into new scenario 'NEP 2035'

DELETE FROM calc_ego_hv_powerflow.transformer WHERE scn_name = 'NEP 2035';

INSERT INTO calc_ego_hv_powerflow.transformer
SELECT 'NEP 2035', a.trafo_id, a.bus0, a.bus1, a.x, a.r, a.g, a.b, a.s_nom, a.s_nom_extendable, a.s_nom_min,
	a.s_nom_max, a.tap_ratio, a.phase_shift, a.capital_cost, a.geom, a.topo
FROM 	calc_ego_hv_powerflow.transformer a
WHERE scn_name= 'Status Quo';


/* Improve full-text index to search items */

ALTER TABLE item_detail_search
DROP INDEX ids_all,
ADD FULLTEXT INDEX ids_all(isbn13, title, author, description, classification)
;


Add some dummy data to allow access to the application. The username/password has been taken from the RBC1B database.

-- Create dummy data to access the ROMS database
use ROMS;

insert into Person(Forename, Surname, Comments)
values ('Ramindur', 'Singh', 'Test user account');

insert into User(PersonId, UserName, Password)
values (1, 'RaminderSingh', 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3');

-- Attendance & Invitations
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 1, 4, 4);

-- Circuit
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 2, 4, 4);

-- Congregation
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 3, 4, 4);

-- Database
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 4, 4, 4);

-- Kingdom Halls
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 5, 4, 4);

-- Projects
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 6, 4, 4);

-- Skills
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 7, 4, 4);

-- User
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 8, 4, 4);

-- Volunteers
insert into ApplicationAccess(PersonId, ApplicationId, DepartmentAccess, NonDepartmentAccess)
values (1, 9, 4, 4);


Add hash index for execution url in db schema for Dr. Elephant

# add/remove index on flow_exec_url

# --- !Ups

create index ix_job_result_flow_exec_url on job_result (flow_exec_url) using HASH;

# --- !Downs

drop index ix_job_result_flow_exec_url ON job_result;


-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema library
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `library` ;

-- -----------------------------------------------------
-- Schema library
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `library` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `library` ;

-- -----------------------------------------------------
-- Table `library`.`member`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `library`.`member` ;

CREATE TABLE IF NOT EXISTS `library`.`member` (
  `id` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `email` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `library`.`book`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `library`.`book` ;

CREATE TABLE IF NOT EXISTS `library`.`book` (
  `id` INT NOT NULL,
  `author` VARCHAR(45) NOT NULL,
  `title` VARCHAR(45) NOT NULL,
  `pages` INT NOT NULL,
  `memberid` INT NULL,
  PRIMARY KEY (`id`),
  INDEX `memberid_idx` (`memberid` ASC),
  CONSTRAINT `memberid`
    FOREIGN KEY (`memberid`)
    REFERENCES `library`.`member` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;


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

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


Implement notification for IACUC Rescheduled event

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'),'202','IACUC Protocol Rescheduled','IACUC Protocol {PROTOCOL_NUMBER} Rescheduled','The IACUC protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Reschedule" 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 DESCRIPTION='IACUC Protocol Rescheduled'),'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 Rescheduled'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/


Implement IACUC notification for Request Lift Hold

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'),'108','IACUC Protocol Request Lift Hold','IACUC Protocol {PROTOCOL_NUMBER} Request Lift Hold Action','The IACUC protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has had the action "Request Lift 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 Request Lift 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 Request Lift Hold'),'KC-UNT:IACUC Administrator','admin',SYSDATE,1,SYS_GUID(), null)
/


CREATE INDEX xcolab_TrackedVisit_createDate_index ON xcolab_TrackedVisit (createDate DESC);
CREATE INDEX xcolab_TrackedVisitor2User_createDate_index ON xcolab_TrackedVisitor2User (createDate DESC);
CREATE INDEX xcolab_TrackedVisitor2User_userId_index ON xcolab_TrackedVisitor2User (userId);


Add DB-migration for removing useless column.

BEGIN;

DO $$DECLARE
   -- THESE MUST BE CHANGED WHEN YOU COPY THE SCRIPT!

   -- The version you expect the database to have _before_ the migration
   old_version numeric := 4;
   -- The version the database should have _after_ the migration
   new_version numeric := 5;

   -- hands off
   existing_version numeric;

BEGIN

   -- Check existing version
   SELECT version from lddb__schema INTO existing_version;
   IF ( existing_version <> old_version) THEN
      RAISE EXCEPTION 'ASKED TO MIGRATE FROM INCORRECT EXISTING VERSION!';
      ROLLBACK;
   END IF;
   UPDATE lddb__schema SET version = new_version;

   -- ACTUAL SCHEMA CHANGES HERE:
   ALTER TABLE lddb DROP COLUMN depMinModified;

END$$;

COMMIT;


ALTER PROCEDURE dw.p_lataa_d_ohjauksenala AS

if not exists (select * from dw.d_ohjauksenala where id=-1) begin
  set identity_insert dw.d_ohjauksenala on;
  insert into dw.d_ohjauksenala (
    id,ohjauksenala_koodi,ohjauksenala_nimi_fi,
    ohjauksenala_nimi_sv,ohjauksenala_nimi_en,source
  )
  select -1,koodi,nimi,nimi_sv,nimi_en,source
  from sa.sa_koodistot
  where koodisto='vipunenmeta' and koodi='-1'
  ;
  set identity_insert dw.d_ohjauksenala off;
end else begin
  update d
  set
    ohjauksenala_koodi=s.koodi,
    ohjauksenala_nimi_fi=s.nimi,
    ohjauksenala_nimi_sv=s.nimi_sv,
    ohjauksenala_nimi_en=s.nimi_en,
    d.source=s.source
  from dw.d_ohjauksenala d
  join sa.sa_koodistot s on s.koodi=d.ohjauksenala_koodi
  where s.koodisto='vipunenmeta' and s.koodi='-1'
  ;
end

MERGE dw.d_ohjauksenala AS target
USING (
  SELECT koodi,nimi,nimi_sv,nimi_en,source
  FROM sa.sa_koodistot
  WHERE koodisto = 'okmohjauksenala' and loppupvm IS NULL
) AS src
ON target.ohjauksenala_koodi = src.koodi
WHEN MATCHED THEN
  UPDATE SET
    ohjauksenala_nimi_fi = src.nimi,
    ohjauksenala_nimi_sv = src.nimi_sv,
    ohjauksenala_nimi_en = src.nimi_en,
    target.source = src.source
WHEN NOT MATCHED THEN
  INSERT (
    ohjauksenala_koodi,
    ohjauksenala_nimi_fi,
    ohjauksenala_nimi_sv,
    ohjauksenala_nimi_en,
    source
  )
  VALUES (src.koodi, src.nimi, src.nimi_sv, src.nimi_en, src.source)
;


--
-- Table structure for table `user_talk_attend`
--

DROP TABLE IF EXISTS `user_talk_attend`;
CREATE TABLE `user_talk_attend` (
  `uid` int(11) NOT NULL,
  `tid` int(11) NOT NULL,
  `ID` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ALTER TABLE user_talk_attend ADD INDEX idx_talk (tid);

-- Add a unique constraint so that each user can only be attending each talk once

ALTER TABLE user_talk_attend ADD UNIQUE INDEX idx_unique_user_talk (uid, tid);

INSERT INTO patch_history SET patch_number = 43;


-- +goose Up
ALTER TABLE School
  ALTER COLUMN CreatedAt SET DEFAULT 'now()';

ALTER TABLE Application
  ALTER COLUMN CreatedAt SET DEFAULT 'now()';

ALTER TABLE Users
  ALTER COLUMN CreatedAt SET DEFAULT 'now()';

-- +goose Down
ALTER TABLE School
  ALTER COLUMN CreatedAt DROP DEFAULT;

ALTER TABLE Application
  ALTER COLUMN CreatedAt DROP DEFAULT;

ALTER TABLE Users
  ALTER COLUMN CreatedAt DROP DEFAULT;


Add an SQL file to create a super administrator

INSERT INTO `users` (`users_user_id`, `name`, `email`, `enabled`, `send_email`, `users_role_id`, `last_visited_on`, `created_by`, `created_on`, `modified_by`, `modified_on`, `locked_by`, `locked_on`, `activation`, `params`, `uuid`)
VALUES
	(NULL, 'Administrator', 'admin@localhost.home', 1, 1, 25, '', NULL, NULL, NULL, NULL, NOW(), '', '', '', UUID());

INSERT INTO `users_passwords` (`email`, `expiration`, `hash`, `reset`) VALUES
('admin@localhost.home', NULL, '$2y$10$UT7uLipGnbJbTcjZ6D.OAeVByFn.2ZpPmd.thZ5e5xHLwKXAxdvNG', '');


Create parameter protocolProjectEndDateNumberOfYears for creating proposal development from IRB protocol

INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-PROTOCOL', 'Document', 'protocolProjectEndDateNumberOfYears', SYS_GUID(), 1, 'CONFG', '5', 'Number of Years for Proposal Development Project End Date', 'A', 'KUALI')
/


UPDATE oskari_statistical_datasource SET config='{
	"url": "https://pxnet2.stat.fi/pxweb/api/v1/{language}/Kuntien_avainluvut/2018/kuntien_avainluvut_2018_aikasarja.px",
	"info": {
		"url": "http://www.tilastokeskus.fi"
	},
	"regionKey": "Alue 2018",
	"indicatorKey": "Tiedot",
  "hints" : {
    "dimensions" : [ {
      "id" : "Vuosi",
      "sort" : "DESC"
    }]
  }
}' where locale LIKE '%Tilastokeskus%';


Fix the description of two weight PolicyUnits

-- Power saving:
-- weight function
UPDATE policy_units SET description='Gives hosts with higher CPU usage, lower weight (means that hosts with higher CPU usage are more likely to be selected)' WHERE id = '736999d0-1023-46a4-9a75-1316ed50e15b';

-- Even distribution:
-- weight function
UPDATE policy_units SET description='Gives hosts with lower CPU usage, lower weight (means that hosts with lower CPU usage are more likely to be selected)' WHERE id = '7db4ab05-81ab-42e8-868a-aee2df483edb';


update xcolab_ContentArticleVersion set content = replace(content,'http://climatecolab.org','https://www.climatecolab.org');
update xcolab_ContentArticleVersion set content = replace(content,'http://www.climatecolab.org','https://www.climatecolab.org');
update xcolab_ContentArticleVersion set content = replace(content,'https://climatecolab.org','https://www.climatecolab.org');

update xcolab_ContentArticleVersion set content = replace(content,'http://i.imgur.com','https://i.imgur.com');
update xcolab_ContentArticleVersion set content = replace(content,'http://imgur.com','https://i.imgur.com');
update xcolab_ContentArticleVersion set content = replace(content,'http://youtube.com','https://www.youtube.com');
update xcolab_ContentArticleVersion set content = replace(content,'http://energy.mit.edu','https://energy.mit.edu');
update xcolab_ContentArticleVersion set content = replace(content,'http://lae.mit.edu','https://lae.mit.edu');
update xcolab_ContentArticleVersion set content = replace(content,'http://environment.yale.edu','https://environment.yale.edu');
update xcolab_ContentArticleVersion set content = replace(content,'http://static3.businessinsider.com','https://static3.businessinsider.com');


CREATE TABLE users (
    id serial primary key,
    username varchar(32) NOT NULL,
    password varchar(32) NOT NULL,
    auth_token varchar(255),
    veri_token varchar(255),
    created_at timestamp without time zone NOT NULL,
    email_confirmed boolean NOT NULL DEFAULT false,
    salt varchar(32) NOT NULL,
    timezone_offset integer NOT NULL DEFAULT 0
);

CREATE TABLE images (
    id serial primary key,
    location varchar(50) NOT NULL,
    type varchar(20),
    owner_id integer NOT NULL,
    created_at timestamp without time zone NOT NULL,
	enabled boolean NOT NULL DEFAULT true,
    title varchar(50)
);

CREATE TABLE relationships (
    id serial primary key,
    sender_id integer NOT NULL,
    receiver_id integer NOT NULL,
    accepted boolean NOT NULL DEFAULT false,
    created_at timestamp without time zone NOT NULL
);


CREATE TABLE IF NOT EXISTS ContentInfo (
  PartititonKey text NOT NULL,
  RowKey text NOT NULL,
  "Timestamp" datetime NOT NULL,
  AbuseLevel integer NOT NULL,
  AttributionLink text,
  AttributionText text,
  "Blob" text NOT NULL,
  Error text,
  Id text PRIMARY KEY NOT NULL,
  Mime text,
  NumAbuseReports integer NOT NULL,
  Progress float NOT NULL DEFAULT(0),
  Size integer,
  Stage integer NOT NULL,
  Title text,
  Type integer NOT NULL,
  Url text NOT NULL,
  Version integer NOT NULL
);


/*
MySQL Data Transfer
Source Host: localhost
Source Database: vzlom
Target Host: localhost
Target Database: vzlom
Date: 26.01.2012 1:49:40
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for log
-- ----------------------------
DROP TABLE IF EXISTS `log`;
CREATE TABLE `log` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `module` varchar(50) DEFAULT NULL,
  `action` varchar(50) DEFAULT NULL,
  `task` varchar(50) DEFAULT NULL,
  `refid` varchar(50) DEFAULT NULL,
  `ip` varchar(50) DEFAULT NULL,
  `browser` varchar(255) DEFAULT NULL,
  `referer` varchar(500) DEFAULT NULL,
  `sessionid` varchar(50) DEFAULT NULL,
  `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `ik_browser` (`browser`) USING BTREE,
  KEY `ik_module` (`module`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for sef
-- ----------------------------
DROP TABLE IF EXISTS `sef`;
CREATE TABLE `sef` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `request` varchar(255) NOT NULL,
  `link` varchar(255) NOT NULL,
  `viewed` int(11) unsigned DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records
-- ----------------------------


/*
	generate-table-xml.sql will document the Tables, Columns and Foreign keys from a database as an Xmldocument.
*/

select
	tableXml
from
	(
		select
			'<Tables>' as [tableXml],
			'0.0' as [sort_order]
	UNION ALL
		select
			'  <Table tableId="' + cast(object_id as varchar) + '" name="' + t.name + '">"' as [tableXml],
			cast(object_id as varchar) + '.0' as [sort_order]
		from
			sys.tables t
	UNION ALL
		select
			'    <Column columnId="' + cast(c.column_id as varchar) + '" name="' + c.name + '"/>"' as [xml],
			cast(t.object_id as varchar) +'.' + cast(c.column_id as varchar(2)) as [sort_order]
			--*
		from sys.tables t
		inner join
			sys.columns c
		on t.object_id = c.object_id
		where t.type = 'U'
	UNION ALL
		select
			'  </Table>"' as [tableXml],
			cast(object_id as varchar) + '.999' as [sort_order]
		from
			sys.tables t
	UNION ALL
		select
			'</Tables>' as [tableXml],
			'99999999999999.999' as [sort_order]
	UNION ALL
		select
			'    <ForeignKey name="' + fk.name
			--+ '" tableId="' + cast(fkc.parent_object_id as varchar)
			+ '" columnId="' + cast(fkc.parent_column_id as varchar)
			+ '" refTableId="' + CAST(fkc.referenced_object_id as varchar)
			+ '" refColumnId="' + CAST(fkc.referenced_column_id as varchar)
			+ '"/>' as [tableXml],
			cast(fkc.parent_object_id as varchar) + '.00.' + cast(fkc.parent_column_id as varchar) as [sort_order]
		from
			sys.foreign_key_columns fkc
		inner join
			sys.foreign_keys fk
		on
			fk.object_id = fkc.constraint_object_id

	) qry
order by qry.sort_order


Make the timestamps in server_keys_json bigints

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

CREATE TABLE IF NOT EXISTS new_server_keys_json (
    server_name TEXT NOT NULL, -- Server name.
    key_id TEXT NOT NULL, -- Requested key id.
    from_server TEXT NOT NULL, -- Which server the keys were fetched from.
    ts_added_ms BIGINT UNSIGNED NOT NULL, -- When the keys were fetched
    ts_valid_until_ms BIGINT UNSIGNED NOT NULL, -- When this version of the keys exipires.
    key_json bytea NOT NULL, -- JSON certificate for the remote server.
    CONSTRAINT uniqueness UNIQUE (server_name, key_id, from_server)
);

INSERT INTO new_server_keys_json
    SELECT server_name, key_id, from_server,ts_added_ms, ts_valid_until_ms, key_json FROM server_keys_json ;

DROP TABLE new_server_keys_json;

ALTER TABLE new_server_keys_json RENAME TO server_keys_json;


ALTER TABLE ONLY accessgrants
    DROP CONSTRAINT accessgrants_groupid_fkey;
ALTER TABLE ONLY accessgrants
    DROP CONSTRAINT accessgrants_secretid_fkey;
ALTER TABLE ONLY memberships
    DROP CONSTRAINT memberships_clientid_fkey;
ALTER TABLE ONLY memberships
    DROP CONSTRAINT memberships_groupid_fkey;
ALTER TABLE ONLY secrets_content
    DROP CONSTRAINT secrets_content_secretid_fkey;


--;
-- Schema upgrade from 2.2.9 to 2.2.10;
--;

ALTER TABLE `cloud`.`account` ADD COLUMN `network_domain` varchar(255);
ALTER TABLE `cloud`.`domain` ADD COLUMN `network_domain` varchar(255);

INSERT IGNORE INTO configuration VALUES ('Advanced', 'DEFAULT', 'NetworkManager', 'use.external.dns', 'false', 'Bypass internal dns, use exetrnal dns1 and dns2');

ALTER TABLE `cloud`.`domain_router` ADD COLUMN `is_redundant_router` int(1) unsigned NOT NULL COMMENT 'if in redundant router mode';
ALTER TABLE `cloud`.`domain_router` ADD COLUMN `priority` int(4) unsigned COMMENT 'priority of router in the redundant router mode';
ALTER TABLE `cloud`.`domain_router` ADD COLUMN `redundant_state` varchar(64) NOT NULL COMMENT 'the state of redundant virtual router';

ALTER TABLE `cloud`.`cluster` ADD COLUMN  `managed_state` varchar(32) NOT NULL DEFAULT 'Managed' COMMENT 'Is this cluster managed by cloudstack';

INSERT IGNORE INTO configuration VALUES ('Network', 'DEFAULT', 'management-server', 'network.redundantrouter', 'false', 'enable/disable redundant virtual router');

Add some default values for 2.2.9-2.2.10 scheme

--;
-- Schema upgrade from 2.2.9 to 2.2.10;
--;

ALTER TABLE `cloud`.`account` ADD COLUMN `network_domain` varchar(255);
ALTER TABLE `cloud`.`domain` ADD COLUMN `network_domain` varchar(255);

INSERT IGNORE INTO configuration VALUES ('Advanced', 'DEFAULT', 'NetworkManager', 'use.external.dns', 'false', 'Bypass internal dns, use exetrnal dns1 and dns2');

ALTER TABLE `cloud`.`domain_router` ADD COLUMN `is_redundant_router` int(1) unsigned NOT NULL DEFAULT 0 COMMENT 'if in redundant router mode';
ALTER TABLE `cloud`.`domain_router` ADD COLUMN `priority` int(4) unsigned COMMENT 'priority of router in the redundant router mode';
ALTER TABLE `cloud`.`domain_router` ADD COLUMN `redundant_state` varchar(64) NOT NULL DEFAULT 'UNKNOWN' COMMENT 'the state of redundant virtual router';

ALTER TABLE `cloud`.`cluster` ADD COLUMN  `managed_state` varchar(32) NOT NULL DEFAULT 'Managed' COMMENT 'Is this cluster managed by cloudstack';

INSERT IGNORE INTO configuration VALUES ('Network', 'DEFAULT', 'management-server', 'network.redundantrouter', 'false', 'enable/disable redundant virtual router');


Create a summary table of the extents of the hocr areas

/*
We're going to introduce some data quality here that needs to be
carried through when using this table:
- text with less than 2 characters is skipped
- images with x or y < 100 are skipped
- words who's x0, x1, y0, or y1 are within 10 of the edge of the image
  are skipped
*/

CREATE TABLE
  text_extent
SELECT
  occid,
  min(area_x0) as min_area_x0,
  min(area_y0) as min_area_y0,
  max(area_x1) as max_area_x1,
  max(area_y1) as max_area_y1,
  min(line_x0) as min_line_x0,
  min(line_y0) as min_line_y0,
  max(line_x1) as max_line_x1,
  max(line_y1) as max_line_y1,
  min(word_x0) as min_word_x0,
  min(word_y0) as min_word_y0,
  max(word_x1) as max_word_x1,
  max(word_y1) as max_word_y1
FROM
  hocr_results
WHERE
  x > 100
  AND y > 100
  AND area_x0 > 10
  AND area_y0 > 10
  AND area_x1 < (x - 10)
  AND area_y1 < (y - 10)
  AND line_x0 > 10
  AND line_y0 > 10
  AND line_x1 < (x - 10)
  AND line_y1 < (y - 10)
  AND word_x0 > 10
  AND word_y0 > 10
  AND word_x1 < (x - 10)
  AND word_y1 < (y - 10)
  AND LENGTH(TRIM(text))>2
GROUP BY
  occid;


drop table computer_terms;
create table computer_terms(term text, category text, comments char(16));
create index computer_terms_index1 on computer_terms using btree(term);
create index computer_terms_index2 on computer_terms using btree(category);
insert into computer_terms values('computer display', 'X-A01-Y', 'a comment 1');
insert into computer_terms values('computer graphics', 'T-B01-Y', 'a comment 2');
insert into computer_terms values('computer programmer', 'S-Z01-Y', 'a comment 3');
vacuum computer_terms;
select * from computer_terms;
select * from computer_terms where category = 'X-A01-Y';
select * from computer_terms where category ~* 'x-a01-y';
select * from computer_terms where category like '_-A01-_';
select * from computer_terms where category like '_-A%';
select * from computer_terms where term ~ 'computer [dg]';
select * from computer_terms where term ~* 'computer [DG]';
select *,character_length(term) from computer_terms;
select *,octet_length(term) from computer_terms;
select *,position('s' in term) from computer_terms;
select *,substring(term from 10 for 4) from computer_terms;


Add the ship changes script for current round.

UPDATE smr_new.ship_type SET cost = 12026598 WHERE ship_type_id = 21; -- Federal Warrant to 12,026,598 credits
UPDATE smr_new.ship_type SET cost = 23675738 WHERE ship_type_id = 22; -- Federal Ultimatum to 23,675,738 credits
UPDATE smr_new.ship_type SET cost = 7483452 WHERE ship_type_id = 24; -- Assasin to 7,483,452 credits
UPDATE smr_new.ship_type SET cost = 19890100 WHERE ship_type_id = 25; -- Death Cruiser to 19,890,100 credits


CREATE TABLE HELLOWORLD (
   HELLO VARCHAR(15),
   WORLD VARCHAR(15),
   DIALECT VARCHAR(15) NOT NULL,
   PRIMARY KEY (DIALECT)
);
PARTITION TABLE HELLOWORLD ON COLUMN DIALECT;

CREATE PROCEDURE Insert PARTITION ON TABLE Helloworld COLUMN Dialect
   AS INSERT INTO HELLOWORLD (Dialect, Hello, World) VALUES (?, ?, ?);
CREATE PROCEDURE Select PARTITION ON TABLE Helloworld COLUMN Dialect
   AS SELECT HELLO, WORLD FROM HELLOWORLD WHERE DIALECT = ?;


Add ITN roads view (roadlink with roadname and road.fid)

-- Lookup between road and roadlink on fid
CREATE OR REPLACE VIEW osmm_itn.road_roadlink AS
SELECT road_fid,
       replace(roadlink_fid, '#', '') AS roadlink_fid
FROM
  (SELECT fid AS road_fid,
          unnest(networkmember_href) AS roadlink_fid
   FROM osmm_itn.road) AS a;

-- Each roadlink with associated roadname(s) and fid of road in case more info
-- is required
CREATE OR REPLACE VIEW osmm_itn.roads AS
SELECT array_to_string(road.roadname, ', ', '') AS roadname,
       road.fid AS road_fid,
       roadlink.*
FROM osmm_itn.roadlink AS roadlink
LEFT JOIN osmm_itn.road_roadlink AS road_roadlink ON (roadlink.fid = road_roadlink.roadlink_fid)
LEFT JOIN osmm_itn.road AS road ON (road_roadlink.road_fid = road.fid);


Add sql function for dropping all schema objects

set define off

create or replace FUNCTION          "DROP_ALL_SCHEMA_OBJECTS" RETURN NUMBER AS
PRAGMA AUTONOMOUS_TRANSACTION;
cursor c_get_objects is
  select object_type,'"'||object_name||'"'||decode(object_type,'TABLE' ,' cascade constraints',null) obj_name
  from user_objects
  where object_type in ('TABLE','VIEW','PACKAGE','SEQUENCE','SYNONYM', 'MATERIALIZED VIEW')
  order by object_type;
cursor c_get_objects_type is
  select object_type, '"'||object_name||'"' obj_name
  from user_objects
  where object_type in ('TYPE');
BEGIN
  begin

    for object_rec in c_get_objects loop
      execute immediate ('drop '||object_rec.object_type||' ' ||object_rec.obj_name);
    end loop;
    for object_rec in c_get_objects_type loop
      begin
        execute immediate ('drop '||object_rec.object_type||' ' ||object_rec.obj_name);
      end;
    end loop;
  end;

  -- drop procedures
  BEGIN
    FOR I IN (SELECT DISTINCT NAME FROM USER_SOURCE WHERE TYPE = 'PROCEDURE') LOOP

    EXECUTE IMMEDIATE 'DROP PROCEDURE '||I.NAME;
    END LOOP;
  END;

  RETURN 0;
END DROP_ALL_SCHEMA_OBJECTS;
/
set define on


Add flightsystem connection creation script

CREATE USER flightsystem IDENTIFIED BY flightsystempass;
GRANT CONNECT, RESOURCE, CREATE CLUSTER, QUERY REWRITE, CREATE PROCEDURE, CREATE SEQUENCE, CREATE SESSION, CREATE SYNONYM, CREATE TABLE, CREATE TRIGGER, CREATE TYPE, CREATE VIEW, CREATE LIBRARY, UNLIMITED TABLESPACE TO flightsystem;
COMMIT;


Create an index on FY in budgethistory table

-- Index: public."idx_budgethistory_FY"

-- DROP INDEX public."idx_budgethistory_FY";

CREATE INDEX "idx_budgethistory_FY"
  ON public.budget_app_budgethistory
  USING btree
  (fiscal_year COLLATE pg_catalog."default");


SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `approvals`
    DROP FOREIGN KEY `approvals_ibfk_3`;

ALTER TABLE `approvals` ADD CONSTRAINT `approvals_ibfk_3`
    FOREIGN KEY `approvals_ibfk_3` (`addon_id`)
    REFERENCES `addon` (`id`)
    ON DELETE CASCADE;

SET FOREIGN_KEY_CHECKS=1;


Add SQL file for adding year prefix to old cases

# See how many case numbers of what length there are
select count(*), o.name, LENGTH(case_number) c
from "case" join meeting m on "case".meeting_id=m.id
join organization o on m.organization_id=o.id
group by o.name, c;

# See how many case numbers needing upgrade there are
select count(*), o.name, LENGTH(case_number) c
from "case" join meeting m on "case".meeting_id=m.id
join organization o on m.organization_id=o.id
where m.date < '2016-08-01' and length(case_number)=8
group by o.name, c;

# Add a year number to old case numbers
update "case"
set case_number=date_part('year', m.date) || '-' || case_number
from meeting m
where LENGTH(case_number)=8 and m.date < '2016-08-01' and "case".meeting_id=m.id;


Remove old files upload API, filter files by owner.

-- remove put support from files view, only allow reading owned files

CREATE OR REPLACE VIEW "1".files as
  SELECT uuid, user_id, org, created_at, name, file
    FROM mappa.files
    WHERE user_id = current_setting('request.jwt.claim.sub', true)::uuid;

REVOKE insert ON "1".files FROM member;


--PostGIS (ppg_geometry)
-- http://www.postgis.org/docs/ST_GeomFromText.html
DO
$$DECLARE
	pt_curse CURSOR FOR SELECT x_coord, y_coord FROM sde.test_xy;
	x_var numeric;
	y_var numeric;
BEGIN
	OPEN pt_curse;
LOOP
	FETCH pt_curse INTO x_var, y_var;
	EXIT WHEN NOT FOUND;
		INSERT INTO sde.points_pggeom (objectid, shape)
		SELECT sde.next_rowid('sde', 'points_pggeom'),
			   ST_GeomFROMText('POINT(' || cast(x_var as text)|| ' ' || cast(y_var as text) || ')''', 4326);
END LOOP;
CLOSE pt_curse;
END$$;

-- Esri ST_Geometry
DO
$$DECLARE
	pt_curse CURSOR FOR SELECT x_coord, y_coord FROM sde.test_xy;
	x_var numeric;
	y_var numeric;
BEGIN
	OPEN pt_curse;
LOOP
	FETCH pt_curse INTO x_var, y_var;
	EXIT WHEN NOT FOUND;
	    INSERT INTO sde.points_stgeom (objectid, geom)
		SELECT
		sde.next_rowid('sde', 'points_stgeom'),
		ST_point(x_var , y_var, 4326);
END LOOP;
CLOSE pt_curse;
END$$;


Add sql script to convert the DB model from MM1 to MM2

-- SQL Script to update the DB schema from MirrorManager1 to MirrorManager2

-- Drop tables that we no longer use
DROP TABLE "group_permission";
DROP TABLE "permission";
DROP TABLE "tg_visit_identity";
DROP TABLE "visit_identity";

-- Either drop or rename/adjust (see below)
DROP TABLE "user_group";
DROP TABLE "tg_group";
DROP TABLE "tg_user";
DROP TABLE "visit";

-- If you fear there is some data in these table this will rename and
-- adjust them

RENAME TABLE "user_group" TO "mm_user_group";
RENAME TABLE "tg_group" TO "mm_group";

RENAME TABLE "tg_user" TO "mm_user";
ALTER TABLE "mm_user" ADD COLUMN token varchar(50);
ALTER TABLE "mm_user" ADD COLUMN updated_on timestamp without time zone

RENAME TABLE "visit" TO "mm_user_visit";
ALTER TABLE "mm_user_visit" ADD COLUMN user_id integer;
ALTER TABLE ONLY "mm_user_visit"
    ADD CONSTRAINT user_id_exists FOREIGN KEY (user_id) REFERENCES mm_user(id);


CREATE OR REPLACE VIEW ISSUE_OUTLINE_VIEW AS
 SELECT
   A.USER_NAME,
   A.REPOSITORY_NAME,
   A.ISSUE_ID,
   IFNULL(B.COMMENT_COUNT, 0) + IFNULL(C.COMMENT_COUNT, 0) AS COMMENT_COUNT
 FROM ISSUE A
 LEFT OUTER JOIN (
   SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM ISSUE_COMMENT
   WHERE ACTION IN ('comment', 'close_comment', 'reopen_comment')
   GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID
 ) B
 ON (A.USER_NAME = B.USER_NAME AND A.REPOSITORY_NAME = B.REPOSITORY_NAME AND A.ISSUE_ID = B.ISSUE_ID)
 LEFT OUTER JOIN (
   SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM COMMIT_COMMENT
   GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID
 ) C
 ON (A.USER_NAME = C.USER_NAME AND A.REPOSITORY_NAME = C.REPOSITORY_NAME AND A.ISSUE_ID = C.ISSUE_ID);

Use COALESCE instead of IFNULL

CREATE OR REPLACE VIEW ISSUE_OUTLINE_VIEW AS
 SELECT
   A.USER_NAME,
   A.REPOSITORY_NAME,
   A.ISSUE_ID,
   COALESCE(B.COMMENT_COUNT, 0) + COALESCE(C.COMMENT_COUNT, 0) AS COMMENT_COUNT
 FROM ISSUE A
 LEFT OUTER JOIN (
   SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM ISSUE_COMMENT
   WHERE ACTION IN ('comment', 'close_comment', 'reopen_comment')
   GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID
 ) B
 ON (A.USER_NAME = B.USER_NAME AND A.REPOSITORY_NAME = B.REPOSITORY_NAME AND A.ISSUE_ID = B.ISSUE_ID)
 LEFT OUTER JOIN (
   SELECT USER_NAME, REPOSITORY_NAME, ISSUE_ID, COUNT(COMMENT_ID) AS COMMENT_COUNT FROM COMMIT_COMMENT
   GROUP BY USER_NAME, REPOSITORY_NAME, ISSUE_ID
 ) C
 ON (A.USER_NAME = C.USER_NAME AND A.REPOSITORY_NAME = C.REPOSITORY_NAME AND A.ISSUE_ID = C.ISSUE_ID);


# --- !Ups

DROP TABLE IF EXISTS task_board;
DROP TABLE IF EXISTS task_comment;
DROP TABLE IF EXISTS card;
DROP TABLE IF EXISTS card_assignee;
DROP TABLE IF EXISTS card_label;
DROP TABLE IF EXISTS checklist;

DROP SEQUENCE IF EXISTS task_board_seq;
DROP SEQUENCE IF EXISTS task_comment_seq;
DROP SEQUENCE IF EXISTS card_seq;
DROP SEQUENCE IF EXISTS card_assignee_seq;
DROP SEQUENCE IF EXISTS card_label_seq;
DROP SEQUENCE IF EXISTS checklist_seq;

# --- !Downs

CREATE TABLE task_board (
  id                        BIGINT NOT NULL,
  project_id                BIGINT,
  CONSTRAINT pk_task_board PRIMARY KEY (id))
;

CREATE TABLE task_comment (
  id                        BIGINT NOT NULL,
  body                      VARCHAR(255),
  card_id                   BIGINT,
  CONSTRAINT pk_task_comment PRIMARY KEY (id))
;

CREATE TABLE task_board (
  id                        BIGINT NOT NULL,
  project_id                BIGINT,
  CONSTRAINT pk_task_board PRIMARY KEY (id))
;

CREATE TABLE task_comment (
  id                        BIGINT NOT NULL,
  body                      VARCHAR(255),
  card_id                   BIGINT,
  CONSTRAINT pk_task_comment PRIMARY KEY (id))
;

CREATE TABLE checklist (
  id                        BIGINT NOT NULL,
  title                     VARCHAR(255),
  CONSTRAINT pk_checklist PRIMARY KEY (id))
;

CREATE TABLE card_assignee (
  id                        BIGINT NOT NULL,
  card_id                   BIGINT,
  project_user_id           BIGINT,
  CONSTRAINT pk_card_assignee PRIMARY KEY (id))
;

CREATE SEQUENCE IF NOT EXISTS task_board_seq;
CREATE SEQUENCE IF NOT EXISTS task_comment_seq;
CREATE SEQUENCE IF NOT EXISTS card_seq;
CREATE SEQUENCE IF NOT EXISTS card_assignee_seq;
CREATE SEQUENCE IF NOT EXISTS card_label_seq;
CREATE SEQUENCE IF NOT EXISTS checklist_seq;


CREATE TABLE Article (
  pmcId   TEXT PRIMARY KEY NOT NULL,
  tittle  TEXT
);

CREATE TABLE TraitTable (
  tableId   TEXT PRIMARY KEY NOT NULL,
  numOfCol  INTEGER NOT NULL,
  numOfRows INTEGER NOT NULL,
  pmcId     TEXT NOT NULL,
  FOREIGN KEY(pmcId) REFERENCES article(pmcId)
);

CREATE TABLE Abbreviation (
  abbreviation  TEXT NOT NULL,
  expansion     TEXT NOT NULL,
  pmcId         TEXT NOT NULL,
  FOREIGN KEY(pmcId) REFERENCES Article(pmcId)
);

CREATE TABLE ColumnEntry (
  colId         TEXT PRIMARY KEY NOT NULL,
  colHeader     TEXT,
  colType       TEXT,
  colAnnotation TEXT,
  tableId       TEXT,
  FOREIGN KEY(tableId) REFERENCES TraitTable(tableId)
);

CREATE TABLE CellEntry (
  cellId    INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  rowNumber INT,
  cellValue TEXT,
  cellType  TEXT,
  colId     TEXT,
  FOREIGN KEY(colId) REFERENCES columnEntry(colId)
);

CREATE TABLE Trait (
  traitId           INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  traitName         Text,
  traitValuePair    TEXT,
  traitPropertyPair TEXT,
  otherTraitPair    TEXT,
  pmcId             TEXT,
  tableId           TEXT,
  rowNumber         INTEGER,
  FOREIGN KEY(pmcId) REFERENCES Article(pmcId),
  FOREIGN KEY(tableId) REFERENCES TraitTable(tableId)
);

CREATE TABLE Qtl (
  qtlId               INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  traitNameInArticle  Text,
  traitNameInOntology Text,
  traitUri            TEXT,
  chromosomeNumber    TEXT,
  markerAssociated    TEXT,
  markerUri           TEXT,
  geneAssociated      TEXT,
  geneUri             TEXT,
  snpAssociated       TEXT,
  snpUri              TEXT,
  pmcId               TEXT,
  tableId             TEXT,
  rowNumber           TEXT,
  FOREIGN KEY(pmcId) REFERENCES Article(pmcId),
  FOREIGN KEY(tableId) REFERENCES TraitTable(tableId)
);


Create parameter to display list of special review items for Approver View

INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-PD', 'Document', 'proposal.summary.validSpecialReviewCodes', SYS_GUID(), 1, 'CONFG', '1,2,3,4', 'comma delimited list of special review codes that should be displayed on approver view for proposal development', 'A', 'KC')
/


Implement COI event type of "IACUC Protocol"

INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-IACUC', 'Document', 'PROTOCOL_DISCLOSE_STATUS_CODES', SYS_GUID(), 1, 'CONFG', '100;101;102;200;202', 'IACUC Protocol status codes for which a COI disclosure event will be considered active.', 'A', 'KC')
/
INSERT INTO KRCR_PARM_T (NMSPC_CD, CMPNT_CD, PARM_NM, OBJ_ID, VER_NBR, PARM_TYP_CD, VAL, PARM_DESC_TXT, EVAL_OPRTR_CD, APPL_ID)
VALUES ('KC-IACUC', 'Document', 'ALL_SPONSORS_FOR_IACUC_PROTOCOL_DISCLOSE', SYS_GUID(), '1', 'CONFG', 'N', 'All IACUC protocols require disclosure, irrespective to funding sponsor code.', 'A', 'KC')
/


UPDATE oskari_maplayer
SET locale = '{"fi": {"name": "Pääkaupunkiseudun pienaluejako"},"en": {"name": "Helsinki Region Small Areas"}, "sv": {"name": "Helsingforsregionens småområden"}}'
WHERE name = 'seutukartta:Seutu_pienalueet';

UPDATE oskari_maplayer
SET locale = '{"fi": {"name": "Pääkaupunkiseudun suuraluejako"},"en": {"name": "Helsinki Region Major Areas"}, "sv": {"name": "Helsingforsregionens storområden"}}'
WHERE name = 'seutukartta:Seutu_suuralueet';


Add migration for the disallow-thunderbird-and-seamonkey waffle switch

INSERT INTO waffle_switch (name, active, note, created, modified)
VALUES ('disallow-thunderbird-and-seamonkey', 0, 'Reject submissions for add-ons that are not compatible with Firefox.', NOW(), NOW());


Test script for Niels' newly introduced code + bugs

SET auto_commit=true;

-- the following statements should all fail
ROLLBACK;

COMMIT;

SAVEPOINT failingsavepoint;

-- this one is incorrect since the savepoint should not exist
-- however, the server might answer with an error about auto_commit
-- for that might be cheaper
RELEASE SAVEPOINT failingsavepoint;


# add url column to source table
ALTER TABLE `source` ADD `url` varchar(255) NULL DEFAULT NULL  AFTER `description`;

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL,'patch', 'patch_57_58_d.sql|new column url in source table');


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

/**
@header patch_84_85_r.sql - Remove regulatory build entries from feature_set table, relink everything else.
@desc Remove regulatory build entries from feature_set table, relink everything else.
*/

alter table regulatory_feature_feature_set add column epigenome_id int(10) unsigned default NULL;

update regulatory_feature_feature_set, feature_set
set regulatory_feature_feature_set.epigenome_id = feature_set.epigenome_id
where regulatory_feature_feature_set.feature_set_id = feature_set.feature_set_id;

alter table regulatory_feature_feature_set drop key uniqueness_constraint_idx;
alter table regulatory_feature_feature_set add unique key uniqueness_constraint_idx (epigenome_id, regulatory_feature_id);

alter table regulatory_feature_feature_set drop column feature_set_id;

rename table regulatory_feature_feature_set to regulatory_activity;

drop table if exists regulatory_build;
create table regulatory_build (
  regulatory_build_id int(4) unsigned not null auto_increment,
  feature_type_id int(4) unsigned not null,
  analysis_id int(4) unsigned not null,
  version int(4) unsigned not null,
  name varchar(45) not null,
  primary key  (regulatory_build_id)
) ENGINE=MyISAM;

insert into regulatory_build (
  feature_type_id,
  analysis_id,
  version,
  name
) select feature_type_id, analysis_id, 1, "Regulatory features" from feature_set where type = "regulatory" group by "feature_type_id, analysis_id";

delete from feature_set where type = "regulatory";

alter table regulatory_feature add column regulatory_build_id int(10) unsigned default null

update regulatory_feature, regulatory_build set regulatory_feature.regulatory_build_id = regulatory_build.regulatory_build_id;

insert into meta (species_id, meta_key, meta_value) values (null, 'patch', 'patch_84_85_r.sql|Remove regulatory build entries from feature_set table, relink everything else.');


CREATE TEXT SEARCH DICTIONARY ispell_ru (
  template  =   ispell,
  dictfile  =   ru,
  afffile   =   ru,
  stopwords =   russian
);

CREATE TEXT SEARCH DICTIONARY ispell_en (
    template  = ispell,
    dictfile  = en,
    afffile   = en,
    stopwords = english
);

CREATE TEXT SEARCH CONFIGURATION ff_search_config ( COPY = russian );

ALTER TEXT SEARCH CONFIGURATION ff_search_config ALTER MAPPING FOR word, hword, hword_part WITH ispell_ru, russian_stem;

ALTER TEXT SEARCH CONFIGURATION ff_search_config ALTER MAPPING FOR asciiword, asciihword, hword_asciipart WITH ispell_en, english_stem;

SET default_text_search_config = 'ff_search_config';


Add sorting to time variable for TK datasource

UPDATE oskari_statistical_datasource SET config='{
	"url": "https://pxnet2.stat.fi/pxweb/api/v1/fi/Kuntien_avainluvut/2018/kuntien_avainluvut_2018_aikasarja.px",
	"info": {
		"url": "http://www.tilastokeskus.fi"
	},
	"regionKey": "Alue 2018",
	"indicatorKey": "Tiedot",
  "hints" : {
    "dimensions" : [ {
      "id" : "Vuosi",
      "sort" : "DESC"
    }]
  }
}' where locale LIKE '%Tilastokeskus%';


-- Add column to distinguish what entity this is, currently either VM or TEMPLATE.
SELECT fn_db_add_column('vm_static', 'entity_type', 'VARCHAR(32)');

-- Add specific templates columns to vm_static.
SELECT fn_db_add_column('vm_static', 'child_count', 'INTEGER DEFAULT 0');
SELECT fn_db_add_column('vm_static', 'template_status', 'INTEGER');

-- Add the internal_drive_mapping field to image_templates since we don't want it in image_vm_map.
SELECT fn_db_add_column('image_templates', 'internal_drive_mapping', 'VARCHAR(50)');


Move 0.x crate schema sql to separate file

select
    'Tables' as table_type,
    'YES' as is_nullable,
    tables.schema_name as table_schema,
    tables.table_name as table_name,
    column_name,
    data_type
from
    information_schema.tables, information_schema.columns
where
    tables.schema_name not in ('information_schema')
    and columns.schema_name = tables.schema_name
    and columns.table_name = tables.table_name


Migrate segments_union to plpgsql implementation

-- Implement segments_union(segment[], segment) in plpgsql. Remove the unused
-- segments_union(segment[], segment[]).

-- Fare thee well
drop function if exists segments_union(segment[], segment[]);

-- | Add a new segment to a list, merging overlapping or adjacent segments into
-- one.
--
-- We assume acc (the input list) is sorted and non-overlapping.
create or replace function segments_union(acc segment[], seg segment)
returns segment[]
immutable strict parallel safe language plpgsql
as $$
declare
    result segment[] := '{}';
    acc_length constant int := coalesce(array_length(acc, 1), 0);
    i int;
    tmp segment;
begin
    raise notice 'acc_length %', acc_length;
    raise notice 'SEG %', seg;
    raise notice 'ACC %', acc;
    if coalesce(array_ndims(acc), 0) > 1 then
        raise exception 'Expected one-dimensional state';
    end if;
    if isempty(seg) then
        result := acc;
    else -- seg is non-empty range
        if acc_length = 0 then
            result := ARRAY[seg];
        else -- state is non-empty array
            -- 1. Skip over strictly less-than, unadjacent segments
            i := 1;
            while acc[i] << seg and acc[i] -|- seg is false and i <= acc_length
            loop
                result := result || acc[i];
                i := i + 1;
            end loop;
            -- 2. Merge with overlapping/adjacent segments. (Or none, if seg
            --    fell directly between two.)
            tmp := seg;
            while (tmp && acc[i] or tmp -|- acc[i]) and i <= acc_length
            loop
                tmp := acc[i] + tmp;
                i := i + 1;
            end loop;
            -- 3. Tack on the rest.
            result := result || tmp || acc[i :];
        end if;
    end if;
    return result;
end
$$;

-- and now, a test
-- FIXME add this to the test suite
/*
create temporary table segs ( s segment );
insert into segs values ('empty'), ('(4,5)'),('(0,1)'), ('(1,2)'), ('(2,3)'), ('(-1,0)'), ('[2,2]'), ('[6,7]'),('(5,7]'),('[6,8]'), ('empty');
select ss.s = '{"(-1,0)","(0,1)","(1,3)","(4,5)","(5,8]"}' as "test passed"
from (select segments_union(s) s from segs) as ss;
*/


Add database column for product category by program - Add a configuration setting to override category - add migration script to apply the currently selected category on the new pp column

-- create the new column for the category id
ALTER TABLE program_products
ADD COLUMN productcategoryid INT NULL REFERENCES product_categories (id);

-- migrate the existing product categories
UPDATE program_products SET productcategoryid = p.categoryid
FROM products p where p.id = program_products.productid;

-- add the configuration option to force the category to either be the one on the program products or programs table.
INSERT INTO configuration_settings (key, name, groupname, description, value, valueType,displayOrder)
  values ('ALLOW_PRODUCT_CATEGORY_PER_PROGRAM', 'Allow product categories at program level','Settings','','false', 'BOOLEAN', 12);


-- Exactly one root AnswerGroup per response_id
-- Below should return no rows.
select response_id, count(*)
  from answers
  where parent_id is null and deleted_at is null
  group by response_id
  having count(*) > 1;

-- Exactly one AnswerGroup per non-repeat group and response_id
-- Below should return no rows.
select response_id, questioning_id, count(a.id)
  from answers a inner join form_items f on f.id = a.questioning_id
  where a.type = 'AnswerGroup' and f.repeatable = 'f'
  group by questioning_id, response_id
  having count(a.id) > 1;

-- Exactly one AnswerGroupSet per repeat group and response_id
-- Below should return no rows.
select response_id, questioning_id, count(a.id)
  from answers a inner join form_items f on f.id = a.questioning_id
  where a.type = 'AnswerGroupSet' and f.repeatable = 't'
  group by questioning_id, response_id
  having count(a.id) > 1;

-- Contiguous new_rank
-- Below should return no rows.
SELECT a1.id, a1.new_rank
  FROM answers a1
  WHERE a1.new_rank > 1 AND a1.deleted_at IS NULL AND NOT EXISTS (
    SELECT id
      FROM answers a2
      WHERE a2.deleted_at IS NULL AND a2.parent_id = a1.parent_id AND a2.new_rank = a1.new_rank - 1
  );

-- Non-duplicate ranks
-- Below should return no rows.
SELECT parent_id, new_rank, COUNT(id)
  FROM answers
  WHERE deleted_at IS NULL AND parent_id is NOT NULL
  GROUP BY parent_id, new_rank
  HAVING COUNT(id) > 1;

-- Roots have new_rank 1.
-- Below should return no rows.
SELECT a1.id, a1.new_rank
  FROM answers a1
  WHERE a1.new_rank != 1 AND a1.parent_id IS NULL AND a1.deleted_at IS NULL;

-- Old inst_num is the same for all Answers in an AnswerGroup
-- Below should return no rows.
SELECT parent_id, COUNT(DISTINCT inst_num)
  FROM answers
  WHERE answers.deleted_at IS NULL AND answers.type = 'Answer'
  GROUP BY parent_id
  HAVING COUNT(DISTINCT inst_num) > 1;

-- Answers with old_rank != 1 should have AnswerSet as parent
-- Below should return no rows.
SELECT answers.id
  FROM answers INNER JOIN answers parents ON answers.parent_id = parents.id
  WHERE answers.old_rank IS NOT NULL
    AND answers.deleted_at IS NULL
    AND answers.old_rank > 1
    AND parents.type != 'AnswerSet';


-- -----------------------------------------------------------------
-- Create the entry for BBN GRAM Starter Rack:
--
-- Execute as:
--
--    psql -U portal -h localhost -f add-gram-bbncam.sql portal
--
-- -----------------------------------------------------------------

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: zero = aggregate
      0,
      -- URL
      'https://128.89.118.100:5002',
      -- CERT
      '/usr/share/geni-ch/sr/certs/gr-bbncam-am.pem',
      -- NAME
      'BBN-CAM GRAM (BBN-CAM)',
      -- DESCRIPTION
      'BBN-CAM GRAM Rack (BBN-CAM)',
      -- URN
      'urn:publicid:IDN+bbn-cam-ctrl-1.bbn.com+authority+am'
    );

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: 7 = CA
      7,
      -- URL
      '',
      -- CERT (self signed)
      '/usr/share/geni-ch/sr/certs/gr-bbncam-ch.pem',
      -- NAME
      '',
      -- DESCRIPTION
      'BBN-CAM GRAM Rack cert signer (self)',
      -- URN
     ''
    );

Update GPO OpenGENI to new hostname

-- -----------------------------------------------------------------
-- Create the entry for BBN GRAM Starter Rack:
--
-- Execute as:
--
--    psql -U portal -h localhost -f add-gram-bbncam.sql portal
--
-- -----------------------------------------------------------------

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: zero = aggregate
      0,
      -- URL
      'https://bbn-cam-ctrl-1.gpolab.bbn.com:5002',
      -- CERT
      '/usr/share/geni-ch/sr/certs/gr-bbncam-am.pem',
      -- NAME
      'GPO OpenGENI',
      -- DESCRIPTION
      'GPO OpenGENI Rack',
      -- URN
      'urn:publicid:IDN+bbn-cam-ctrl-1.bbn.com+authority+am'
    );

insert into service_registry
    (service_type, service_url, service_cert, service_name,
     service_description, service_urn)
  values
    ( -- TYPE: 7 = CA
      7,
      -- URL
      '',
      -- CERT (self signed)
      '/usr/share/geni-ch/sr/certs/gr-bbncam-ch.pem',
      -- NAME
      '',
      -- DESCRIPTION
      'GPO OpenGENI Rack cert signer (self)',
      -- URN
     ''
    );


# --- !Ups

CREATE INDEX "authorize_activity_idx" ON audit."authorize" ("audit_time" DESC) WHERE "audit_action" IN ('add', 'change') AND "site" > 'NONE';
CREATE INDEX "volume_share_activity_idx" ON audit."volume_access" ("audit_time" DESC) WHERE "audit_action" = 'add' AND "party" = 0 AND "children" > 'NONE';

# --- !Downs

DROP INDEX audit."authorize_activity_idx";
DROP INDEX audit."volume_share_activity_idx";


BEGIN;

DO $$DECLARE
   -- THESE MUST BE CHANGED WHEN YOU COPY THE SCRIPT!

   -- The version you expect the database to have _before_ the migration
   old_version numeric := 19;
   -- The version the database should have _after_ the migration
   new_version numeric := 20;

   -- hands off
   existing_version numeric;

BEGIN

   -- Check existing version
SELECT version from lddb__schema INTO existing_version;
IF ( existing_version <> old_version) THEN
      RAISE EXCEPTION 'ASKED TO MIGRATE FROM INCORRECT EXISTING VERSION!';
ROLLBACK;
END IF;
UPDATE lddb__schema SET version = new_version;

-- ACTUAL SCHEMA CHANGES HERE:

CREATE TABLE IF NOT EXISTS lddb__user_data (
    id text PRIMARY KEY,
    data jsonb NOT NULL,
    created timestamp with time zone DEFAULT now() NOT NULL,
    modified timestamp with time zone DEFAULT now() NOT NULL
);

END$$;

COMMIT;


Clean up the charset and engine on wiki_helpfulvote.

-- Fix the charset and engine of wiki_helpfulvote.

ALTER TABLE `wiki_helpfulvote`
    DROP INDEX `wiki_helpfulvote_2291b592`;

ALTER TABLE `wiki_helpfulvote`
    MODIFY COLUMN `anonymous_id` BLOB,
    MODIFY COLUMN `user_agent` BLOB;

ALTER TABLE `wiki_helpfulvote`
    MODIFY COLUMN `anonymous_id` VARCHAR(40) CHARACTER SET utf8 NOT NULL,
    MODIFY COLUMN `user_agent` VARCHAR(1000) CHARACTER SET utf8 NOT NULL;

ALTER TABLE `wiki_helpfulvote`
    CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

ALTER TABLE `wiki_helpfulvote`
    ENGINE=InnoDB;

ALTER TABLE `wiki_helpfulvote`
    ADD INDEX `wiki_helpfulvote_2291b592` ( `anonymous_id` );


Remove deprecated columns for user indicator

-- These have been moved to oskari_user_indicator_data (category is no longer needed)
ALTER TABLE oskari_user_indicator DROP COLUMN year;
ALTER TABLE oskari_user_indicator DROP COLUMN data;
ALTER TABLE oskari_user_indicator DROP COLUMN category;
ALTER TABLE oskari_user_indicator DROP COLUMN layer_id;


SELECT handle.id, message.text
	FROM message INNER JOIN handle
		ON message.handle_id = handle.ROWID
	WHERE is_from_me=0 AND
	datetime(message.date/1000000000 + strftime("%s", "2001-01-01") ,"unixepoch","localtime") >= datetime('now','-20 seconds', 'localtime');


Delete all previously made agent jobs. Plans change...

USE msdb

-- DELETE ALL PREVIOUSLY MADE AGENT JOBS. PLANS CHANGE...
EXEC msdb.dbo.sp_delete_job @job_name=N'Tabular process AMK Talous', @delete_unused_schedule=1
EXEC msdb.dbo.sp_delete_job @job_name=N'Tabular process ARVO', @delete_unused_schedule=1
EXEC msdb.dbo.sp_delete_job @job_name=N'Tabular process Opintotietopalvelu KV-liikkuvuus', @delete_unused_schedule=1


Allow null values in repo_name so code can be removed

ALTER TABLE comments MODIFY COLUMN repo_name varchar(255) NULL;
ALTER TABLE pull_labels MODIFY COLUMN repo_name varchar(255);
ALTER TABLE pulls MODIFY COLUMN repo_name varchar(255);

ALTER TABLE pulls DROP KEY pulls_repo;
ALTER TABLE pulls ADD KEY pulls_repo (repo);


-- ---------------------------------------------
-- ------------- TestME Empty DB ---------------
-- ---------------------------------------------

CREATE TABLE IF NOT EXISTS `questions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question` varchar(255) NOT NULL,
  `answers` text NOT NULL,
  `dlevel` int(11) NOT NULL,
  `prive` tinyint(1) NOT NULL,
  `uid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nametag` varchar(255) NOT NULL,
  `qid` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `nametag` (`nametag`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` varchar(255) NOT NULL,
  `pass` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `securitycode` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


Store precedure for insert data into bioassay_data_data_category

--  insert data into bioassay_data_data_category
declare
  cursor c_bioassay_data_category is
select chara.NAME name, data.DERIVED_BIOASSAY_DATA_PK_ID id, data.CATEGORY category
from DERIVED_BIOASSAY_DATA data, CHARACTERIZATION chara
where data.CHARACTERIZATION_PK_ID = chara.CHARACTERIZATION_PK_ID
and data.CATEGORY is not null
and data.CATEGORY != 'Image'
and data.CATEGORY != 'Graph'
group by (data.DERIVED_BIOASSAY_DATA_PK_ID, data.CATEGORY, chara.NAME);

 v_data_pk_id number;
 v_category_index number;

begin
	 v_category_index := 0;
	 v_data_pk_id := 0;
	 for i in c_bioassay_data_category loop
	    if i.id != v_data_pk_id then
     	    v_data_pk_id := i.id;
			v_category_index := 0;
		end if;

	    insert into bioassay_data_data_category
		(derived_bioassay_data_pk_id, category_index, category_name, update_date)
		values (v_data_pk_id, v_category_index, i.category, sysdate);

		v_category_index := v_category_index + 1;

	 end loop;
end;


# Add coord_system_id column into the seq_region table
ALTER TABLE seq_region ADD COLUMN coord_system_id INT(10) UNSIGNED NOT NULL;
ALTER TABLE seq_region ADD KEY cs_idx (coord_system_id);
# Update unique key from name_idx to name_cs_idx ?
# Check with Will
#ALTER TABLE seq_region DROP KEY name_idx;
#ALTER TABLE seq_region ADD UNIQUE KEY name_cs_idx(name, coord_system_id);

# Add coord_system table

CREATE TABLE coord_system (

  coord_system_id             INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  species_id                  INT(10) UNSIGNED NOT NULL DEFAULT 1,
  name                        VARCHAR(40) NOT NULL,
  version                     VARCHAR(255) DEFAULT NULL,
  rank                        INT NOT NULL,
  attrib                      SET('default_version', 'sequence_level'),

  PRIMARY   KEY (coord_system_id),
  UNIQUE    KEY rank_idx (rank, species_id),
  UNIQUE    KEY name_idx (name, version, species_id),
            KEY species_idx (species_id)

);

# patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL,'patch', 'patch_65_66_d.sql|Added coord_system table and update seq_region table in include coord_system_id in order to support multiple-species variation databases');


-- Create syntax for TABLE 'article_tags'
CREATE TABLE `article_tags` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `article_id` int(11) unsigned NOT NULL,
  `tag_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Create syntax for TABLE 'articles'
CREATE TABLE `articles` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL,
  `body` text NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- Create syntax for TABLE 'tags'
CREATE TABLE `tags` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Create syntax for TABLE 'users'
CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(256) NOT NULL DEFAULT '',
  `gender` int(1) unsigned DEFAULT NULL,
  `attr1` int(11) DEFAULT NULL,
  `attr2` int(11) DEFAULT NULL,
  `attr3` int(11) DEFAULT NULL,
  `attr4` int(11) DEFAULT NULL,
  `attr5` int(11) DEFAULT NULL,
  `attr6` int(11) DEFAULT NULL,
  `attr7` int(11) DEFAULT NULL,
  `attr8` int(11) DEFAULT NULL,
  `attr9` int(11) DEFAULT NULL,
  `attr10` int(11) DEFAULT NULL,
  `attr11` int(11) DEFAULT NULL,
  `attr12` int(11) DEFAULT NULL,
  `attr13` int(11) DEFAULT NULL,
  `attr14` int(11) DEFAULT NULL,
  `attr15` int(11) DEFAULT NULL,
  `attr16` int(11) DEFAULT NULL,
  `attr17` int(11) DEFAULT NULL,
  `attr18` int(11) DEFAULT NULL,
  `attr19` int(11) DEFAULT NULL,
  `attr20` int(11) DEFAULT NULL,
  `attr21` int(11) DEFAULT NULL,
  `attr22` int(11) DEFAULT NULL,
  `attr23` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


Add view for koulutusluokitus to API schema.

IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'api.koulutusluokitus'))
EXEC dbo.sp_executesql @statement = N'create view api.koulutusluokitus as select 1 as a'
GO

ALTER VIEW api.koulutusluokitus AS

SELECT koulutusluokitus_avain
,koulutusluokitus_koodi
,koulutusluokitus_fi
,koulutusluokitus_sv
,koulutusluokitus_en
,koulutusastetaso1_koodi
,koulutusastetaso1_fi
,koulutusastetaso1_sv
,koulutusastetaso1_en
,koulutusastetaso2_koodi
,koulutusastetaso2_fi
,koulutusastetaso2_sv
,koulutusastetaso2_en
,koulutusalataso1_koodi
,koulutusalataso1_fi
,koulutusalataso1_sv
,koulutusalataso1_en
,koulutusalataso2_koodi
,koulutusalataso2_fi
,koulutusalataso2_sv
,koulutusalataso2_en
,koulutusalataso3_koodi
,koulutusalataso3_fi
,koulutusalataso3_sv
,koulutusalataso3_en
,okmohjauksenala_koodi
,okmohjauksenala_fi
,okmohjauksenala_sv
,okmohjauksenala_en
,koulutussektori_koodi
,koulutussektori_fi
,koulutussektori_sv
,koulutussektori_en
,tutkintotyyppi_koodi
,tutkintotyyppi_fi
,tutkintotyyppi_sv
,tutkintotyyppi_en
FROM dw.d_koulutusluokitus


Add SQL script for associating people with orgs

/*
*
* A script that automatically generates org names from email
* addresses, moves those orgs to their own table, and associates people
* with orgs, instead of their "upeople" ids (as is normal with VizGrimoire).
*
*/

-- create org table
DROP TABLE IF EXISTS geonode_orgs;

CREATE TABLE `geonode_orgs` (`id` int(11) NOT NULL AUTO_INCREMENT,
`organization` varchar(255) UNIQUE,
PRIMARY KEY (`id`));

-- pulls out org from email address and inserts into org table

insert into geonode_orgs (organization) SELECT DISTINCT SUBSTRING_INDEX(email, '@', -1) AS org FROM people;

-- rename people_upeople to safe_people_upeople

RENAME TABLE people_upeople TO safe_people_upeople;

-- create new "people_upeople" aka "people_org"
-- upeople_id here is org_id

CREATE TABLE `people_upeople` (
  `people_id` int(11) NOT NULL,
  `upeople_id` int(11) NOT NULL,
  PRIMARY KEY (`people_id`)
);

-- links person and org in people_upeople table

INSERT INTO people_upeople (people_id, upeople_id) SELECT people.id, geonode_orgs.id FROM people INNER JOIN geonode_orgs ON ( geonode_orgs.organization COLLATE utf8_general_ci = SUBSTRING_INDEX(people.email, '@', -1) );


IF NOT EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='sa' and TABLE_NAME='sa_virta_jtp_julkaisut'
  and COLUMN_NAME='konferenssinNimi'
) BEGIN
ALTER TABLE sa.sa_virta_jtp_julkaisut ADD konferenssinNimi nvarchar(500) NULL
END
IF NOT EXISTS (
  select * from INFORMATION_SCHEMA.COLUMNS
  where TABLE_SCHEMA='sa' and TABLE_NAME='sa_virta_jtp_julkaisut'
  and COLUMN_NAME='pysyvaOsoiteTeksti'
) BEGIN
ALTER TABLE sa.sa_virta_jtp_julkaisut ADD pysyvaOsoiteTeksti nvarchar(500) NULL
END

/* revert
ALTER TABLE sa.sa_virta_jtp_julkaisut DROP COLUMN
  konferenssinNimi,
  pysyvaOsoiteTeksti
--*/


Add database patch for Open Humans token

/*
10 December 2014
---------------
Adds a column for the Open Humans access token to the ag_kit table.
*/
ALTER TABLE ag.ag_kit ADD COLUMN open_humans_token varchar(64);

COMMENT ON COLUMN ag.ag_kit.open_humans_token IS 'The Open Humans access token corresponding to the Open Humans user that has authorized data sharing with this kit.';


Update maintenance tables for IACUC protocol types

UPDATE IACUC_PROTOCOL_TYPE SET PROTOCOL_TYPE_CODE=444 WHERE DESCRIPTION='Teaching'
/
UPDATE IACUC_PROTOCOL_TYPE SET PROTOCOL_TYPE_CODE=5 WHERE DESCRIPTION='Testing'
/
UPDATE IACUC_PROTOCOL_TYPE SET PROTOCOL_TYPE_CODE=4 WHERE DESCRIPTION='Teaching'
/


Add SQL script to get the size of all tables in a database

SELECT table_name AS "Table Name",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size in (MB)"
FROM information_schema.TABLES
WHERE table_schema = "ENTER_YOUR_DATABASE_NAME_HERE"
ORDER BY (data_length + index_length) DESC;


SELECT from dataversefacet showing displayorder per dataverse

select dataversefacet.id,title,name,datasetfield.id,dataversefacet.displayorder,dataverse_id from dataversefacet, datasetfield where dataversefacet.datasetfield_id = datasetfield.id order by displayorder;


Add DB Revision 12: Include Roaming Flag and Equipment Description

BEGIN;

ALTER TABLE subscribers RENAME TO sub;
create table subscribers (
        id              serial primary key,
        msisdn          varchar,
        name            varchar,
        authorized      smallint not null default 0,
        balance         decimal not null default 0.00,
        subscription_status     smallint not null default 0,
        subscription_date       timestamp default current_timestamp,
	location	varchar,
	roaming		smallint not null default 0,
	equipment	varchar,
        created         timestamp default current_timestamp
);
INSERT INTO subscribers(id,msisdn,name,authorized,balance,subscription_status,subscription_date,location,created) SELECT id,msisdn,name,authorized,balance,subscription_status,subscription_date,location,created FROM sub;
DROP TABLE sub;
UPDATE meta SET value='12' WHERE key='db_revision';

COMMIT;


-- Add column to allow more active feeds to be downloaded more frequently.
ALTER TABLE feeds ADD COLUMN last_new_story_time timestamp without time zone;
UPDATE feeds SET last_new_story_time = greatest( last_download_time, last_new_story_time );
ALTER TABLE feeds ALTER COLUMN last_download_time TYPE timestamp with time zone;
ALTER TABLE feeds ALTER COLUMN last_new_story_time TYPE timestamp with time zone;


Add tag-merging step for lines and stations

-- assign tags to stations missing them
begin;
drop table if exists osm_tags;
create table osm_tags (
    osm_id varchar(64),
    tags   hstore,
    primary key (osm_id)
);
create table merged_tags (
    osm_id varchar(64),
    tags   hstore,
    primary key (osm_id)
);

drop function if exists merge_power_tags(a hstore array);
create function merge_power_tags (a hstore array) returns hstore as $$
declare
    r hstore;
    t hstore;
    k text;
begin
    r = hstore('_merged', 'yes');
    for t in select unnest(a) loop
        for k in select skeys(t) loop
            if not r ? k then
                r = r || hstore(k, t->k);
            elsif (r->k) != (t->k) and k in ('voltage', 'wires', 'cables', 'frequency') then
                -- assume we can't fix this now, so join them separated by semicolons
                r = r || hstore(k, (r->k) || ';' || (t->k));
            end if;
        end loop;
    end loop;
    return r;
end
$$ language plpgsql;

insert into osm_tags (osm_id, tags)
    select concat('n', id), hstore(tags) from planet_osm_nodes;
insert into osm_tags (osm_id, tags)
    select concat('w', id), hstore(tags) from planet_osm_ways;

update power_line l set tags = t.tags
    from osm_tags t
    where l.tags is null and array_length(l.objects, 1) = 1
        and t.osm_id = l.objects[1];

insert into merged_tags (osm_id, tags)
    select l.osm_id, merge_power_tags(array_agg(t.tags))
        from power_line l join osm_tags t on t.osm_id = any(l.objects)
        where l.tags is null group by l.osm_id;

insert into merged_tags (osm_id, tags)
    select s.osm_id, merge_power_tags(array_agg(t.tags))
        from power_station s join osm_tags t on t.osm_id = any(s.objects)
        where s.tags is null group by s.osm_id;

update power_line l set tags = m.tags
       from merged_tags m where l.tags is null and m.osm_id = l.osm_id;

update power_station s set tags = m.tags
       from merged_tags m where s.tags is null and m.osm_id = s.osm_id;

commit;


-- --------------------------------------------------------------
-- fill_quizz.sql
-- Used to fill 'question' table
-- Use the following form for your insert clauses:
-- INSERT question (q_quest, q_ans_a, q_ans_b, q_ans_c, q_ans_d, q_right_ans) VALUES ('<question>', '<answer a>', '<answer b>', '<answer c>', '<answer d>', '<list of right answers>');
-- IMPORTANT:
--  - Do not forget to escape simple quotes in strings ;
--  - for the right answers, list letters without spaces (example: 'ab', or 'acd')
--  - Each time I'll execute the script, I'll comment insert clauses runed
-- And very important : sorry for my bad english :o)
-- --------------------------------------------------------------


Add Database script for regimen summary report

DROP VIEW IF EXISTS  vw_regimen_summary;

CREATE OR REPLACE VIEW vw_regimen_summary AS
 SELECT DISTINCT r.programid, ps.id AS scheduleid, pp.id AS periodid, rg.id AS rgroupid, li.regimencategory, regimens.categoryid, regimens.id AS regimenid, regimens.name AS regimen, li.patientsontreatment, li.patientstoinitiatetreatment, li.patientsstoppedtreatment, rg.name AS rgroup, r.status
   FROM regimen_line_items li
   JOIN requisitions r ON r.id = li.rnrid
   JOIN processing_periods pp ON pp.id = r.periodid
   JOIN processing_schedules ps ON ps.id = pp.scheduleid
   JOIN requisition_group_members ON requisition_group_members.facilityid = r.facilityid
   JOIN requisition_groups rg ON rg.id = requisition_group_members.requisitiongroupid
   JOIN regimens ON regimens.programid = r.programid;

ALTER TABLE vw_regimen_summary
  OWNER TO postgres;


-- update layer name
UPDATE oskari_statistical_datasource set config='{
  "url" : "https://khr.maanmittauslaitos.fi/tilastopalvelu/rest",
  "info" : {
    "url" : "https://khr.maanmittauslaitos.fi/"
  },
  "hints" : {
    "dimensions" : [ {
      "id" : "year",
      "sort" : "DESC"
    }]
  }
}' where locale like '%KHR%';


Add DB patch to add cache columns for event talk/comment/tracks.

-- Add cache count columns for talks and tracks

INSERT INTO patch_history SET patch_number = 46;

ALTER TABLE events ADD comment_count INT(10) DEFAULT 0, ADD talk_count INT(10) DEFAULT 0, ADD track_count INT(10) DEFAULT 0;

UPDATE events e SET comment_count = (SELECT COUNT(*) FROM event_comments ec WHERE ec.event_id = e.ID);
UPDATE events e SET talk_count = (SELECT COUNT(*) FROM talks t WHERE t.event_id = e.ID);
UPDATE events e SET track_count = (SELECT COUNT(*) FROM event_track et WHERE et.event_id = e.ID);


CREATE TABLE IF NOT EXISTS `users` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
	`name` varchar(255) NOT NULL,
	`role` enum('employee', 'manager') NOT NULL DEFAULT 'employee',
	`email` varchar(255) DEFAULT NULL,
	`phone` varchar(255) DEFAULT NULL,
	PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `shifts` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
	`manager_id` int(11) NOT NULL,
	`employee_id` int(11) DEFAULT NULL,
	`break` double DEFAULT NULL,
	`start_time` timestamp NOT NULL,
	`end_time` timestamp NOT NULL,
	PRIMARY KEY (`id`),
	FOREIGN KEY (`manager_id`) REFERENCES users(`id`),
	FOREIGN KEY (`employee_id`) REFERENCES users(`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


Add SQL to insert a reading or ignore it if any data is already in the database

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

INSERT INTO readings (meter_id, reading, start_timestamp, end_timestamp)
VALUES (${meterID}, ${reading}, ${startTimestamp}, ${endTimestamp})
ON CONFLICT DO NOTHING;


/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
-- Allows us to use gist in this database
CREATE EXTENSION IF NOT EXISTS btree_gist;
-- create baseline table
CREATE TABLE baseline (
	meter_id INT NOT NULL REFERENCES meters (id),
	apply_range tsrange NOT NULL,
	calc_range tsrange NOT NULL,
	baseline_value DOUBLE PRECISION NOT NULL,
	PRIMARY KEY (meter_id, apply_range),
	EXCLUDE USING GIST (
		meter_id WITH =,
		apply_range WITH &&
	)
);


-- https://stackoverflow.com/a/42508688/2298061

IF OBJECT_ID('tempdb..#AllTables','U') IS NOT NULL DROP TABLE #AllTables;

CREATE TABLE #AllTables(
     DBName            SYSNAME
   , TableCount        INT
   , ViewCount         INT
   , ProcedureCount    INT
   , TriggerCount      INT
   , FullTextCatalog   INT
   , XmlIndexes        INT
   , SpatialIndexes    INT
   , TableColumnsCount INT
);

DECLARE @SQL NVARCHAR(MAX);

SELECT @SQL = COALESCE(@SQL, N'') + N'USE ' + quotename(name) + N';
    INSERT INTO #AllTables
    SELECT N' + QUOTENAME(name,'''') + N' AS DBName,
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.tables),
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.views),
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.procedures),
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.triggers),
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.fulltext_catalogs),
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.xml_indexes),
    (SELECT COUNT(*) FROM ' + QUOTENAME(name) + N'.sys.spatial_indexes),
    (SELECT SUM(max_column_id_used) FROM ' + QUOTENAME(name) + N'.sys.tables);
'
FROM sys.databases
ORDER BY name;

EXECUTE sp_executesql @SQL;

SELECT DBName
     , TableCount
     , ViewCount
     , ProcedureCount
     , TriggerCount
     , FullTextCatalog
     , XmlIndexes
     , SpatialIndexes
     , TableColumnsCount
  FROM #AllTables;


Implement Protocol Review Rescheduled notification

UPDATE NOTIFICATION_TYPE SET MESSAGE='The IACUC protocol number {PROTOCOL_NUMBER}, Principal Investigator {PI_NAME} has been rescheduled to {DATE}.
The action was performed by {USER_FULLNAME}.  Additional information and further actions can be accessed through the Kuali Coeus system.'
       WHERE MODULE_CODE='9' AND ACTION_CODE='202'
/


Patch x which fixes missing autoincrements

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

/**
@header patch_92_93_x.sql - Fix ids, add missing autoincrement
@desc   Fix ids, add missing autoincrement
*/

alter table peak modify peak_id int(10) unsigned NOT NULL AUTO_INCREMENT;
alter table alignment_qc_flagstats modify alignment_qc_flagstats_id int(28) unsigned NOT NULL AUTO_INCREMENT;

-- patch identifier
INSERT INTO meta (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_92_93_x.sql|Fix ids, add missing autoincrement');


Drop deprecated columns from wfs-search db table

ALTER TABLE oskari_wfs_search_channels DROP COLUMN topic;
ALTER TABLE oskari_wfs_search_channels DROP COLUMN description;
ALTER TABLE oskari_wfs_search_channels DROP COLUMN is_address;


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE `rewards` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `user_id` INT(11) NOT NULL,
  `reward` INT(11) NOT NULL COMMENT 'reward in 1 Satonish',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `rewards`
ADD INDEX (`user_id`),
ADD INDEX (`created_at`);

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE `rewards`;