1 package SL::DBUpgrade2::Base;
5 use parent qw(Rose::Object);
7 use English qw(-no_match_vars);
8 use Rose::Object::MakeMethods::Generic (
9 scalar => [ qw(dbh myconfig) ],
17 my $file_name = delete $params{file_name};
19 if (!eval { require $file_name }) {
20 delete $INC{$file_name};
24 my $package = delete $params{tag};
25 $package =~ s/[^a-zA-Z0-9_]+/_/g;
26 $package = "SL::DBUpgrade2::${package}";
28 $package->new(%params)->run;
32 my ($self, $msg) = @_;
34 die $self->locale->text("Database update error:") . "<br>$msg<br>" . $DBI::errstr;
38 my ($self, $query, $may_fail) = @_;
40 return if $self->dbh->do($query);
42 $self->db_error($query) unless $may_fail;
45 $self->dbh->begin_work;
49 my ($self, $wanted_coa) = @_;
51 my ($have_coa) = selectrow_query($::form, $self->dbh, q{ SELECT count(*) FROM defaults WHERE coa = ? }, $wanted_coa);
59 my $query = q{ SELECT count(*)
60 FROM ar, ap, gl, invoice, acc_trans, customer, vendor, parts
62 my ($empty) = selectrow_query($::form, $self->dbh, $query);
76 SL::DBUpgrade2::Base - Base class for Perl-based database upgrade files
80 Database scripts written in Perl must be derived from this class and
81 provide a method called C<run>.
83 The functions in this base class offer functionality for the upgrade
88 The following properties (which can be accessed with
89 C<$self-E<gt>property_name>) are available to the database upgrade
96 The database handle; an Instance of L<DBI>. It is connected, and a
97 transaction has been started right before the script (the method
98 L</run>)) was executed.
102 The stripped-down version of the C<%::myconfig> hash: this hash
103 reference only contains the database connection parameters applying to
104 the current database.
113 =item C<check_coa $coa_name>
115 Returns trueish if the database uses the chart of accounts named
118 =item C<db_error $message>
120 Outputs an error message C<$message> to the user and aborts execution.
122 =item C<db_query $query, $may_fail>
124 Executes an SQL query. What the method does if the query fails depends
125 on C<$may_fail>. If it is falsish then the method will simply die
126 outputting the error message via L</db_error>. If C<$may_fail> is
127 trueish then the current transaction will be rolled back, a new one
130 =item C<execute_script>
132 Executes a named database upgrade script. This function is not
133 supposed to be called from an upgrade script. Instead, the upgrade
134 manager L<SL::DBUpgrade2> uses it in order to execute the actual
135 database upgrade scripts.
137 =item C<is_coa_empty>
139 Returns trueish if no transactions have been recorded in the table
144 This method is the entry point for the actual upgrade. Each upgrade
145 script must provide this method.
155 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>