[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
SF.net SVN: ledger-smb:[3385] trunk
- Subject: SF.net SVN: ledger-smb:[3385] trunk
- From: ..hidden..
- Date: Thu, 30 Jun 2011 07:33:40 +0000
Revision: 3385
http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=3385&view=rev
Author: einhverfr
Date: 2011-06-30 07:33:40 +0000 (Thu, 30 Jun 2011)
Log Message:
-----------
Initial refactor of database creation tests and db module, a little more needs to be done
Modified Paths:
--------------
trunk/LedgerSMB/Database.pm
trunk/t/40-dbsetup.t
Modified: trunk/LedgerSMB/Database.pm
===================================================================
--- trunk/LedgerSMB/Database.pm 2011-06-29 21:44:11 UTC (rev 3384)
+++ trunk/LedgerSMB/Database.pm 2011-06-30 07:33:40 UTC (rev 3385)
@@ -27,7 +27,10 @@
use LedgerSMB::Sysconfig;
use base('LedgerSMB');
+use strict;
+my $temp = $LedgerSMB::Sysconfig::temp_dir;
+
=item LedgerSMB::Database->new({dbname = $dbname, countrycode = $cc, chart_name = $name, company_name = $company, username = $username, password = $password})
This function creates a new database management object with the specified
@@ -48,13 +51,17 @@
my ($class, $args) = @_;
my $self = {};
- for (qw(dbname countrycode chart_name company_name username password)){
+ for (qw(countrycode chart_name chart_gifi company_name username password
+ contrib_dir source_dir)){
$self->{$_} = $args->{$_};
}
+ if ($self->{source_dir}){
+ $self->{source_dir} =~ s/\/*$/\//;
+ }
if (isa($args, 'LedgerSMB')){
for (keys %$args){
if ($_ =~ /^_/){
- $self->{$_} = $arg->{$_};
+ $self->{$_} = $args->{$_};
}
}
}
@@ -64,63 +71,127 @@
=item $db->create();
-Creates a database with the characteristics in the object
+Creates a database and loads the contrib files.
+Returns true if successful, false of not. Creates a log called dblog in the
+temporary directory with all the output from the psql files.
+
+In DEBUG mode, will show all lines to STDERR. In ERROR logging mode, will
+display only those lines containing the word ERROR.
+
=cut
sub create {
- my $self = (@_);
- $self->_init_environment();
- system('createdb $self->{dbname}');
- my $error = $!;
- if ($error){
- $self->error($!);
+ my ($self) = @_;
+
+ my $rc = system("createdb -E UTF8 > $temp/dblog");
+
+ my @contrib_scripts = qw(pg_trgm tsearch2 tablefunc);
+
+ for my $contrib (@contrib_scripts){
+ my $rc2;
+ $rc2=system("psql -f $ENV{PG_CONTRIB_DIR}/$contrib.sql >>$temp/dblog");
+ $rc ||= $rc2
+ }
+ if (!system("psql -f $self->{source_dir}sql/Pg-database.sql >> $temp/dblog"
+ )){
+ $rc = 1;
+ }
+ # TODO Add logging of errors/notices
+
+ return !$rc;
+}
+
+=item $db->load_modules($loadorder)
+
+Loads or reloads sql modules from $loadorder
+
+=cut
+
+sub load_modules {
+ my ($self, $loadorder) = @_;
+ open (LOADORDER, '<', "$self->{source_dir}sql/modules/$loadorder");
+ for my $mod (<LOADORDER>){
+ chomp($mod);
+ $mod =~ s/#.*//;
+ $mod =~ s/^\s*//;
+ $mod =~ s/\s*$//;
+ next if $mod eq '';
+ $self->exec_script({script => "$self->{source_dir}sql/modules/$mod",
+ log => "$temp/dblog"});
+
}
- for (qw(Database Central)){
- $self->_execute_script("Pg-$_.sql");
- }
- my $chart_path = "sql/$self->{country_code}/";
- $self->_execute_script(
- "coa/$self->{country_code}/chart/$self->{chart_name}"
- );
- my @gifis = glob('sql/$self->{country_code}/gifi/*.sql');
- my @gifi_search;
- my $search_string = $self->{chart_name};
- while ($search_string and (scalar @gifi_search == 0)){
- @gifi_search = grep /^$search_string.sql$/, @gifis;
- if (scalar @gifi_search == 0){
- if ($search_string !~ /[_-]/){
- $search_string = "";
+ close (LOADORDER);
+}
+
+=item $db->exec_script({script => 'path/to/file', logfile => 'path/to/log'})
+
+Executes the script. Returns 0 if successful, 1 if there are errors suggesting
+that types are already created, and 2 if there are other errors.
+
+=cut
+
+sub exec_script {
+ my ($self, $args) = @_;
+ open (LOG, '>>', $args->{log});
+ open (PSQL, '-|', "psql -f $args->{script}");
+ my $test = 0;
+ while (my $line = <PSQL>){
+ if ($line =~ /ERROR/){
+ if (($test < 2) and ($line =~ /relation .* exists/)){
+ $test = 1;
} else {
- $search_string =~ s/(.*)[_-].*$/$1/;
+ $test =2;
}
}
+ print LOG $line;
}
- if (! scalar @gifi_search){
- push @gifi_search, 'Default';
- }
- my $gifi = $gifi_search[0];
- $gifi =~ s/\.sql$//;
- $self->_execute_script("coa/$self->{country_code}/gifi/$gifi");
- $self->_create_roles();
+ close(PSQL);
+ close(LOG);
+ return $test;
}
-# Private method. Executes the sql script in psql.
-sub _execute_script {
- my ($self, $script) = @_;
- # Note that this needs to be changed so that it works with Win32!
- system('psql $self->{dbname} < "sql/$script.sql"');
- return $!;
-}
+=item $db->create_and_load();
-sub _create_roles {
-
-#TODO
+Creates a database and then loads it.
+=cut
+
+sub create_and_load(){
+ my ($self) = @_;
+ $self->create();
+ $self->load_modules('LOADORDER');
}
-sub update {
- # TODO
+
+=item $db->process_roles($rolefile);
+
+Loads database Roles templates.
+
+=cut
+
+sub process_roles {
+ my ($self, $rolefile) = @_;
+
+ open (ROLES, '<', "sql/modules/$rolefile");
+ open (TROLES, '>', "$temp/lsmb_roles.sql");
+
+ for my $line (<ROLES>){
+ $line =~ s/<\?lsmb dbname \?>/$self->{company_name}/;
+ print TROLES $line;
+ }
+
+ close ROLES;
+ close TROLES;
+
+ $self->exec_script({script => "sql/modules/$rolefile",
+ log => "$temp/dblog"});
}
-=back
+=item $db->log_from_logfile();
+
+Process log file and log relevant pieces via the log classes.
+
+=cut
+
+#TODO
Modified: trunk/t/40-dbsetup.t
===================================================================
--- trunk/t/40-dbsetup.t 2011-06-29 21:44:11 UTC (rev 3384)
+++ trunk/t/40-dbsetup.t 2011-06-30 07:33:40 UTC (rev 3385)
@@ -1,6 +1,7 @@
# Database setup tests.
use Test::More;
+use LedgerSMB::Database;
use strict;
use DBI;
@@ -24,68 +25,35 @@
$ENV{PGDATABASE} = $ENV{LSMB_NEW_DB};
}
+my $db = LedgerSMB::Database->new({
+ countrycode => $ENV{LSMB_COUNTRY_CODE},
+ chart_name => $ENV{LSMB_LOAD_COA},
+ chart_gifi => $ENV{LSMB_LOAD_GIFI},
+ company_name => $ENV{LSMB_NEW_DB},
+ username => $ENV{PGUSER},
+ password => $ENV{PGPASSWORD},
+ contrib_dir => $ENV{PG_CONTRIB_DIR},
+ source_dir => $ENV{LSMB_SOURCE_DIR}
+});
+
# Manual tests
-ok(!system ('createdb -E UTF8'), 'Database Created 2') || BAIL_OUT('Database could not be created!');
+ok($db->create, 'Database Created')
+ || BAIL_OUT('Database could not be created!');
+ok($db->load_modules('LOADORDER'), 'Modules loaded');
if (!$ENV{LSMB_INSTALL_DB}){
open (DBLOCK, '>', "$temp/LSMB_TEST_DB");
print DBLOCK $ENV{LSMB_NEW_DB};
close (DBLOCK);
}
-my @contrib_scripts = qw(pg_trgm tsearch2 tablefunc);
+ok($db->process_roles('Roles.sql');
-for my $contrib (@contrib_scripts){
- ok(!system "psql -f $ENV{PG_CONTRIB_DIR}/$contrib.sql");
-}
-
-
-open (PSQL, '-|', "psql -f sql/Pg-database.sql");
-my $test = 0;
-while (my $line = <PSQL>){
- chomp($line);
- if ($line eq 'COMMIT'){
- $test = 1;
- }
- if ($line =~ /error/i){
- $test = 0;
- }
-}
-cmp_ok($test, 'eq', '1', "DB Schema loaded and committed");
+#TODO: Change the COA and GIFI loading to use this, and move admin user to
+#Database.pm --CT
close(PSQL);
-open (LOADORDER, '<', 'sql/modules/LOADORDER');
-for my $mod (<LOADORDER>){
- chomp($mod);
- $mod =~ s/#.*//;
- $mod =~ s/^\s*//;
- $mod =~ s/\s*$//;
- next if $mod eq '';
- ok(open (PSQL, '-|', "psql -f sql/modules/$mod"), "$mod loaded");
- my $test = 0;
- while (my $line = <PSQL>){
- chomp($line);
- if ($line eq 'COMMIT'){
- $test = 1;
- }
- }
- close(PSQL);
-}
-close (LOADORDER);
-
-# Roles processing for later permission tests and db install.
-open (PSQL, '|-', "psql");
-
-(open (ROLES, '<', 'sql/modules/Roles.sql') && pass("Roles description found"))
-|| fail("Roles description found");
-
-for my $roleline (<ROLES>){
- $roleline =~ s/<\?lsmb dbname \?>/$ENV{LSMB_NEW_DB}/;
- print PSQL $roleline;
-}
-
-close (PSQL);
SKIP: {
skip 'No admin info', 4
if (!defined $ENV{LSMB_ADMIN_USERNAME}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.