Revision a6a8a9a8
Von Moritz Bunkus vor fast 18 Jahren hinzugefügt
scripts/dbupgrade2_tool.pl | ||
---|---|---|
9 | 9 |
push(@INC, "modules"); |
10 | 10 |
} |
11 | 11 |
|
12 |
use English '-no_match_vars'; |
|
13 |
|
|
12 | 14 |
use DBI; |
13 | 15 |
use Data::Dumper; |
14 | 16 |
use Getopt::Long; |
... | ... | |
18 | 20 |
$lxdebug = LXDebug->new(); |
19 | 21 |
|
20 | 22 |
use SL::Form; |
23 |
use SL::User; |
|
21 | 24 |
use SL::Locale; |
22 | 25 |
use SL::DBUpgrade2; |
26 |
use SL::DBUtils; |
|
23 | 27 |
|
24 | 28 |
####### |
25 | 29 |
####### |
26 | 30 |
####### |
27 | 31 |
|
32 |
my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help); |
|
33 |
my ($opt_user, $opt_apply); |
|
34 |
|
|
35 |
our (%myconfig, $form, $user); |
|
36 |
|
|
28 | 37 |
sub show_help { |
29 | 38 |
print("dbupgrade2_tool.pl [--list] [--tree] [--rtree] [--graphviz]\n" . |
30 |
" [--nodepds] [--help]\n"); |
|
39 |
" [--nodepds] [--user=name --apply=tag] [--help]\n"); |
|
40 |
} |
|
41 |
|
|
42 |
sub error { |
|
31 | 43 |
} |
32 | 44 |
|
33 | 45 |
sub calc_rev_depends { |
... | ... | |
144 | 156 |
"\n\n"); |
145 | 157 |
} |
146 | 158 |
|
159 |
sub apply_upgrade { |
|
160 |
my $name = shift; |
|
161 |
|
|
162 |
$form->error("Unknown dbupgrade tag '$name'") if (!$controls->{$name}); |
|
163 |
|
|
164 |
my (@order, %tags); |
|
165 |
|
|
166 |
build_upgrade_order($name, \@order, \%tags); |
|
167 |
|
|
168 |
my @upgradescripts = map { $controls->{$_}->{"applied"} = 0; $controls->{$_} } @order; |
|
169 |
|
|
170 |
my $dbh = $form->dbconnect_noauto(\%myconfig); |
|
171 |
|
|
172 |
$dbh->{PrintWarn} = 0; |
|
173 |
$dbh->{PrintError} = 0; |
|
174 |
|
|
175 |
$user->create_schema_info_table($form, $dbh); |
|
176 |
|
|
177 |
my $query = qq|SELECT tag FROM schema_info|; |
|
178 |
$sth = $dbh->prepare($query); |
|
179 |
$sth->execute() || $form->dberror($query); |
|
180 |
while (($tag) = $sth->fetchrow_array()) { |
|
181 |
$controls->{$tag}->{"applied"} = 1 if defined $controls->{$tag}; |
|
182 |
} |
|
183 |
$sth->finish(); |
|
184 |
|
|
185 |
my $all_applied = 1; |
|
186 |
foreach (@upgradescripts) { |
|
187 |
if (!$_->{"applied"}) { |
|
188 |
$all_applied = 0; |
|
189 |
last; |
|
190 |
} |
|
191 |
} |
|
192 |
|
|
193 |
if ($all_applied) { |
|
194 |
print "The upgrade has already been applied.\n"; |
|
195 |
exit 0; |
|
196 |
} |
|
197 |
|
|
198 |
foreach my $control (@upgradescripts) { |
|
199 |
next if ($control->{"applied"}); |
|
200 |
|
|
201 |
$control->{"file"} =~ /\.(sql|pl)$/; |
|
202 |
my $file_type = $1; |
|
203 |
|
|
204 |
# apply upgrade |
|
205 |
print "Applying upgrade $control->{file}\n"; |
|
206 |
|
|
207 |
if ($file_type eq "sql") { |
|
208 |
$user->process_query($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control); |
|
209 |
} else { |
|
210 |
$user->process_perl_script($form, $dbh, "sql/$form->{dbdriver}-upgrade2/$control->{file}", $control); |
|
211 |
} |
|
212 |
} |
|
213 |
|
|
214 |
$dbh->disconnect(); |
|
215 |
} |
|
216 |
|
|
217 |
sub build_upgrade_order { |
|
218 |
my $name = shift; |
|
219 |
my $order = shift; |
|
220 |
my $tag = shift; |
|
221 |
|
|
222 |
my $control = $controls->{$name}; |
|
223 |
|
|
224 |
foreach my $dependency (@{ $control->{"depends"} }) { |
|
225 |
next if $tags->{$dependency}; |
|
226 |
$tags->{$dependency} = 1; |
|
227 |
build_upgrade_order($dependency, $order, $tag); |
|
228 |
} |
|
229 |
|
|
230 |
push @{ $order }, $name; |
|
231 |
} |
|
232 |
|
|
147 | 233 |
####### |
148 | 234 |
####### |
149 | 235 |
####### |
... | ... | |
157 | 243 |
####### |
158 | 244 |
####### |
159 | 245 |
|
160 |
my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help); |
|
161 |
|
|
162 | 246 |
GetOptions("list" => \$opt_list, |
163 | 247 |
"tree" => \$opt_tree, |
164 | 248 |
"rtree" => \$opt_rtree, |
165 | 249 |
"nodeps" => \$opt_nodeps, |
166 | 250 |
"graphviz" => \$opt_graphviz, |
251 |
"user=s" => \$opt_user, |
|
252 |
"apply=s" => \$opt_apply, |
|
167 | 253 |
"help" => \$opt_help, |
168 | 254 |
); |
169 | 255 |
|
... | ... | |
193 | 279 |
if ($opt_nodeps) { |
194 | 280 |
dump_nodeps(); |
195 | 281 |
} |
282 |
|
|
283 |
if ($opt_user) { |
|
284 |
my $file_name = "users/${opt_user}.conf"; |
|
285 |
|
|
286 |
eval { require($file_name); }; |
|
287 |
$form->error("File '$file_name' was not found") if $@; |
|
288 |
$locale = new Locale($myconfig{countrycode}, "all"); |
|
289 |
$user = new User("users/members", $opt_user); |
|
290 |
map { $form->{$_} = $myconfig{$_} } keys %myconfig; |
|
291 |
} |
|
292 |
|
|
293 |
if ($opt_apply) { |
|
294 |
$form->error("--apply used but no configuration file given with --user.") if (!$user); |
|
295 |
apply_upgrade($opt_apply); |
|
296 |
} |
Auch abrufbar als: Unified diff
Optionen implementiert, um von der Kommandozeile aus SQL-Upgrades aus sql/Pg-upgrade2 anzuwenden.