| 1 | package Test::DBIx::Class::SchemaManager::Trait::Testpostgresql; { | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use MooseX::Attribute::ENV; | ||
| 5 | use Test::Postgresql58; | ||
| 6 | use Test::More (); | ||
| 7 | use Path::Class qw(dir); | ||
| 8 | |||
| 9 | |||
| 10 | has postgresqlobj => ( | ||
| 11 | is=>'ro', | ||
| 12 | init_arg=>undef, | ||
| 13 | lazy_build=>1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | |||
| 17 | has [qw/base_dir initdb postmaster/] => ( | ||
| 18 | is=>'ro', | ||
| 19 | traits=>['ENV'], | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub _build_postgresqlobj { | ||
| 23 | my ($self) = @_; | ||
| 24 | if($self->keep_db) { | ||
| 25 | $ENV{TEST_POSTGRESQL_PRESERVE} = 1; | ||
| 26 | } | ||
| 27 | |||
| 28 | my %config = ( | ||
| 29 | initdb_args => $Test::Postgresql58::Defaults{initdb_args} ."", | ||
| 30 | postmaster_args => $Test::Postgresql58::Defaults{postmaster_args}, | ||
| 31 | ); | ||
| 32 | |||
| 33 | $config{base_dir} = $self->base_dir if $self->base_dir; | ||
| 34 | $config{initdb} = $self->initdb if $self->initdb; | ||
| 35 | $config{postmaster} = $self->postmaster if $self->postmaster; | ||
| 36 | |||
| 37 | if($self->base_dir && -e $self->base_dir) { | ||
| 38 | $self->builder->ok(-w $self->base_dir, "Path ".$self->base_dir." is accessible, forcing 'force_drop_table'"); | ||
| 39 | $self->force_drop_table(1); | ||
| 40 | } | ||
| 41 | |||
| 42 | if(my $testdb = Test::Postgresql58->new(%config)) { | ||
| 43 | return $testdb; | ||
| 44 | } else { | ||
| 45 | die $Test::Postgresql58::errstr; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | sub get_default_connect_info { | ||
| 50 | my ($self) = @_; | ||
| 51 | my $port = $self->postgresqlobj->port; | ||
| 52 | |||
| 53 | if ($self->tdbic_debug || ($self->keep_db && !$self->base_dir)){ | ||
| 54 | Test::More::diag( | ||
| 55 | "Starting postgresql with: ". | ||
| 56 | $self->postgresqlobj->postmaster. | ||
| 57 | ' -p ', $port, | ||
| 58 | ' -D ', $self->postgresqlobj->base_dir . '/data' | ||
| 59 | ); | ||
| 60 | |||
| 61 | Test::More::diag("DBI->connect('DBI:Pg:dbname=template1;host=127.0.0.1;port=$port','postgres',''])"); | ||
| 62 | } | ||
| 63 | return ["DBI:Pg:dbname=template1;host=127.0.0.1;port=$port",'postgres','']; | ||
| 64 | } | ||
| 65 | |||
| 66 | after 'cleanup' => sub { | ||
| 67 | my ($self) = @_; | ||
| 68 | unless($self->keep_db) { | ||
| 69 | if($self->base_dir) { | ||
| 70 | my $dir = dir($self->base_dir); | ||
| 71 | $dir->rmtree; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | |||
| 76 | override drop_table_sql => sub { | ||
| 77 | my $self = shift; | ||
| 78 | my $table = shift; | ||
| 79 | return "drop table $table cascade"; | ||
| 80 | }; | ||
| 81 | |||
| 82 | } 1; | ||
| 83 | |||
| 84 | __END__ |