Revision 9281a92a
Von Tamino Steinert vor etwa 1 Monat hinzugefügt
sql/Pg-upgrade2/add_file_version.pl | ||
---|---|---|
9 | 9 |
use SL::DB::File; |
10 | 10 |
use SL::File::Backend::Webdav; |
11 | 11 |
|
12 |
use SL::Locale::String qw(t8); |
|
12 | 13 |
use SL::System::Process; |
13 | 14 |
|
14 | 15 |
use UUID::Tiny ':std'; |
... | ... | |
16 | 17 |
use parent qw(SL::DBUpgrade2::Base); |
17 | 18 |
|
18 | 19 |
sub get_all_versions { |
19 |
my ($fileobj) = @_;
|
|
20 |
my ($dbfile) = @_;
|
|
20 | 21 |
|
21 | 22 |
my @versionobjs; |
23 |
my $fileobj = SL::File::Object->new( |
|
24 |
db_file => $dbfile, |
|
25 |
id => $dbfile->id, |
|
26 |
loaded => 1, |
|
27 |
); |
|
22 | 28 |
|
23 |
my $maxversion = $fileobj->version_count;
|
|
29 |
my $maxversion = $fileobj->backend eq 'Filesystem' ? $fileobj->db_file->backend_data : 1;
|
|
24 | 30 |
$fileobj->version($maxversion); |
25 | 31 |
push @versionobjs, $fileobj; |
26 | 32 |
if ($maxversion > 1) { |
... | ... | |
31 | 37 |
push @versionobjs, $clone; |
32 | 38 |
} |
33 | 39 |
} |
40 |
|
|
41 |
return @versionobjs; |
|
42 |
} |
|
43 |
|
|
44 |
sub get_filepath { |
|
45 |
my ($file_obj) = @_; |
|
46 |
|
|
47 |
my $file_id = $file_obj->id; |
|
48 |
my $backend = $file_obj->backend |
|
49 |
or die "File with ID '$file_id' has no backend specified."; |
|
50 |
my $db_file = $file_obj->db_file; |
|
51 |
my $file_name = $file_obj->file_name; |
|
52 |
my $version = $file_obj->version; |
|
53 |
|
|
54 |
my $path; |
|
55 |
if ($backend eq 'Webdav') { |
|
56 |
($path) = $file_obj->backend_class->webdav_path($db_file); |
|
57 |
} elsif ($backend eq 'Filesystem') { |
|
58 |
# use filesystem with depth 3 |
|
59 |
my $iddir = sprintf("%04d", $db_file->id % 1000); |
|
60 |
my $base_path = File::Spec->catdir($::lx_office_conf{paths}->{document_path}, $::auth->client->{id}, $iddir, $db_file->id); |
|
61 |
$path = File::Spec->catdir($base_path, $db_file->id . '_' . $version); |
|
62 |
} else { |
|
63 |
die "Unknown backend '$backend' for file with ID '$file_id'."; |
|
64 |
} |
|
65 |
|
|
66 |
die "No file found at $path. Expected: $file_name, file.id: $file_id" if !-f $path; |
|
67 |
return $path; |
|
68 |
} |
|
69 |
|
|
70 |
sub test_all_files_exists { |
|
71 |
my @errors; |
|
72 |
|
|
73 |
my $all_dbfiles = SL::DB::Manager::File->get_all; |
|
74 |
|
|
75 |
foreach my $dbfile (@{$all_dbfiles}) { |
|
76 |
my @versions = get_all_versions($dbfile); |
|
77 |
foreach my $version (@versions) { |
|
78 |
eval { |
|
79 |
get_filepath($version); |
|
80 |
} or do { |
|
81 |
push @errors, $@; |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
return @errors; |
|
87 |
} |
|
88 |
|
|
89 |
sub print_errors { |
|
90 |
my ($self, $errors) = @_; |
|
91 |
|
|
92 |
print $::form->parse_html_template("dbupgrade/add_file_version_form", { |
|
93 |
ERRORS => $errors |
|
94 |
}); |
|
34 | 95 |
} |
35 | 96 |
|
36 | 97 |
sub run { |
37 | 98 |
my ($self) = @_; |
38 | 99 |
|
100 |
my $query = <<SQL; |
|
101 |
SELECT f.id |
|
102 |
FROM files f |
|
103 |
FULL OUTER JOIN file_versions fv ON (f.id = fv.file_id) |
|
104 |
GROUP BY f.id |
|
105 |
HAVING count(fv.file_id) = 0 |
|
106 |
SQL |
|
107 |
my @file_ids_without_version = map {$_->[0]} @{$self->dbh->selectall_arrayref($query)}; |
|
108 |
|
|
109 |
unless ($::form->{delete_file_entries_of_missing_files}) { |
|
110 |
my @errors = $self->test_all_files_exists(); |
|
111 |
if (scalar @errors) { |
|
112 |
$self->print_errors(\@errors); |
|
113 |
return 2; |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
39 | 117 |
SL::DB->client->with_transaction(sub { |
40 | 118 |
my @errors; |
41 | 119 |
my $all_dbfiles = SL::DB::Manager::File->get_all; |
... | ... | |
57 | 135 |
foreach my $version (@versions) { |
58 | 136 |
my $tofile; |
59 | 137 |
eval { |
60 |
$tofile = $version->get_file();
|
|
138 |
$tofile = get_filepath($version);
|
|
61 | 139 |
} or do { |
62 |
my @values; |
|
63 |
push @values, $@; # error message |
|
64 |
push @values, $version->file_name; |
|
65 |
push @values, $version->id; |
|
66 |
push @errors, '<td>' . join('</td><td>', @values) . '</td>';; |
|
67 | 140 |
next; |
68 | 141 |
}; |
69 | 142 |
my $rel_file = $tofile; |
70 | 143 |
$rel_file =~ s/$doc_path//; |
71 | 144 |
|
72 | 145 |
my $fv = SL::DB::FileVersion->new( |
73 |
file_id => $dbfile->id,
|
|
74 |
version => $version->version || 1,
|
|
75 |
file_location => $rel_file,
|
|
76 |
doc_path => $doc_path,
|
|
77 |
backend => $dbfile->backend,
|
|
78 |
guid => create_uuid_as_string(UUID_V4),
|
|
79 |
)->save;
|
|
146 |
file_id => $dbfile->id, |
|
147 |
version => $version->version || 1, |
|
148 |
file_location => $rel_file, |
|
149 |
doc_path => $doc_path, |
|
150 |
backend => $dbfile->backend, |
|
151 |
guid => create_uuid_as_string(UUID_V4), |
|
152 |
)->save; |
|
80 | 153 |
} |
81 | 154 |
} |
82 |
if (scalar @errors) { |
|
83 |
my $error_message = <<MESSAGE; |
|
84 |
Please resolve the errors by deleting the invalid database entries or by adding the corresponding files to the expected paths. |
|
85 |
To delete the invalid database entries, run the 'RemoveInvalidFileEntries' background job. Before running the background job, ensure that these files are no longer required. |
|
86 |
<table class="tbl-list" border="1" style="border-collapse: collapse"> |
|
87 |
<thead><tr> |
|
88 |
<th>error message</th> |
|
89 |
<th>file_name</th> |
|
90 |
<th>file_id</th> |
|
91 |
</tr></thead> |
|
92 |
MESSAGE |
|
93 |
$error_message .= '<tr>' . join('</tr><tr>', @errors) . '</tr>'; |
|
94 |
$error_message .= '</table>'; |
|
95 |
die $error_message; |
|
155 |
|
|
156 |
my $query = <<SQL; |
|
157 |
SELECT f.id |
|
158 |
FROM files f |
|
159 |
FULL OUTER JOIN file_versions fv ON (f.id = fv.file_id) |
|
160 |
GROUP BY f.id |
|
161 |
HAVING count(fv.file_id) = 0 |
|
162 |
SQL |
|
163 |
my @file_ids_without_version = |
|
164 |
map {$_->[0]} |
|
165 |
@{SL::DB::->client->dbh->selectall_arrayref($query)}; |
|
166 |
if (scalar @file_ids_without_version) { |
|
167 |
if ($::form->{delete_file_entries_of_missing_files}) { |
|
168 |
SL::DB::Manager::File->delete_all(where => [id => \@file_ids_without_version]); |
|
169 |
} else { |
|
170 |
die "Files without versions: " . join(', ', @file_ids_without_version); |
|
171 |
} |
|
96 | 172 |
} |
173 |
|
|
97 | 174 |
1; |
98 | 175 |
}) or do { die SL::DB->client->error }; |
99 | 176 |
|
Auch abrufbar als: Unified diff
FileVersion: Migration: Interaktives Update