Skip to content

Commit

Permalink
support aliases in the database configuration
Browse files Browse the repository at this point in the history
[database-something]
alias = combust


git-svn-id: https://svn.develooper.com/combust/trunk@338 516b4451-ceb8-0310-875f-b17149c99682
  • Loading branch information
abh committed Jul 12, 2005
1 parent f83b0a7 commit ec014fe
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions combust.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ default=1
# data_source = dbi:...
# ...

# configure the "something" database to be an alias to the combust database configuration
# [database-something]
# alias = combust


# Configure sites
Expand Down
20 changes: 17 additions & 3 deletions lib/Combust/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ sub _setup_dbs {
}

unless ($got_default) {
if (keys %dbs > 1) {
if (scalar (grep { !$dbs{$_}->{alias} } keys %dbs) > 1) {
croak 'You defined more than one database but didn\'t mark one of them "default=1"';
}
elsif (keys %dbs == 1) {
my ($db_name) = keys %dbs;
else {
my ($db_name) = grep { !$dbs{$_}->{alias} } keys %dbs;
$dbs{default} = $dbs{$db_name};
}
}
Expand Down Expand Up @@ -115,13 +115,27 @@ sub base_url {
sub database {
my ($self, $db_name) = @_;
carp "No databases configured in the combust configuration" and return unless %dbs;

$db_name ||= 'default';

$db_name = $self->_resolve_db_alias($db_name);

unless ($db_name and $dbs{$db_name}) {
cluck "no database $db_name defined, using default" if $db_name;
$db_name = 'default';
$db_name = $self->_resolve_db_alias($db_name);
}
$dbs{$db_name};
}

sub _resolve_db_alias {
my ($self, $db_name) = @_;
if ($dbs{$db_name} and $dbs{$db_name}->{alias}) {
return $dbs{$db_name}->{alias};
}
return $db_name;
}

sub db_data_source { shift->database->{data_source}; }
sub db_password { shift->database->{password}; }
sub db_user { shift->database->{user}; }
Expand Down
3 changes: 3 additions & 0 deletions t/config/30.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ ok(my $c = Combust::Config->new, "new");
is($c->database('test1')->{user}, "my_user1", "database->{user}");
is($c->db_password, 'my_password2', 'db_password (Get default password)');
is($c->db_data_source, 'dbi:driver2:database=database2;host=host2', 'db_data_source (Get default)');
# If Test::Warning is installed we should test we get the warning...
is($c->database('not_here'), $c->database('test2'), q[unconfigured database (get default, with warning)]);


11 changes: 11 additions & 0 deletions t/config/40.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


[database-test1]
data_source = dbi:driver1:database=database1;host=host1
user = my_user1
password = my_password1

[database-test2]
alias = test1
default = 1

12 changes: 12 additions & 0 deletions t/config/40alias.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use Test::More qw(no_plan);
use lib "$ENV{CBROOT}/lib";
BEGIN {
$ENV{CBCONFIG} = "$ENV{CBROOT}/t/config/40.conf";
use_ok('Combust::Config');
}

ok(my $c = Combust::Config->new, "new");
is($c->database('test1')->{user}, "my_user1", "database->{user}");
is($c->database('test2')->{user}, "my_user1", "aliased database->{user}");
is($c->database('test-default')->{user}, "my_user1", "defaulted to aliased database->{user}");

0 comments on commit ec014fe

Please sign in to comment.