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

SF.net SVN: ledger-smb:[6368] trunk



Revision: 6368
          http://sourceforge.net/p/ledger-smb/code/6368
Author:   ehuelsmann
Date:     2014-01-03 22:58:40 +0000 (Fri, 03 Jan 2014)
Log Message:
-----------
Implement a protocol whereby setup.pm can detect successful execution of the

Modified Paths:
--------------
    trunk/LedgerSMB/Database.pm
    trunk/LedgerSMB/Scripts/setup.pm
    trunk/sql/upgrade/1.2-1.4.sql
    trunk/sql/upgrade/1.3-1.4.sql
    trunk/sql/upgrade/sl2.8-1.3.sql

Added Paths:
-----------
    trunk/sql/upgrade/README

Modified: trunk/LedgerSMB/Database.pm
===================================================================
--- trunk/LedgerSMB/Database.pm	2014-01-03 22:28:55 UTC (rev 6367)
+++ trunk/LedgerSMB/Database.pm	2014-01-03 22:58:40 UTC (rev 6368)
@@ -509,14 +509,12 @@
     $dbh->disconnect();
 
     $logger->trace("after create db \$rc=$rc");
-    if (!$rc) {
-        return $rc;
-    }
-    $rc = $self->load_base_schema();
+    die "Failed to create database named $dbn"
+	if ! $rc;
 
-     # TODO Add logging of errors/notices
+    $self->load_base_schema();
 
-     return $rc;
+    return 1;
 }
 
 =item $db->copy('new_name')
@@ -546,12 +544,35 @@
 
 sub load_base_schema {
     my ($self) = @_;
+    my $success;
+    
+    # The statement below is likely to fail, because
+    # the language already exists. Unfortunately, it's an error.
+    # If it had been a notice, 
+    $self->dbh->do("CREATE LANGAGE plpgsql");
+    $self->dbh->commit;
+    $self->exec_script(
+	{
+	    script => "$self->{source_dir}sql/Pg-database.sql",
+	    log => "$temp/dblog_stdout",
+	    errlog => "$temp/dblog_stderr"
+	});
 
-    return $self->exec_script({
-	script => "$self->{source_dir}sql/Pg-database.sql",
-	log => "$temp/dblog_stdout",
-	errlog => "$temp/dblog_stderr"
-			      });
+    my $dbh = $self->dbh;
+    my $sth = $dbh->prepare(
+	qq|select true
+	        from pg_class cls
+	        join pg_namespace nsp
+	          on nsp.oid = cls.relnamespace
+	       where cls.relname = 'defaults'
+                 and nsp.nspname = 'public'
+             |);
+    $sth->execute();
+    ($success) = $sth->fetchrow_array();
+    $sth->finish();
+
+    die "Base schema failed to load"
+	if ! $success;
 }
 
 
@@ -563,18 +584,31 @@
 
 sub load_modules {
     my ($self, $loadorder) = @_;
+
+    my $dbh = $self->dbh;
     open (LOADORDER, '<', "$self->{source_dir}sql/modules/$loadorder");
-    for my $mod (<LOADORDER>){
+    for my $mod (<LOADORDER>) {
         chomp($mod);
         $mod =~ s/#.*//;
         $mod =~ s/^\s*//;
         $mod =~ s/\s*$//;
         next if $mod eq '';
+
+	$dbh->do("delete from defaults where setting_key='module_load_ok'");
+	$dbh->do("insert into defaults (setting_key, value)" .
+		 " values ('module_load_ok','no')");
+	$dbh->commit;
         $self->exec_script({script => "$self->{source_dir}sql/modules/$mod",
                             log    => "$temp/dblog"});
-
+	my $sth = $dbh->prepare("select value='yes' from defaults" .
+				" where setting_key='module_load_ok'");
+	$sth->execute();
+	my ($is_success) = $sth->fetchrow_array();
+	$sth->finish();
+	die "Module $mod failed to load"
+	    if ! $is_success;
     }
-    close (LOADORDER); // ### return failure to execute the script?
+    close (LOADORDER); ### return failure to execute the script?
 }
 
 =item $db->exec_script({script => 'path/to/file', log => 'path/to/log',

Modified: trunk/LedgerSMB/Scripts/setup.pm
===================================================================
--- trunk/LedgerSMB/Scripts/setup.pm	2014-01-03 22:28:55 UTC (rev 6367)
+++ trunk/LedgerSMB/Scripts/setup.pm	2014-01-03 22:58:40 UTC (rev 6368)
@@ -546,7 +546,7 @@
 
 =cut
 
-sub create_db{
+sub create_db {
     use LedgerSMB::Sysconfig;
     my ($request) = @_;
     my $rc=0;
@@ -561,7 +561,7 @@
     closedir(COA); 
 
     $request->{coa_lcs} =[];
-    foreach my $lcs (sort @coa){
+    foreach my $lcs (sort @coa) {
          push @{$request->{coa_lcs}}, {code => $lcs};
     } 
 
@@ -778,8 +778,15 @@
     $dbh->do('CREATE SCHEMA PUBLIC');
     $dbh->commit;
 
-    $rc = $database->load_base_schema();
-    $rc ||= $database->load_modules('LOADORDER');
+    $database->load_base_schema();
+    $database->load_modules('LOADORDER');
+
+    $dbh->do(qq(
+       INSERT INTO defaults (setting_key, value)
+                     VALUES ('migration_ok', 'no')
+     ));
+    $dbh->commit;
+
     my $dbtemplate = LedgerSMB::Template->new(
         user => {}, 
         path => 'sql/upgrade',
@@ -789,13 +796,31 @@
         output_file => 'upgrade',
         format => 'TXT' );
     $dbtemplate->render($request);
-    $rc ||= $database->exec_script(
+    $database->exec_script(
         { script => "$temp/upgrade.sql",
           log => "$temp/dblog_stdout",
           errlog => "$temp/dblog_stderr"
         });
+
+
+    my $sth = $dbh->prepare(qq(select value='yes'
+                                 from defaults
+                                where setting_key='migration_ok'));
+    $sth->execute();
+    my ($success) = $sth->fetchrow_array();
+    $sth->finish();
+
+    $request->error(qq(Upgrade failed;
+           logs can be found in
+           $temp/dblog_stdout and $temp/dblog_stderr))
+	if ! $success;
+
+    $dbh->do("delete from defaults where setting_key='migration_ok'");
+    $dbh->commit;
+
 }
 
+
 =item run_upgrade
 
 
@@ -831,7 +856,6 @@
     my ($request) = @_;
     my $database = _init_db($request);
     my $rc = 0;
-    my $temp = $LedgerSMB::Sysconfig::tempdir;
 
     my $dbh = $request->{dbh};
     $dbh->do('ALTER SCHEMA public RENAME TO sl28');

Modified: trunk/sql/upgrade/1.2-1.4.sql
===================================================================
--- trunk/sql/upgrade/1.2-1.4.sql	2014-01-03 22:28:55 UTC (rev 6367)
+++ trunk/sql/upgrade/1.2-1.4.sql	2014-01-03 22:58:40 UTC (rev 6368)
@@ -767,6 +767,7 @@
 
 UPDATE defaults SET value = '1.3.999' WHERE setting_key = 'version';
 
+update defaults set value = 'yes' where setting_key = 'migration_ok';
 
 COMMIT;
 --TODO:  Translation migratiion.  Partsgroups?

Modified: trunk/sql/upgrade/1.3-1.4.sql
===================================================================
--- trunk/sql/upgrade/1.3-1.4.sql	2014-01-03 22:28:55 UTC (rev 6367)
+++ trunk/sql/upgrade/1.3-1.4.sql	2014-01-03 22:58:40 UTC (rev 6368)
@@ -684,5 +684,6 @@
 
 UPDATE defaults SET value = '1.4.0' WHERE setting_key = 'version';
 
+update defaults set value = 'yes' where setting_key = 'migration_ok';
 
 COMMIT;

Added: trunk/sql/upgrade/README
===================================================================
--- trunk/sql/upgrade/README	                        (rev 0)
+++ trunk/sql/upgrade/README	2014-01-03 22:58:40 UTC (rev 6368)
@@ -0,0 +1,11 @@
+
+This directory contains upgrade scripts for various LedgerSMB and
+SQL-Ledger versions.
+
+Note that every upgrade (and downgrade) script *must* adhere to
+the protocol which allows the script runner to determine success
+or failure:
+
+ Upon successful completion, the upgrade script must change
+ the value associated with the setting_key 'migration_ok' to 'yes'.
+

Modified: trunk/sql/upgrade/sl2.8-1.3.sql
===================================================================
--- trunk/sql/upgrade/sl2.8-1.3.sql	2014-01-03 22:28:55 UTC (rev 6367)
+++ trunk/sql/upgrade/sl2.8-1.3.sql	2014-01-03 22:58:40 UTC (rev 6368)
@@ -95,7 +95,7 @@
 UPDATE sl28.customer SET credit_id = 
 	(SELECT id FROM entity_credit_account e 
 	WHERE e.meta_number = customernumber and entity_class = 2
-        and e.entity_id = vendor.entity_id);
+        and e.entity_id = customer.entity_id);
 
 --Company
 
@@ -614,8 +614,9 @@
  SELECT setval('cr_report_id_seq', max(id)) FROM cr_report;
  SELECT setval('cr_report_line_id_seq', max(id)) FROM cr_report_line;
 
-UPDATE defaults SET value = '1.3.0' WHERE setting_key = 'version';
+UPDATE defaults SET value = '1.4.0' WHERE setting_key = 'version';
 
+UPDATE defaults SET value = 'yes' where setting_key = 'migration_ok';
 
 COMMIT;
 --TODO:  Translation migratiion.  Partsgroups?

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