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

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

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

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

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

alter table regulatory_feature_feature_set drop column feature_set_id;

rename table regulatory_feature_feature_set to regulatory_activity;

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

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

delete from feature_set where type = "regulatory";

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

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

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

Remove regulatory build entries from feature_set table, relink everything else.
Remove regulatory build entries from feature_set table, relink everything else.
