Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: Change DBI database on runtime

by miguelele (Beadle)
on Nov 13, 2011 at 15:55 UTC ( #937832=note: print w/replies, xml ) Need Help??

in reply to Re: Change DBI database on runtime
in thread Change DBI database on runtime

Wow!, isn't it overkill just for a string with the name of the database?, a name that (yes or yes) must be used in each request?

It seems strange that I can't split up the connection data from the schema itself

I have been looking for info on the plugins you mention. Thank you!! and Factory::PerRequest seems to be a solution, but info is very schematic. I am still learning from tutorials!!!

What i have is this model:

package the_application::Model::DB; use strict; use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => 'the_application::Schema', connect_info => { dsn => '', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); 1;

- How can I wrap it using Factory::PerRequest and change "dsn" on the fly from a Controller?


Replies are listed 'Best First'.
Re^3: Change DBI database on runtime
by zwon (Abbot) on Nov 14, 2011 at 13:44 UTC
    Wow!, isn't it overkill just for a string with the name of the database?, a name that (yes or yes) must be used in each request?

    Well, actually no, normally this name is used only once, when you're instantiating model the first time. It's a rather weird idea to use different databases for different users.

    As for how to do this... Look documentation for Catalyst::Model::Adapter, you need to define prepare_arguments and mangle_arguments methods. If you want more control (and you probably do, you can cache models for different users), you can write your own ACCEPT_CONTEXT method (see docs for Catalyst::Component and sources of Catalyst::Model::Factory). All this may be a bit tricky, and documentation is not especially verbose, but I'm sure you will enjoy the process.

      Obviously, there is something big that I do not understand in the Adaptor/Factory/Factory::PerRequest question.
      - I have the working db class posted before, called 'DB'
      - Now I wrap it from this new model called 'DBWrapper'. And I call this one from the Controller.
      - I need to configure the __PACKAGE__ to avoid compile errors from DBI. Configuration data must be present. I do not understand it, because this info should be replaced from the info coming from the request.
      - The ACCEPT_CONTEXT sub really executes. I see the required info printed in the log (THEALIAS comes from Fastcgi/Nginx custom variable)

      In the end it always query the db named "database_one" configured here, but it ignores the dynamic one from ACCEPT_CONTEXT. I tried also static values, but obviously there is something that I am doing very bad

      Really lost.

      package the_application::Model::DBWrapper; use strict; use warnings; use parent 'Catalyst::Model::Factory::PerRequest'; __PACKAGE__->config( class => 'PWC::Model::DB', connect_info => { dsn => '', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); use Moose; use namespace::autoclean; sub ACCEPT_CONTEXT { my($self, $c) = @_; # Current db name comes from fastcgi enviromental variable from Ng +inx my $current_db = $c->engine->env->{THEALIAS}; $c->log->debug('*** Reached ACCEPT_CONTEXT current db = ' . $curre +nt_db); my $new = $self->meta->clone_object($self, connect_info => { dsn => 'dbi:mysql:' . $current_db . '', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); return $new; } 1;
        clone_object method creates exact copy of your model object and replaces attribute with connection parameters, but at this time connection already established, so these parameters are not used. Instead of cloning you should create new object using connect method.

      Yes, I am enjoying it because I am learning other things in the process. But info is very raw and other sources are too complex. Not very enjoyable.

      Now I am stuck in the ACCEPT_CONTEXT concept. I can feel "the thing" almost there, but I still haven't seen it :-)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://937832]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-06-23 05:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.