corporate_gadfly has asked for the wisdom of the Perl Monks concerning the following question:

I am running mod_perl and Apache::DBI to maintain persistent database connections. Any good techniques to flip a switch in httpd.conf so that the site connects to the development database? Flip the switch again and it connects to the production database.

Ideas??

(Should I set a PerlSetVar variable to either DEVL or PROD and then read its value in startup.pl which is where I establish the connect_on_init database connection?)

Thanks in advance
-- Corporate Gadfly

  • Comment on apache::dbi how to switch between development or production database?

Replies are listed 'Best First'.
Re: apache::dbi how to switch between development or production database?
by perrin (Chancellor) on Dec 17, 2001 at 23:34 UTC
    Your PerlSetVar idea is good. You can set a key there that you use to pull the values from a hash in your startup.pl or a similar config file. You can also do something where you pass a value to the httpd and use that to switch between sets of PerlSetVars in httpd.conf.

    On command-line:

    > httpd -DPROD
    In httpd.conf:
    <IfDefine PROD> PerlSetVar DATABASE_NAME foo PerlSetVar DATABASE_USER bar PerlSetVar DATABASE_PASS baz </IfDefine>

    You can do fancier stuff too. In one place that I worked, we used a small script with Template Toolkit to re-generate httpd.conf for different environments based on a small file of values keyed on different environments. It was nice because we had the ability to do inheritance, where we would define defaults and override them for development or other special cases.

      Why does this set my spidey senses to tingling even though it is totally responsive to the question? Personaly, httpd.conf is one file that I do not want in any state of flux, especially if there is more than one person working on a site.
        I told you one way to do it without modifying files (httpd -DSOMETHING). You could also put all of this in a perl config file if you like that better, or use virtual hosts so that one goes to development and one to production depending on the hostname or port you use.
Re: apache::dbi how to switch between development or production database?
by chromatic (Archbishop) on Dec 18, 2001 at 01:16 UTC
    That's exactly what I'd do. You could go one step further and use DBIx::Password to store your connection strings. That's what Slash does, and it handles virtual hosts rather nicely.
Re: apache::dbi how to switch between development or production database?
by IlyaM (Parson) on Dec 18, 2001 at 03:51 UTC
    I put all information that can change between production and development server into config file (I use XML configs parsed with XML::Simple but it could be any other format including just perl script) and I use PerlSetVar to point to this file.

    --
    Ilya Martynov (http://martynov.org/)

Re: apache::dbi how to switch between development or production database?
by talexb (Chancellor) on Dec 18, 2001 at 09:53 UTC
    It doesn't look like this has already been covered. I have been using DBI for a little while and when I first started using it I was astounded that
    $dbh ||= DBI->connect ( ... );
    created a new connection if an existing one was not already available -- meaning that the first time through, $dbh would contain a handle to a database, but after that, the handle could be re-used by other hits on the module.

    My concern with your situation is that a handle to the wrong database might persist between changes from production to development. Just wanted to warn against that possibility (hopefully this comment isn't too far off base for you to use).

    Of course, if you are doing a graceful or a full restart of Apache, then there won't be a problem.

    "Excellent. Release the hounds." -- Monty Burns.