perl - 有没有办法在催化剂中使用测试数据库

标签 perl catalyst

使用 Catalyst 时,有没有办法像在 Rails 中一样指定开发、测试和生产数据库?我已经查看了文档,但没有找到答案。

最佳答案

引用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>

另请检查this使用单独的数据库时

关于perl - 有没有办法在催化剂中使用测试数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740424/

相关文章:

perl - 如何为 Catalyst 中的每个响应设置 Cache-Control header ?

perl - 在 Catalyst 中使用 Plack::Middleware 兼容异常时缺少日志输出

perl - 如何在 Strawberry Perl 中安装 DBD::Oracle

c - 如何访问 C 程序中的 perl 控制台输出?

Perl 的文件测试运算符 -f 对符号链接(symbolic link)返回 true

apache-flex - Flex/AIR + 催化剂 : What's the best way to get them to talk to each other?

perl - 如何为 catalyst.pl 生成的文件设置样板信息?

perl - 如何使用进度条在 Catalyst 中启动长时间运行的作业?

perl - 为什么我不应该在 Perl 代码中使用 shell 工具?

perl - Perl 中的 Nano sleep 命令