From b44049954d71427ff634384b314536c6a6a5b6d9 Mon Sep 17 00:00:00 2001 From: Naama Menda Date: Sat, 17 Sep 2011 05:20:10 -0400 Subject: [PATCH] using txn_do for the transaction --- .../solcap/load_solcap_TA_phenotypes.pl | 36 ++++++++++++---------- .../solcap/load_solcap_data_entry.pl | 33 ++++++++++---------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/bin/loading_scripts/solcap/load_solcap_TA_phenotypes.pl b/bin/loading_scripts/solcap/load_solcap_TA_phenotypes.pl index 2f206a3..4a4d463 100644 --- a/bin/loading_scripts/solcap/load_solcap_TA_phenotypes.pl +++ b/bin/loading_scripts/solcap/load_solcap_TA_phenotypes.pl @@ -49,7 +49,7 @@ use Date::Calc qw( use File::Basename; use Carp qw /croak/ ; - +use Try::Tiny; our ($opt_H, $opt_D, $opt_i, $opt_t, $opt_l); @@ -136,15 +136,14 @@ my $spreadsheet=CXGN::Tools::File::Spreadsheet->new($file, 2); my @rows = $spreadsheet->row_labels(); my @columns = $spreadsheet->column_labels(); -eval { +my $coderef = sub { foreach my $row_label (@rows ) { #$plot number is the row label. Need to get the matching stock print "label # = $row_label\n"; my $plot = $spreadsheet->value_at($row_label, "Plot #"); - - ########################################## + ########################################## # find the child stock based on plot name my ($stock) = $schema->resultset("Stock::Stockprop")->search( { value => $location, @@ -184,9 +183,9 @@ eval { my ($db_name, $sp_accession) = split (/\:/ , $label); next() if (!$sp_accession); - if (!$value || $value =~ /\./) { next; } ; + if (!$value || $value =~ /^\.$/) { next; } ; - my ($sp_term) = $schema->resultset("General::Db")->find( { + my ($sp_term) = $schema->resultset("General::Db")->find( { name => $db_name } )->find_related("dbxrefs", { accession=>$sp_accession , } )->search_related("cvterm"); @@ -227,18 +226,21 @@ eval { #print "Loaded phenotype_cvterm with cvterm '" . $unit_cvterm->name() . " '\n" if $unit_cvterm ; } } + if ($opt_t) { + die "TEST RUN! rolling back\n"; + } }; -if ($@) { print "An error occured! Rolling backl!\n\n $@ \n\n "; } -elsif ($opt_t) { - print "TEST RUN. Rolling back and reseting database sequences!!\n\n"; + +try { + $schema->txn_do($coderef); + print "Transaction succeeded! Commiting phenotyping experiments! \n\n" if !$opt_t; +} catch { + # Transaction failed foreach my $value ( keys %seq ) { - my $maxval= $seq{$value} || 0; - if ($maxval) { $dbh->do("SELECT setval ('$value', $maxval, true)") ; } - else { $dbh->do("SELECT setval ('$value', 1, false)"); } + my $maxval= $seq{$value} || 0; + if ($maxval) { $dbh->do("SELECT setval ('$value', $maxval, true)") ; } + else { $dbh->do("SELECT setval ('$value', 1, false)"); } } - $dbh->rollback; -}else { - print "Transaction succeeded! Commiting phenotyping experiments! \n\n"; - $dbh->commit(); -} + die "An error occured! Rolling back and reseting database sequences! " . $_ . "\n"; +}; diff --git a/bin/loading_scripts/solcap/load_solcap_data_entry.pl b/bin/loading_scripts/solcap/load_solcap_data_entry.pl index e13a7d6..7b4023f 100644 --- a/bin/loading_scripts/solcap/load_solcap_data_entry.pl +++ b/bin/loading_scripts/solcap/load_solcap_data_entry.pl @@ -46,6 +46,7 @@ use Date::Calc qw( check_date ); use Carp qw /croak/ ; +use Try::Tiny; our ($opt_H, $opt_D, $opt_i, $opt_t); @@ -135,8 +136,7 @@ my $unit_cv = $schema->resultset("Cv::Cv")->find( my @rows = $spreadsheet->row_labels(); my @columns = $spreadsheet->column_labels(); -eval { - +my $coderef = sub { foreach my $uniq_id (@rows ) { my $sct = $spreadsheet->value_at($uniq_id, "Line #"); my $plot = $spreadsheet->value_at($uniq_id, "Plot Number"); @@ -186,7 +186,7 @@ eval { COLUMN: foreach my $label (@columns) { my $value = $spreadsheet->value_at($uniq_id, $label); ($value, undef) = split (/\s/, $value) ; - $value = undef if $value =~ /\./ ; #for some reason data files came with '.' instead of empty cells. + $value = undef if $value =~ /^\.$/ ; #for some reason data files came with '.' instead of empty cells. #sp terms have a label to determine if these have a scale or a quantitative unit my ($term, $type) = split (/\|/ , $label) ; @@ -289,20 +289,21 @@ eval { print "Loaded phenotype_cvterm with cvterm '" . $unit_cvterm->name() . " '\n" if $unit_cvterm ; } } + if ($opt_t) { + die "TEST RUN! rolling back\n"; + } }; -if ($@) { print "An error occured! Rolling backl!\n\n $@ \n\n "; } -elsif ($opt_t) { - print "TEST RUN. Rolling back and reseting database sequences!!\n\n"; - foreach my $value ( keys %seq ) { - my $maxval= $seq{$value} || 0; - if ($maxval) { $dbh->do("SELECT setval ('$value', $maxval, true)") ; } - else { $dbh->do("SELECT setval ('$value', 1, false)"); } +try { + $schema->txn_do($coderef); + if (!$opt_t) { print "Transaction succeeded! Commiting phenotyping experiments! \n\n"; } +} catch { + # Transaction failed + foreach my $value ( keys %seq ) { + my $maxval= $seq{$value} || 0; + if ($maxval) { $dbh->do("SELECT setval ('$value', $maxval, true)") ; } + else { $dbh->do("SELECT setval ('$value', 1, false)"); } } - $dbh->rollback; - -}else { - print "Transaction succeeded! Commiting phenotyping experiments! \n\n"; - $dbh->commit(); -} + die "An error occured! Rolling back and reseting database sequences!" . $_ . "\n"; +}; -- 2.11.4.GIT