Catalyst Config Hack

With a lot of modules for our Catalyst systems we have separate models. We then use a subset of them in a single application, and it makes sense to actually store all those database models in a single physical database. This means we end up with a lot of duplicate model config keys in our catalyst config.

<Model::Processor>
    connect_info dbi:Pg:dbname=app_db;host=db-server
</Model::Processor>
<Model::SysParams>
    connect_info dbi:Pg:dbname=app_db;host=db-server
</Model::SysParams>
<Model::AuditTrail>
    connect_info dbi:Pg:dbname=app_db;host=db-server
</Model::AuditTrail>

A lot of database configurations aren’t just a single line, and you end up spending forever copy/pasting and then modifying the config. I wanted to come up with a way to avoid all that repetition.

The Catalyst::Plugin::ConfigLoader provides two potential hooks for things to do after the configuration has been loaded. One is a finalize_config, the other is config_substitutions, via the substitutions settings. Because we are using CatalystX::AppBuilder the finalize_config doesn’t appear to be hookable, or at least I didn’t figure out how to. The substitutions is however perfectly usable because that just requires config setup in code.

   $config->{'Plugin::ConfigLoader'}->{substitutions} = {
        duplicate => sub {
            my $c = shift;
            my $from = shift;
            my $to = shift;
            $c->config->{$to} = $c->config->{$from};
        }
    };

Then this lets me do this in the config file.

<Model::Processor>
    connect_info dbi:Pg:dbname=app_db;host=db-server
    connect_info dbusername
    connect_info dbpassword
    <connect_info>
      quote_char "\""
      name_sep .
    </connect_info>
</Model::Processor>

__duplicate(Model::TokenProcessor,Model::SysParams)__
__duplicate(Model::TokenProcessor,Model::AuditTrail)__
__duplicate(Model::TokenProcessor,Model::AuthDB)__

This copies the configuration specified for the Processor to the SysParams, AuditTrail and AuthDB model config settings. This happens right after the configuration has been loaded, and before the models are loaded so all the settings are there just in time. That saves me lots of copy/paste, and even more editing. I don’t even need to copy those directives into my _local.conf because the _local.conf settings for the Processor model will be what get copied.