Configure DBIC connect_info based on your local environment

ASSUMPTION: You have a Catalyst application with which you want to use one database for development and another database for production.

A SOLUTION: Use a configuration that is based on the user and host the application is running as and on repsectively. For example if you have a development machine named devster and you start myapp_server.pl as the user pornstar then you could load a configuration file named myapp_pornstar_devster.conf. In general, the following setup will load the configuration file conf/myapp_login_host.conf. Next is an example of what the .conf file looks like to define the database connection.

connect_info in conf/myapp_login_host.conf

<Model::DBIC>
    schema_class   MyApp::Schema
    connect_info   dbi:Pg:dbname=mydb;host=localhost;port=5432
    connect_info   username
    connect_info   password
<Model::DBIC>

Notes: This configuration example is in the Config::General format. Further, the example assumes MyApp/Schema.pm is a subclass of DBIx::Class::Schema. For example, a minimal MyApp/Schema.pm with automatic loading of classes from the MyApp/Schema/Result/* and MyApp/Schema/ResultSet/* directories looks like:

package MyApp::Schema;
use strict;
use warnings;
use base 'DBIx::Class::Schema';

__PACKAGE__->load_namespaces;

1;

Using Catalyst::Plugin::ConfigLoader to load the local .conf

To make the DBIC schema aware of the connect_info in the myapp_login_host.conf one can use Catalyst::Plugin::ConfigLoader. It is done by configuring parts of MyApp.pm like follows:

In MyApp.pm

use Catalyst qw(
    ConfigLoader
);

Further down in MyApp.pm is where the configuration file name is created based on login and host name.

my $login = getlogin() || $ENV{USER};
my ($host) = Sys::Hostname::hostname() =~ m/^([^\.]+)/;
__PACKAGE__->config(
    name                   => 'MyApp',
    'Plugin::ConfigLoader' => {
        file                => __PACKAGE__->path_to('conf'),
        config_local_suffix => $login . '_' . $host,
    },
);

The code above causes ConfigLoader to configure your Catalyst application using the file conf/myapp_login_host.conf.

Note: Make ConfigLoader the first plugin you load so the others are aware of the configuration information. The ConfigLoader manual mentions a helpful way of converting configuration file formats. The following changes whatever format you are using (e.g YAML) to the Config::General format:

perl -Ilib -MMyApp -MConfig::General -e 'Config::General->new->save_file("myapp.conf", MyApp->config);

Override Configuration with Environment Variables using ConfigLoader::Environment

The module Catalyst::Plugin::ConfigLoader::Environment allows one to add configuration information as well. If ConfigLoader::Environment is loaded after ConfigLoader then what's set in the shell environment will take precedent. An example of setting DBIC connect_info via the environment is:

export MYAPP_Model__DBIC_connect_info='["dbi:Pg:dbname=mydb;host=localhost;port=5432", "user", "pass"]'
My tags:
 
Popular tags:
  DBIC connect_info ConfigLoader
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker