Test::Database::Driver::Pg - A Test::Database driver for Pg


Test-Database documentation Contained in the Test-Database distribution.

Index


Code Index:

NAME

Top

Test::Database::Driver::Pg - A Test::Database driver for Pg

SYNOPSIS

Top

    use Test::Database;
    my @handles = Test::Database->handles( 'Pg' );

DESCRIPTION

Top

This module is the Test::Database driver for DBD::Pg.

EXTRA PARAMETERS

Top

This driver understands the following extra parameters in the configuration file:

template

The template to use when creating a new database.

SEE ALSO

Top

Test::Database::Driver

AUTHOR

Top

Philippe Bruhat (BooK), <book@cpan.org>

COPYRIGHT

Top

LICENSE

Top

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


Test-Database documentation Contained in the Test-Database distribution.

package Test::Database::Driver::Pg;
use strict;
use warnings;
use Carp;

use Test::Database::Driver;
our @ISA = qw( Test::Database::Driver );

sub _version {
    DBI->connect_cached( $_[0]->connection_info() )
        ->selectcol_arrayref('SELECT VERSION()')->[0] =~ /^PostgreSQL (\S+)/;
    return $1;
}

sub create_database {
    my ($self) = @_;
    my $dbname = $self->available_dbname();

    DBI->connect_cached( $self->connection_info() )
        ->do( "CREATE DATABASE $dbname"
            . ( $self->{template} ? " TEMPLATE $self->{template}" : '' ) );

    # return the handle
    return Test::Database::Handle->new(
        dsn      => $self->dsn($dbname),
        name     => $dbname,
        username => $self->username(),
        password => $self->password(),
        driver   => $self,
    );
}

sub drop_database {
    my ( $self, $dbname ) = @_;

    DBI->connect_cached( $self->connection_info() )
        ->do("DROP DATABASE $dbname")
        if grep { $_ eq $dbname } $self->databases();
}

sub databases {
    my ($self)    = @_;
    my $basename  = qr/^@{[$self->_basename()]}/;
    my $databases = eval {
        DBI->connect_cached( $self->connection_info(), { PrintError => 0 } )
            ->selectall_arrayref(
            'SELECT datname FROM pg_catalog.pg_database');
    };
    return grep {/$basename/} map {@$_} @$databases;
}

'Pg';

__END__