line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::DB::Postgres; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19288
|
use 5.014; |
|
1
|
|
|
|
|
4
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
21
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
26
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Venus::Class; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Venus::Role::Optional'; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
2186
|
use DBI; |
|
1
|
|
|
|
|
17838
|
|
|
1
|
|
|
|
|
1092
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# ATTRIBUTES |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
attr 'dbh'; |
21
|
|
|
|
|
|
|
attr 'dsn'; |
22
|
|
|
|
|
|
|
attr 'hostname'; |
23
|
|
|
|
|
|
|
attr 'hostport'; |
24
|
|
|
|
|
|
|
attr 'initial'; |
25
|
|
|
|
|
|
|
attr 'uri'; |
26
|
|
|
|
|
|
|
attr 'username'; |
27
|
|
|
|
|
|
|
attr 'password'; |
28
|
|
|
|
|
|
|
attr 'database'; |
29
|
|
|
|
|
|
|
attr 'template'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# OPTIONS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub lazy_build_dbh { |
34
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
0
|
|
|
|
$data ||= DBI->connect($self->dsn, $self->username, $self->password, { |
37
|
|
|
|
|
|
|
RaiseError => 1, |
38
|
|
|
|
|
|
|
AutoCommit => 1 |
39
|
|
|
|
|
|
|
}); |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
return $data; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub lazy_build_dsn { |
45
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
return $self->dsngen($self->database); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub lazy_build_hostname { |
51
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
|
|
|
|
return $data ? $data : $ENV{TESTDB_HOSTNAME}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub lazy_build_hostport { |
57
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
return $data ? $data : $ENV{TESTDB_HOSTPORT}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub lazy_build_initial { |
63
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
0
|
|
|
|
return $data ? $data : $ENV{TESTDB_INITIAL} || 'postgres'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub lazy_build_uri { |
69
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $self->urigen($self->database); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub lazy_build_username { |
75
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
0
|
|
|
|
return $data ? $data : $ENV{TESTDB_USERNAME} || ''; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub lazy_build_password { |
81
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
0
|
|
|
|
return $data ? $data : $ENV{TESTDB_PASSWORD} || ''; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub lazy_build_database { |
87
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
return $data ? $data : join '_', 'testing_db', time, $$, sprintf "%04d", rand 999; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub lazy_build_template { |
93
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
return $data ? $data : $ENV{TESTDB_TEMPLATE}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# METHODS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub clone { |
101
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my $source = $self->template; |
104
|
0
|
|
|
|
|
|
my $initial = $self->initial; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my $dbh = DBI->connect($self->dsngen($initial), |
107
|
|
|
|
|
|
|
$self->username, |
108
|
|
|
|
|
|
|
$self->password, |
109
|
|
|
|
|
|
|
{ |
110
|
|
|
|
|
|
|
RaiseError => 1, |
111
|
|
|
|
|
|
|
AutoCommit => 1 |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
); |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare(qq(CREATE DATABASE "@{[$self->database]}" TEMPLATE "$source")); |
|
0
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
$sth->execute; |
118
|
0
|
|
|
|
|
|
$dbh->disconnect; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
$self->dbh; |
121
|
0
|
|
|
|
|
|
$self->uri; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $self; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub create { |
127
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
my $dbh = DBI->connect($self->dsngen($self->initial), |
130
|
|
|
|
|
|
|
$self->username, |
131
|
|
|
|
|
|
|
$self->password, |
132
|
|
|
|
|
|
|
{ |
133
|
|
|
|
|
|
|
RaiseError => 1, |
134
|
|
|
|
|
|
|
AutoCommit => 1 |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare(qq(CREATE DATABASE "@{[$self->database]}")); |
|
0
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$sth->execute; |
141
|
0
|
|
|
|
|
|
$dbh->disconnect; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
$self->dbh; |
144
|
0
|
|
|
|
|
|
$self->uri; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
return $self; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub destroy { |
150
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
151
|
|
|
|
|
|
|
|
152
|
0
|
0
|
|
|
|
|
$self->dbh->disconnect if $self->{dbh}; |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
my $dbh = DBI->connect($self->dsngen($self->initial), |
155
|
|
|
|
|
|
|
$self->username, |
156
|
|
|
|
|
|
|
$self->password, |
157
|
|
|
|
|
|
|
{ |
158
|
|
|
|
|
|
|
RaiseError => 1, |
159
|
|
|
|
|
|
|
AutoCommit => 1 |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
); |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare(qq(DROP DATABASE "@{[$self->database]}")); |
|
0
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
$sth->execute; |
166
|
0
|
|
|
|
|
|
$dbh->disconnect; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
return $self; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub dsngen { |
172
|
0
|
|
|
0
|
0
|
|
my ($self, $name) = @_; |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my $hostname = $self->hostname; |
175
|
0
|
|
|
|
|
|
my $hostport = $self->hostport; |
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
|
|
|
|
return join ';', "dbi:Pg:dbname=$name", join ';', |
|
|
0
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
($hostname ? ("host=@{[$hostname]}") : ()), |
179
|
0
|
|
|
|
|
|
($hostport ? ("port=@{[$hostport]}") : ()) |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub urigen { |
183
|
0
|
|
|
0
|
0
|
|
my ($self, $name) = @_; |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
my $username = $self->username; |
186
|
0
|
|
|
|
|
|
my $password = $self->password; |
187
|
0
|
|
|
|
|
|
my $hostname = $self->hostname; |
188
|
0
|
|
|
|
|
|
my $hostport = $self->hostport; |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
return join( |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
191
|
|
|
|
|
|
|
'/', 'postgresql', |
192
|
|
|
|
|
|
|
($username ? '' : ()), |
193
|
|
|
|
|
|
|
( |
194
|
|
|
|
|
|
|
$username |
195
|
|
|
|
|
|
|
? join('@', |
196
|
|
|
|
|
|
|
join(':', $username ? ($username, ($password ? $password : ())) : ()), |
197
|
|
|
|
|
|
|
$hostname |
198
|
|
|
|
|
|
|
? ($hostport ? (join(':', $hostname, $hostport)) : $hostname) |
199
|
|
|
|
|
|
|
: '') |
200
|
|
|
|
|
|
|
: () |
201
|
|
|
|
|
|
|
), |
202
|
|
|
|
|
|
|
$name |
203
|
|
|
|
|
|
|
) |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 NAME |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Test::DB::Postgres - Temporary Testing Databases for Postgres |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 ABSTRACT |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Temporary Postgres Database for Testing |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 VERSION |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
0.10 |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=cut |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head1 SYNOPSIS |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
package main; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
use Test::DB::Postgres; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
my $tdbo = Test::DB::Postgres->new; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# my $dbh = $tdbo->create->dbh; |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 DESCRIPTION |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
This package provides methods for generating and destroying Postgres databases |
243
|
|
|
|
|
|
|
for testing purposes. The attributes can be set using their respective |
244
|
|
|
|
|
|
|
environment variables: C, C, |
245
|
|
|
|
|
|
|
C, C, C, and |
246
|
|
|
|
|
|
|
C. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=cut |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
This package has the following attributes: |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=cut |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 dbh |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
dbh(Object) |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Object)> values, and is optional. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=cut |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=head2 dsn |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
dsn(Str) |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=cut |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head2 database |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
database(Str) |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=cut |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head2 hostname |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
hostname(Str) |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=cut |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head2 hostport |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
hostport(Str) |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=cut |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head2 uri |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
uri(Str) |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=cut |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head2 username |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
username(Str) |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=cut |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=head2 password |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
password(Str) |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=cut |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head1 METHODS |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
This package provides the following methods: |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=cut |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=head2 clone |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
clone(Str $source) : Object |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
The clone method creates a temporary database from a database template. |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=over 4 |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=item clone example 1 |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
# given: synopsis |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
$tdbo->clone('template0'); |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
# |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=back |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=cut |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
=head2 create |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
create() : Object |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
The create method creates a temporary database and returns the invocant. |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=over 4 |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=item create example 1 |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
# given: synopsis |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
$tdbo->create; |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
=back |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
=cut |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head2 destroy |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
destroy() : Object |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
The destroy method destroys (drops) the database and returns the invocant. |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
=over 4 |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
=item destroy example 1 |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
# given: synopsis |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
$tdbo->create; |
379
|
|
|
|
|
|
|
$tdbo->destroy; |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
# |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
=back |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
=cut |