1 package SL::DBUpgrade2;
13 return bless({}, $package)->init(@_);
17 my ($self, %params) = @_;
20 $params{path_suffix} = "-auth";
21 $params{schema} = "auth.";
24 $params{path_suffix} ||= '';
25 $params{schame} ||= '';
27 map { $self->{$_} = $params{$_} } keys %params;
32 sub parse_dbupdate_controls {
33 $::lxdebug->enter_sub();
37 my $form = $self->{form};
38 my $locale = $::locale;
43 my $path = "sql/" . $self->{dbdriver} . "-upgrade2" . $self->{path_suffix};
45 foreach my $file_name (<$path/*.sql>, <$path/*.pl>) {
46 next unless (open(IN, $file_name));
48 my $file = $file_name;
58 next unless (/^(--|\#)\s*\@/);
63 my @fields = split(/\s*:\s*/, $_, 2);
64 next unless (scalar(@fields) == 2);
66 if ($fields[0] eq "depends") {
67 push(@{$control->{"depends"}}, split(/\s+/, $fields[1]));
69 $control->{$fields[0]} = $fields[1];
73 next if ($control->{ignore});
75 $control->{charset} ||= Common::DEFAULT_CHARSET;
77 if (!$control->{"tag"}) {
78 _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ;
81 if ($control->{"tag"} =~ /[^a-zA-Z0-9_\(\)\-]/) {
82 _control_error($form, $file_name, $locale->text("The 'tag' field must only consist of alphanumeric characters or the carachters - _ ( )"))
85 if (defined($all_controls{$control->{"tag"}})) {
86 _control_error($form, $file_name, sprintf($locale->text("More than one control file with the tag '%s' exist."), $control->{"tag"}))
89 if (!$control->{"description"}) {
90 _control_error($form, $file_name, sprintf($locale->text("Missing 'description' field."))) ;
93 $control->{"priority"} *= 1;
94 $control->{"priority"} ||= 1000;
95 $control->{"file"} = $file;
97 delete @{$control}{qw(depth applied)};
99 $all_controls{$control->{"tag"}} = $control;
104 foreach my $control (values(%all_controls)) {
105 foreach my $dependency (@{$control->{"depends"}}) {
106 _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency}));
109 map({ $_->{"loop"} = 0; } values(%all_controls));
110 _check_for_loops($form, $control->{"file"}, \%all_controls, $control->{"tag"});
113 map({ _dbupdate2_calculate_depth(\%all_controls, $_->{"tag"}) }
114 values(%all_controls));
116 $self->{all_controls} = \%all_controls;
118 $::lxdebug->leave_sub();
120 return \%all_controls;
124 $::lxdebug->enter_sub();
126 my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
128 my $form = $self->{form};
129 my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
134 my $file_charset = Common::DEFAULT_CHARSET;
137 next if !/^--\s*\@charset:\s*(.+)/;
141 $fh->seek(0, SEEK_SET);
143 $db_charset ||= Common::DEFAULT_CHARSET;
148 $_ = SL::Iconv::convert($file_charset, $db_charset, $_);
150 # Remove DOS and Unix style line endings.
156 for (my $i = 0; $i < length($_); $i++) {
157 my $char = substr($_, $i, 1);
159 # Are we inside a string?
161 if ($char eq $quote_chars[-1]) {
167 if (($char eq "'") || ($char eq "\"")) {
168 push(@quote_chars, $char);
170 } elsif ($char eq ";") {
172 # Query is complete. Send it.
174 $sth = $dbh->prepare($query);
175 if (!$sth->execute()) {
176 my $errstr = $dbh->errstr;
179 $form->dberror("The database update/creation did not succeed. " .
180 "The file ${filename} containing the following " .
181 "query failed:<br>${query}<br>" .
182 "The error message was: ${errstr}<br>" .
183 "All changes in that file have been reverted.");
195 # Insert a space at the end of each line so that queries split
196 # over multiple lines work properly.
198 $query .= @quote_chars ? "\n" : ' ';
202 if (ref($version_or_control) eq "HASH") {
203 $dbh->do("INSERT INTO " . $self->{schema} . "schema_info (tag, login) VALUES (" . $dbh->quote($version_or_control->{"tag"}) . ", " . $dbh->quote($form->{"login"}) . ")");
204 } elsif ($version_or_control) {
205 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control));
211 $::lxdebug->leave_sub();
214 # Process a Perl script which updates the database.
215 # If the script returns 1 then the update was successful.
216 # Return code "2" means "needs more interaction; remove
217 # users/nologin and end current request".
218 # All other return codes are fatal errors.
219 sub process_perl_script {
220 $::lxdebug->enter_sub();
222 my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
224 my $form = $self->{form};
225 my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
226 my $file_charset = Common::DEFAULT_CHARSET;
228 if (ref($version_or_control) eq "HASH") {
229 $file_charset = $version_or_control->{charset};
234 next if !/^--\s*\@charset:\s*(.+)/;
238 $fh->seek(0, SEEK_SET);
241 my $contents = join "", <$fh>;
244 $db_charset ||= Common::DEFAULT_CHARSET;
246 my $iconv = SL::Iconv::get_converter($file_charset, $db_charset);
250 # setup dbup_ export vars
251 my %dbup_myconfig = ();
252 map({ $dbup_myconfig{$_} = $form->{$_}; } qw(dbname dbuser dbpasswd dbhost dbport dbconnect));
254 my $dbup_locale = $::locale;
256 my $result = eval($contents);
263 if (!defined($result)) {
264 print $form->parse_html_template("dbupgrade/error",
265 { "file" => $filename,
268 } elsif (1 != $result) {
269 unlink("users/nologin") if (2 == $result);
273 if (ref($version_or_control) eq "HASH") {
274 $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" . $dbh->quote($version_or_control->{"tag"}) . ", " . $dbh->quote($form->{"login"}) . ")");
275 } elsif ($version_or_control) {
276 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version_or_control));
280 $::lxdebug->leave_sub();
284 my ($self, $dbh, $filename, $version_or_control, $db_charset) = @_;
286 if ($filename =~ m/sql$/) {
287 $self->process_query($dbh, $filename, $version_or_control, $db_charset);
289 $self->process_perl_script($dbh, $filename, $version_or_control, $db_charset);
293 sub _check_for_loops {
294 my ($form, $file_name, $controls, $tag, @path) = @_;
298 my $ctrl = $controls->{$tag};
300 if ($ctrl->{"loop"} == 1) {
302 _control_error($form, $file_name, $::locale->text("Dependency loop detected:") . " " . join(" -> ", @path))
304 } elsif ($ctrl->{"loop"} == 0) {
307 map({ _check_for_loops($form, $file_name, $controls, $_, @path); } @{ $ctrl->{"depends"} });
313 my ($form, $file_name, $message) = @_;
316 my $locale = $::locale;
318 $form->error(sprintf($locale->text("Error in database control file '%s': %s"), $file_name, $message));
321 sub _dbupdate2_calculate_depth {
322 $::lxdebug->enter_sub(2);
324 my ($tree, $tag) = @_;
326 my $node = $tree->{$tag};
328 return $::lxdebug->leave_sub(2) if (defined($node->{"depth"}));
332 foreach $tag (@{$node->{"depends"}}) {
333 _dbupdate2_calculate_depth($tree, $tag);
334 my $value = $tree->{$tag}->{"depth"};
335 $max_depth = $value if ($value > $max_depth);
338 $node->{"depth"} = $max_depth + 1;
340 $::lxdebug->leave_sub(2);
343 sub sort_dbupdate_controls {
346 return sort { ($a->{depth} <=> $b->{depth}) || ($a->{priority} <=> $b->{priority}) || ($a->{tag} cmp $b->{tag}) } values %{ $self->{all_controls} };