Commit b46325e6 authored by Leo Gordon's avatar Leo Gordon
Browse files

dataflow & control rule adaptors were creating duplicated rules when storing - now fixed

parent 51b97944
......@@ -100,16 +100,24 @@ sub fetch_all {
=cut
sub store {
my ( $self, $rule ) = @_;
#print("\nAnalysisCtrlRuleAdaptor->store()\n");
my $sth = $self->prepare(q{INSERT ignore INTO analysis_ctrl_rule
(ctrled_analysis_id, condition_analysis_url) VALUES(?,?) });
if($sth->execute($rule->ctrled_analysis_id, $rule->condition_analysis_url)) {
$sth->finish();
}
$rule->adaptor( $self );
my ( $self, $rule ) = @_;
my $newly_inserted_rule = 0;
my $condition_analysis_url = $rule->condition_analysis_url;
my $ctrled_analysis_id = $rule->ctrled_analysis_id;
my $sth = $self->prepare("INSERT IGNORE INTO analysis_ctrl_rule (condition_analysis_url, ctrled_analysis_id) VALUES(?,?)");
my $rtnCode = $sth->execute($condition_analysis_url, $ctrled_analysis_id);
if($rtnCode and ($rtnCode != 0E0)) { # 0E0 is returned when the command succeeds, but 0 rows are updated (so in case of 'INSERT IGNORE' likely a key clash)
$newly_inserted_rule = 1;
$sth->finish();
} elsif(!$rtnCode) {
die "Could not create analysis_ctrl_rule('$condition_analysis_url', '$ctrled_analysis_id')";
}
$rule->adaptor( $self );
return $newly_inserted_rule;
}
......@@ -147,16 +155,16 @@ sub remove_by_condition_analysis_url {
sub create_rule {
my ($self, $conditionAnalysis, $ctrledAnalysis) = @_;
my ($self, $conditionAnalysis, $ctrledAnalysis) = @_;
return unless($conditionAnalysis and $ctrledAnalysis);
my $rule = Bio::EnsEMBL::Hive::AnalysisCtrlRule->new();
#must set ctrled_analysis first in order for internal logic to abreviate 'to_url'
$rule->ctrled_analysis($ctrledAnalysis);
$rule->condition_analysis($conditionAnalysis);
$self->store($rule);
return unless($conditionAnalysis and $ctrledAnalysis);
my $rule = Bio::EnsEMBL::Hive::AnalysisCtrlRule->new();
# NB: ctrled_analysis must be set first in order for internal logic to abbreviate 'to_url'
$rule->ctrled_analysis($ctrledAnalysis);
$rule->condition_analysis($conditionAnalysis);
return $self->store($rule);
}
############################
......@@ -273,5 +281,4 @@ sub _generic_fetch {
return $self->_objs_from_sth($sth);
}
1;
......@@ -70,6 +70,34 @@ sub fetch_from_analysis_id_branch_code {
return $self->_generic_fetch($constraint);
}
sub check_rule_exists_in_db {
my ( $self, $rule ) = @_;
my $from_analysis_id = $rule->from_analysis_id;
my $to_analysis_url = $rule->to_analysis_url;
my $branch_code = $rule->branch_code;
my $input_id_template = ref($rule->input_id_template) ? stringify($rule->input_id_template) : $rule->input_id_template;
my $sql = qq{
SELECT dataflow_rule_id
FROM dataflow_rule
WHERE from_analysis_id='$from_analysis_id'
AND to_analysis_url='$to_analysis_url'
AND branch_code='$branch_code'
AND input_id_template
} . ( defined($input_id_template) ? "='$input_id_template'" : "IS NULL" );
my $sth = $self->prepare($sql);
$sth->execute();
if(my ($dataflow_rule_id) = $sth->fetchrow()) {
$sth->finish;
return $dataflow_rule_id;
} else {
$sth->finish;
return 0;
}
}
=head2 store
......@@ -82,34 +110,37 @@ sub fetch_from_analysis_id_branch_code {
=cut
sub store {
my ( $self, $rule ) = @_;
my ( $self, $rule ) = @_;
my $dataflow_rule_id;
my $newly_inserted_rule = 0;
my $sth = $self->prepare( q{INSERT IGNORE INTO dataflow_rule (from_analysis_id, to_analysis_url, branch_code, input_id_template) VALUES (?,?,?,?) } );
my $template = ref($rule->input_id_template) ? stringify($rule->input_id_template) : $rule->input_id_template;
my $rtnCode = $sth->execute($rule->from_analysis_id, $rule->to_analysis_url, $rule->branch_code, $template);
if($rtnCode and $rtnCode != 0E0) {
$dataflow_rule_id = $sth->{'mysql_insertid'};
$sth->finish();
$rule->dbID($dataflow_rule_id);
$newly_inserted_rule = 1;
} else {
$sth->finish();
$sth = $self->prepare(q{SELECT dataflow_rule_id FROM dataflow_rule WHERE
from_analysis_id = ? AND to_analysis_url = ? AND branch_code = ?} );
$sth->execute($rule->from_analysis_id, $rule->to_analysis_url, $rule->branch_code);
$sth->bind_columns(\$dataflow_rule_id);
if($sth->fetch()) {
$rule->dbID($dataflow_rule_id);
my $dataflow_rule_id;
if($dataflow_rule_id = $self->check_rule_exists_in_db($rule)) {
$rule->dbID($dataflow_rule_id);
$rule->adaptor( $self );
return 0;
} else {
my $from_analysis_id = $rule->from_analysis_id;
my $to_analysis_url = $rule->to_analysis_url;
my $branch_code = $rule->branch_code;
my $input_id_template = ref($rule->input_id_template) ? stringify($rule->input_id_template) : $rule->input_id_template;
my $sth = $self->prepare("INSERT INTO dataflow_rule (from_analysis_id, to_analysis_url, branch_code, input_id_template) VALUES (?,?,?,?)");
my $rtnCode = $sth->execute($from_analysis_id, $to_analysis_url, $branch_code, $input_id_template);
if($rtnCode and ($rtnCode != 0E0)) { # we managed to insert a new row (0E0 would indicate success when no rows were inserted)
$dataflow_rule_id = $sth->{'mysql_insertid'};
$sth->finish();
$rule->dbID($dataflow_rule_id);
$rule->adaptor( $self );
return 1;
} else {
die "Could not create a dataflow_rule('$from_analysis_id', '$to_analysis_url', '$branch_code', '$input_id_template')";
}
}
$sth->finish;
}
$rule->adaptor( $self );
return $newly_inserted_rule;
}
......
......@@ -410,8 +410,11 @@ sub run {
# create control rules:
foreach my $condition_url (@$wait_for) {
if(my $condition_analysis = $analysis_adaptor->fetch_by_logic_name_or_url($condition_url)) {
$ctrl_rule_adaptor->create_rule( $condition_analysis, $analysis);
warn "Created Control rule: $condition_url -| $logic_name\n";
my $new_cfr = $ctrl_rule_adaptor->create_rule( $condition_analysis, $analysis);
my $cfr_action = $new_cfr ? 'Created a new' : 'Found an existing';
warn "$cfr_action Control rule: $condition_url -| $logic_name\n";
} else {
die "Could not fetch analysis '$condition_url' to create a control rule";
}
......@@ -431,9 +434,10 @@ sub run {
my $heir_analysis = $analysis_adaptor->fetch_by_logic_name_or_url($heir_url);
$dataflow_rule_adaptor->create_rule( $analysis, $heir_analysis || $heir_url, $branch_code, $input_id_template);
my $new_dfr = $dataflow_rule_adaptor->create_rule( $analysis, $heir_analysis || $heir_url, $branch_code, $input_id_template);
my $dfr_action = $new_dfr ? 'Created a new' : 'Found an existing';
warn "Created DataFlow rule: [$branch_code] $logic_name -> $heir_url"
warn "$dfr_action DataFlow rule: [$branch_code] $logic_name -> $heir_url"
.($input_id_template ? ' WITH TEMPLATE: '.stringify($input_id_template) : '')."\n";
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment