metadata :name        => 'File Staging Agent',
         :description => 'Agent to manage the staging of files to a node over a network connection',
         :author      => 'Thomas Linkin',
         :license     => 'Apache 2.0',
         :version     => '0.1',
         :url         => '',
         :timeout     => 120

action "stage", :description => 'Stage a file from a source to a destination' do

  input :source,
        :promt       => 'File Source',
        :description => 'Remote location to stage file from',
        :optional     => false,
        :type        => :string,
        :validation  => :stage_src,
        :maxlength   => 0

  input :dest,
        :promt       => 'File Destination',
        :description => 'Destination on the end system to store file',
        :optional     => false,
        :type        => :string,
        :validation  => :absolute_path,
        :maxlength   => 0

  input :force,
        :promtp      => 'Force Overwrite',
        :description => 'Force the overwrite of file already at the destination',
        :type        => :bool,
        :default     => false,
        :optional     => false

  output :summary,
         :description => "Initial status of the staging operation",
         :display_as  => "Operation Status",
         :default     => "unknown status"
end

action "status", :description => 'Display status of running stage operations' do

  input :dest,
        :promt       => 'Operation Destination',
        :description => 'Destination of operation to obtain status of',
        :optional     => true,
        :type        => :string,
        :validation  => :absolute_path,
        :maxlength   => 0

  output :status,
         :description => "Status of currently running operation(s)",
         :display_as  => "Currently Running Operation Status(es)"
end


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

Add some indexes on repository and host_category

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

-- Add some indexes
CREATE INDEX t_repository_category_id_idx ON repository (category_id);
CREATE INDEX t_repositoryversion_id_idx ON repository (version_id);
CREATE INDEX t_repositoryarch_id_idx ON repository (arch_id);
CREATE INDEX t_repositorydirectory_id_idx ON repository (directory_id);

CREATE INDEX t_hostcategory_host_id_idx ON host_category (host_id);
CREATE INDEX t_hostcategory_category_id_idx ON host_category (category_id);


Add script to create an index that will help us recover file downloads.

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


/*
Author: Chris Yates
Original link: http://www.sqlservercentral.com/blogs/the-sql-professor/2017/01/26/backuprestorewhats-my-status/
*/

--By checking only for the restore and backup command lines you will be able to quickly identify your session id
--and get an  approximate ETA and percentage complete. you can tinker of course with the estimations
--if you’d like or pull back more fields. This is just a simple technique in utilizing a helpful DMV to provide info quickly.
SELECT  r.session_id
      , r.command
      , r.start_time
      , r.status
      , CONVERT(NUMERIC(6, 2), r.percent_complete) AS [Percent Complete]
      , CONVERT(VARCHAR(20), DATEADD(ms, r.estimated_completion_time , GETDATE()), 20) AS [ETA Completion Time]
      , CONVERT(NUMERIC(10, 2), r.total_elapsed_time / 1000.0 / 60.0) AS [Elapsed Min]
      , CONVERT(NUMERIC(10, 2), r.total_elapsed_time / 1000.0 / 60.0 / 60.0) AS [Elapsed Hours]
      , CONVERT(NUMERIC(10, 2), r.estimated_completion_time / 1000.0 / 60.0) AS [ETA Min]
      , CONVERT(NUMERIC(10, 2), r.estimated_completion_time / 1000.0 / 60.0/ 60.0) AS [ETA Hours]
      , CONVERT(VARCHAR(1000), (
                 SELECT SUBSTRING(TEXT, r.statement_start_offset / 2, CASE
                             WHEN r.statement_end_offset = -1
                                 THEN 1000
                             ELSE (r.statement_end_offset - r.statement_start_offset) / 2
                             END)
                 FROM   sys.dm_exec_sql_text(sql_handle)
                 )) AS TSQLStatement
FROM sys.dm_exec_requests r
WHERE command IN (
        'RESTORE DATABASE'
        , 'BACKUP DATABASE'
        );


-- +goose Up
ALTER TABLE users ALTER COLUMN created TYPE timestamp;
ALTER TABLE users ALTER COLUMN updated TYPE timestamp;
ALTER TABLE schools ALTER COLUMN created TYPE timestamp;
ALTER TABLE schools ALTER COLUMN updated TYPE timestamp;

-- +goose Down
ALTER TABLE users ALTER COLUMN created TYPE date;
ALTER TABLE users ALTER COLUMN updated TYPE date;
ALTER TABLE schools ALTER COLUMN created TYPE date;
ALTER TABLE schools ALTER COLUMN updated TYPE date;


Add a missing migration for transaction actors

UPDATE {$NAMESPACE}_differential.differential_transaction
  SET authorPHID = 'PHID-APPS-PhabricatorHeraldApplication'
  WHERE authorPHID = 'PHID-APPS-PhabricatorApplicationHerald';
UPDATE {$NAMESPACE}_maniphest.maniphest_transaction
  SET authorPHID = 'PHID-APPS-PhabricatorHeraldApplication'
  WHERE authorPHID = 'PHID-APPS-PhabricatorApplicationHerald';
UPDATE {$NAMESPACE}_pholio.pholio_transaction
  SET authorPHID = 'PHID-APPS-PhabricatorHeraldApplication'
  WHERE authorPHID = 'PHID-APPS-PhabricatorApplicationHerald';

UPDATE {$NAMESPACE}_differential.differential_transaction
  SET authorPHID = 'PHID-APPS-PhabricatorHarbormasterApplication'
  WHERE authorPHID = 'PHID-APPS-PhabricatorApplicationHarbormaster';


-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
BEGIN;
UPDATE `m_country` SET name_ja='台湾（台湾省/中華民国）' WHERE id = 158;
COMMIT;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
BEGIN;
UPDATE `m_country` SET name_ja='台湾' WHERE id = 158;
COMMIT;


CREATE INDEX ix_search_accessions__urs_taxid ON search_export_accessions(urs_taxid);
CREATE INDEX ix_search_accessions__precompute_id ON search_export_accessions(search_export_id);

ALTER TABLE search_export_accessions
  ADD CONSTRAINT fk_search_accessions__search_id FOREIGN KEY (search_export_id) REFERENCES search_export_urs(id)
;


-- PG12_DEPRECATED
-- Create geomval if it doesn't exist (in postgis 3+ it only exists in postgis_raster)
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'geomval') THEN
        CREATE TYPE geomval AS (
            geom geometry,
            val double precision
        );
    END IF;
END$$;


Add migration script to create district demographic estimates table

CREATE TABLE district_demographic_estimates
(
  id                SERIAL  PRIMARY KEY,
  year              INT NOT NULL,
  districtId        INT NOT NULL REFERENCES geographic_zones(id),
  demographicEstimateId INT NOT NULL REFERENCES demographic_estimate_categories(id),
  conversionFactor  DECIMAL NULL,
  value             INT NOT NULL DEFAULT (0),

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


ALTER TABLE `index_data` ADD `hidden` ENUM( '0', '1' ) NOT NULL DEFAULT '0' AFTER `special` , ADD `locked` ENUM( '0', '1' ) NOT NULL DEFAULT '0' AFTER `hidden` ;


Migrate db add user_id index to auth_tokens

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE `auth_tokens`
ADD INDEX (`user_id`);

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP INDEX `user_id` ON `auth_tokens`;


BEGIN;
-- Plan count should match the number of tests. If it does not then pg_prove will fail the test
SELECT plan(1);

-- Run the tests.
-- Given
TRUNCATE TABLE item;
CALL new_item('foo', 0, 0);

-- When
CALL update_quality();

-- Then
SELECT is( name, 'fixme', 'name did change' ) FROM item;

-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;


Add SQL constraint to ensure unicity of emails

DO $$
  BEGIN
    BEGIN
      CREATE UNIQUE INDEX sys_user_unique_email ON sys_user(email) WHERE email != '';
    END;
    BEGIN
      CREATE UNIQUE INDEX cd_profiles_unique_email ON cd_profiles(email) WHERE email != '';
    END;
  END;
$$


Set the format of the crontab file and gave some examples to show how useful it will be

# Islamic crontab

# gm:	Gregorian Month (1 to 12)
# gd:	Gregorian Day of Month (1 to 31)
# hm:	Hijri Month (1 to 12)
# hd:	Hijri day of month (1 to 30)
# dow: 	day of week (0 - 6 or 1 - 7) (Sunday = 0 or 7)
# pr: 	Prayer (0 = Fajr, 1 = Shrooq, 2 = Dhuhr, 3 = Asr, 4 = Maghrib, 5 = Isha)
# m: 	Minutes offset from prayer (from -1000 to 1000)
# cmd:	Command (any shell command or shell script)

# Format (tab or space separated values)
# gm   gd   hm   hd   dow   pr          m   cmd

# Play Athan
# *    *    *    *    *     0,2,3,4,5   0    totem /opt/islam/athan.mp3

# Iqama for Fajr, Dhuhr, Asr and Isha is after 20 minutes
# *    *    *    *    *     0,2,3,5     20   totem /opt/islam/iqama.mp3

# Iqama for Maghrib is after 15 minutes
# *    *    *    *    *     4   	    15   totem /opt/islam/iqama.mp3

# Monday and Thursday Fasting (remind me Sunday and Wednesday before Isha by
# 10 minutes)
# *    *    *    *    0,3   5   	   -10   /opt/reminder_for_fasting.sh

# Prepare Early for Jom'a prayer (before Athan by 2 hours)
# *    *    *    *    5     2   	   -120   /opt/reminder_for_fasting.sh

# Set status as busy/away on Skype during Jom'a prayer (before Athan by 2 hours)
# *    *    *    *    5     2   	   -30   /opt/set_busy_for.sh 90

# Fast the 3 white days (reminder 12,13,14 of the hijri month before Isha by 8
# minutes all months except Ramadan and ThulHija)
# *    *    1,2,3,4,5,6,7,8,10,11    12,13,14    *     5   	   -8   /opt/reminder_for_fasting.sh

# Qyam at night before Fajr by 2 Hours
# *    *    *    *    *     0   	   -120   /opt/alarm_wakeup.sh

# Zakah every Hijri year
# *    *    9    25   *     *           0     /opt/remember_zakah.sh


Change the smallest varchar() fields to use text type instead of an arbitrary limit. The remaining varchars should probably be changed to text as well, but these are the most obvious for now.

# --- !Ups

ALTER TABLE ENTRY ALTER num TYPE text;
ALTER TABLE section ALTER num TYPE text;
ALTER TABLE chapter ALTER num TYPE text;

ALTER TABLE manual_change ALTER old_num TYPE text;
ALTER TABLE manual_change ALTER new_num TYPE text;

ALTER TABLE person ALTER gender TYPE text;
ALTER TABLE person ALTER grade TYPE text;

# --- !Downs

ALTER TABLE ENTRY ALTER num TYPE VARCHAR(8);
ALTER TABLE section ALTER num TYPE VARCHAR(8);
ALTER TABLE chapter ALTER num TYPE VARCHAR(8);
ALTER TABLE manual_change ALTER old_num TYPE VARCHAR(8);
ALTER TABLE manual_change ALTER new_num TYPE VARCHAR(8);

ALTER TABLE person ALTER gender TYPE VARCHAR(10);
ALTER TABLE person ALTER grade TYPE VARCHAR(8);


Include script to generate coverage. Should be in migrations or something, but currently we can't do the simplification etc. in a migration. Also, it takes like forever to run.

/*
    Terrängkartan and Vägkartan has overlapping data for most of Sweden,
    but Terrängkartan is missing data for parts of northern Sweden (this area
    is covered by Fjällkartan, which we do not have access to).

    Since Terrängkartan is preferable, at least for roads, but doesn't have
    100% coverage, we want to cut out the parts of Vägkartan which Terrängkartan
    does not cover.

    To do this, we generate the coverage area for Terrängkartan. Note that this
    is a computationally expensive operation (at least as it is now).
*/

with a as
    (select (st_dump(st_buffer(st_geomfromtext(_union.geom), 0.5))).geom as g
        from (select st_union(st_envelope(the_geom)) as geom from terrang_my) as _union)
select st_astext(st_union(st_envelope(g))) from a;

/*
    The resulting geometry can be simplified since it is overly detailed
    (for our purposes) on the border between Sweden and Norway. This can be
    done with ogr2ogr:

    ogr2ogr -simplify 100 [...]

    See terrang_coverage.wkt for the resulting geometry

*/


Fix campaign_id length on osdial_user_log.

# 2010/03/01

ALTER TABLE osdial_user_log MODIFY campaign_id VARCHAR(20) NOT NULL;##|##
 ##    Field length or NULL correction.;

UPDATE system_settings SET version='2.2.1.046',last_update_check=DATE_SUB(NOW(), INTERVAL 1 DAY);##|##
 ##    Updating database to version 2.2.1.046 and clearing last_update_check flag.;


Add SA table for VIRTA-JTP/Latausraportit/Duplikaatit

IF NOT EXISTS (
  select * from INFORMATION_SCHEMA.TABLES
  where TABLE_SCHEMA='sa' and TABLE_NAME='sa_virta_jtp_latausraportit_duplikaatit'
) BEGIN

CREATE TABLE sa.sa_virta_jtp_latausraportit_duplikaatit(
  id bigint IDENTITY(1,1) NOT NULL,

  duplikaattiID int null,
  organisaatiotunnus nvarchar(50) null,
  organisaationimi nvarchar(max) null,
  kuvaus nvarchar(max) null,
  julkaisunorgtunnus nvarchar(50) null,
  duplikaattijulkaisunorgtunnus nvarchar(50) null,
  julkaisunnimi nvarchar(max) null,
  duplikaattijulkaisunnimi nvarchar(max) null,
  julkaisutyyppikoodi varchar(4) null,
  tila int null,
  tarkistusID int null,
  luontipaivamaara varchar(50) null,
  ilmoitusvuosi int null,
  julkaisuvuosi int null,

  loadtime datetime2(4) NOT NULL,
  source nvarchar(255) NULL,
  username nvarchar(128) NOT NULL,
  CONSTRAINT PK__sa_virta_jtp_latausraportit_duplikaatit PRIMARY KEY CLUSTERED (id ASC)
)
;

ALTER TABLE sa.sa_virta_jtp_latausraportit_duplikaatit ADD  CONSTRAINT DF__sa_virta_jtp_latausraportit_duplikaatit__loadtime  DEFAULT (getdate()) FOR loadtime
;
ALTER TABLE sa.sa_virta_jtp_latausraportit_duplikaatit ADD  CONSTRAINT DF__sa_virta_jtp_latausraportit_duplikaatit__username  DEFAULT (suser_name()) FOR username
;

END


INSERT INTO users values (-1, '$2a$10$WNHPWraFAOfbPA.goukqjujAfk1rr7Gc7.tm5y56NnbzPSHf1Bj.S', 'admin', 'admin', 'a@a.com', 1, '2017-01-10');
INSERT INTO users values (-2, '$2a$10$WNHPWraFAOfbPA.goukqjujAfk1rr7Gc7.tm5y56NnbzPSHf1Bj.S', 'user', 'admin', 'user@default.com', 1, '2017-01-10');


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


Add SQL script to get the size of all databases

SELECT table_schema AS "Database Name",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size in (MB)"
FROM information_schema.TABLES
GROUP BY table_schema;


Fix Midsummer Celebrants in Tirisfal Glades and add Festival Scorchling

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

UPDATE `creature` SET `modelid` = 16444, `orientation` = 1.2727 WHERE `guid` = 202761;
UPDATE `creature` SET `modelid` = 16432, `orientation` = 4.35146 WHERE `guid` = 202762;
UPDATE `creature` SET `modelid` = 16438 WHERE `guid` = 202759;
UPDATE `creature` SET `modelid` = 16445 WHERE `guid` = 202760;

DELETE FROM `creature` WHERE `guid` = 86895;
INSERT INTO `creature`
(`guid`,`id`,`map`,`spawnMask`,`phaseMask`,`modelid`,`equipment_id`,`position_x`,`position_y`,`position_z`,`orientation`,`spawntimesecs`,`spawndist`,`currentwaypoint`,`curhealth`,`curmana`,`MovementType`,`npcflag`,`unit_flags`,`dynamicflags`)
VALUES
(86895,26520,0,1,1,8409,0,2281.25,428.862,33.9647,3.54294,300,0,0,42,0,0,0,0,0);


Fix the flyway issue due to conflicting versions

-- Copyright 2017 Hortonworks.
--
-- 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.

-- Change column type from TEXT to MEDIUMTEXT

ALTER TABLE `topology_component_bundle` MODIFY `topologyComponentUISpec` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_stream` MODIFY `fieldsData` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_component` MODIFY `configData` MEDIUMTEXT;
ALTER TABLE `topology_source` MODIFY `configData` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_sink` MODIFY `configData` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_processor` MODIFY `configData` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_edge` MODIFY `streamGroupingsData` MEDIUMTEXT NOT NULL;
ALTER TABLE `service_configuration` MODIFY `configuration` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_editor_toolbar` MODIFY `data` MEDIUMTEXT NOT NULL;
ALTER TABLE `service_bundle` MODIFY `serviceUISpecification` MEDIUMTEXT NOT NULL;
ALTER TABLE `ml_model` MODIFY `pmml` MEDIUMTEXT NOT NULL;
ALTER TABLE `dashboard` MODIFY `data` MEDIUMTEXT NOT NULL;
ALTER TABLE `widget` MODIFY `data` MEDIUMTEXT NOT NULL;
ALTER TABLE `datasource` MODIFY `data` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology` MODIFY `config` MEDIUMTEXT NOT NULL;
ALTER TABLE `topology_editor_metadata` MODIFY `data` MEDIUMTEXT NOT NULL;

ALTER TABLE `topology_rule`
MODIFY `streams` MEDIUMTEXT NULL,
MODIFY `outputStreams` MEDIUMTEXT NULL,
MODIFY `condition` MEDIUMTEXT NULL,
MODIFY `sql` MEDIUMTEXT NULL,
MODIFY `parsedRuleStr` MEDIUMTEXT NOT NULL,
MODIFY `projections` MEDIUMTEXT NOT NULL,
MODIFY `window` MEDIUMTEXT NOT NULL,
MODIFY `actions` MEDIUMTEXT NOT NULL;

ALTER TABLE `topology_branchrule`
MODIFY `stream` MEDIUMTEXT NOT NULL,
MODIFY `outputStreams` MEDIUMTEXT NULL,
MODIFY `condition` MEDIUMTEXT NOT NULL,
MODIFY `parsedRuleStr` MEDIUMTEXT NOT NULL,
MODIFY `actions` MEDIUMTEXT NOT NULL;

ALTER TABLE `topology_window`
MODIFY `streams` MEDIUMTEXT NULL,
MODIFY `outputStreams` MEDIUMTEXT NULL,
MODIFY `condition` MEDIUMTEXT NULL,
MODIFY `parsedRuleStr` MEDIUMTEXT NOT NULL,
MODIFY `window` MEDIUMTEXT NOT NULL,
MODIFY `actions` MEDIUMTEXT NOT NULL,
MODIFY `projections` MEDIUMTEXT NULL,
MODIFY `groupbykeys` MEDIUMTEXT NULL;

ALTER TABLE `notifier`
MODIFY `properties` MEDIUMTEXT,
MODIFY `fieldValues` MEDIUMTEXT;


alter table part add primary key (p_partkey);
alter table supplier add primary key (s_suppkey);
alter table partsupp add primary key (ps_partkey, ps_suppkey);
alter table customer add primary key (c_custkey);
alter table orders add primary key (o_orderkey);
alter table lineitem add primary key (l_orderkey, l_linenumber);
alter table nation add primary key (n_nationkey);
alter table region add primary key (r_regionkey);


INSERT INTO customer (id,civilite,lastname,firstname,birthday,email,telephone,address,city,country,zipcode) VALUES (1,'Mr','Lagaffe','Gaston','1956-10-02','toto@franquin.com','0163458950','40 rue des champs','paris','france','75000');

INSERT INTO account (id,description,balance,typeCarte,numberCard,crytogram,MONTANT_OPDEBIT,dateExpiredCarte,dateCreationAccount,customerid) VALUES (1,'compte ou il y a de argent','10000','CarteBleue','123456789','123','7','2017-12-01','2014-10-01',1);

INSERT INTO operation (id,label,amount,accountid) VALUES (1,'achat d un ordinateur','500',1);


CREATE TABLE Parking2014 (
	ParkingEventId INTEGER,
	DeviceId INTEGER,
	ArrivalTime TEXT, -- from source
	ArrivalTimeSeconds INTEGER, -- Seconds since midnight
	ArrivalDayOfWeek INTEGER, -- 1-7, 1 is Monday
	ArrivalDayOfMonth INTEGER, -- 1-31
	ArrivalMonth INTEGER, -- 1-12
	ArrivalYear INTEGER,
	DepartureTime TEXT, -- from source
	DepartureTimeSeconds INTEGER, -- Seconds since midnight
	DepartureDayOfWeek INTEGER, -- 1-7, 1 is Monday
	DepartureDayOfMonth INTEGER, -- 1-31
	DepartureMonth INTEGER, -- 1-12
	DepartureYear INTEGER,
	Duration INTEGER,
	PermittedDuraction INTEGER,
	StreetMarker TEXT,
	SignPlateId INTEGER,
	Sign TEXT,
	Area INTEGER,
	AreaName TEXT,
	StreetId INTEGER,
	StreetName TEXT,
	BetweenStreet1Id INTEGER,
	BetweenStreet1Description TEXT,
	BetweenStreet2Id INTEGER,
	BetweenStreet2Description TEXT,
	SideOfStreet INTEGER,
	SideCode TEXT,
	SideName TEXT,
	BayID INTEGER,
	InViolation INTEGER
	--PRIMARY KEY (ParkingEventId, DeviceId, StreetId, BayID)
);


Add BigBlueButton join & left room actions into ActionLog

INSERT IGNORE INTO tiki_actionlog_conf(action, `objectType`, status) VALUES ('Joined Room', 'bigbluebutton', 'n');
INSERT IGNORE INTO tiki_actionlog_conf(action, `objectType`, status) VALUES ('Left Room', 'bigbluebutton', 'n');


Add migration to add an exempt pairing board to every project

INSERT INTO pairing_board (project_id, name, is_exempt)
(SELECT DISTINCT project_id, 'OUT OF OFFICE', TRUE FROM pairing_board
    WHERE project_id NOT IN
    (SELECT project_id FROM pairing_board
        WHERE is_exempt = TRUE));


Add sql script to dump the size of database tables.

SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_relation_size(C.oid)) AS "non_index_size",   pg_size_pretty(pg_total_relation_size(C.oid)) AS "index_size"
  FROM pg_class C
  LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
  WHERE nspname NOT IN ('pg_catalog', 'information_schema')
    AND C.relkind <> 'i'
    AND nspname !~ '^pg_toast'
  ORDER BY pg_relation_size(C.oid) DESC
  LIMIT 40;


Create view to add average to maandverbruik

DROP VIEW APP.MAANDVERBRUIK_EXTRA;

CREATE VIEW APP.MAANDVERBRUIK_EXTRA (
	  ID
	, JAAR
	, MAAND
	, ID_METERSOORT
	, VERBRUIK
	, AVERAGE
	, DELTA
) AS (
	SELECT
 	  m.ID
	, m.JAAR
	, m.MAAND
	, m.ID_METERSOORT
	, m.VERBRUIK
	, av.average
	, m.VERBRUIK-av.AVERAGE AS delta
	FROM APP.MAANDVERBRUIK M
	LEFT JOIN APP.MAANDVERBRUIK_AVG av
	ON m.ID_METERSOORT = av.ID_METERSOORT
	AND m.MAAND = av.MAAND
	ORDER BY JAAR, MAAND, ID_METERSOORT
);


Fix campaign_id length on osdial_agent_log.

# 2010/03/01

ALTER TABLE osdial_agent_log MODIFY campaign_id VARCHAR(20);##|##
 ##    Field length or NULL correction. This may take a while.;

UPDATE system_settings SET version='2.2.1.045',last_update_check=DATE_SUB(NOW(), INTERVAL 1 DAY);##|##
 ##    Updating database to version 2.2.1.045 and clearing last_update_check flag.;


Change mapstats path on mapfull-template

UPDATE portti_bundle SET startup='{
    "bundlename" : "mapfull"
    "metadata" : {
        "Import-Bundle" : {
            "core-base" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "core-map" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "sandbox-base" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "sandbox-map" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "event-base" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "event-map" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "event-map-layer" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "request-base" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "request-map" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "request-map-layer" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "service-base" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "service-map" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "domain" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "mapmodule-plugin" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "mapwfs2" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "mapwmts" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "mapstats" : {
                "bundlePath" : "/Oskari/packages/mapping/ol2/"
            },
            "mapuserlayers" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "mapanalysis" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
             "mapmyplaces" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "oskariui" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "mapfull" : {
                "bundlePath" : "/Oskari/packages/framework/bundle/"
            },
            "ui-components": {
                "bundlePath": "/Oskari/packages/framework/bundle/"
            }
        }
    }
}' where name = 'mapfull';


Save example query used to compute de-duplication rate

-- Queries use to calculate deduplicaiton rate for two partners
declare @distinct_patids_UFH int
declare @distinct_uuids_UFH int
declare @linked_patients_UFH_FLM int
declare @linked_percentage_UFH float

set @distinct_patids_UFH=(select count(distinct linkage_patid) from linkage where partner_code = 'UFH')
set @distinct_uuids_UFH=(select count(distinct linkage_uuid) from linkage where partner_code = 'UFH')
set @linked_patients_UFH_FLM=(select count(*) from (select linkage_uuid, count(distinct partner_code) as cc from linkage where partner_code in ('UFH', 'FLM') group by linkage_uuid having count(distinct partner_code) > 1) as c)
set @linked_percentage_UFH = (@linked_patients_UFH_FLM * 100.0)/@distinct_uuids_UFH

print CONCAT('Distinct UFH patients: ', CAST(@distinct_patids_UFH as varchar))
print CONCAT('Linked UFH patients: ', CAST(@linked_patients_UFH_FLM as varchar(100)))
print CONCAT('Distinct UFH UUIDs: ', CAST(@distinct_uuids_UFH as varchar(100)))
print CONCAT('Percentage of linked UFH patients: ', CAST(@linked_percentage_UFH  as varchar(100)))


Create : 181 Employees Earning More Than Their Managers

SELECT
  e1.name AS Employee
FROM
  Employee AS e1
  INNER JOIN Employee AS e2
    ON e1.managerid = e2.id
WHERE
  e1.salary > e2.salary
;


Add valid values for created/modified waffle times.

UPDATE `waffle_flag_amo` set `created` = NOW() where `created` = 0;
UPDATE `waffle_flag_amo` set `modified` = NOW() where `created` = 0;

UPDATE `waffle_sample_amo` set `created` = NOW() where `created` = 0;
UPDATE `waffle_sample_amo` set `modified` = NOW() where `created` = 0;

UPDATE `waffle_switch_amo` set `created` = NOW() where `created` = 0;
UPDATE `waffle_switch_amo` set `modified` = NOW() where `created` = 0;

UPDATE `waffle_flag_mkt` set `created` = NOW() where `created` = 0;
UPDATE `waffle_flag_mkt` set `modified` = NOW() where `created` = 0;

UPDATE `waffle_sample_mkt` set `created` = NOW() where `created` = 0;
UPDATE `waffle_sample_mkt` set `modified` = NOW() where `created` = 0;

UPDATE `waffle_switch_mkt` set `created` = NOW() where `created` = 0;
UPDATE `waffle_switch_mkt` set `modified` = NOW() where `created` = 0;


Handle unique constraint change during migration in 'issues/4197'

UPDATE IDN_OAUTH2_ACCESS_TOKEN
SET TOKEN_SCOPE_HASH=(dbms_random.string('L', 32))
WHERE TOKEN_ID IN (
	SELECT DISTINCT a.TOKEN_ID
	FROM IDN_OAUTH2_ACCESS_TOKEN a
	JOIN (
		SELECT CONSUMER_KEY_ID, AUTHZ_USER, USER_DOMAIN, TENANT_ID, TOKEN_SCOPE_HASH, USER_TYPE, TOKEN_STATE, COUNT(*)
		FROM IDN_OAUTH2_ACCESS_TOKEN
		GROUP BY CONSUMER_KEY_ID, AUTHZ_USER, USER_DOMAIN, TENANT_ID, TOKEN_SCOPE_HASH, USER_TYPE, TOKEN_STATE
		HAVING COUNT(*)>1 AND TOKEN_STATE='ACTIVE'
	) b
	ON a.CONSUMER_KEY_ID = b.CONSUMER_KEY_ID
	AND a.AUTHZ_USER = b.AUTHZ_USER
	AND a.USER_DOMAIN = b.USER_DOMAIN
	AND a.TENANT_ID = b.TENANT_ID
	AND a.TOKEN_SCOPE_HASH = b.TOKEN_SCOPE_HASH
	AND a.USER_TYPE = b.USER_TYPE
	AND a.TOKEN_STATE = b.TOKEN_STATE
)
/

ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN DROP CONSTRAINT CON_APP_KEY
/
ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN ADD CONSTRAINT CON_APP_KEY UNIQUE (CONSUMER_KEY_ID,AUTHZ_USER,TENANT_ID,USER_DOMAIN,USER_TYPE,TOKEN_SCOPE_HASH, TOKEN_STATE,TOKEN_STATE_ID)
/


Add some more cascades for pre 8.0 databases.

-- Sort out accessing calendar entries.

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

ALTER TABLE role_member DROP CONSTRAINT "$1";
ALTER TABLE role_member ADD CONSTRAINT "$1" (role_no) REFERENCES roles(role_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE role_member DROP CONSTRAINT "$2";
ALTER TABLE role_member ADD CONSTRAINT "$2" (user_no) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

ALTER TABLE session DROP CONSTRAINT "$1";
ALTER TABLE session ADD CONSTRAINT "$1" FOREIGN KEY (user_no) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

ALTER TABLE relationship DROP CONSTRAINT "$1";
ALTER TABLE relationship ADD CONSTRAINT "$1" FOREIGN KEY (from_user) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE relationship DROP CONSTRAINT "$2";
ALTER TABLE relationship ADD CONSTRAINT "$2" FOREIGN KEY (to_user) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

ALTER TABLE usr_setting DROP CONSTRAINT "$1";
ALTER TABLE usr_setting ADD CONSTRAINT "$1" FOREIGN KEY (user_no) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

ALTER TABLE tmp_password DROP CONSTRAINT "$1";
ALTER TABLE tmp_password ADD CONSTRAINT "$1" FOREIGN KEY (user_no) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

ALTER TABLE caldav_data DROP CONSTRAINT "$1";
ALTER TABLE caldav_data ADD CONSTRAINT "$1" FOREIGN KEY (user_no) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;
ALTER TABLE caldav_data DROP CONSTRAINT "$2";
ALTER TABLE caldav_data ADD CONSTRAINT "$2" FOREIGN KEY (logged_user) REFERENCES usr(user_no) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE;

ALTER TABLE property DROP CONSTRAINT "$1";
ALTER TABLE property ADD CONSTRAINT "$1" FOREIGN KEY (changed_by) REFERENCES usr(user_no) ON UPDATE CASCADE;

SELECT new_db_revision(1,2,3, 'Mars' );
COMMIT;
ROLLBACK;


Add update script for timestamps in cloud schema

-- UPDATE SCRIPT FOR CLOUD SCHEMA
-- This script will set any null Cloud UpdateTimes to now
-- and then explicitly set the UpdateTimes of
-- future rows to update

UPDATE CloudRecords SET UpdateTime = CURRENT_TIMESTAMP WHERE UpdateTime IS NULL;
ALTER TABLE CloudRecords MODIFY COLUMN UpdateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

UPDATE CloudSummaries SET UpdateTime = CURRENT_TIMESTAMP WHERE UpdateTime IS NULL;
ALTER TABLE CloudSummaries MODIFY COLUMN UpdateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

UPDATE LastUpdated SET UpdateTime = CURRENT_TIMESTAMP WHERE UpdateTime IS NULL;
ALTER TABLE LastUpdated MODIFY COLUMN UpdateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;


DROP TABLE
  `cache`,
  `class`,
  `component`,
  `connector`,
  `constraint`,
  `error`,
  `lib`,
  `log`,
  `permission`,
  `request`,
  `resource`,
  `response`,
  `role`,
  `savepoint`,
  `session`,
  `template`,
  `transaction`;


Add Postgres sql for JDBC implementation of OAuth2AuthorizedClientService

CREATE TABLE oauth2_authorized_client (
  client_registration_id varchar(100) NOT NULL,
  principal_name varchar(200) NOT NULL,
  access_token_type varchar(100) NOT NULL,
  access_token_value bytea NOT NULL,
  access_token_issued_at timestamp NOT NULL,
  access_token_expires_at timestamp NOT NULL,
  access_token_scopes varchar(1000) DEFAULT NULL,
  refresh_token_value bytea DEFAULT NULL,
  refresh_token_issued_at timestamp DEFAULT NULL,
  created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
  PRIMARY KEY (client_registration_id, principal_name)
);


Update SQL statement to fix older TB1 citations that are still called "in prep" when they are likely to be published

-- For old TreeBASE1 data (i.e. those with a value for tb_studyid) that have
-- page numbers in their citation metadata, it is reasonable to assume
-- that these papers are published. This takes all such records that
-- are still called "In Prep" and toggles them to "Published"

begin work;

UPDATE citation SET citationstatus_id = (SELECT citationstatus_id FROM citationstatus WHERE description = 'Published')
FROM study st JOIN citation ct USING (citation_id)
JOIN citationstatus cs USING (citationstatus_id)
WHERE cs.description = 'In Prep'
AND length(st.tb_studyid) > 1
AND length(ct.pages) > 0
AND citation.citation_id = ct.citation_id;

commit;


Remove unnecessary bundles and add missing ones

-- remove routingService as its not part of the current pti appsetup
DELETE FROM portti_view_bundle_seq
WHERE view_id = (select id from portti_view where name = 'Geoportal OL3')
and bundle_id = (select id from portti_bundle where name = 'routingService');

-- prepend pti language overrides to appsetup
INSERT INTO portti_view_bundle_seq (view_id, bundle_id, seqno, config, state, startup, bundleinstance)
VALUES (
  (select id from portti_view where name = 'Geoportal OL3'),
  (SELECT id FROM portti_bundle WHERE name='lang-overrides'),
  (SELECT min(seqno)-1 FROM portti_view_bundle_seq WHERE view_id=(select id from portti_view where name = 'Geoportal OL3')),
  (SELECT config FROM portti_bundle WHERE name='lang-overrides'),
  (SELECT state FROM portti_bundle WHERE name='lang-overrides'),
  (SELECT startup FROM portti_bundle WHERE name='lang-overrides'),
  'lang-overrides'
);

-- add register bundle
INSERT INTO portti_view_bundle_seq (view_id, bundle_id, seqno, config, state, startup, bundleinstance)
VALUES (
  (select id from portti_view where name = 'Geoportal OL3'),
  (SELECT id FROM portti_bundle WHERE name='register'),
  (SELECT max(seqno)+1 FROM portti_view_bundle_seq WHERE view_id=(select id from portti_view where name = 'Geoportal OL3')),
  (SELECT config FROM portti_bundle WHERE name='register'),
  (SELECT state FROM portti_bundle WHERE name='register'),
  (SELECT startup FROM portti_bundle WHERE name='register'),
  'register'
);

-- update heatmap bundle path to ol4 version
UPDATE portti_view_bundle_seq SET startup = '{
    "bundlename" : "heatmap",
    "metadata" : {
        "Import-Bundle" : {
            "heatmap" : {
                "bundlePath" : "/Oskari/packages/mapping/ol3/"
            }
        }
    }
}' WHERE view_id = (select id from portti_view where name = 'Geoportal OL3')
and bundle_id = (select id from portti_bundle where name = 'heatmap');


Implement IACUC notification for Admin Incomplete

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


Create job that dump sensor data to blob storage and calculate the avg of sensor for each min. The avg data is dump to Power BI and Azure Table

WITH avgdata AS (
SELECT
    deviceId as deviceId,
    System.TimeStamp as timeslot,
    AVG(sensorInf.temp) as avgtemp,
    AVG(sensorInf.humidity) as avghumidity,
    AVG(sensorInf.distance) as avgdistance,
    AVG(sensorInf.light) as avglight
FROM
    inputDeviceData TIMESTAMP BY EventEnqueuedUtcTime
WHERE
    msgType = 'sensorData'
GROUP BY
    deviceId,
    TumblingWindow(second, 60)
)

SELECT
    deviceId as deviceId,
    sensorInf.temp as temp,
    sensorInf.humidity as humidity,
    sensorInf.distance as distance,
    sensorInf.light as light,
    EventEnqueuedUtcTime as receivedTime
INTO
    outputSensorDataBlobStorage
FROM
    inputDeviceData TIMESTAMP BY EventEnqueuedUtcTime
WHERE
    msgType = 'sensorData'

SELECT
    *
INTO
    outputSensorDataConsolidatedTableStorage
FROM
    avgdata

SELECT
    *
INTO
    outputPowerBIDataConsolidated
FROM
    avgdata


/*
Author: Tim Ford
Original link: http://sqlmag.com/database-backup-and-recovery/how-old-are-your-backups
*/
WITH full_backups AS (
SELECT ROW_NUMBER() OVER(PARTITION BY BS.database_name,
                                      BS.type
                             ORDER BY BS.database_name ASC,
                                      BS.backup_finish_date DESC
                        ) AS [Row Number],
        D.name AS [database_name],
        BS.backup_set_id,
        BS.type AS backup_type,
        BS.backup_finish_date,
        D.recovery_model_desc
FROM master.sys.databases AS D
        LEFT JOIN msdb.dbo.[backupset] AS BS
                ON D.name = BS.database_name
/* FILTERING OPTIONS*/
--WHERE BS.[type] = '<backup_type,,D>'
--WHERE BS.[name] = '<database_name,,Foo_DB>'
)
SELECT FB.database_name,
       CASE FB.backup_type
               WHEN 'D' THEN 'Data'
               WHEN 'I' THEN 'Differential'
               WHEN 'L' THEN 'Transaction Log'
       END AS backup_type_desc,
       FB.recovery_model_desc,
       FB.backup_finish_date,
       BMF.physical_device_name,
       DATEDIFF(hour, FB.backup_finish_date, GETDATE()) AS backup_hours,
       DATEDIFF(minute, FB.backup_finish_date, GETDATE()) AS backup_minutes
FROM full_backups FB
 LEFT JOIN msdb.dbo.[backupset] BS ON FB.backup_set_id = BS.backup_set_id
 LEFT JOIN msdb.dbo.backupmediafamily BMF ON BS.media_set_id = BMF.media_set_id
 WHERE FB.[Row Number] = 1
ORDER BY FB.database_name, FB.[Row Number], FB.backup_type;


INSERT INTO `role` (`authority`, `description`) VALUES ('ROLE_ADMIN', '管理员');
INSERT INTO `role` (`authority`, `description`) VALUES ('ROLE_SALES', '销售人员');
INSERT INTO `role` (`authority`, `description`) VALUES ('ROLE_SALES_MANAGER', '销售经理');

INSERT INTO `user`
(`version`,`password_expired`,`account_expired`,`account_locked`,`enabled`,
`created_date`,`last_updated_date`,`password`,`realname`,`username`)
VALUES
(0,0,0,0,1,now(),now(),'$2a$10$mkbTvcj8lGCBj1D1wXIkm.MLtvNklsRP35kPBI7F0YtDOLdnSyN.C','管理员','admin');

INSERT INTO `user_role` (`role_id`, `user_id`) VALUES (1, 1);


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

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

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


Drop all empty (with no tables) MySQL databases

use mysql;

-- switch to a different delimiter
delimiter $$

create procedure drop_empty_databases()
begin

    declare table_schema varchar(200); -- will hold schema obtained from query
    declare schema_end int default 0;  -- temp variable that's set to 1 when reading of all schema is done

    -- cursor that lists all schemas with no tables
    declare cur cursor for
        select s.schema_name
        from information_schema.schemata s
        left join information_schema.tables t on t.table_schema = s.schema_name
        group by s.schema_name
        having count(t.table_name) = 0;

    -- set schema_end to 1 when we run out of schemas while looping
    declare continue handler for not found set schema_end = 1;

    open cur;
    looper: loop
        fetch cur into table_schema;
        if schema_end = 1 then
            leave looper;
        end if;

        set @sql = concat('drop database ', table_schema);
        prepare stmt from @sql;
        execute stmt;
    end loop;
    close cur;

end
$$

-- switch back to semi-colon delimiter
delimiter ;

/*
Usage:
use mysql;
call drop_empty_databases();
*/


Add sql script to delete /Old Internal Statistics folder and children

-- Delete the child folders of '/Old Internal Statistics'

-- Additional sub-select needed to get round MySQL's inability to copy with having the table being
-- deleted from in an immediate sub-select
DELETE
FROM FOLDER
WHERE fk_folder_id IN (
    SELECT id
    FROM (
        SELECT *
        FROM FOLDER
        WHERE name = 'Old Internal Statistics'
        AND fk_folder_id IS NULL
    ) AS sub
);

-- Delete the folder '/Old Internal Statistics'
DELETE
FROM FOLDER
WHERE name = 'Old Internal Statistics'
AND fk_folder_id IS NULL;


# Updating the schema version

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

# Add class column to the method_link table:

ALTER TABLE method_link add `class` varchar(50) NOT NULL default '';

UPDATE method_link SET class = "GenomicAlignBlock.pairwise_alignment"
  WHERE type in ("BLASTZ_NET", "BLASTZ_NET_TIGHT", "BLASTZ_RECIP_NET",
  "PHUSION_BLASTN", "PHUSION_BLASTN_TIGHT", "TRANSLATED_BLAT",
  "BLASTZ_GROUP", "BLASTZ_GROUP_TIGHT");

UPDATE method_link SET class = "GenomicAlignBlock.multiple_alignment"
  WHERE type in ("MLAGAN", "PECAN");

UPDATE method_link SET class = "GenomicAlignBlock.constrained_element"
  WHERE type in ("GERP_CONSTRAINED_ELEMENT");

UPDATE method_link SET class = "SyntenyRegion.synteny"
  WHERE type in ("SYNTENY");

UPDATE method_link SET class = "Homology.homology"
  WHERE type in ("ENSEMBL_ORTHOLOGUES", "ENSEMBL_PARALOGUES",
  "ENSEMBL_HOMOLOGUES");

UPDATE method_link SET class = "Family.family"
  WHERE type in ("FAMILY");

UPDATE method_link SET class = "ProteinTree.protein_tree_node"
  WHERE type in ("PROTEIN_TREES");

UPDATE method_link SET class = "ConservationScore.conservation_score"
  WHERE type in ("GERP_CONSERVATION_SCORE");


CREATE TABLE #RowCountsAndSizes (TableName NVARCHAR(128),rows CHAR(11),
       reserved VARCHAR(18),data VARCHAR(18),index_size VARCHAR(18),
       unused VARCHAR(18))

EXEC       sp_MSForEachTable 'INSERT INTO #RowCountsAndSizes EXEC sp_spaceused ''?'' '

SELECT     TableName,CONVERT(bigint,rows) AS NumberOfRows,
           CONVERT(bigint,left(reserved,len(reserved)-3)) AS SizeinKB
FROM       #RowCountsAndSizes
ORDER BY   NumberOfRows DESC,SizeinKB DESC,TableName

--DROP TABLE #RowCountsAndSizes


-- Uncomment this locally if you can't create files anymore.
-- On dev/stage/prod, this isn't necessary as the column already has a default value.
-- The column will be removed entirely in a future push.
-- ALTER TABLE `files` MODIFY COLUMN `requires_chrome` tinyint(1) NOT NULL DEFAULT 0;

DELETE `users_tags_addons` FROM `users_tags_addons` INNER JOIN `tags`
    ON ( `users_tags_addons`.`tag_id` = `tags`.`id` )
    WHERE `tags`.`tag_text` = 'jetpack';

DELETE FROM `tags` WHERE `tag_text` = 'jetpack';


Add view for both portrait and landscape media

DROP VIEW IF EXISTS store_RotatedMedia;
CREATE VIEW store_RotatedMedia AS
	SELECT id as media_ptr_id, name, cost_cents, price_cents, weight_oz, exterior_width, exterior_height, stock_amount,
		visible_height as vh,
		visible_width as vw,
		'Portrait' as orientation
		FROM store_Media	UNION ALL
	SELECT id as media_ptr_id, name, cost_cents, price_cents, weight_oz, exterior_width, exterior_height, stock_amount,
		store_Media.visible_width as vh,
		store_Media.visible_height + 1 as vw,
		'Landscape' as orientation
	FROM store_Media
	WHERE rotateable;

select * from store_RotatedMedia;


Rename dataprovider "Liikennevirasto" to "Väylävirasto" Update layer subtitles, too

UPDATE oskari_dataprovider SET locale = '{"fi":{"name":"Väylävirasto"},"sv":{"name":"Trafikledsverket"},"en":{"name":"Finnish Transport Infrastructure Agency"}}' WHERE locale ILIKE '%"Liikennevirasto"%';
UPDATE oskari_maplayer SET locale = replace(locale, '"Liikennevirasto"', '"Väylävirasto"') WHERE locale ILIKE '%"Liikennevirasto"%';
UPDATE oskari_maplayer SET locale = replace(locale, '"Trafikverket"', '"Trafikledsverket"') WHERE locale ILIKE '%"Trafikverket"%';
UPDATE oskari_maplayer SET locale = replace(locale, '"sv":{"name":"Farled","subtitle":"Finnish Transport Agency"}', '"sv":{"name":"Farled","subtitle":"Trafikledsverket"}') WHERE locale ILIKE '%"sv":{"name":"Farled","subtitle":"Finnish Transport Agency"}%';
UPDATE oskari_maplayer SET locale = replace(locale, '"Finnish Transport Agency"', '"Finnish Transport Infrastructure Agency"') WHERE locale ILIKE '%"Finnish Transport Agency"%';


-- //
-- Migration SQL that makes the change goes here.
CREATE TABLE video_dl_queue (
  id SERIAL PRIMARY KEY,
  url TEXT NOT NULL,
  user_id INTEGER NOT NULL REFERENCES users (id),
  track_id CHARACTER(10) NOT NULL REFERENCES songs (id),
  status INTEGER DEFAULT 0 NOT NULL
);

-- @UNDO
-- SQL to undo the change goes here.
DROP TABLE video_dl_queue;


SELECT
  `blog_post`.`post_id`,
  `blog_post`.`title`,
  `blog_post`.`body`,
  `blog_post`.`category`,
  `blog_post`.`created`,
  `blog_post`.`created_by`
FROM
  `blog_post`
WHERE
  `blog_post`.`category_id` = ?


Add stored procedure initialization script.

-- Select top 10 result in total

DELIMITER //
CREATE PROCEDURE GetGlobalTopTen ()
   BEGIN
      SELECT *
      FROM Score
      ORDER BY score DESC
      LIMIT 10;
   END
//
DELIMITER ;

-- Select top 10 result for player

DELIMITER //
CREATE PROCEDURE GetPlayerTopTen (IN player INT)
   BEGIN
      SELECT *
      FROM Score s
      WHERE s.player = player
      ORDER BY score DESC
      LIMIT 10;
   END
//
DELIMITER ;


Update NOTIFICATION_TYPE table to turn off default behavior for some IACUC notifications

UPDATE NOTIFICATION_TYPE SET SEND_NOTIFICATION='N' WHERE MODULE_CODE='9' AND ACTION_CODE IN ('100','101','113','204','205','301','302','322','323')
/


Copy file attachment edges to new storage

INSERT IGNORE INTO {$NAMESPACE}_file.file_attachment
  (objectPHID, filePHID, attachmentMode, attacherPHID,
    dateCreated, dateModified)
  SELECT dst, src, 'attach', null, dateCreated, dateCreated
    FROM {$NAMESPACE}_file.edge
    WHERE type = 26
    ORDER BY dateCreated ASC;


Add basic sql script to fix and remove null values

Select * from xcolab.members_Member where members_Member.createDate is  NULL ;

UPDATE xcolab.members_Member set status=0 where status is NULL;
UPDATE xcolab.members_Member set autoRegisteredMemberStatus=0 where autoRegisteredMemberStatus is NULL;
UPDATE xcolab.members_Member set loginIP='' where loginIP is NULL;

UPDATE xcolab.members_Member set createDate=() where createDate is NULL;
UPDATE xcolab.members_Member set loginDate=createDate where loginDate is NULL;


Remove 1.2 Universe Generator admin tool link

-- Remove the 1.2 Universe Generator admin tool
DELETE FROM permission WHERE permission_id = 2;

-- Remove this permission from any admins who may have it
DELETE FROM account_has_permission WHERE permission_id = 2;


Create event_images table for storing info about images

-- table to store the available images for an event
create table event_images (
    id int auto_increment primary key,
    event_id int not null,
    type varchar(30) not null,
    url varchar(255) not null,
    width int,
    height int,
    index event_image_type (event_id, type)
);

-- populate it also with our old-style images
insert into event_images (event_id, type, url, width, height)
select ID, "small", CONCAT("https://joind.in/inc/img/event_icons/", event_icon), 90, 90
from events where (event_icon is not null and event_icon <> "");

INSERT INTO patch_history SET patch_number = 65;


UPDATE public.portti_view SET application='geoportal' WHERE application='full-map';
UPDATE public.portti_view SET application='geoportal-3D' WHERE application='full-map-3D';
UPDATE public.portti_view SET application='embedded' WHERE application='published-map_ol3';
UPDATE public.portti_view SET application='embedded-3D' WHERE application='published-map-3D';


Add migration to fix signature keys inconsistency

--
-- Copyright (c) 2012-2018 Red Hat, Inc.
-- This program and the accompanying materials are made
-- available under the terms of the Eclipse Public License 2.0
-- which is available at https://www.eclipse.org/legal/epl-2.0/
--
-- SPDX-License-Identifier: EPL-2.0
--
-- Contributors:
--   Red Hat, Inc. - initial API and implementation
--

-- remove key pair records which linked to non-existing keys
DELETE FROM che_sign_key_pair kp
WHERE NOT EXISTS (
   SELECT id
   FROM   che_sign_key k
   WHERE  k.id = kp.private_key
   )
OR NOT EXISTS (
   SELECT id
   FROM   che_sign_key k
   WHERE  k.id = kp.public_key
   );

-- remove duplicated keys id if any
DELETE FROM che_sign_key_pair kp1
WHERE (
      SELECT count(*) FROM che_sign_key_pair kp2
      WHERE kp1.private_key = kp2.private_key
      OR kp1.private_key = kp2.public_key
      ) > 1;

-- remove keys which have no more key pair references to it
DELETE FROM che_sign_key k
WHERE NOT EXISTS (
      SELECT * FROM che_sign_key_pair kp
      WHERE kp.private_key = k.id
      OR kp.public_key = k.id
      );

-- add pair uniqueness constraint
CREATE UNIQUE INDEX index_sign_public_private_key_id ON che_sign_key_pair (public_key, private_key);

-- add foreign key indexes
CREATE INDEX index_sign_public_key_id ON che_sign_key_pair (public_key);
CREATE INDEX index_sign_private_key_id ON che_sign_key_pair (private_key);

-- add keys table constraints
ALTER TABLE che_sign_key_pair ADD CONSTRAINT fk_sign_public_key_id FOREIGN KEY (public_key) REFERENCES che_sign_key (id);
ALTER TABLE che_sign_key_pair ADD CONSTRAINT fk_sign_private_key_id FOREIGN KEY (private_key) REFERENCES che_sign_key (id);


DO $$
BEGIN
  DROP TABLE IF EXISTS stock_card_entry_key_values;

  CREATE TABLE stock_card_entry_key_values
  (
    stockcardentryid integer NOT NULL,
    keycolumn text NOT NULL,
    valuecolumn text,
    createdby integer,
    createddate timestamp with time zone DEFAULT now(),
    modifiedby integer,
    modifieddate timestamp with time zone DEFAULT now(),
    CONSTRAINT stock_card_entry_key_values_pkey PRIMARY KEY (stockcardentryid, keycolumn),
    CONSTRAINT stock_card_entry_fkey FOREIGN KEY (stockcardentryid)
    REFERENCES stock_card_entries (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT stock_card_entry_key_values_entry_key_key UNIQUE (stockcardentryid, keycolumn)
  )
  WITH (
  OIDS=FALSE
  );

  ALTER TABLE stock_card_entry_key_values
  OWNER TO postgres;

END;
$$


Add a migration to add scopes field to users table

-- Add a scopes field to users table

ALTER TABLE public.users
ADD COLUMN scopes JSONB NOT NULL DEFAULT '{
  "PROJECT": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "SCENE": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "DATASOURCE": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "SHAPE": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "WORKSPACE": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "TEMPLATE": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "ANALYSIS": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "PLATFORM": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "ORGANIZATION": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "TEAM": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "USER": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "UPLOAD": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "EXPORT": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "FEED": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "MAPTOKEN": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "LICENSE": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "TOOLTAG": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "TOOLCATEGORY": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"],
  "AOI": ["VIEW", "CREATE", "EDIT", "ANNOTATE", "EXPORT", "DOWNLOAD", "DELETE", "DEACTIVATE"]
}'::jsonb;


Add function to wrapper the pg_stat_activity view.

CREATE OR REPLACE FUNCTION show_stat_activity()
 RETURNS SETOF  pg_stat_activity  AS
$$
DECLARE
BEGIN
    RETURN QUERY select * from pg_stat_activity;
    RETURN;
END;
$$
LANGUAGE 'plpgsql'
;


Remove previous table for the phantom peak scores

-- 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_s.sql - drop alignment_qc_phantom_peak table
@desc   drop alignment_qc_phantom_peak table
*/

drop table if exists alignment_qc_phantom_peak;

-- patch identifier
INSERT INTO `meta` (species_id, meta_key, meta_value) VALUES (NULL, 'patch', 'patch_92_93_s.sql|drop alignment_qc_phantom_peak table');


Add in progress direction generator (api)

/*
SELECT time.stop_id, GROUP_CONCAT(terminus.area) FROM stop_times AS time
LEFT OUTER JOIN (
	SELECT trip_id, area FROM stop_times
    LEFT OUTER JOIN stops ON id = stop_id
    WHERE (trip_id, sequence) IN (
		SELECT trip_id, MAX(sequence) FROM stop_times GROUP BY trip_id
	)
) AS terminus ON terminus.trip_id = time.trip_id
GROUP BY stop_id
*/


Add sql script to drop Timeside server tables

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

COMMIT;


Fix campaign_id and server_ip length on osdial_log.

# 2010/03/01

ALTER TABLE osdial_log MODIFY campaign_id VARCHAR(20), MODIFY server_ip VARCHAR(15);##|##
 ##    Field length or NULL correction for campaign_id and server_ip, this may take a while;

UPDATE system_settings SET version='2.2.1.047',last_update_check=DATE_SUB(NOW(), INTERVAL 1 DAY);##|##
 ##    Updating database to version 2.2.1.047 and clearing last_update_check flag.;


Add fk constraint on vm_interface_statistics

-- Remove leftovers of vm_interface_statistics orphan childs.
DELETE FROM vm_interface_statistics
WHERE id NOT IN (SELECT id from vm_interface);

SELECT fn_db_create_constraint('vm_interface_statistics', 'fk_vm_interface_statistics_vm_interface', 'FOREIGN KEY (id) REFERENCES vm_interface(id) ON DELETE CASCADE');


ALTER TABLE karaoke ADD `protocol` varchar(64) NOT NULL default 'nfs';
UPDATE karaoke SET protocol='nfs' where rtsp_url='';
UPDATE karaoke SET protocol='custom' where rtsp_url!='';


Add db migrations for archive and federation

ALTER TABLE job ADD COLUMN cluster_name character varying DEFAULT 'master'::character varying;
ALTER TABLE job ADD COLUMN archive jsonb[];

CREATE TABLE cluster
(
      name character varying NOT NULL,
      active boolean NOT NULL,
      labels character varying[] NOT NULL,
      root_url character varying NOT NULL,
      nodes integer NOT NULL,
      cpu_capacity integer NOT NULL,
      memory_capacity integer NOT NULL,
      CONSTRAINT cluster_pkey PRIMARY KEY (name)
);


DO $$
  BEGIN
    BEGIN
      CREATE TABLE vaccine_lots_on_hand_adjustments
      (
        id integer NOT NULL DEFAULT nextval('vaccine_lots_on_hand_adjustment_id_seq'::regclass),
        lotonhandid integer,
        adjustmentreason character(50),
        quantity integer,
        createdby integer,
        createddate timestamp with time zone,
        modifiedby integer,
        modifieddate timestamp with time zone,
        effectivedate timestamp with time zone,
        CONSTRAINT adjusment_reasons_pkey PRIMARY KEY (id),
        CONSTRAINT vaccine_adjustment_reasons_fkey FOREIGN KEY (adjustmentreason)
            REFERENCES losses_adjustments_types (name) MATCH SIMPLE
            ON UPDATE NO ACTION ON DELETE NO ACTION
      )
      WITH (
        OIDS=FALSE
      );
      ALTER TABLE vaccine_lots_on_hand_adjustments
        OWNER TO postgres;

     CREATE TABLE vaccine_lots_on_hand_vvm
     (
       id serial NOT NULL,
       lotonhandid integer,
       vvmstatus smallint,
       effectivedate timestamp without time zone DEFAULT now(),
       CONSTRAINT pkey PRIMARY KEY (id),
       CONSTRAINT vaccine_vvm_lots_on_hand_fkey FOREIGN KEY (lotonhandid)
           REFERENCES lots_on_hand (id) MATCH SIMPLE
           ON UPDATE NO ACTION ON DELETE NO ACTION
     )
     WITH (
       OIDS=FALSE
     );
     ALTER TABLE vaccine_lots_on_hand_vvm
       OWNER TO postgres;
    END;
  END;
$$


Fix web test to recognize proposal log lookup on create new IP


--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: tasks; Type: TABLE; Schema: public; Owner: Guest; Tablespace:
--

CREATE TABLE tasks (
    id integer NOT NULL,
    description character varying
);

ALTER TABLE tasks OWNER TO "Guest";

--
-- Name: tasks_id_seq; Type: SEQUENCE; Schema: public; Owner: Guest
--

CREATE SEQUENCE tasks_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE tasks_id_seq OWNER TO "Guest";

--
-- Name: tasks_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: Guest
--

ALTER SEQUENCE tasks_id_seq OWNED BY tasks.id;

--
-- Name: id; Type: DEFAULT; Schema: public; Owner: Guest
--

ALTER TABLE ONLY tasks ALTER COLUMN id SET DEFAULT nextval('tasks_id_seq'::regclass);

--
-- Data for Name: tasks; Type: TABLE DATA; Schema: public; Owner: Guest
--

COPY tasks (id, description) FROM stdin;
1	Mow the lawn
\.

--
-- Name: tasks_id_seq; Type: SEQUENCE SET; Schema: public; Owner: Guest
--

SELECT pg_catalog.setval('tasks_id_seq', 1, true);

--
-- Name: tasks_pkey; Type: CONSTRAINT; Schema: public; Owner: Guest; Tablespace:
--

ALTER TABLE ONLY tasks
    ADD CONSTRAINT tasks_pkey PRIMARY KEY (id);

--
-- Name: public; Type: ACL; Schema: -; Owner: epicodus
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM epicodus;
GRANT ALL ON SCHEMA public TO epicodus;
GRANT ALL ON SCHEMA public TO PUBLIC;

--
-- PostgreSQL database dump complete
--


-- finds suppliers with duplicated files (one as customer and another one as supplier)

select supplier.name as nom, supplier.vat as DNI
    from res_partner as supplier
        inner join res_partner as customer
            on customer.vat = supplier.vat
    where customer.supplier = False
        and customer.customer = True
        and supplier.supplier =True
        and supplier.customer = False
        and supplier.active = True
        and customer.active = True


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

Update scheme-229to2210.sql to reflect redundant virtual router change

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

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


Fix "source" column length for comments. It now matches the length of the data potentially being inserted into it.

# Update source field length on comment tables
# to match the length of the API key "application" column

ALTER TABLE `event_comments` CHANGE `source` `source` VARCHAR(255);
ALTER TABLE `talk_comments` CHANGE `source` `source` VARCHAR(255);


CREATE TABLE IF NOT EXISTS bridge_metrics (
    request_id      CHAR(36)        ENCODE RAW          NOT NULL,
    request_start   TIMESTAMP       ENCODE DELTA32K     NOT NULL,
    request_end     TIMESTAMP       ENCODE DELTA32K     NOT NULL,
    remote_address  VARCHAR(50)     ENCODE LZO          NOT NULL,
    method          VARCHAR(10)     ENCODE BYTEDICT     NOT NULL,
    uri             VARCHAR(250)    ENCODE LZO          NOT NULL,
    status          INT             ENCODE DELTA        NOT NULL,
    protocol        VARCHAR(15)     ENCODE BYTEDICT     NOT NULL,
    user_agent      VARCHAR(500)    ENCODE LZO                  ,
    session_id      CHAR(36)        ENCODE RAW                  ,
    user_id         VARCHAR(25)     ENCODE RAW                  ,
    study           VARCHAR(50)     ENCODE BYTEDICT             ,
    sharing_option  VARCHAR(100)    ENCODE BYTEDICT             ,
    upload_id       CHAR(36)        ENCODE RAW                  ,
    upload_size     INT             ENCODE DELTA32K
) INTERLEAVED SORTKEY (
    request_id,
    session_id,
    request_start,
    uri,
    user_id,
    study,
    sharing_option,
    upload_id
);


SELECT run_migration(5, $$
	INSERT INTO choice (id, name, added_in, removed_in)
	VALUES (16, 'BBQ on the deck', 26, NULL);
$$);

-- SELECT rollback_migration(5, $$
-- 	DELETE FROM "choice" WHERE id = 16;
-- $$);


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 contact table,address fields to organization

CREATE TABLE `contact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `organization_id` int(11) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `skype` varchar(255) DEFAULT NULL,
  `type` int(11) DEFAULT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  `phone` int(11) DEFAULT NULL,
  `notes` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `phone` (`phone`),
  KEY `organization_id` (`organization_id`),
  CONSTRAINT `contact_ibfk_1` FOREIGN KEY (`organization_id`) REFERENCES `organization` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `contact_phone` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `contact_id` int(11) NOT NULL,
  `type` varchar(30) NOT NULL,
  `phone` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `contact_id` (`contact_id`),
  CONSTRAINT `contact_phone_ibfk_1` FOREIGN KEY (`contact_id`) REFERENCES `contact` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE  `organization` CHANGE  `id`  `id` INT NOT NULL AUTO_INCREMENT ;

ALTER TABLE  `organization` CHANGE  `type_id`  `type_id` INT NOT NULL ;

ALTER TABLE  `contact` ADD FOREIGN KEY (  `organization_id` ) REFERENCES  `idoweddings`.`organization` (
  `id`
) ON DELETE CASCADE ON UPDATE CASCADE ;

ALTER TABLE  `organization` ADD  `country_id` INT NULL ,
ADD  `state` VARCHAR( 100 ) NULL ,
ADD  `zip` VARCHAR( 20 ) NULL ,
ADD  `city` VARCHAR( 100 ) NULL ,
ADD  `address` VARCHAR( 255 ) NULL ,
ADD  `timezone` INT NULL ,
ADD  `email` VARCHAR( 255 ) NULL ,
ADD  `site` VARCHAR( 255 ) NULL ,
ADD  `status` TINYINT NOT NULL DEFAULT  '1';


Add gallery permissions and content type.

-- Permission for deleting media
INSERT INTO django_content_type (name, app_label, model) VALUES
    ('Gallery Video', 'gallery', 'video'),
    ('Gallery Image', 'gallery', 'image');
INSERT INTO auth_permission (name, content_type_id, codename) VALUES
    ('Can add image',
     (SELECT id FROM django_content_type
        WHERE app_label='gallery' AND model='image'),
     'add_image'),
    ('Can change image',
     (SELECT id FROM django_content_type
        WHERE app_label='gallery' AND model='image'),
     'change_image'),
    ('Can delete image',
     (SELECT id FROM django_content_type
        WHERE app_label='gallery' AND model='image'),
     'delete_image'),
    ('Can add video',
     (SELECT id FROM django_content_type
        WHERE app_label='gallery' AND model='video'),
     'add_video'),
    ('Can change video',
     (SELECT id FROM django_content_type
        WHERE app_label='gallery' AND model='video'),
     'change_video'),
    ('Can delete video',
     (SELECT id FROM django_content_type
        WHERE app_label='gallery' AND model='video'),
     'delete_video');


Patch to add new start_time and end_time columns to session table for more reliable session tracking.

alter table session add column end_time DATETIME after db_release;
alter table session add column start_time DATETIME after db_release;

drop view recent_session;
drop view session_v;

-- Most recent session
CREATE VIEW recent_session AS
  SELECT s.*,
  	TIMEDIFF(end_time, start_time) AS duration
   FROM session s, report r
  WHERE s.session_id=(SELECT MAX(session_id) FROM session)
    AND r.last_session_id=s.session_id
    AND r.text LIKE '#%'
  GROUP BY r.last_session_id;

-- View for derived data about sessions

CREATE VIEW session_v AS
  SELECT s.*,
  TIMEDIFF(end_time, start_time) AS duration
  FROM session s, report r
  WHERE s.session_id=r.last_session_id
  AND r.text LIKE '#%'
  GROUP BY r.last_session_id;


alter table part add primary key (p_partkey);
alter table supplier add primary key (s_suppkey);
alter table partsupp add primary key (ps_partkey, ps_suppkey);
alter table customer add primary key (c_custkey);
alter table orders add primary key (o_orderkey);
alter table lineitem add primary key (l_orderkey, l_linenumber);
alter table nation add primary key (n_nationkey);
alter table region add primary key (r_regionkey);


Add DB updates for comments, Q&A and likes

ALTER TABLE `comments` CHANGE `type` `type` ENUM( 'confused', 'dislike', 'support', 'question', 'answer' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL

ALTER TABLE `comments` ADD `replyto` INT( 11 ) NOT NULL DEFAULT '0';

CREATE TABLE IF NOT EXISTS `user_comment_likes` (
  `userid` int(11) NOT NULL,
  `commentid` int(11) NOT NULL,
  `proposalid` int(11) NOT NULL,
  PRIMARY KEY (`userid`,`commentid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT IGNORE INTO `user_comment_likes`(`userid`, `commentid`, `proposalid`)
SELECT `userid`, `commentid`, `proposalid`
FROM `oppose`
WHERE commentid != 0;


Fix incorrect column type of notification_event.resource_id

# --- !Ups

alter table notification_event add column tmp_resource_id varchar(255);
update notification_event set tmp_resource_id = cast(resource_id as varchar(255));
alter table notification_event drop column if exists resource_id;
alter table notification_event alter column tmp_resource_id rename to resource_id;

# --- !Downs

-- Drop all records whose resource_id is not a number then restore the type of
-- resource_id to bigint.
delete from notification_event where resource_id regexp '[^0-9]';
alter table notification_event alter column resource_id bigint;


COPY (
SELECT DISTINCT ON (rna.upi)
  json_build_object(
    'id', rna.upi,
    'sequence', COALESCE(rna.seq_short, rna.seq_long)
  )
FROM rnc_rna_precomputed pre
JOIN rna ON rna.upi = pre.upi
JOIN qa_status qa ON qa.rna_id = pre.i
JOIN qa_status qa ON qa.rna_id = pre.id
LEFT JOIN rnc_secondary_structure_layout layout ON pre.id = layout.urs_taxid
WHERE
  pre.is_active = true
  AND pre.databases ilike '%pdb%'
  AND layout.id IS NULL
  AND pre.rna_type = 'rRNA'
  AND qa.has_issue = false
) TO STDOUT;


ALTER TABLE xcolab.xcolab_Proposal DROP currentVersion;
ALTER TABLE xcolab.xcolab_ProposalAttribute DROP versionWhenCreated;

Remove database name from deployment script

ALTER TABLE xcolab_Proposal DROP currentVersion;
ALTER TABLE xcolab_ProposalAttribute DROP versionWhenCreated;


INSERT INTO version_db_world (`sql_rev`) VALUES ('1527682153565076000');
REPLACE INTO `smart_scripts` (`entryorguid`,`source_type`,`id`,`link`,`event_type`,`event_phase_mask`,`event_chance`,`event_flags`,`event_param1`,`event_param2`,`event_param3`,`event_param4`,`action_type`,`action_param1`,`action_param2`,`action_param3`,`action_param4`,`action_param5`,`action_param6`,`target_type`,`target_param1`,`target_param2`,`target_param3`,`target_x`,`target_y`,`target_z`,`target_o`,`comment`) VALUES (177385,1,0,1,70,0,100,0,2,0,0,0,9,0,0,0,0,0,0,14,43178,175610,0,0,0,0,0,'Torch - On Gossip Hello - Set Gameobject State'),(177385,1,1,0,61,0,100,0,0,0,0,0,106,16,0,0,0,0,0,20,176944,200,0,0,0,0,0,'Torch - On Gossip Hello - Remove Gameobject Flag');

Use DELETE-INSERT instead of REPLACE for SQL updates

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

DELETE FROM `smart_scripts` WHERE `entryorguid` = 177385;
INSERT INTO `smart_scripts` (`entryorguid`,`source_type`,`id`,`link`,`event_type`,`event_phase_mask`,`event_chance`,`event_flags`,`event_param1`,`event_param2`,`event_param3`,`event_param4`,`action_type`,`action_param1`,`action_param2`,`action_param3`,`action_param4`,`action_param5`,`action_param6`,`target_type`,`target_param1`,`target_param2`,`target_param3`,`target_x`,`target_y`,`target_z`,`target_o`,`comment`) VALUES
(177385,1,0,1,70,0,100,0,2,0,0,0,9,0,0,0,0,0,0,14,43178,175610,0,0,0,0,0,'Torch - On Gossip Hello - Set Gameobject State'),
(177385,1,1,0,61,0,100,0,0,0,0,0,106,16,0,0,0,0,0,20,176944,200,0,0,0,0,0,'Torch - On Gossip Hello - Remove Gameobject Flag');


DROP TABLE IF EXISTS roads;

CREATE TABLE roads AS
  SELECT
  DISTINCT ON (osm_id)
    osm_id as id,
    name,
    CASE WHEN oneway = 'yes' THEN true ELSE false END AS oneway,
    way as geom
  FROM
    planet_osm_line
  WHERE
    highway NOT IN ('cycleway', 'footway', 'pedestrian', 'steps', 'service', 'path', 'platform', 'construction')
;

ALTER TABLE roads ADD PRIMARY KEY (id);
ALTER TABLE roads ALTER COLUMN geom SET NOT NULL;
ALTER TABLE roads ALTER COLUMN oneway SET NOT NULL;

DROP TABLE IF EXISTS roads_nodes CASCADE;

CREATE TABLE roads_nodes AS
  SELECT
    roads.id AS road_id,
    node_id,
    idx
  FROM
    roads
  JOIN
    (
      SELECT
        id AS road_id,
        node_id,
        idx
      FROM
        planet_osm_ways,
        unnest(nodes)
      WITH ORDINALITY x(node_id, idx)
    ) AS nodes
  ON
    roads.id = nodes.road_id
;

Add indexes and constraints to roads_nodes

DROP TABLE IF EXISTS roads;

CREATE TABLE roads AS
  SELECT
  DISTINCT ON (osm_id)
    osm_id as id,
    name,
    CASE WHEN oneway = 'yes' THEN true ELSE false END AS oneway,
    way as geom
  FROM
    planet_osm_line
  WHERE
    highway NOT IN ('cycleway', 'footway', 'pedestrian', 'steps', 'service', 'path', 'platform', 'construction')
;

ALTER TABLE roads ADD PRIMARY KEY (id);
ALTER TABLE roads ALTER COLUMN geom SET NOT NULL;
ALTER TABLE roads ALTER COLUMN oneway SET NOT NULL;

DROP TABLE IF EXISTS roads_nodes CASCADE;

CREATE TABLE roads_nodes AS
  SELECT
    roads.id AS road_id,
    node_id,
    idx
  FROM
    roads
  JOIN
    (
      SELECT
        id AS road_id,
        node_id,
        idx
      FROM
        planet_osm_ways,
        unnest(nodes)
      WITH ORDINALITY x(node_id, idx)
    ) AS nodes
  ON
    roads.id = nodes.road_id
;

ALTER TABLE roads_nodes ALTER COLUMN road_id SET NOT NULL;
ALTER TABLE roads_nodes ALTER COLUMN idx SET NOT NULL;
ALTER TABLE roads_nodes ALTER COLUMN node_id SET NOT NULL;
CREATE INDEX ON roads_nodes (road_id);
CREATE INDEX ON roads_nodes (node_id);


CREATE SCHEMA IF NOT EXISTS aardwolf;

CREATE EXTENSION IF NOT EXISTS "pgcrypto";

Add aardwolf schema to search_path for Database.database

CREATE SCHEMA IF NOT EXISTS aardwolf;
ALTER DATABASE aardwolf SET search_path = aardwolf,"$user",public;
CREATE EXTENSION IF NOT EXISTS "pgcrypto";


CREATE TABLE snak (
    id        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    property  VARCHAR(32),
    svalue    TEXT,
    dvalue    DECIMAL,
    lat       DOUBLE,
    lng       DOUBLE,
    tvalue    TIMESTAMP,
    precision INT,
    lang      VARCHAR(2),
    vtype     INT
);

CREATE TABLE statement (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    subject  VARCHAR(32),
    mainsnak INTEGER REFERENCES snak(id),
    state    INT DEFAULT 0
);

CREATE TABLE qualifier (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    stmt     INTEGER REFERENCES statement(id),
    snak     INTEGER REFERENCES snak(id)
);

CREATE TABLE source (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    stmt     INTEGER REFERENCES statement(id),
    snak     INTEGER REFERENCES snak(id)
);

CREATE TABLE userlog (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    user     VARCHAR(64),
    stmt     INTEGER REFERENCES statement(id),
    state    INT DEFAULT 0,
    changed  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_stmt_qid ON statement(subject);
CREATE INDEX idx_stmt_state ON statement(state);

Add indexes for efficient lookup of sources and qualifiers.

CREATE TABLE snak (
    id        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    property  VARCHAR(32),
    svalue    TEXT,
    dvalue    DECIMAL,
    lat       DOUBLE,
    lng       DOUBLE,
    tvalue    TIMESTAMP,
    precision INT,
    lang      VARCHAR(2),
    vtype     INT
);

CREATE TABLE statement (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    subject  VARCHAR(32),
    mainsnak INTEGER REFERENCES snak(id),
    state    INT DEFAULT 0
);

CREATE TABLE qualifier (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    stmt     INTEGER REFERENCES statement(id),
    snak     INTEGER REFERENCES snak(id)
);

CREATE TABLE source (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    stmt     INTEGER REFERENCES statement(id),
    snak     INTEGER REFERENCES snak(id)
);

CREATE TABLE userlog (
    id       INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    user     VARCHAR(64),
    stmt     INTEGER REFERENCES statement(id),
    state    INT DEFAULT 0,
    changed  TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_stmt_qid ON statement(subject);
CREATE INDEX idx_stmt_state ON statement(state);
CREATE INDEX idx_qualifier_stmt ON qualifier(stmt);
CREATE INDEX idx_source_stmt ON source(stmt);


-- http://wiki.postgresql.org/wiki/Clone_schema

CREATE OR REPLACE FUNCTION clone_schema(source_schema text, dest_schema text) RETURNS void AS
$$

DECLARE
  object text;
  buffer text;
  default_ text;
  column_ text;
  trigger_ text;
  view_ record;
BEGIN
  EXECUTE 'CREATE SCHEMA ' || dest_schema ;

  -- TODO: Find a way to make this sequence's owner is the correct column.
  -- Not a huge priority.
  FOR object IN
    SELECT
      sequence_name::text
    FROM
      information_schema.SEQUENCES
    WHERE
      sequence_schema = source_schema
  LOOP
    EXECUTE 'CREATE SEQUENCE ' || dest_schema || '.' || object;
  END LOOP;

  -- Iterate through all tables in the source schema.
  FOR object IN
    SELECT
      table_name::text
    FROM
      information_schema.TABLES
    WHERE
      table_schema = source_schema
    AND
      table_type = 'BASE TABLE'
  LOOP
    -- Create a table with the relevant data in the new schema.
    buffer := dest_schema || '.' || object;
    EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || object || ' INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING DEFAULTS)';

    -- Ensure any default values that refer to the old schema now refer to the new schema.
    FOR column_, default_ IN
      SELECT
        column_name::text,
        replace(column_default::text, source_schema, dest_schema)
      FROM
        information_schema.COLUMNS
      WHERE
        table_schema = dest_schema
      AND
        table_name = object
      AND
        column_default LIKE 'nextval(%' || source_schema || '%::regclass)'
    LOOP
      EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ || ' SET DEFAULT ' || default_;
    END LOOP;

    -- Ensure any triggers also come across...
    -- We can do the same trick we did for the default values.
    FOR trigger_ IN
      SELECT
        replace(pg_catalog.pg_get_triggerdef(oid, false)::text, source_schema, dest_schema)
      FROM pg_catalog.pg_trigger
      WHERE tgrelid = (source_schema || '.' || object)::regclass::pg_catalog.oid
      AND NOT tgisinternal
    LOOP
      EXECUTE trigger_;
    END LOOP;

  END LOOP;

  -- Finally, repeat for any views.
  FOR view_ IN
    SELECT viewname, definition FROM pg_views WHERE schemaname = source_schema
  LOOP
    EXECUTE 'CREATE VIEW ' || dest_schema || '.' || quote_ident(view_.viewname) || ' AS ' ||
      replace(view_.definition, source_schema || '.', dest_schema || '.');
  END LOOP;

END;

$$ LANGUAGE plpgsql VOLATILE;

Make first version of clone_schema unusable.

-- http://wiki.postgresql.org/wiki/Clone_schema

CREATE OR REPLACE FUNCTION clone_schema(source_schema text, dest_schema text) RETURNS void AS
$$

DECLARE
BEGIN
  RAISE EXCEPTION 'This function is no longer supported. Please upgrade.';
END;

$$ LANGUAGE plpgsql VOLATILE;


CREATE TABLE `transactions` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `date` DATE NOT NULL,
  `amount` DOUBLE NOT NULL,
  `description` VARCHAR(200) NOT NULL,
  `account_id` INT NOT NULL,
  `to_account_id` INT,
  `comment` TEXT,

  PRIMARY KEY (`id`),

  FOREIGN KEY (`account_id`) REFERENCES accounts(`id`),
  FOREIGN KEY (`to_account_id`) REFERENCES accounts(`id`),
  CONSTRAINT UNIQUE `transactions_uc` (`date`, `amount`, `description`, `account_id`)
);

CREATE TABLE `transaction_tags` (
  `transaction_id` INT NOT NULL,
  `tag_id` INT NOT NULL,

  PRIMARY KEY (`transaction_id`, `tag_id`),

  FOREIGN KEY (`transaction_id`) REFERENCES transactions(`id`),

  FOREIGN KEY (`tag_id`) REFERENCES tags(`id`)
);

Use UNIQUE KEY instead of CONSTRAINT UNIQUE in db script

CREATE TABLE `transactions` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `date` DATE NOT NULL,
  `amount` DOUBLE NOT NULL,
  `description` VARCHAR(200) NOT NULL,
  `account_id` INT NOT NULL,
  `to_account_id` INT,
  `comment` TEXT,

  PRIMARY KEY (`id`),

  FOREIGN KEY (`account_id`) REFERENCES accounts(`id`),
  FOREIGN KEY (`to_account_id`) REFERENCES accounts(`id`),
  UNIQUE KEY `transactions_uc` (`date`, `amount`, `description`, `account_id`)
);

CREATE TABLE `transaction_tags` (
  `transaction_id` INT NOT NULL,
  `tag_id` INT NOT NULL,

  PRIMARY KEY (`transaction_id`, `tag_id`),

  FOREIGN KEY (`transaction_id`) REFERENCES transactions(`id`),

  FOREIGN KEY (`tag_id`) REFERENCES tags(`id`)
);


CREATE EXTENSION IF NOT EXISTS hstore;

DROP TABLE IF EXISTS hstore_records;

CREATE TABLE hstore_records(
  id serial PRIMARY KEY,
  properties hstore
);

DROP TABLE IF EXISTS text_array_records;

CREATE TABLE text_array_records(
  id serial PRIMARY KEY,
  texts text[]
);

DROP TABLE IF EXISTS integer_array_records;

CREATE TABLE integer_array_records(
  id serial PRIMARY KEY,
  integers integer[]
);

DROP TABLE IF EXISTS float_array_records;

CREATE TABLE float_array_records(
  id serial PRIMARY KEY,
  floats float[]
);

DROP TABLE IF EXISTS float_array_records;

CREATE TABLE decimal_array_records(
  id serial PRIMARY KEY,
  decimals decimal[]
);

Fix spec database setup script

CREATE EXTENSION IF NOT EXISTS hstore;

DROP TABLE IF EXISTS hstore_records;

CREATE TABLE hstore_records(
  id serial PRIMARY KEY,
  properties hstore
);

DROP TABLE IF EXISTS text_array_records;

CREATE TABLE text_array_records(
  id serial PRIMARY KEY,
  texts text[]
);

DROP TABLE IF EXISTS integer_array_records;

CREATE TABLE integer_array_records(
  id serial PRIMARY KEY,
  integers integer[]
);

DROP TABLE IF EXISTS float_array_records;

CREATE TABLE float_array_records(
  id serial PRIMARY KEY,
  floats float[]
);

DROP TABLE IF EXISTS decimal_array_records;

CREATE TABLE decimal_array_records(
  id serial PRIMARY KEY,
  decimals decimal[]
);


-- In this upgrade I add a GUID column to feed items.
--
-- This is a unique identifier that the feed may specify. It's optional though.
--
-- I also remove the NOT NULL constraint on the last_update_time column. I'm
-- going to use it as NULL to know that we haven't polled a feed yet.

ALTER TABLE rss_item ADD COLUMN guid VARCHAR;
ALTER TABLE rss_item_archive ADD COLUMN guid VARCHAR;

ALTER TABLE rss_feed ALTER COLUMN last_update_time DROP NOT NULL;

Add unique index using guid

-- In this upgrade I add a GUID column to feed items.
--
-- This is a unique identifier that the feed may specify. It's optional though.
--
-- I also remove the NOT NULL constraint on the last_update_time column. I'm
-- going to use it as NULL to know that we haven't polled a feed yet.

ALTER TABLE rss_item ADD COLUMN guid VARCHAR;
ALTER TABLE rss_item_archive ADD COLUMN guid VARCHAR;

CREATE UNIQUE INDEX ON rss_item (rss_feed_id, guid);
CREATE UNIQUE INDEX ON rss_item_archive (rss_feed_id, guid);

ALTER TABLE rss_feed ALTER COLUMN last_update_time DROP NOT NULL;


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

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

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

Revert "Revert "Fix DDL on timestamp""

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

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

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


@&&run_dir_begin

prompt Creating type QUILT_REPORT_PROCESS_TYPE
@@quilt_report_process.tps

prompt Creating type QUILT_REPORT_TYPE
@@quilt_report_type.tps

prompt Creating type body QUILT_REPORT_PROCESS
@@quilt_report_process.tpb

prompt Creating type QUILT_OBJECT_TYPE
@@quilt_object_type.tps

prompt Creating type QUILT_OBJECT_LIST_TYPE
@@quilt_object_list_type.tps

@&&run_dir_end

Rename typu - oprava SQL skriptu pro nasazeni typu

@&&run_dir_begin

prompt Creating type QUILT_REPORT_PROCESS_TYPE
@@quilt_report_process_type.tps

prompt Creating type QUILT_REPORT_TYPE
@@quilt_report_type.tps

prompt Creating type body QUILT_REPORT_PROCESS_TYPE
@@quilt_report_process_type.tpb

prompt Creating type QUILT_OBJECT_TYPE
@@quilt_object_type.tps

prompt Creating type QUILT_OBJECT_LIST_TYPE
@@quilt_object_list_type.tps

@&&run_dir_end


drop database if exists apim;
create database apim;
use apim;
source ~/wso2am-2.1.0/dbscripts/apimgt/mysql5.7.sql

drop database if exists registry;
create database registry;
use registry;
source ~/wso2am-2.1.0/dbscripts/mysql5.7.sql

drop database if exists userstore;
create database userstore;
use userstore;
source ~/wso2am-2.1.0/dbscripts/mysql5.7.sql

Read SQL from temp Dir

drop database if exists apim;
create database apim;
use apim;
source /tmp/apimgt-mysql5.7.sql

drop database if exists registry;
create database registry;
use registry;
source /tmp/mysql5.7.sql

drop database if exists userstore;
create database userstore;
use userstore;
source /tmp/mysql5.7.sql


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

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

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

Switch index creation to online mode

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

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

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


create table :tablename as
select distinct
  xref.upi
from xref
left join rnc_rna_precomputed pre
on
  pre.upi = xref.upi
  and pre.taxid = xref.taxid
where
  pre.id is null
  or xref.last > pre.last_release
;

alter table :tablename
  add constraint fk_to_precompute__upi FOREIGN KEY (upi) REFERENCES rna(upi),
  add column id bigserial primary key
;

Remove temp precompute table before creating

drop table if exists :tablename;

create table :tablename as
select distinct
  xref.upi
from xref
left join rnc_rna_precomputed pre
on
  pre.upi = xref.upi
  and pre.taxid = xref.taxid
where
  pre.id is null
  or xref.last > pre.last_release
;

alter table :tablename
  add constraint fk_to_precompute__upi FOREIGN KEY (upi) REFERENCES rna(upi),
  add column id bigserial primary key
;


DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS posts_tags;
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS tags;

CREATE TABLE posts (
	id INT PRIMARY KEY AUTO_INCREMENT
	,title VARCHAR(255) NOT NULL
	,content TEXT NOT NULL
	,created_at TIMESTAMP NOT NULL DEFAULT NOW()
	,updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE tags (
	id INT PRIMARY KEY AUTO_INCREMENT
	,name VARCHAR(255) NOT NULL
	,created_at TIMESTAMP NOT NULL DEFAULT NOW()
	,updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE posts_tags (
	post_id INT NOT NULL
	,tag_id INT NOT NULL
	,PRIMARY KEY (post_id, tag_id)
	,FOREIGN KEY (post_id) REFERENCES posts (id)
	,FOREIGN KEY (tag_id) REFERENCES tags (id)
);

CREATE TABLE comments (
	id INT PRIMARY KEY AUTO_INCREMENT
	,post_id INT NOT NULL
	,author_name VARCHAR(255) NOT NULL
	,author_email VARCHAR(255) NOT NULL
	,content TEXT NOT NULL
	,created_at TIMESTAMP NOT NULL DEFAULT NOW()
	,updated_at TIMESTAMP NOT NULL DEFAULT NOW()
	,FOREIGN KEY (post_id) REFERENCES posts (id)
);

Revert "Fix error on creating test schema for MySQL"

DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS posts_tags;
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS tags;

CREATE TABLE posts (
	id INT PRIMARY KEY AUTO_INCREMENT
	,title VARCHAR(255) NOT NULL
	,content TEXT NOT NULL
	,created_at TIMESTAMP NOT NULL
	,updated_at TIMESTAMP NOT NULL
);

CREATE TABLE tags (
	id INT PRIMARY KEY AUTO_INCREMENT
	,name VARCHAR(255) NOT NULL
	,created_at TIMESTAMP NOT NULL
	,updated_at TIMESTAMP NOT NULL
);

CREATE TABLE posts_tags (
	post_id INT NOT NULL
	,tag_id INT NOT NULL
	,PRIMARY KEY (post_id, tag_id)
	,FOREIGN KEY (post_id) REFERENCES posts (id)
	,FOREIGN KEY (tag_id) REFERENCES tags (id)
);

CREATE TABLE comments (
	id INT PRIMARY KEY AUTO_INCREMENT
	,post_id INT NOT NULL
	,author_name VARCHAR(255) NOT NULL
	,author_email VARCHAR(255) NOT NULL
	,content TEXT NOT NULL
	,created_at TIMESTAMP NOT NULL
	,updated_at TIMESTAMP NOT NULL
	,FOREIGN KEY (post_id) REFERENCES posts (id)
);


UPDATE MINUTE_ENTRY_TYPE SET SORT_ID = 3 where DESCRIPTION='Adverse Events'
/
UPDATE MINUTE_ENTRY_TYPE SET SORT_ID = 5 where DESCRIPTION='Protocol'
/

Change default sort order (make Protocol and Protocol Review Comment equal)

UPDATE MINUTE_ENTRY_TYPE SET SORT_ID = 3 where DESCRIPTION='Adverse Events'
/
UPDATE MINUTE_ENTRY_TYPE SET SORT_ID = 5 where DESCRIPTION='Protocol'
/
UPDATE MINUTE_ENTRY_TYPE SET SORT_ID = 5 where DESCRIPTION='Protocol Reviewer Comment'
/


CREATE TABLE mail_manager_log
(
  id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  recipient VARCHAR(255) NOT NULL,
  subject VARCHAR(255) NOT NULL,
  body TEXT NOT NULL,
  log_time DATETIME NOT NULL
);

Set newline at end of file


BEGIN;

INSERT INTO company VALUES (1, 'Test Company 1');

INSERT INTO employee (person_id, name, firstname, login, password, driving_license, company_id) VALUES
    (1, 'admin', 'admin', 'admin', 'root', 1, NULL),
    (2, 'Tester', 'Tester', 'test_login', 'test_psswd', 0, NULL),
    (3, 'Tester', 'Hired', 'hired_tester', 'hired_tester_psswd', 1, 1);

COMMIT;

Add order column in SQL for tests

BEGIN;

INSERT INTO company VALUES (1, 'Test Company 1');

INSERT INTO employee (person_id, name, firstname, login, password, driving_license, "order", company_id) VALUES
    (1, 'admin', 'admin', 'admin', 'root', 1, 1, NULL),
    (2, 'Tester', 'Tester', 'test_login', 'test_psswd', 0, 2, NULL),
    (3, 'Tester', 'Hired', 'hired_tester', 'hired_tester_psswd', 1, 3, 1);

COMMIT;


COPY (
select distinct
    pre.so_rna_type
FROM rnc_rna_precomputed pre
where
  pre.taxid is not null
  and pre.is_active = true
  and pre.so_rna_type is not null
) TO STDOUT

Update so rna types query

COPY (
select distinct
    coalesce(pre.assigned_so_rna_type, pre.so_rna_type)
FROM rnc_rna_precomputed pre
where
  coalesce(pre.assigned_so_rna_type, pre.so_rna_type) is not null
) TO STDOUT


SELECT
FilePath = (
CASE c.PostType
WHEN 2 THEN 'articles-' + c.EntryName + '.aspx.markdown'
ELSE SUBSTRING(CONVERT(VARCHAR, DATEADD(hh, blog.TimeZoneOffset, c.DatePublishedUtc), 120), 1, 10) + '-' + c.EntryName + '.aspx.markdown'
END),
Content =
'---
layout: ' + (CASE c.PostType WHEN 2 THEN 'page' ELSE 'post' END) + '
title: "' + REPLACE(c.Title, '"', '"') + '"
date: ' + SUBSTRING(CONVERT(VARCHAR, DATEADD(hh, blog.TimeZoneOffset, c.DatePublishedUtc), 120), 1, 10) + '
comments: true
categories: [' +
 ISNULL(SUBSTRING(
   (SELECT ',' + cat.Title AS [text()]
    FROM subtext_LinkCategories cat
    INNER JOIN subtext_Links l
    ON l.CategoryID = cat.CategoryID
    WHERE c.ID = l.PostID
    FOR XML PATH ('')), 2, 1000), '') + ']
---
' + CAST(c.[Text] as NVARCHAR(MAX))
FROM subtext_Content c
INNER JOIN subtext_Config blog
ON c.BlogID = blog.BlogID
WHERE blog.BlogID = 0
 AND c.PostConfig & 1 = 1

Make sure pages show in articles subdir

SELECT
FilePath = (
CASE c.PostType
WHEN 2 THEN 'articles/' + c.EntryName + '.aspx.markdown'
ELSE SUBSTRING(CONVERT(VARCHAR, DATEADD(hh, blog.TimeZoneOffset, c.DatePublishedUtc), 120), 1, 10) + '-' + c.EntryName + '.aspx.markdown'
END),
Content =
'---
layout: ' + (CASE c.PostType WHEN 2 THEN 'page' ELSE 'post' END) + '
title: "' + REPLACE(c.Title, '"', '"') + '"
date: ' + SUBSTRING(CONVERT(VARCHAR, DATEADD(hh, blog.TimeZoneOffset, c.DatePublishedUtc), 120), 1, 10) + '
comments: true
categories: [' +
 ISNULL(SUBSTRING(
   (SELECT ',' + cat.Title AS [text()]
    FROM subtext_LinkCategories cat
    INNER JOIN subtext_Links l
    ON l.CategoryID = cat.CategoryID
    WHERE c.ID = l.PostID
    FOR XML PATH ('')), 2, 1000), '') + ']
---
' + CAST(c.[Text] as NVARCHAR(MAX))
FROM subtext_Content c
INNER JOIN subtext_Config blog
ON c.BlogID = blog.BlogID
WHERE blog.BlogID = 0
 AND c.PostConfig & 1 = 1


-- Note: pgTap is loaded by setup.sql
CREATE EXTENSION cat_tools;

-- Add any test dependency statements here

-- Used by several unit tests
\set no_use_role cat_tools_testing__no_use_role
\set use_role cat_tools_testing__use_role
CREATE ROLE :no_use_role;
CREATE ROLE :use_role;

GRANT cat_tools__usage TO :use_role;

Add bit to test upgrade

-- Note: pgTap is loaded by setup.sql
/*
CREATE EXTENSION cat_tools VERSION '0.1.0';
ALTER EXTENSION cat_tools UPDATE;
*/
CREATE EXTENSION cat_tools;

-- Add any test dependency statements here

-- Used by several unit tests
\set no_use_role cat_tools_testing__no_use_role
\set use_role cat_tools_testing__use_role
CREATE ROLE :no_use_role;
CREATE ROLE :use_role;

GRANT cat_tools__usage TO :use_role;


CREATE TABLE messages (
    id integer NOT NULL,
    title character varying(255),
    message text,
    "time" text
);

CREATE TABLE suivi_news (
    id integer NOT NULL,
    dt_creat character varying(255) NOT NULL,
    nom_log character varying(10) NOT NULL,
    ver_log character varying(10) NOT NULL,
    titre_new character varying(50) NOT NULL,
    url character varying(250) NOT NULL,
    nivutil character varying(25) DEFAULT NULL::character varying
);

Add constraint on pg tables

CREATE TABLE messages (
    id integer NOT NULL,
    title character varying(255),
    message text,
    "time" text
);

CREATE TABLE suivi_news (
    id integer NOT NULL,
    dt_creat character varying(255) NOT NULL,
    nom_log character varying(10) NOT NULL,
    ver_log character varying(10) NOT NULL,
    titre_new character varying(50) NOT NULL,
    url character varying(250) NOT NULL,
    nivutil character varying(25) DEFAULT NULL::character varying
);

ALTER TABLE ONLY messages
    ADD CONSTRAINT messages_pkey PRIMARY KEY (id);

ALTER TABLE ONLY suivi_news
    ADD CONSTRAINT suivi_news_pkey PRIMARY KEY (id);