| 1 | package Database::Temp::Driver::Pg; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Create an ad-hoc database, driver for Postgres | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.003'; # VERSION: generated by DZP::OurPkgVersion | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Module::Load::Conditional qw( can_load ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Log::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub is_available { | ||||||||||||||||||||||||||
|
Subroutine
is_available: called CC=3 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 1 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||||||||||||||||
| 18 | 1 | my %needed = ( 'DBD::Pg' => 1.41_01, ); | |||||||||||||||||||||||||||
| 19 | 1 | ▶ | if ( !can_load( modules => \%needed ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | $_log->infof( 'Cannot load module %s, %s', %needed ); | |||||||||||||||||||||||||||
| 21 | 1 | return 0; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 0 | my $dbh; | |||||||||||||||||||||||||||
| 24 | 0 | my $ok = 1; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 0 | $dbh = _open_dbh( _main_db_connection() ); | |||||||||||||||||||||||||||
| 27 | 0 | $dbh->disconnect(); | |||||||||||||||||||||||||||
| 28 | 0 | 1; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 0 | my $error = $_; | |||||||||||||||||||||||||||
| 32 | 0 | $_log->errorf( 'Cannot connect to main db connection. Error: %s', $error ); | |||||||||||||||||||||||||||
| 33 | 0 | $ok = 0; | |||||||||||||||||||||||||||
| 34 | 0 | }; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | return 0 if ( !$ok ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | return 1; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _main_db_connection { | ||||||||||||||||||||||||||
|
Subroutine
_main_db_connection: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | return { | ||||||||||||||||||||||||||||
| 41 | 0 | 'dsn' => _compile_dsn( 'postgres', undef, undef, 'Database--Temp' ), | |||||||||||||||||||||||||||
| 42 | 'username' => undef, | ||||||||||||||||||||||||||||
| 43 | 'password' => undef, | ||||||||||||||||||||||||||||
| 44 | 'attr' => { | ||||||||||||||||||||||||||||
| 45 | 'AutoCommit' => 1, | ||||||||||||||||||||||||||||
| 46 | 'RaiseError' => 1, | ||||||||||||||||||||||||||||
| 47 | 'PrintError' => 1, | ||||||||||||||||||||||||||||
| 48 | 'pg_server_prepare' => 0, | ||||||||||||||||||||||||||||
| 49 | 'TraceLevel' => 0, | ||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _open_dbh { | ||||||||||||||||||||||||||
|
Subroutine
_open_dbh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | 0 | my ($db_connection) = @_; | |||||||||||||||||||||||||||
| 56 | return DBI->connect( $db_connection->{'dsn'}, $db_connection->{'username'}, $db_connection->{'password'}, | ||||||||||||||||||||||||||||
| 57 | 0 | $db_connection->{'attr'} ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub _create { | ||||||||||||||||||||||||||
|
Subroutine
_create: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $temp_db_name = $name; | |||||||||||||||||||||||||||
| 65 | 0 | my $temp_user_name = $name; | |||||||||||||||||||||||||||
| 66 | 0 | my $main_dbh = _open_dbh(_main_db_connection); | |||||||||||||||||||||||||||
| 67 | 0 | $main_dbh->do("CREATE ROLE \"$temp_user_name\" LOGIN ENCRYPTED PASSWORD '$temp_user_name'"); | |||||||||||||||||||||||||||
| 68 | 0 | $_log->debugf( 'Created test user \'%s\'', $temp_user_name ); | |||||||||||||||||||||||||||
| 69 | 0 | $main_dbh->do("CREATE DATABASE \"$temp_db_name\" OWNER \"$temp_user_name\""); | |||||||||||||||||||||||||||
| 70 | 0 | $_log->debugf( 'Created temp database \'%s\'', $temp_db_name ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # $main_dbh->do("GRANT ALL ON DATABASE \"$temp_db_name\" TO \"$temp_db_name\""); | ||||||||||||||||||||||||||||
| 73 | 0 | $main_dbh->disconnect(); | |||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _compile_dsn { | ||||||||||||||||||||||||||
|
Subroutine
_compile_dsn: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 78 | 0 | my ( $name, $host, $port, $app_name ) = @_; | |||||||||||||||||||||||||||
| 79 | 0 | my $dsn; | |||||||||||||||||||||||||||
| 80 | 0 | my $driver = ( __PACKAGE__ =~ m/::( [[:alnum:]]{1,}) $/msx )[0]; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( $host && $port ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$host and $port
MC/DC0%0/2
$host and $port
$host
$port
Truth table0%0/3
A and B
A= $hostB= $port
| |||||||||||||||||||||||||||||
| 82 | 0 | $dsn = sprintf 'dbi:%s:dbname=%s;host=%s;port=%s;application_name=%s', $driver, $name, $host, $port, $app_name; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | 0 | $dsn = sprintf 'dbi:%s:dbname=%s;application_name=%s', $driver, $name, $app_name; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | return $dsn; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | my ( $class, %params ) = @_; | |||||||||||||||||||||||||||
| 92 | 0 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $name = $params{'name'}; | |||||||||||||||||||||||||||
| 95 | 0 | _create($name); | |||||||||||||||||||||||||||
| 96 | 0 | my $dsn = _compile_dsn( $name, 'localhost', '5432', 'DBHandle' ); | |||||||||||||||||||||||||||
| 97 | 0 | $_log->debugf( 'Created temp database \'%s\'', $name ); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my %attrs = ( | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # 'ReadOnly' => 0, | ||||||||||||||||||||||||||||
| 102 | 'AutoCommit' => 1, | ||||||||||||||||||||||||||||
| 103 | 'RaiseError' => 1, | ||||||||||||||||||||||||||||
| 104 | 'PrintError' => 1, | ||||||||||||||||||||||||||||
| 105 | 'RaiseWarn' => 1, | ||||||||||||||||||||||||||||
| 106 | 'PrintWarn' => 1, | ||||||||||||||||||||||||||||
| 107 | 'TaintIn' => 1, | ||||||||||||||||||||||||||||
| 108 | 'TaintOut' => 0, | ||||||||||||||||||||||||||||
| 109 | 'TraceLevel' => 0, | ||||||||||||||||||||||||||||
| 110 | 'pg_server_prepare' => 0, | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | 0 | my %info = (); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Construct start method | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $_start = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | ## no critic (Variables::ProhibitReusedNames) | ||||||||||||||||||||||||||||
| 117 | 0 | my ( $dbh, $name ) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); | |||||||||||||||||||||||||||
| 119 | 0 | $_log->debugf( 'Created temp db \'%s\'', $name ); | |||||||||||||||||||||||||||
| 120 | 0 | }; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Construct init method | ||||||||||||||||||||||||||||
| 123 | 0 | my $init; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ( ref $params{'init'} eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $init = $params{'init'}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { # SCALAR | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $init = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 129 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 130 | 0 | $dbh->begin_work(); | |||||||||||||||||||||||||||
| 131 | 0 | foreach my $row ( split qr/;\s*/msx, $params{'init'} ) { | |||||||||||||||||||||||||||
| 132 | 0 | $dbh->do($row); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | $dbh->commit; | |||||||||||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | } | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Construct deinit method | ||||||||||||||||||||||||||||
| 140 | 0 | my $deinit; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( ref $params{'deinit'} eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | $deinit = $params{'deinit'}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | else { # SCALAR | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $deinit = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 146 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 147 | 0 | $dbh->begin_work(); | |||||||||||||||||||||||||||
| 148 | 0 | foreach my $row ( split qr/;\s*/msx, $params{'deinit'} ) { | |||||||||||||||||||||||||||
| 149 | 0 | $dbh->do($row); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | $dbh->commit; | |||||||||||||||||||||||||||
| 152 | 0 | return; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | } | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Construct _cleanup method | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my $_cleanup = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 158 | 0 | my ( $dbh, $name ) = @_; ## no critic (Variables::ProhibitReusedNames) | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Drop database | ||||||||||||||||||||||||||||
| 161 | 0 | my $_log = Log::Any->get_logger( category => 'Database::Temp' ); ## no critic (Variables::ProhibitReusedNames) | |||||||||||||||||||||||||||
| 162 | 0 | my $temp_db_name = $name; | |||||||||||||||||||||||||||
| 163 | 0 | my $temp_user_name = $name; | |||||||||||||||||||||||||||
| 164 | 0 | my $main = { | |||||||||||||||||||||||||||
| 165 | 'dsn' => _compile_dsn( 'postgres', undef, undef, 'Database--Temp--Driver--Pg' ), | ||||||||||||||||||||||||||||
| 166 | 'username' => undef, | ||||||||||||||||||||||||||||
| 167 | 'password' => undef, | ||||||||||||||||||||||||||||
| 168 | 'attr' => { | ||||||||||||||||||||||||||||
| 169 | 'AutoCommit' => 1, | ||||||||||||||||||||||||||||
| 170 | 'RaiseError' => 1, | ||||||||||||||||||||||||||||
| 171 | 'PrintError' => 1, | ||||||||||||||||||||||||||||
| 172 | 'pg_server_prepare' => 0, | ||||||||||||||||||||||||||||
| 173 | 'TraceLevel' => 0, | ||||||||||||||||||||||||||||
| 174 | }, | ||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||
| 176 | 0 | my $main_dbh = DBI->connect( $main->{'dsn'}, $main->{'username'}, $main->{'password'}, $main->{'attr'}, ); | |||||||||||||||||||||||||||
| 177 | 0 | my $rc = $main_dbh->do("ALTER DATABASE \"$temp_db_name\" WITH ALLOW_CONNECTIONS false"); | |||||||||||||||||||||||||||
| 178 | 0 | $rc = $main_dbh->do("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='$temp_db_name'"); | |||||||||||||||||||||||||||
| 179 | 0 | $_log->infof( 'Dropping database \'%s\'', $temp_db_name ); | |||||||||||||||||||||||||||
| 180 | 0 | $rc = $main_dbh->do("DROP DATABASE \"$temp_db_name\""); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if ($rc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | $_log->debugf( 'Dropped temp database \'%s\'', $temp_db_name ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||
| 186 | 0 | $_log->warningf( 'Probably not managed to drop temp database \'%s\'', $temp_db_name ); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 0 | $rc = $main_dbh->do("DROP ROLE \"$temp_user_name\""); | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | if ($rc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | $_log->debugf( 'Dropped temp user \'%s\'', $temp_user_name ); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | else { | ||||||||||||||||||||||||||||
| 193 | 0 | $_log->warningf( 'Probably not managed to drop temp user \'%s\'', $temp_user_name ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 0 | $main_dbh->disconnect(); | |||||||||||||||||||||||||||
| 196 | 0 | }; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # Create database representing object. | ||||||||||||||||||||||||||||
| 199 | return Database::Temp::DB->new( | ||||||||||||||||||||||||||||
| 200 | driver => __PACKAGE__ =~ m/^Database::Temp::Driver::(.*)$/msx, | ||||||||||||||||||||||||||||
| 201 | name => $params{'name'}, | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | cleanup => $params{'cleanup'} // 0, | ||||||||||||||||||||||||||
|
Condition0%0/2
$params{"cleanup"} // 0
MC/DC0%0/1
$params{"cleanup"} // 0
$params{"cleanup"}
Truth table0%0/2
A
A= $params{"cleanup"}
| |||||||||||||||||||||||||||||
| 203 | _cleanup => $_cleanup, | ||||||||||||||||||||||||||||
| 204 | _start => $_start, | ||||||||||||||||||||||||||||
| 205 | init => $init, | ||||||||||||||||||||||||||||
| 206 | deinit => $deinit, | ||||||||||||||||||||||||||||
| 207 | dsn => $dsn, | ||||||||||||||||||||||||||||
| 208 | username => $name, | ||||||||||||||||||||||||||||
| 209 | password => $name, | ||||||||||||||||||||||||||||
| 210 | attr => \%attrs, | ||||||||||||||||||||||||||||
| 211 | info => \%info, | ||||||||||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | 1; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | __END__ | ||||||||||||||||||||||||||||