The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::DBIC::Pg - Connect to and deploy a DBIx::Class::Schema on Postgres

SYNOPSIS

The preferred way:

    #! perl -w
    use Test::More;
    use Test::DBIC::Pg;

    my $td = Test::DBIC::Pg->new(schema_class => 'My::Schema');
    my $schema = $td->connect_dbi_ok();
    ...
    $schema->storage->disconnect();
    $td->drop_dbic_ok();
    done_testing();

The compatible with Test::DBIC::SQLite way:

    #! perl -w
    use Test::More;
    use Test::DBIC::Pg;
    my $schema = connect_dbic_pg_ok('My::Schema');
    ...
    $schema->storage->disconnect();
    drop_dbic_pg_ok();
    done_testing();

DESCRIPTION

This is an implementation of Test::DBIC::Pg that uses the Moo::Role: Test::DBIC::DBDConnector from the Test::DBIC::SQLite package.

It will import() warnings and strict for you.

Test::DBIC::Pg->new

    my $td = Test::DBIC::Pg->new(%parameters);
    my $schema = $td->connect_dbic_ok();
    ...
    $schema->storage->disconnect();
    $td->drop_dbic_ok();

Parameters

Named, list:

schema_class => $schema_class (Required)

The class name of the DBIx::Class::Schema to use.

dbi_connect_info => $pg_connect_info (Optional, { dsn => "dbi:Pg:dbname=_test_dbic_pg_$$" })

This is a HashRef that will be used to connect to the PostgreSQL server:

dsn => dbi:Pg:host=mypg;dbname=_my_test_x

This Data Source Name (dsn) must also contain the dbi:Pg: bit that is needed for DBI to connect to your database/server. We do allow for DBI options syntax: dbi:Pg(FetchHashKeyName=>NAME_uc):dbname=blah

If your database doesn't exist it will be created. This will need an extra temporary database connection.

username => $username

This is the username that will be used to connect to the PostgreSQL server, if omitted DBD::Pg will try to use $ENV{PGUSER}.

password => $password

This is the password that will be used to connect to the PostgreSQL server, if omitted DBD::Pg will look at ~/.pgpass to see if it can find a suitable password in there. (See also postgres docs for $ENV{PGPASSWORD} en $ENV{PGPASSFILE}).

options => $options_hash

This options hashref is also passed to the DBIx::Class::Schema->connect() method for extra options. This hash will contain the extra key/value pair skip_version => 1 whenever the wants_deploy attribute is true.

pre_deploy_hook => $pre_deploy_hook (Optional)

A CodeRef to execute before $schema->deploy is called.

This CodeRef is called with an instantiated $your_schema_class object as argument.

post_connect_hook => $post_connect_hook (Optional)

A coderef to execute after $schema->deploy is called, if at all.

This coderef is called with an instantiated $your_schema_class object as argument.

TMPL_DB => $template_database (Optional, template1)

In order to create and drop your test database a temporary connection needs to be made to the PostgreSQL instance from your dsn, but with a template database (tools like createdb and dropdb also do this in the background). The default database for these type of connections is template1 - and this module uses that as well - but your DBA could have configured a different database for this function, therefore we support the setting of TMPL_DB.

$td->connect_dbic_ok()

This method is inherited from Test::DBIC::DBDConnoctor.

If the database needs deploying, there will be another temporary database connection to the template database in order to issue the CREATE DATABASE $dbname statement.

Returns

An initialised instance of $schema_class.

$td->drop_dbic_ok

This method implements a dropdb $dbname, in order not to litter your server with test databases.

During this method there will be another temporary database connection to the template database, in order to issue the DROP DATABASE $dbname statement (that cannot be run from the connection with the test database it self).

connect_dbic_pg_ok(@parameters)

Create a PostgreSQL database and deploy a dbic_schema. This function is provided for compatibility with Test::DBIC::SQLite.

See Test::DBIC::Pg->new for further information, although only these 4 arguments are supported.

Parameters

Positional:

1. $schema_class (Required)
2. $pg_connect_info (Optional)
3. $pre_deploy_hook (Optional)
4. $post_connect_hook (Optional)

drop_dbic_pg_ok()

This function uses the cached information of the call to connect_dbic_pg_ok() and clears it after the database is dropped, using another temporary connection to the template database.

See the drop_dbic_ok() method.

Implementation of MyDBD_connection_parameters

As there is no fiddling with the already provided connection paramaters, this method sets up the connection parameter for the temporary connection to the template database in order to create or drop the (temporary) test database.

Implementation of MyDBD_check_wants_deploy

In this method the temporary connection to the template database is set up and a list of available database is requested - via $dbh->data_sources() - to check if the test database already exists. If it doesn't, the database will be created and a true value is returned, otherwise a false value is returned and no new database is created.

AUTHOR

© MMXXI - Abe Timmerman <abeltje@cpan.org>

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.