[Catalyst] One App, multiple databases

Mesdaq, Ali amesdaq at websense.com
Wed Nov 19 19:43:56 GMT 2008


Are these db's exact copies as far as schema from each other? Or is it different tables and structure as well? 

Reason I am asking is because if its exactly the same and all your queries work the same and your logic works the same as well and the only difference is if user1 is connected then connect to one schema and if user2 is connected connect to another schema you might be able to more cleanly determine the db in your controller code. You would also then create a model for each user. This would give you granular control over if the user db's ever move or if you need to configure specific connection data per users db like different user accounts and passwords etc.

So instead of something like:
$c->model('DB::Blah')->all

You could do:
$c->model("$user:Blah")->all

But I think knowing a little more about your exact situation might help understand the issue more.

Thanks,
------------------------------------------
Ali Mesdaq (CISSP, GIAC-GREM)
Sr. Security Researcher
Websense Security Labs
http://www.WebsenseSecurityLabs.com
------------------------------------------


-----Original Message-----
From: Jose Luis Martinez [mailto:jlmartinez-lists-catalyst at capside.com] 
Sent: Wednesday, November 19, 2008 9:54 AM
To: The elegant MVC web framework
Subject: [Catalyst] One App, multiple databases

Hello,

This question has been asked a couple of times on the list, and I have 
found yet another solution to it, but I would like to hear if maybe I'm 
doing something wrong, or I will suffer serious pain by doing it my way :)

We have an app that will connect to one database or another depending on 
the logged in user.

My solution:

package App::Model::DB;

use strict;
use base 'Catalyst::Model::DBIC::Schema';
...
sub ACCEPT_CONTEXT {
     my ($self, $c) = @_;

     my $user_db = $c->lookup_the_users_db();
     $self->config->{'connect_info'}->[0] =~ s/#DATABASE#/$user_db/;
     $self->schema->connection(@{$self->config->{'connect_info'}});

     return $self;
}


1;

I've done the same with Catalyst::Model::DBI:

package App::Model::AnotherDB;

use strict;
use base 'Catalyst::Model::DBI';

sub ACCEPT_CONTEXT {
     my ($self, $c) = @_;

     my $user_db = $c->lookup_the_users_db();
     $self->{'dsn'} =~ s/#DATABASE#/$user_db/;

     return $self;
}

1;

And the two seem to be working OK, but I'm worried about what will 
happen when we fire it up in a FastCGI environment (I suspect the 
connection to  user1's database will be kept live, and the next user 
will get the connection to it). Am I right? Any pointers?

Does this way of using the models trigger any warning lights to Catalyst 
gurus?

Thanks in advance,

Jose Luis Martinez
jlmartinez at capside.com

_______________________________________________
List: Catalyst at lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


 Protected by Websense Hosted Email Security -- www.websense.com 



More information about the Catalyst mailing list