sql for granting web_usr roles to breedbase db. Useful when the roles get messed...
[sgn-devtools.git] / one-offs / migrate_images.pl
blobfdd05f3cb04d131dbc5b6e4b676dff63ead4e907
2 =head1 NAME
4 migrate_images.pl - image migration script
6 =head1 DESCRIPTION
8 migrates the images from a id-based directory structure to a 2 byte stemmed directory structure based on md5sums.
10 Requires the ImageAddMD5sum.pm db patch (available in cxgn/sgn/db).
12 Options:
14 =over 5
16 =item -D
18 database name (default sandbox)
20 =item -H
22 host name (default localhost)
24 =item -o
26 old image location directory (default /data/prod/public/images/image_files)
28 =item -n
30 new image location directory (default /data/prod/public/images/new_image_files)
32 =back
34 =head1 AUTHOR
36 Lukas Mueller
38 =cut
40 use strict;
42 use Getopt::Std;
44 use CXGN::DB::InsertDBH;
45 use CXGN::Image;
47 our($opt_o, $opt_n, $opt_H, $opt_D);
49 getopts('o:n:H:D:');
51 my $dbh = CXGN::DB::InsertDBH->new( {
52 dbname => $opt_D || 'sandbox',
53 dbhost => $opt_H || 'localhost',
54 });
56 my $old_image_dir = $opt_o || '/data/prod/public/images/image_files';
57 my $new_image_dir = $opt_n || '/data/prod/public/images/new_image_files';
59 my $sql = "SELECT image_id FROM metadata.md_image where obsolete !='t' order by image_id";
60 my $sth = $dbh->prepare($sql);
61 $sth->execute();
63 my %md5sums;
64 my $processed=0;
65 my @not_found = ();
67 while (my ($image_id) = $sth->fetchrow_array()) {
69 my $old_image = CXGN::Image->new(dbh=>$dbh, image_id=>$image_id, image_dir=> $old_image_dir);
71 my $i = CXGN::Image->new(dbh=>$dbh, image_id=>$image_id, image_dir=>$new_image_dir);
74 if (! -d $old_image_dir."/".$image_id) {
75 push @not_found, "$image_id ".$i->get_name()."\n";
76 print STDERR "\nNot found image: $image_id\n";
77 next();
80 my $filepath = $old_image_dir."/".$image_id."/".$old_image->get_original_filename().$old_image->get_file_ext();
81 if (! -e $filepath) {
82 print STDERR "WARNING! $filepath does not exist!\n";
83 next();
85 my $md5sum = $i -> calculate_md5sum($filepath);
87 $md5sums{$md5sum}++;
90 $i->set_md5sum($md5sum);
91 $i->store();
92 $dbh->commit();
93 $i->make_dirs();
96 $i->copy_location($old_image_dir."/".$image_id);
98 $processed++;
99 print STDERR "Processing image $image_id \r";
102 foreach my $nf (@not_found) {
103 print STDERR $nf;
107 print STDERR "\nProcessed $processed images, generating ".scalar(keys(%md5sums))." unique md5 keys.\n";