[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
SF.net SVN: ledger-smb:[6351] trunk/LedgerSMB/Database.pm
- Subject: SF.net SVN: ledger-smb:[6351] trunk/LedgerSMB/Database.pm
- From: ..hidden..
- Date: Thu, 2 Jan 2014 15:05:51 +0000
Revision: 6351
http://sourceforge.net/p/ledger-smb/code/6351
Author: ehuelsmann
Date: 2014-01-02 15:05:48 +0000 (Thu, 02 Jan 2014)
Log Message:
-----------
Make database functions clean up behind themselves;
Do a schema lookup for a 'version' column before querying and littering logs.
Modified Paths:
--------------
trunk/LedgerSMB/Database.pm
Modified: trunk/LedgerSMB/Database.pm
===================================================================
--- trunk/LedgerSMB/Database.pm 2014-01-02 15:01:03 UTC (rev 6350)
+++ trunk/LedgerSMB/Database.pm 2014-01-02 15:05:48 UTC (rev 6351)
@@ -87,12 +87,17 @@
sub dbh {
my ($self) = @_;
+
+ return $LedgerSMB::App_State::DBH
+ if defined $LedgerSMB::App_State::DBH;
+
my $creds = LedgerSMB::Auth::get_credentials();
- return DBI->connect(
+ $LedgerSMB::App_State::DBH = DBI->connect(
"dbi:Pg:dbname=$self->{company_name}",
"$creds->{login}", "$creds->{password}",
{ AutoCommit => 0, PrintError => $logger->is_warn(), }
);
+ return $LedgerSMB::App_State::DBH;
}
=item base_backup
@@ -292,13 +297,9 @@
full_version => undef,
status => undef,
};
- $logger->debug("\$self->{dbh}=$self->{dbh}");
+
my $creds = LedgerSMB::Auth->get_credentials();
- my $dbh = DBI->connect(
- "dbi:Pg:dbname=$self->{company_name}",
- "$creds->{login}", "$creds->{password}",
- { AutoCommit => 0, PrintError => $logger->is_warn(), }
- );
+ my $dbh = $self->dbh();
if (!$dbh){ # Could not connect, try to validate existance by connecting
# to postgres and checking
$dbh = DBI->connect(
@@ -307,7 +308,9 @@
);
return $retval unless $dbh;
$logger->debug("DBI->connect dbh=$dbh");
- $self->{dbh}=$dbh;#make available to upper levels
+ # don't assign to App_State::DBH, since we're a fallback connection,
+ # not one to the company database
+
my $sth = $dbh->prepare(
"select count(*) = 1 from pg_database where datname = ?"
);
@@ -321,29 +324,59 @@
$sth = $dbh->prepare("SELECT SESSION_USER");
$sth->execute;
$retval->{username} = $sth->fetchrow_array();
+ $sth->finish();
+ $dbh->disconnect();
+
return $retval;
} else { # Got a db handle... try to find the version and app by a few
# different means
$logger->debug("DBI->connect dbh=$dbh");
- $self->{dbh}=$dbh;#make it available to upper levels
+
my $sth;
$sth = $dbh->prepare("SELECT SESSION_USER");
$sth->execute;
$retval->{username} = $sth->fetchrow_array();
- # Legacy SL and LSMB
- $sth = $dbh->prepare('SELECT version FROM defaults');
- #avoid DBD::Pg::st fetchrow_hashref failed: no statement executing
- my $rv=$sth->execute();
- if(defined($rv))
- {
- if (my $ref = $sth->fetchrow_hashref('NAME_lc')){
- if ($ref->{version}){
- $retval->{appname} = 'ledgersmb';
- $retval->{version} = 'legacy';
- $retval->{full_version} = $ref->{version};
- return $retval;
- }
- }
+ $sth->finish();
+
+ # Is there a chance this is an SL or LSMB legacy version?
+ # (ie. is there a VERSION column to query in the DEFAULTS table?
+ $sth = $dbh->prepare(
+ qq|select count(*)=1
+ from pg_attribute attr
+ join pg_class cls
+ on cls.oid = attr.attrelid
+ join pg_namespace nsp
+ on nsp.oid = cls.relnamespace
+ where cls.relname = 'defaults'
+ and attr.attname='version'
+ and nsp.nspname = 'public'
+ |
+ );
+ $sth->execute();
+ my ($have_version_column) =
+ $sth->fetchrow_array();
+ $sth->finish();
+
+ if ($have_version_column) {
+ # Legacy SL and LSMB
+ $sth = $dbh->prepare(
+ 'SELECT version FROM defaults'
+ );
+ #avoid DBD::Pg::st fetchrow_hashref failed: no statement executing
+ my $rv=$sth->execute();
+ if(defined($rv))
+ {
+ if (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
+ if ($ref->{version}){
+ $retval->{appname} = 'ledgersmb';
+ $retval->{version} = 'legacy';
+ $retval->{full_version} = $ref->{version};
+
+ $dbh->rollback();
+ return $retval;
+ }
+ }
+ }
}
$dbh->rollback;
# LedgerSMB 1.2 and above
@@ -362,6 +395,8 @@
$retval->{version} = '1.3';
}
if ($retval->{version}){
+
+ $dbh->rollback();
return $retval;
}
}
@@ -471,6 +506,7 @@
$dbh->{AutoCommit} = 1;
my $dbn = $dbh->quote_identifier($self->{company_name});
my $rc = $dbh->do("CREATE DATABASE $dbn WITH TEMPLATE template0 ENCODING 'UTF8'");
+ $dbh->disconnect();
$logger->trace("after create db \$rc=$rc");
if (!$rc) {
@@ -646,6 +682,7 @@
$key = 'eca' if $t eq 'entity_credit_account';
$retval->{"${key}_count"} = $count;
}
+ $dbh->disconnect();
return $retval;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
Ledger-smb-commits mailing list
..hidden..
https://lists.sourceforge.net/lists/listinfo/ledger-smb-commits