File Coverage

blib/lib/Database/Migrator/Pg.pm
Criterion Covered Total %
statement 33 77 42.8
branch 0 18 0.0
condition n/a
subroutine 11 20 55.0
pod n/a
total 44 115 38.2


line stmt bran cond sub pod time code
1             package Database::Migrator::Pg;
2              
3 1     1   881 use strict;
  1         1  
  1         43  
4 1     1   6 use warnings;
  1         2  
  1         38  
5 1     1   741 use namespace::autoclean;
  1         31358  
  1         6  
6              
7             our $VERSION = '0.05';
8              
9 1     1   910 use Database::Migrator 0.07;
  1         34  
  1         41  
10 1     1   661 use Database::Migrator::Types qw( HashRef Str );
  1         993835  
  1         8  
11 1     1   7977 use File::Slurp qw( read_file );
  1         12120  
  1         96  
12 1     1   488 use Pg::CLI 0.11;
  1         29  
  1         25  
13 1     1   482 use Pg::CLI::createdb;
  1         882975  
  1         37  
14 1     1   629 use Pg::CLI::dropdb;
  1         157531  
  1         52  
15 1     1   712 use Pg::CLI::psql;
  1         135256  
  1         52  
16              
17 1     1   10 use Moose;
  1         3  
  1         8  
18              
19             with 'Database::Migrator::Core';
20              
21             for my $create_flag (
22             qw( encoding locale lc_collate lc_ctype owner tablespace template )) {
23              
24             has $create_flag => (
25             is => 'ro',
26             isa => Str,
27             predicate => '_has_' . $create_flag,
28             );
29             }
30              
31             has _psql => (
32             is => 'ro',
33             isa => 'Pg::CLI::psql',
34             init_arg => undef,
35             lazy => 1,
36             builder => '_build_psql',
37             );
38              
39             has _cli_constructor_args => (
40             is => 'ro',
41             isa => HashRef,
42             init_arg => undef,
43             lazy => 1,
44             builder => '_build_cli_constructor_args',
45             );
46              
47             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
48             sub _create_database {
49 0     0     my $self = shift;
50              
51 0           my $database = $self->database();
52              
53 0           $self->logger()->info("Creating the $database database");
54              
55 0           my @opts;
56 0 0         push @opts, '--encoding', $self->encoding()
57             if $self->_has_encoding();
58 0 0         push @opts, '--locale', $self->locale()
59             if $self->_has_locale();
60 0 0         push @opts, '--lc-collate', $self->lc_collate()
61             if $self->_has_lc_collate();
62 0 0         push @opts, '--lc-ctype', $self->lc_ctype()
63             if $self->_has_lc_ctype();
64 0 0         push @opts, '--owner', $self->owner()
65             if $self->_has_owner();
66 0 0         push @opts, '--tablespace', $self->tablespace()
67             if $self->_has_tablespace();
68 0 0         push @opts, '--template', $self->template()
69             if $self->_has_template();
70              
71 0           $self->_run_cli_or_die(
72             'createdb',
73             'run',
74             database => $self->database(),
75             options => \@opts,
76             );
77              
78 0           return;
79             }
80              
81             sub _run_ddl {
82 0     0     my $self = shift;
83 0           my $file = shift;
84              
85 0           $self->_run_cli_or_die(
86             'psql',
87             'execute_file',
88             database => $self->database(),
89             file => $file,
90             );
91              
92 0           return;
93             }
94              
95             sub _drop_database {
96 0     0     my $self = shift;
97              
98 0           my $database = $self->database();
99              
100 0           $self->logger()->info("Dropping the $database database");
101              
102 0           $self->_run_cli_or_die(
103             'dropdb',
104             'run',
105             database => $self->database(),
106             options => ['--if-exists'],
107             );
108              
109 0           return;
110             }
111              
112             sub _run_cli_or_die {
113 0     0     my $self = shift;
114 0           my $cli_obj = shift;
115 0           my $method = shift;
116 0           my %args = @_;
117              
118 0           my $cli_obj_method = q{_} . $cli_obj;
119              
120 0           my $stdout;
121             my $stderr;
122 0           $self->$cli_obj_method()->$method(
123             %args,
124             stdout => \$stdout,
125             stderr => \$stderr,
126             );
127              
128 0 0         die $stderr if $stderr;
129              
130 0           return $stdout;
131             }
132              
133             sub _createdb {
134 0     0     my $self = shift;
135              
136 0           return Pg::CLI::createdb->new( $self->_cli_constructor_args() );
137             }
138              
139             sub _dropdb {
140 0     0     my $self = shift;
141              
142 0           return Pg::CLI::dropdb->new( $self->_cli_constructor_args() );
143             }
144             ## use critic
145              
146             sub _build_psql {
147 0     0     my $self = shift;
148              
149             return Pg::CLI::psql->new(
150 0           %{ $self->_cli_constructor_args() },
  0            
151             quiet => 1,
152             );
153             }
154              
155             sub _build_cli_constructor_args {
156 0     0     my $self = shift;
157              
158 0           my %args;
159 0           for my $m (qw( username password host port )) {
160 0 0         $args{$m} = $self->$m()
161             if defined $self->$m();
162             }
163              
164 0           return \%args;
165             }
166              
167             around _build_dbh => sub {
168             my $orig = shift;
169             my $self = shift;
170              
171             my $dbh = $self->$orig(@_);
172              
173             $dbh->do('SET CLIENT_MIN_MESSAGES = ERROR');
174              
175             return $dbh;
176             };
177              
178             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
179 0     0     sub _driver_name {'Pg'}
180             ## use critic
181              
182             __PACKAGE__->meta()->make_immutable();
183              
184             1;
185              
186             #ABSTRACT: Database::Migrator implementation for Postgres
187              
188             __END__
189              
190             =pod
191              
192             =head1 NAME
193              
194             Database::Migrator::Pg - Database::Migrator implementation for Postgres
195              
196             =head1 VERSION
197              
198             version 0.05
199              
200             =head1 SYNOPSIS
201              
202             package MyApp::Migrator;
203              
204             use Moose;
205              
206             extends 'Database::Migrator::Pg';
207              
208             has '+database' => (
209             required => 0,
210             default => 'MyApp',
211             );
212              
213             =head1 DESCRIPTION
214              
215             This module provides a L<Database::Migrator> implementation for Postgres. See
216             L<Database::Migrator> and L<Database::Migrator::Core> for more documentation.
217              
218             =head1 ATTRIBUTES
219              
220             This class adds several attributes in addition to those implemented by
221             L<Database::Migrator::Core>. All of these attributes are optional.
222              
223             =over 4
224              
225             =item * encoding
226              
227             The encoding of the database. This is only used when creating a new
228             database.
229              
230             =item * locale
231              
232             The locale of the database. This is only used when creating a new
233             database.
234              
235             =item * lc_collate
236              
237             The LC_COLLATE setting for the database. This is only used when creating a new
238             database.
239              
240             =item * lc_ctype
241              
242             The LC_CTYPE setting for the database. This is only used when creating a new
243             database.
244              
245             =item * owner
246              
247             The owner of the database. This is only used when creating a new
248             database.
249              
250             =item * tablespace
251              
252             The tablespace for the database. This is only used when creating a new
253             database.
254              
255             =item * template
256              
257             The template for the database. This is only used when creating a new database.
258              
259             =back
260              
261             =head1 SUPPORT
262              
263             Please submit bugs to the CPAN RT system at
264             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Database-Migrator-Pg or via email at
265             bug-database-migrator-pg@rt.cpan.org.
266              
267             =head1 AUTHOR
268              
269             Dave Rolsky <autarch@urth.org>
270              
271             =head1 COPYRIGHT AND LICENSE
272              
273             This software is Copyright (c) 2015 by MaxMind, Inc..
274              
275             This is free software, licensed under:
276              
277             The Artistic License 2.0 (GPL Compatible)
278              
279             =cut