Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / db / 00046 / DeleteNullLocalSynonyms.pm
blobbe1226641d04a1801b78cbdaf14541c75fdb5b38
1 #!/usr/bin/env perl
4 =head1 NAME
6 DeleteMullLocalSynonyms
8 =head1 SYNOPSIS
10 mx-run ThisPackageName [options] -H hostname -D dbname -u username [-F]
12 this is a subclass of L<CXGN::Metadata::Dbpatch>
13 see the perldoc of parent class for more details.
15 =head1 DESCRIPTION
17 This patch deletes the ubiquitous 'synonym' cvterms with cv_id of 'null' or 'local'
18 synonyms should be loaded with an explicit cv_id providing the right context, e.g. cvterm.name = stock_synoym cv_id = stock_property, organism_synonym cv_id = organism_property
19 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
21 =head1 AUTHOR
23 Naama Menda<nm249@cornell.edu>
25 =head1 COPYRIGHT & LICENSE
27 Copyright 2010 Boyce Thompson Institute for Plant Research
29 This program is free software; you can redistribute it and/or modify
30 it under the same terms as Perl itself.
32 =cut
35 package DeleteNullLocalSynonyms ;
37 use Moose;
38 use Bio::Chado::Schema;
39 use Try::Tiny;
41 extends 'CXGN::Metadata::Dbpatch';
44 has '+description' => ( default => <<'' );
45 This patch will find cvterms with name ilike synonyms and cv.name
46 local or null and will delete those.
47 this is important for using only synonyms with an explicit cvterm.name and cv.name.
49 has '+prereq' => (
50 default => sub {
51 ['UpdateOrganismSynonymCvterms', 'UpdateStockSynonymCvterms' ],
55 sub patch {
56 my $self=shift;
58 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
60 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
62 print STDOUT "\nExecuting the SQL commands.\n";
63 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
66 #find or create cvterm with name stock_synonym
67 #make sure it has a dbxref of autocreated:stock_synonym and db = null
68 ##there might be an existing dbxref with accession = autocreated:synonym
69 #we will not be using that one anymore for distincting between synonyms of different entities.
70 my $coderef = sub {
73 ###########################
75 #find all cvterms with name ilike 'synonym%' and cv.name = local
76 # delete the cvterm of name = synonym and cv name = "local" or "null"
78 my $local_syn_cvterms = $schema->resultset("Cv::Cvterm")->search(
80 'me.name' => { ilike => 'synonym%' },
81 'cv.name' => 'local'
84 join => 'cv'
88 print "** found " . $local_syn_cvterms->count . " cv.name=local cvterms \n\n";
90 if ($local_syn_cvterms ) {
91 print "**Deleting ... \n";
92 $local_syn_cvterms->delete;
94 #############################
96 my $null_syn_cvterms = $schema->resultset("Cv::Cvterm")->search(
98 'me.name' => { ilike => 'synonym%' },
99 'cv.name' => 'null'
102 join => 'cv'
106 print "** found " . $null_syn_cvterms->count . " cv.name=null cvterms \n\n";
108 if ($null_syn_cvterms) {
109 print "**Deleting ... \n";
110 $null_syn_cvterms->delete;
113 ###############################
115 if ($self->trial) {
116 print "Trial mode! Rolling back transaction\n\n";
117 $schema->txn_rollback;
118 return 0;
120 return 1;
123 try {
124 $schema->txn_do($coderef);
126 } catch {
127 die "Load failed! " . $_ . "\n" ;
131 print "You're done!\n";
135 ####
136 1; #
137 ####