Skip to content

Commit

Permalink
first pass at instance tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
dnorman committed Sep 13, 2012
1 parent a59b15f commit 6dfcd63
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions example/schemas/dbr_schema_sqlite.sql
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE dbr_instances (
schema_id int(10) NOT NULL,
handle varchar(50) NOT NULL,
class varchar(50) NOT NULL,
tag varchar(250),
dbname varchar(250),
username varchar(250),
password varchar(250),
Expand Down
6 changes: 6 additions & 0 deletions lib/DBR.pm
Expand Up @@ -70,6 +70,12 @@ sub import {
$dbr->get_instance(@_);
};

*{"${callpack}::dbr_session"} =
sub {
shift if blessed($_[0]) || $_[0]->isa( [caller]->[0] );
$dbr->session;
};


}
sub new {
Expand Down
22 changes: 11 additions & 11 deletions lib/DBR/Config/Instance.pm
Expand Up @@ -56,10 +56,12 @@ sub lookup{
}else{
my $handle = $params{handle} || return $self->_error('handle is required');
my $class = $params{class} || 'master';

$self->{guid} = $INSTANCE_MAP{$handle}->{$class} || $INSTANCE_MAP{$handle}->{'*'} or # handle aliases if there's no exact match
return $self->_error("No DB instance found for '$handle','$class'");

my $tag = $params{tag} || $self->{session}->tag;

my $h = $INSTANCE_MAP{$handle} or return $self->_error("No DB instance found for handle '$handle'");

$self->{guid} = $h->{$tag}{$class} || $h->{$tag}{'*'} || $h->{''}{$class} || $h->{''}{'*'} or # handle aliases if there's no exact match
return $self->_error("No DB instance found for handle '$handle', class '$class', tag '$tag'");
}

$INSTANCES_BY_GUID{ $self->{guid} } or return $self->_error('no such guid');
Expand All @@ -84,7 +86,7 @@ sub load_from_db{
return $self->_error('Failed to select instances') unless
my $instrows = $dbh->select(
-table => 'dbr_instances',
-fields => 'instance_id schema_id class dbname username password host dbfile module handle readonly'
-fields => 'instance_id schema_id class dbname username password host dbfile module handle readonly tag'
);

my @instances;
Expand Down Expand Up @@ -123,6 +125,7 @@ sub register { # basically the same as a new
hostname => $spec->{hostname} || $spec->{host},
user => $spec->{username} || $spec->{user},
dbfile => $spec->{dbfile},
tag => $spec->{tag} || '',
password => $spec->{password},
class => $spec->{class} || 'master', # default to master
instance_id => $spec->{instance_id} || '',
Expand Down Expand Up @@ -153,18 +156,15 @@ sub register { # basically the same as a new
$config->{connectstring} =~ s/-$key-/$config->{$key}/;
}

#Reuse the guid if we are being reloaded
my $guid = $INSTANCE_MAP{ $config->{handle} }->{ $config->{class} } || $GUID++;

# Register this instance in the global repository
$INSTANCE_MAP{ $config->{handle} }->{ $config->{class} } ||= $guid;
# Register or Reuse the guid
my $guid = $INSTANCE_MAP{ $config->{handle} }{ $config->{tag} }{ $config->{class} } ||= $GUID++;

$INSTANCES_BY_GUID{ $guid } = $config;
$self->{guid} = $config->{guid} = $guid;
# Now we are cool to start calling accessors

if ($spec->{alias}) {
$INSTANCE_MAP{ $spec->{alias} }->{'*'} = $guid;
$INSTANCE_MAP{ $spec->{alias} }{ $config->{tag} }{'*'} = $guid;
}

if ($config->{schema_id}){
Expand Down
9 changes: 9 additions & 0 deletions lib/DBR/Misc/Session.pm
Expand Up @@ -14,6 +14,7 @@ sub new {
admin => $params{admin} ? 1 : 0,
fudge_tz => $params{fudge_tz},
use_exceptions => $params{use_exceptions} ? 1 : 0,
tag => defined($params{tag}) ? $params{tag} : ''
};

bless( $self, $package );
Expand All @@ -27,6 +28,14 @@ sub new {
return $self;
}

sub tag{
my $self = shift;
if(exists $_[0]){
my $set = shift;
return $self->{tag} = defined($set) ? $set : '';
}
return $self->{tag};
}

sub timezone {
my $self = shift;
Expand Down
1 change: 1 addition & 0 deletions sql/dbr_schema_mysql.sql
Expand Up @@ -12,6 +12,7 @@ CREATE TABLE dbr_instances (
schema_id int(10) NOT NULL,
handle varchar(50) NOT NULL,
class varchar(50) NOT NULL COMMENT 'query, master, etc...',
tag varchar(250),
dbname varchar(250),
username varchar(250),
password varchar(250),
Expand Down
1 change: 1 addition & 0 deletions sql/dbr_schema_sqlite.sql
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE dbr_instances (
schema_id int(10) NOT NULL,
handle varchar(50) NOT NULL,
class varchar(50) NOT NULL,
tag varchar(250),
dbname varchar(250),
username varchar(250),
password varchar(250),
Expand Down

0 comments on commit 6dfcd63

Please sign in to comment.