]>
jfr.im git - irc/SurrealServices/srsv.git/blob - branches/0.4.3/SrSv/DB/Schema.pm
1 # This file is part of SurrealServices.
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 package SrSv
::DB
::Schema
;
21 use SrSv
::MySQL
qw( $dbh connectDB disconnectDB );
22 use SrSv
::Conf2Consts
qw( sql );
25 *PREFIX
= \
&main
::PREFIX
;
29 use Exporter
'import';
32 upgrade_schema check_schema find_newest_schema
36 sub find_newest_schema
() {
37 opendir((my $dh), "@{[PREFIX]}/sql/");
39 while (my $dentry = readdir($dh)) {
40 next if ($dentry =~ /^\.\.?$/);
41 if($dentry =~ /^(\d+)\.sql$/) {
45 @schemas = reverse sort { $a <=> $b } @schemas;
48 sub upgrade_schema
($) {
50 opendir((my $dh), "@{[PREFIX]}/sql/");
52 while (my $dentry = readdir($dh)) {
53 next if ($dentry =~ /^\.\.?$/);
54 if($dentry =~ /^(\d+)\.sql$/) {
58 @schemas = sort { $a <=> $b } @schemas;
59 while(scalar(@schemas) && $schemas[0] <= $ver) {
62 foreach my $schema (@schemas) {
63 #print "@{[PREFIX]}/sql/${schema}.sql\n";
64 do_sql_file
("@{[PREFIX]}/sql/${schema}.sql");
73 # SHOW TABLES WHERE doesn't work for MySQL 4.x.
74 my $tables = $dbh->selectall_arrayref("SHOW TABLES");
75 my ($found, undef) = grep { m
"srsv_schema" } map { $_->[0] } @$tables;
80 my $findSchemaVer = $dbh->prepare("SELECT `ver` FROM `srsv_schema`");
81 $findSchemaVer->execute();
82 my ($ver) = $findSchemaVer->fetchrow_array();
83 $findSchemaVer->finish();
84 disconnectDB
() if $disconnect;
90 open ((my $SQL), $file) or die "$file: $!\n";
93 while(my $x = <$SQL>) {
94 unless($x =~ /^#/ or $x eq $/) {
98 foreach my $line (split(/;/s, $sql)) {