Merge pull request #4989 from solgenomics/timestamp_fixes
[sgn.git] / bin / delete_trials.pl
blob0771a686c11657e4d2e49541154b7a900fe9baf2
2 =head1 NAME
4 delete_trials.pl - script to delete trials
6 =head1 DESCRIPTION
8 perl delete_trials.pl -i trial_id -H host -D dbname -U dbuser -P dbpass -b basepath -r temp_file_nd_experiment_id
10 Deletes trials that whose ids are provided as a comma separated list for the -i parameter.
11 First, it deletes metadata, then trial layouts, then phenotypes, and finally the trial entry in the project table. All deletes are hard deletes. There is no way of bringing the trial back, except from a backup. So be careful!
13 =head1 AUTHOR
15 Lukas Mueller <lam87@cornell.edu>
17 =cut
19 use strict;
21 use Getopt::Std;
22 use Bio::Chado::Schema;
23 use CXGN::Metadata::Schema;
24 use CXGN::Phenome::Schema;
25 use CXGN::DB::InsertDBH;
26 use CXGN::Trial;
28 our ($opt_H, $opt_D, $opt_U, $opt_P, $opt_b, $opt_i, $opt_n, $opt_t, $opt_r);
30 getopts('H:D:U:P:b:i:t:r:n');
32 my $dbhost = $opt_H;
33 my $dbname = $opt_D;
34 my $dbuser = $opt_U;
35 my $dbpass = $opt_P;
36 my $trial_ids = $opt_i;
37 my $trial_names = $opt_t;
38 my $non_interactive = $opt_n;
40 my $dbh = CXGN::DB::InsertDBH->new( { dbhost=>$dbhost,
41 dbname=>$dbname,
42 dbargs => {AutoCommit => 0,
43 RaiseError => 1}
47 print STDERR "Connecting to database...\n";
48 my $schema= Bio::Chado::Schema->connect( sub { $dbh->get_actual_dbh() } );
49 my $metadata_schema = CXGN::Metadata::Schema->connect( sub { $dbh->get_actual_dbh() });
50 my $phenome_schema = CXGN::Phenome::Schema->connect( sub { $dbh->get_actual_dbh() });
52 my @trial_ids = split ",", $trial_ids;
53 my @trial_names = split ",", $trial_names;
55 foreach my $name (@trial_names) {
56 my $trial = $schema->resultset("Project::Project")->find( { name => $name });
57 if (!$trial) { print STDERR "Trial $name not found. Skipping...\n"; next; }
58 push @trial_ids, $trial->project_id();
61 foreach my $trial_id (@trial_ids) {
62 print STDERR "Retrieving trial information for trial $trial_id...\n";
64 my $t = CXGN::Trial->new({
65 bcs_schema => $schema,
66 metadata_schema => $metadata_schema,
67 phenome_schema => $phenome_schema,
68 trial_id => $trial_id
69 });
71 my $answer = "";
72 if (!$non_interactive) {
73 print $t->get_name().", ".$t->get_description().". Delete? ";
74 $answer = <>;
76 if ($non_interactive || $answer =~ m/^y/i) {
77 eval {
78 delete_trial($metadata_schema, $phenome_schema, $t);
80 if ($@) {
81 print STDERR "An error occurred trying to delete trial ".$t->get_name()." ($@)\n";
82 $dbh->rollback();
84 else {
85 $dbh->commit();
86 print STDERR "Trial ".$t->get_name()." successfully deleted\n";
93 $dbh->disconnect();
94 print STDERR "Done with everything (though nd_experiment entry deletion may still be occuring asynchronously).\n";
96 sub delete_trial {
97 my $metadata_schema = shift;
98 my $phenome_schema = shift;
99 my $t = shift;
101 print STDERR "Deleting trial ".$t->get_name()."\n";
102 print STDERR "Delete metadata...\n";
103 $t->delete_metadata();
104 print STDERR "Deleting phenotypes...\n";
105 $t->delete_phenotype_data($opt_b, $dbhost, $dbname, $dbuser, $dbpass, $opt_r);
106 print STDERR "Deleting layout...\n";
107 $t->delete_field_layout();
108 print STDERR "Delete project entry...\n";
109 $t->delete_project_entry();