[Catalyst] Switching between different databases during a session

Matt S Trout dbix-class at trout.me.uk
Fri Nov 16 16:05:39 GMT 2007


On Thu, Nov 15, 2007 at 07:01:53PM -0800, Andrew Peebles wrote:
> Andrew Peebles wrote:
> >I've been searching and experimenting and have not found a solution ...
> >
> >I have multiple instances of a database (each in a separate sqlite db 
> >file), same schema, different data.  Each represents a snapshot of 
> >something at a particular time.  During a session, user wants switch 
> >among these databases.  I am trying to add a method to my Model 
> >(Catalyst::Model::DBIC::Schema) that takes a new connection string, 
> >and switches to that database ... all things otherwise remaining the 
> >same.  Seems relatively simple, but I just ain't getting it.  Lots of 
> >hints on the list, but nothing I can find that's helping.
> >
> >I've tried a lot of variants on something like this:
> >
> >package SMART::Model::SMARTDB;
> >
> >use strict;
> >use base 'Catalyst::Model::DBIC::Schema';
> >
> >my $dsn = $ENV{MY_DSN} ||= 
> >'dbi:SQLite:/auto/project/tools/metadot/html/cgi/SMART/smart.db';
> >
> >__PACKAGE__->config(
> >   schema_class => 'SMARTDB',
> >   connect_info => [
> >       $dsn,
> >       '',
> >       '',
> >       {AutoCommit => 1},
> >         ],
> >);
> >
> >sub switch {
> >   my $self = shift;
> >   my $dsn  = shift;
> >
> >   my $connect_info = [ @{$self->{connect_info}} ];
> >   $$connect_info[0] = $dsn;
> >
> >   my $schema = $self->clone();
> >   $schema->connection(@$connect_info);
> >   $self->schema($schema);
> >}
> >
> >1;
>
> Well ... in fact the code above works like a charm.  I had a problem in 
> another part of my environment that was making this look like it didn't 
> work, but it does.  Be nice to add this piece of code to one of the 
> Cookbooks somewhere, maybe.

Maybe if you rewrote it to use Catalyst::Component::InstancePerContext it'd
make a good cookbook entry.

Your current approach is, frankly, scary - you're mutating a global object
without regard for what the next request is going to expect. I wouldn't let
this code anywhere near a production system.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the Catalyst mailing list