From 0b57a95be36ff6d1f53115d6c66d3585f396dd30 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 19 Sep 2015 02:55:41 +0200 Subject: added check for database version added constants diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm index b7d0698..cab89c2 100755 --- a/lib/RHRD/rddb.pm +++ b/lib/RHRD/rddb.pm @@ -26,11 +26,19 @@ use strict; use Config::IniFiles; use DBI; +########################### constants ########################### + +use constant { + DB_VERSION => 242, + RD_CONFIG_FILE => '/etc/rd.conf', + DROPBOX_PSEUDO_STATION_NAME => 'import-dropbox', +}; + ########################### connection handling ########################### sub opendb { - my $RD_CONF = "/etc/rd.conf"; # TODO: hardcoded value + my $RD_CONF = RD_CONFIG_FILE; my $cfg = Config::IniFiles->new(-file => $RD_CONF) or return (undef , 'ERROR', "Config File Error: " . join("\n", @Config::IniFiles::errors)); @@ -42,9 +50,24 @@ sub opendb my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpasswd") or return (undef, 'ERROR', "Database Error: " . $DBI::errstr); + $dbh->do(qq{SET CHARACTER SET utf8;}) or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + my $sth = $dbh->prepare(qq{select DB from VERSION;}) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute() + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($dbver) = $sth->fetchrow_array(); + $sth->finish(); + + if($dbver != DB_VERSION) { + return (undef, 'ERROR', "Wrong Database Version " . $dbver . " (should be " . DB_VERSION . ")"); + } + return ($dbh, 'OK', 'success'); } @@ -371,6 +394,8 @@ sub get_group_carts my ($low_cart, $high_cart, $cart_type, $enforce_cart_range) = $sth->fetchrow_array; $sth->finish(); + unless(defined $low_cart) { return (undef, 'ERROR', "group '" . $groupname . "' does not exist"); } + return ($low_cart, $high_cart, $cart_type, $enforce_cart_range); } @@ -390,6 +415,8 @@ sub set_group_carts $enforce_cart_range = 'N'; } + # TODO: check if cart range is already in use by other group + my $sql = qq{update GROUPS set DEFAULT_LOW_CART = ?, DEFAULT_HIGH_CART = ?, DEFAULT_CART_TYPE = ? , ENFORCE_CART_RANGE = ? where NAME = ?;}; my $sth = $dbh->prepare($sql) or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); @@ -416,6 +443,8 @@ sub get_group_reports my ($now_next, $traffic, $music) = $sth->fetchrow_array; $sth->finish(); + unless(defined $now_next) { return (undef, 'ERROR', "group '" . $groupname . "' does not exist"); } + return ($now_next, $traffic, $music); } @@ -484,7 +513,7 @@ sub get_dropboxes my $sth = $dbh->prepare($sql) or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); - $sth->execute($username, 'import-dropbox') # TODO: hardcoded value + $sth->execute($username, DROPBOX_PSEUDO_STATION_NAME) or return (undef, 'ERROR', "Database Error: " . $sth->errstr); my @allowed_dbs; -- cgit v0.10.2