5

When using catalyst is there a way to specify a dev, test and production database like you would in Rails? I have looked through the documentation but I haven't found an answer.

dflower
  • 83
  • 1
  • 4

1 Answers1

4

With reference from cpan Catalyst Testing Tutorial

You may wish to maintain both a "production database" for your live application and a "testing database" for your test cases.

DATABASE CONFIG SWITCHING IN YOUR MODEL CLASS

One solution is to allow the database specification to be overridden with an environment variable. For example, open lib/MyApp/Model/MyAppDB.pm in your editor and change the __PACKAGE__->config(... declaration to resemble:

my $dsn = $ENV{MYAPP_DSN} ||= 'dbi:SQLite:myapp.db';
__PACKAGE__->config(
    schema_class => 'MyAppDB',
    connect_info => [
        $dsn,
        '',
        '',
        { AutoCommit => 1 },

    ],
);

Then, when you run your test case, you can use commands such as:

$ cp myapp.db myappTEST.db
$ CATALYST_DEBUG=0 MYAPP_DSN="dbi:SQLite:myappTEST.db" prove --lib lib -v t/live_app01.t

This will modify the DSN only while the test case is running. If you launch your normal application without the MYAPP_DSN environment variable defined, it will default to the same dbi:SQLite:myapp.db as before.

DATABASE CONFIG SWITCHING USING MULTIPLE CONFIG FILES

By utilizing Catalyst::Plugin::ConfigLoaders functionality for loading multiple config files based on environment variables you can override your default (production) database connection settings.

Setting $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX } to 'testing' in your test script results in loading of an additional config file named myapp_testing.conf after myapp.conf which will override any parameters in myapp.conf.

You should set the environment variable in the BEGIN block of your test script to make sure it's set before your Catalyst application is started.

The following is an example for a config and test script for a DBIx::Class model named MyDB and a controller named Foo:

myapp_testing.conf:

<Model::MyDB>
    <connect_info>
        dsn dbi:SQLite:myapp.db
    </connect_info>
</Model::MyDB>

Also check this when using seperate databases