http://www.perlmonks.org?node_id=937964


in reply to Re^2: 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?

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.

Replies are listed 'Best First'.
Re^4: Change DBI database on runtime
by miguelele (Beadle) on Nov 15, 2011 at 00:11 UTC

    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 => 'dbi:mysql:database_one:the_domain.com', 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 . ':the_domain.com', 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.

        Great!, now it works perfectly , but it is not like the docs on Factory::PerRequest. I can even comment out the '#use base 'Catalyst::Model::Factory::PerRequest'; line and it keeps working. Isn't it strange?

        This is what I understand reading my code:
        - The basic package config has empty values. But they must be present for the first load of the app, so Catalyst know what it is.
        - The sub ACCEPT_CONTEXT return a new object from PWC::Schema every time this model is requested.
        - So I am not instantiating anything else but this very class, with just one contextual variable that changes from each request.

        Am I right? Is it safe?

        package PWC::Model::MYDB; use strict; use warnings; use base 'Catalyst::Model::DBIC::Schema'; #use base 'Catalyst::Model::Factory::PerRequest'; __PACKAGE__->config( schema_class => 'PWC::Schema', connect_info => { dsn => '', user => '', password => '', AutoCommit => q{1}, } ); sub ACCEPT_CONTEXT { my($self, $c) = @_; my $db = $c->engine->env->{THEALIAS}; #From environment variable i +n Fastcgi/Nginx my $new = $self->meta->new_object( schema_class => 'PWC::Schema', connect_info => { dsn => 'dbi:mysql:' . $db . ':the_host.com', user => 'the_user', password => 'the_password', AutoCommit => q{1}, } ); return $new; } 1;
Re^4: Change DBI database on runtime
by miguelele (Beadle) on Nov 14, 2011 at 16:58 UTC

    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 :-)

    Thanks