Eine Option "@ignore: 1" für Datenbankupgradescripte, mit denen das Script von Lx...
[kivitendo-erp.git] / SL / DBUpgrade2.pm
1 package SL::DBUpgrade2;
2
3 use SL::Common;
4
5 require Exporter;
6 @ISA = qw(Exporter);
7
8 @EXPORT = qw(parse_dbupdate_controls sort_dbupdate_controls);
9
10 sub parse_dbupdate_controls {
11   $main::lxdebug->enter_sub();
12
13   my ($form, $dbdriver) = @_;
14
15   my $locale = $main::locale;
16
17   local *IN;
18   my %all_controls;
19
20   my $path = "sql/${dbdriver}-upgrade2";
21
22   foreach my $file_name (<$path/*.sql>, <$path/*.pl>) {
23     next unless (open(IN, $file_name));
24
25     my $file = $file_name;
26     $file =~ s|.*/||;
27
28     my $control = {
29       "priority" => 1000,
30       "depends"  => [],
31     };
32
33     while (<IN>) {
34       chomp();
35       next unless (/^(--|\#)\s*\@/);
36       s/^(--|\#)\s*\@//;
37       s/\s*$//;
38       next if ($_ eq "");
39
40       my @fields = split(/\s*:\s*/, $_, 2);
41       next unless (scalar(@fields) == 2);
42
43       if ($fields[0] eq "depends") {
44         push(@{$control->{"depends"}}, split(/\s+/, $fields[1]));
45       } else {
46         $control->{$fields[0]} = $fields[1];
47       }
48     }
49
50     next if ($control->{ignore});
51
52     $control->{charset} ||= Common::DEFAULT_CHARSET;
53
54     if (!$control->{"tag"}) {
55       _control_error($form, $file_name, $locale->text("Missing 'tag' field.")) ;
56     }
57
58     if ($control->{"tag"} =~ /[^a-zA-Z0-9_\(\)\-]/) {
59       _control_error($form, $file_name, $locale->text("The 'tag' field must only consist of alphanumeric characters or the carachters - _ ( )"))
60     }
61
62     if (defined($all_controls{$control->{"tag"}})) {
63       _control_error($form, $file_name, sprintf($locale->text("More than one control file with the tag '%s' exist."), $control->{"tag"}))
64     }
65
66     if (!$control->{"description"}) {
67       _control_error($form, $file_name, sprintf($locale->text("Missing 'description' field."))) ;
68     }
69
70     $control->{"priority"}  *= 1;
71     $control->{"priority"} ||= 1000;
72     $control->{"file"}       = $file;
73
74     delete @{$control}{qw(depth applied)};
75
76     $all_controls{$control->{"tag"}} = $control;
77
78     close(IN);
79   }
80
81   foreach my $control (values(%all_controls)) {
82     foreach my $dependency (@{$control->{"depends"}}) {
83       _control_error($form, $control->{"file"}, sprintf($locale->text("Unknown dependency '%s'."), $dependency)) if (!defined($all_controls{$dependency}));
84     }
85
86     map({ $_->{"loop"} = 0; } values(%all_controls));
87     _check_for_loops($form, $control->{"file"}, \%all_controls, $control->{"tag"});
88   }
89
90   map({ _dbupdate2_calculate_depth(\%all_controls, $_->{"tag"}) }
91       values(%all_controls));
92
93   $main::lxdebug->leave_sub();
94
95   return \%all_controls;
96 }
97
98 sub _check_for_loops {
99   my ($form, $file_name, $controls, $tag, @path) = @_;
100
101   push(@path, $tag);
102
103   my $ctrl = $controls->{$tag};
104
105   if ($ctrl->{"loop"} == 1) {
106     # Not done yet.
107     _control_error($form, $file_name, $main::locale->text("Dependency loop detected:") . " " . join(" -> ", @path))
108
109   } elsif ($ctrl->{"loop"} == 0) {
110     # Not checked yet.
111     $ctrl->{"loop"} = 1;
112     map({ _check_for_loops($form, $file_name, $controls, $_, @path); } @{ $ctrl->{"depends"} });
113     $ctrl->{"loop"} = 2;
114   }
115 }
116
117 sub _control_error {
118   my ($form, $file_name, $message) = @_;
119
120   $form = $main::form;
121   my $locale = $main::locale;
122
123   $form->error(sprintf($locale->text("Error in database control file '%s': %s"), $file_name, $message));
124 }
125
126 sub _dbupdate2_calculate_depth {
127   $main::lxdebug->enter_sub();
128
129   my ($tree, $tag) = @_;
130
131   my $node = $tree->{$tag};
132
133   return $main::lxdebug->leave_sub() if (defined($node->{"depth"}));
134
135   my $max_depth = 0;
136
137   foreach $tag (@{$node->{"depends"}}) {
138     _dbupdate2_calculate_depth($tree, $tag);
139     my $value = $tree->{$tag}->{"depth"};
140     $max_depth = $value if ($value > $max_depth);
141   }
142
143   $node->{"depth"} = $max_depth + 1;
144
145   $main::lxdebug->leave_sub();
146 }
147
148 sub sort_dbupdate_controls {
149   return sort({   $a->{"depth"}    !=  $b->{"depth"}    ? $a->{"depth"}    <=> $b->{"depth"}
150                 : $a->{"priority"} !=  $b->{"priority"} ? $a->{"priority"} <=> $b->{"priority"}
151                 :                                         $a->{"tag"}      cmp $b->{"tag"}      } values(%{$_[0]}));
152 }
153
154 1;