From 326c670fafa45d6a2b6c1244cad98a61addec893 Mon Sep 17 00:00:00 2001 From: Luke Reeves Date: Sun, 12 Jul 2009 11:08:37 -0400 Subject: [PATCH] Corrected database schema upgrading --- lib/Insipid/Database.pm | 43 ++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/Insipid/Database.pm b/lib/Insipid/Database.pm index bfbd336..11278da 100755 --- a/lib/Insipid/Database.pm +++ b/lib/Insipid/Database.pm @@ -55,10 +55,6 @@ $dbh = DBI->connect($dsn, $dbuser, $dbpass, { 'RaiseError' => 0}) or die $DBI::e my %options; -#if(need_upgrade() eq 1) { -# dbupgrade(); -#} - sub export_options { my ($writer) = (@_); my ($sth); @@ -107,34 +103,33 @@ sub dbupgrade { return; } -# Check if we need an upgrade -sub need_upgrade { - if(!defined($options{version})) { return 1; } - - if($options{version} ne $version) { - return 1; - } else { - return 0; - } -} - -# Functions - sub get_option { + my ($name) = (@_); + if(keys (%options) == 0) { - my $sql = "select name, value from $tbl_options"; - my $sth = $dbh->prepare($sql); - $sth->execute() or die $DBI::errstr; + reload_options(); + } - while(my $hr = $sth->fetchrow_hashref) { - $options{$hr->{'name'}} = $hr->{'value'}; - } + # Determine if we need to upgrade the database + if($version ne $options{'version'}) { + print STDERR "Upgrading schema from $options{'version'} to $version.\n"; + dbupgrade(); + reload_options(); } - my ($name) = (@_); return $options{$name}; } +sub reload_options { + my $sql = "select name, value from $tbl_options"; + my $sth = $dbh->prepare($sql); + $sth->execute() or die $DBI::errstr; + + while(my $hr = $sth->fetchrow_hashref) { + $options{$hr->{'name'}} = $hr->{'value'}; + } +} + # This configures the URLs in the application to support mod_rewrite or # a webserver sans mod_rewrite. if(get_option('use_rewrite') eq 'yes') { -- 2.39.5