[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[6374] trunk/LedgerSMB



Revision: 6374
          http://sourceforge.net/p/ledger-smb/code/6374
Author:   ehuelsmann
Date:     2014-01-04 21:05:53 +0000 (Sat, 04 Jan 2014)
Log Message:
-----------
Create new output files for each migration run, instead of appending.
Also, check the execution result of the pre-migration checks (not only row-count).

Modified Paths:
--------------
    trunk/LedgerSMB/Database.pm
    trunk/LedgerSMB/Scripts/setup.pm

Modified: trunk/LedgerSMB/Database.pm
===================================================================
--- trunk/LedgerSMB/Database.pm	2014-01-04 21:02:27 UTC (rev 6373)
+++ trunk/LedgerSMB/Database.pm	2014-01-04 21:05:53 UTC (rev 6374)
@@ -29,6 +29,7 @@
 use LedgerSMB::Sysconfig;
 use base('LedgerSMB');
 use strict;
+use DateTime;
 use Log::Log4perl;
 Log::Log4perl::init(\$LedgerSMB::Sysconfig::log4perl_config);
 my $logger = Log::Log4perl->get_logger('');
@@ -41,8 +42,13 @@
 };
 
 my $temp = $LedgerSMB::Sysconfig::tempdir;
-#(-d "$temp") ||  system("mkdir -p $temp"); moved to Sysconfig, so can be trapped earlier
 
+sub loader_log_filename {
+    my $dt = DateTime->now();
+    $dt =~ s/://g; # strip out disallowed Windows characters
+    return $temp . "/dblog_${$}_$dt";
+}
+
 my $logger = Log::Log4perl->get_logger('LedgerSMB::Database');
 
 =item LedgerSMB::Database->new({dbname = $dbname, countrycode = $cc, chart_name = $name, company_name = $company, username = $username, password = $password})
@@ -124,6 +130,9 @@
     local %ENV; # Make sure that - when leaving the scope - %ENV is restored
     $ENV{PGUSER} = $self->{username};
     $ENV{PGPASSWORD} = $self->{password};
+    $ENV{PGDATABASE} = $self->{company_name};
+    $ENV{PGHOST} = $LedgerSMB::Sysconfig::db_host;
+    $ENV{PGPORT} = $LedgerSMB::Sysconfig::db_port;
 
     my @t = localtime(time);
     $t[4]++;
@@ -164,9 +173,12 @@
 sub db_backup {
     my $self = shift @_;
 
-    local %ENV;
+    local %ENV; # Make sure that - when leaving the scope - %ENV is restored
     $ENV{PGUSER} = $self->{username};
     $ENV{PGPASSWORD} = $self->{password};
+    $ENV{PGDATABASE} = $self->{company_name};
+    $ENV{PGHOST} = $LedgerSMB::Sysconfig::db_host;
+    $ENV{PGPORT} = $LedgerSMB::Sysconfig::db_port;
 
     my @t = localtime(time);
     $t[4]++;
@@ -487,7 +499,7 @@
 =cut
 
 sub create {
-    my ($self) = @_;
+    my ($self, $args) = @_;
     # We have to use template0 because of issues that Debian has with database 
     # encoding.  Apparently that causes problems for us, so template0 must be
     # used. Hat tip:  irc user nwnw on #ledgersmb
@@ -512,7 +524,10 @@
     die "Failed to create database named $dbn"
 	if ! $rc;
 
-    $self->load_base_schema();
+    $self->load_base_schema({
+	log => $args->{log},
+	errlog => $args->{errlog}
+			    });
 
     return 1;
 }
@@ -543,8 +558,9 @@
 =cut
 
 sub load_base_schema {
-    my ($self) = @_;
+    my ($self, $args) = @_;
     my $success;
+    my $log = loader_log_filename();
     
     # The statement below is likely to fail, because
     # the language already exists. Unfortunately, it's an error.
@@ -554,8 +570,8 @@
     $self->exec_script(
 	{
 	    script => "$self->{source_dir}sql/Pg-database.sql",
-	    log => "$temp/dblog_stdout",
-	    errlog => "$temp/dblog_stderr"
+	    log => ($args->{log} || "${log}_stdout"),
+	    errlog => ($args->{errlog} || "${log}_stderr")
 	});
 
     my $dbh = $self->dbh;
@@ -583,7 +599,8 @@
 =cut
 
 sub load_modules {
-    my ($self, $loadorder) = @_;
+    my ($self, $loadorder, $args) = @_;
+    my $log = loader_log_filename();
 
     my $dbh = $self->dbh;
     open (LOADORDER, '<', "$self->{source_dir}sql/modules/$loadorder");
@@ -599,7 +616,9 @@
 		 " values ('module_load_ok','no')");
 	$dbh->commit;
         $self->exec_script({script => "$self->{source_dir}sql/modules/$mod",
-                            log    => "$temp/dblog"});
+                            log    => $args->{log} || "${log}_stdout",
+			    errlog => $args->{errlog} || "${log}_stderr"
+			   });
 	my $sth = $dbh->prepare("select value='yes' from defaults" .
 				" where setting_key='module_load_ok'");
 	$sth->execute();
@@ -664,9 +683,15 @@
 =cut
 
 sub create_and_load(){
-    my ($self) = @_;
-    $self->create();
-    $self->load_modules('LOADORDER');
+    my ($self, $args) = @_;
+    $self->create({
+	log     => $args->{log},
+	errlog  => $args->{errlog},
+		  });
+    $self->load_modules('LOADORDER', {
+	log     => $args->{log},
+	errlog  => $args->{errlog},
+			});
 }
 
 

Modified: trunk/LedgerSMB/Scripts/setup.pm
===================================================================
--- trunk/LedgerSMB/Scripts/setup.pm	2014-01-04 21:02:27 UTC (rev 6373)
+++ trunk/LedgerSMB/Scripts/setup.pm	2014-01-04 21:05:53 UTC (rev 6374)
@@ -433,7 +433,8 @@
 	    || ($check->max_version lt $dbinfo->{version})
 	    || ($check->appname ne $dbinfo->{appname});
         my $sth = $request->{dbh}->prepare($check->test_query);
-        $sth->execute();
+        $sth->execute()
+	    or die "Failed to execute pre-migration check " . $check->name;
         if ($sth->rows > 0){ # Check failed --CT
              _failed_check($request, $check, $sth);
              return;
@@ -773,14 +774,21 @@
 sub process_and_run_upgrade_script {
     my ($request, $database, $template) = @_;
     my $dbh = $database->dbh;
-    my $temp = $LedgerSMB::Sysconfig::tempdir;
+    my $temp = $database->loader_log_filename();
     my $rc;
 
-    $dbh->do('CREATE SCHEMA PUBLIC');
+    $dbh->do('CREATE SCHEMA PUBLIC')
+	or die "Failed to create schema PUBLIC (" . $dbh->errstr . ")";
     $dbh->commit;
 
-    $database->load_base_schema();
-    $database->load_modules('LOADORDER');
+    $database->load_base_schema({
+	log     => $temp . "_stdout",
+	errlog  => $temp . "_stderr"
+				});
+    $database->load_modules('LOADORDER', {
+	log     => $temp . "_stdout",
+	errlog  => $temp . "_stderr"
+			    });
 
     $dbh->do(qq(
        INSERT INTO defaults (setting_key, value)
@@ -798,9 +806,9 @@
         format => 'TXT' );
     $dbtemplate->render($request);
     $database->exec_script(
-        { script => "$temp/upgrade.sql",
-          log => "$temp/dblog_stdout",
-          errlog => "$temp/dblog_stderr"
+        { script =>  $LedgerSMB::Sysconfig::tempdir . "/upgrade.sql",
+          log => $temp . "_stdout",
+          errlog => $temp . "_stderr"
         });
 
 
@@ -813,7 +821,7 @@
 
     $request->error(qq(Upgrade failed;
            logs can be found in
-           $temp/dblog_stdout and $temp/dblog_stderr))
+           ${temp}_stdout and ${temp}_stderr))
 	if ! $success;
 
     $dbh->do("delete from defaults where setting_key='migration_ok'");
@@ -860,6 +868,7 @@
 
     my $dbh = $request->{dbh};
     $dbh->do('ALTER SCHEMA public RENAME TO sl28');
+    # process_and_run_upgrade_script commits the transaction
 
     process_and_run_upgrade_script($request, $database,
 				   'sl2.8-1.3');
@@ -916,9 +925,13 @@
 sub rebuild_modules {
     my ($request) = @_;
     my $database = _init_db($request);
+    my $temp = $database->loader_log_filename();
     $request->{dbh}->{AutoCommit} = 0;
 
-    $database->load_modules('LOADORDER');
+    $database->load_modules('LOADORDER', {
+	log     => $temp . "_stdout",
+	errlog  => $temp . "_stderr"
+			    });
     $request->{lsmb_info} = $database->lsmb_info();
 
     my $dbh = $request->{dbh};

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