NAME

Test::DBIx::Class::SchemaManager::Trait::Testpostgresqlv2 - Use Test::PostgreSQL::v2 as the database backend for Test::DBIx::Class

VERSION

Version 2.03

SYNOPSIS

In your test file:

use Test::More;
use Test::DBIx::Class {
    schema_class => 'MyApp::Schema',
    traits       => ['Testpostgresqlv2'],
    deploy_db    => 1,
}, qw/:resultsets/;

ok my $user = ResultSet('User')->create({ name => 'John' }), 'Created user';
is $user->name, 'John', 'Name persisted correctly';

done_testing;

DESCRIPTION

This Moose::Role is a trait for Test::DBIx::Class::SchemaManager that transparently replaces the legacy Testpostgresql trait. Instead of relying on the aging Test::PostgreSQL module, it spins up a temporary PostgreSQL instance using Test::PostgreSQL::v2, which is designed for modern Linux environments (Ubuntu 22.04+, Debian Bookworm, and similar distributions) that restrict writes to system socket directories such as /var/run/postgresql.

When the trait is activated via the traits key in Test::DBIx::Class's configuration hash, the schema manager calls get_default_connect_info during its own initialisation phase. This trait overrides that method to:

  1. Instantiate a Test::PostgreSQL::v2 object, which handles binary discovery, temporary directory creation, initdb, and process management automatically.

  2. Store the live instance on the schema manager object so the PostgreSQL process is kept alive for the entire duration of the test run.

  3. Return a DBI-compatible connection array-ref (DSN, username, password, and attributes) for Test::DBIx::Class to use when connecting to the schema.

The temporary PostgreSQL instance, including all data files, configuration, and logs, is automatically removed when the test process exits, because Test::PostgreSQL::v2 uses File::Temp with CLEANUP => 1.

INTEGRATION

Replacing the legacy Testpostgresql trait

If you are already using Test::DBIx::Class with the Testpostgresql trait, migration is a one-line change:

# Before
traits => ['Testpostgresql'],

# After
traits => ['Testpostgresqlv2'],

No other changes to your test files are required.

Trait discovery

Test::DBIx::Class maps trait names to packages by prepending Test::DBIx::Class::SchemaManager::Trait::. The trait name Testpostgresqlv2 therefore resolves to this package. Ensure the file is on Perl's @INC, typically by placing it under lib/ in your distribution or under t/lib/ for test-only use.

Passing extra options to Test::PostgreSQL::v2

The current implementation uses the default Test::PostgreSQL::v2 constructor. If you need to customise the PostgreSQL instance (for example, to bind to a specific port or host), subclass this trait and override get_default_connect_info accordingly.

METHODS

get_default_connect_info

my $connect_info = $self->get_default_connect_info;

Called internally by Test::DBIx::Class::SchemaManager during schema initialisation. Starts a temporary PostgreSQL instance via Test::PostgreSQL::v2 and returns a four-element array-ref suitable for passing directly to "connect" in DBI:

[ $dsn, $user, $password, \%dbi_attributes ]

The AutoCommit attribute is set to 1 by default, matching the behaviour expected by DBIx::Class.

Dies with a descriptive message (including the content of $Test::PostgreSQL::v2::errstr) if the PostgreSQL instance cannot be started.

Note: This method is an extension point defined by Test::DBIx::Class::SchemaManager. It is not part of the public API of this trait and should not be called directly from test code.

DEPENDENCIES

SEE ALSO

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

REPOSITORY

https://github.com/manwar/Test-PostgreSQL-v2

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/Test-PostgreSQL-v2/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Test::DBIx::Class::SchemaManager::Trait::Testpostgresqlv2

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2026 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and / or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at: http://www.perlfoundation.org/artistic_license_2_0 Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed. Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.