summaryrefslogtreecommitdiffstats
path: root/scripts/migrate-attachments.pl
blob: 2de7c602cd0143b51e067cc3f0fa4ea294864016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use strict;
use warnings;
use lib qw(. lib local/lib/perl5);
$| = 1;


use Bugzilla;
use Bugzilla::Attachment;
use Bugzilla::Install::Util qw(indicate_progress);
use Getopt::Long qw(GetOptions);

my @storage_names = Bugzilla::Attachment->get_storage_names();

my %options;
GetOptions(\%options, 'mirror=s@{2}', 'copy=s@{2}', 'delete=s') or exit(1);
unless ($options{mirror} || $options{copy} || $options{delete}) {
  die <<EOF;
Syntax:
    migrate-attachments.pl --mirror source destination
    migrate-attachments.pl --copy source destination
    migrate-attachments.pl --delete source

'mirror'
    Copies all attachments from the specified source to the destination.
    Attachments which already exist at the destination will not be copied
    again. Attachments deleted on the source will be deleted from the
    destination.

    eg. migrate-attachments.pl --mirror database s3

'copy'
    Copies all attachments from the specified source to the destination.
    Attachments which already exist at the destination will not be copied
    again. Unlike 'mirror', attachments deleted from the source will not be
    removed from the destination.

    eg. migrate-attachments.pl --copy database s3

'delete'
    Deletes all attachments in the specified location.  This operation cannot
    be undone.

    eg. migrate-attachments.pl --delete database

Valid locations:
    @storage_names

EOF
}

my $dbh = Bugzilla->dbh;

if ($options{mirror}) {
  if ($options{mirror}->[0] eq $options{mirror}->[1]) {
    die "Source and destination must be different\n";
  }
  my ($source, $dest) = map { storage($_) } @{$options{mirror}};

  my ($total) = $dbh->selectrow_array("SELECT COUNT(*) FROM attachments");
  confirm(sprintf(
    'Mirror %s attachments from %s to %s?', $total, @{$options{mirror}}));

  my $sth = $dbh->prepare(
    "SELECT attach_id, attach_size FROM attachments ORDER BY attach_id DESC");
  $sth->execute();
  my ($count, $deleted, $stored) = (0, 0, 0);
  while (my ($attach_id, $attach_size) = $sth->fetchrow_array()) {
    indicate_progress({total => $total, current => ++$count});

    # remove deleted attachments
    if ($attach_size == 0 && $dest->exists($attach_id)) {
      $dest->remove($attach_id);
      $deleted++;
    }

    # store attachments that don't already exist
    elsif ($attach_size != 0 && !$dest->exists($attach_id)) {
      if (my $data = $source->retrieve($attach_id)) {
        $dest->store($attach_id, $data);
        $stored++;
      }
    }
  }
  print "\n";
  print "Attachments stored: $stored\n";
  print "Attachments deleted: $deleted\n" if $deleted;
}

elsif ($options{copy}) {
  if ($options{copy}->[0] eq $options{copy}->[1]) {
    die "Source and destination must be different\n";
  }
  my ($source, $dest) = map { storage($_) } @{$options{copy}};

  my ($total)
    = $dbh->selectrow_array(
    "SELECT COUNT(*) FROM attachments WHERE attach_size != 0");
  confirm(sprintf(
    'Copy %s attachments from %s to %s?', $total, @{$options{copy}}));

  my $sth
    = $dbh->prepare(
    "SELECT attach_id, attach_size FROM attachments WHERE attach_size != 0 ORDER BY attach_id DESC"
    );
  $sth->execute();
  my ($count, $stored) = (0, 0);
  while (my ($attach_id, $attach_size) = $sth->fetchrow_array()) {
    indicate_progress({total => $total, current => ++$count});

    # store attachments that don't already exist
    if (!$dest->exists($attach_id)) {
      if (my $data = $source->retrieve($attach_id)) {
        $dest->store($attach_id, $data);
        $stored++;
      }
    }
  }
  print "\n";
  print "Attachments stored: $stored\n";
}

elsif ($options{delete}) {
  my $storage = storage($options{delete});
  my ($total)
    = $dbh->selectrow_array(
    "SELECT COUNT(*) FROM attachments WHERE attach_size != 0");
  confirm(sprintf('DELETE %s attachments from %s?', $total, $options{delete}));

  my $sth
    = $dbh->prepare(
    "SELECT attach_id FROM attachments WHERE attach_size != 0 ORDER BY attach_id DESC"
    );
  $sth->execute();
  my ($count, $deleted) = (0, 0);
  while (my ($attach_id) = $sth->fetchrow_array()) {
    indicate_progress({total => $total, current => ++$count});
    if ($storage->exists($attach_id)) {
      $storage->remove($attach_id);
      $deleted++;
    }
  }
  print "\n";
  print "Attachments deleted: $deleted\n";
}

sub storage {
  my ($name) = @_;
  my $storage = Bugzilla::Attachment::get_storage_by_name($name)
    or die "Invalid attachment location: $name\n";
  return $storage;
}

sub confirm {
  my ($prompt) = @_;
  print $prompt, "\n\nPress <Ctrl-C> to stop or <Enter> to continue..\n";
  getc();
}