Skip to content
Snippets Groups Projects
Commit 9f31585a authored by Arne Stabenau's avatar Arne Stabenau
Browse files

The preload configured database is now automatically created if it doesnt exist on test host

parent cfca09e6
No related branches found
No related tags found
No related merge requests found
......@@ -177,15 +177,6 @@ sub load_databases {
#create a config hash which will be frozen to a file
$self->{'conf'} = {};
#only unzip if there are non-preloaded datbases
UNZIP: foreach my $dbtype (keys %{$db_conf->{'databases'}}) {
if( !$db_conf->{'preloaded'}->{$dbtype} ) {
#unzip database files
$self->unzip_test_dbs($self->curr_dir . $zip);
last UNZIP;
}
}
#connect to the database
my $locator = "DBI:".$driver.":host=".$host.";port=".$port;
my $db = DBI->connect($locator, $user, $pass, {RaiseError => 1});
......@@ -195,10 +186,22 @@ sub load_databases {
return;
}
#only unzip if there are non-preloaded datbases
UNZIP: foreach my $dbtype (keys %{$db_conf->{'databases'}}) {
if(( ! exists $db_conf->{'preloaded'}->{$dbtype} ) ||
( ! _db_exists( $db, $db_conf->{'preloaded'}{$dbtype}) )) {
#unzip database files
$self->unzip_test_dbs($self->curr_dir . $zip);
last UNZIP;
}
}
#create a database for each database specified
foreach my $dbtype (keys %{$db_conf->{'databases'}}) {
#don't create a database if there is a preloaded one specified
if( $db_conf->{'preloaded'}->{$dbtype} ) {
if(( $db_conf->{'preloaded'}->{$dbtype} ) &&
( _db_exists( $db,$db_conf->{'preloaded'}->{$dbtype} ))) {
#copy the general config into a dbtype specific config
$self->{'conf'}->{$dbtype} = {};
%{$self->{'conf'}->{$dbtype}} = %$db_conf;
......@@ -214,30 +217,35 @@ sub load_databases {
$self->{'conf'}->{$dbtype}->{'preloaded'} = 1;
} else {
#create a unique random dbname
my $dbname = $self->_create_db_name($dbtype);
print STDERR "\nCreating db $dbname";
unless($db->do("CREATE DATABASE $dbname")) {
warning("Could not create database [$dbname]");
return;
}
#copy the general config into a dbtype specific config
$self->{'conf'}->{$dbtype} = {};
%{$self->{'conf'}->{$dbtype}} = %$db_conf;
$self->{'conf'}->{$dbtype}->{'module'} = $db_conf->{'databases'}->{$dbtype};
# it's not necessary to store the databases and zip bits of info
delete $self->{'conf'}->{$dbtype}->{'databases'};
delete $self->{'conf'}->{$dbtype}->{'preloaded'};
delete $self->{'conf'}->{$dbtype}->{'zip'};
#create a unique random dbname
my $dbname = $db_conf->{'preloaded'}->{$dbtype};
if( ! defined $dbname ) {
$dbname = $self->_create_db_name($dbtype);
delete $self->{'conf'}->{$dbtype}->{'preloaded'};
} else {
$self->{'conf'}->{$dbtype}->{'preloaded'} = 1;
}
#store the temporary database name in the dbtype specific config
$self->{'conf'}->{$dbtype}->{'dbname'} = $dbname;
print STDERR "\nCreating db $dbname";
unless($db->do("CREATE DATABASE $dbname")) {
warning("Could not create database [$dbname]");
return;
}
#copy the general config into a dbtype specific config
$db->do("use $dbname");
#load the database with data
......@@ -527,6 +535,22 @@ sub save {
}
}
sub _db_exists {
my ( $db, $db_name ) = @_;
my $db_names = $db->selectall_arrayref( "show databases" );
for my $db_name_ref ( @$db_names ) {
if( $db_name_ref->[0] eq $db_name ) {
return 1;
}
}
return 0;
}
sub compare {
my ($self, $dbtype, $table) = @_;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment