File Coverage

blib/lib/Database/Migrator/Pg.pm
Criterion Covered Total %
statement 30 74 40.5
branch 0 18 0.0
condition n/a
subroutine 10 19 52.6
pod n/a
total 40 111 36.0


line stmt bran cond sub pod time code
1             package Database::Migrator::Pg;
2              
3 1     1   847 use strict;
  1         1  
  1         28  
4 1     1   4 use warnings;
  1         2  
  1         25  
5 1     1   377 use namespace::autoclean;
  1         13665  
  1         5  
6              
7             our $VERSION = '0.06';
8              
9 1     1   444 use Database::Migrator 0.07;
  1         86  
  1         23  
10 1     1   350 use Database::Migrator::Types qw( HashRef Str );
  1         599032  
  1         5  
11 1     1   3797 use Pg::CLI 0.11;
  1         45  
  1         25  
12 1     1   360 use Pg::CLI::createdb;
  1         587701  
  1         49  
13 1     1   597 use Pg::CLI::dropdb;
  1         101349  
  1         40  
14 1     1   595 use Pg::CLI::psql;
  1         111027  
  1         42  
15              
16 1     1   8 use Moose;
  1         1  
  1         6  
17              
18             with 'Database::Migrator::Core';
19              
20             for my $create_flag (
21             qw( encoding locale lc_collate lc_ctype owner tablespace template )) {
22              
23             has $create_flag => (
24             is => 'ro',
25             isa => Str,
26             predicate => '_has_' . $create_flag,
27             );
28             }
29              
30             has _psql => (
31             is => 'ro',
32             isa => 'Pg::CLI::psql',
33             init_arg => undef,
34             lazy => 1,
35             builder => '_build_psql',
36             );
37              
38             has _cli_constructor_args => (
39             is => 'ro',
40             isa => HashRef,
41             init_arg => undef,
42             lazy => 1,
43             builder => '_build_cli_constructor_args',
44             );
45              
46             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
47             sub _create_database {
48 0     0     my $self = shift;
49              
50 0           my $database = $self->database;
51              
52 0           $self->logger->info("Creating the $database database");
53              
54 0           my @opts;
55 0 0         push @opts, '--encoding', $self->encoding
56             if $self->_has_encoding;
57 0 0         push @opts, '--locale', $self->locale
58             if $self->_has_locale;
59 0 0         push @opts, '--lc-collate', $self->lc_collate
60             if $self->_has_lc_collate;
61 0 0         push @opts, '--lc-ctype', $self->lc_ctype
62             if $self->_has_lc_ctype;
63 0 0         push @opts, '--owner', $self->owner
64             if $self->_has_owner;
65 0 0         push @opts, '--tablespace', $self->tablespace
66             if $self->_has_tablespace;
67 0 0         push @opts, '--template', $self->template
68             if $self->_has_template;
69              
70 0           $self->_run_cli_or_die(
71             'createdb',
72             'run',
73             database => $self->database,
74             options => \@opts,
75             );
76              
77 0           return;
78             }
79              
80             sub _run_ddl {
81 0     0     my $self = shift;
82 0           my $file = shift;
83              
84 0           $self->_run_cli_or_die(
85             'psql',
86             'execute_file',
87             database => $self->database,
88             file => $file,
89             options => [ -v => 'ON_ERROR_STOP=1' ],
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             =encoding UTF-8
193              
194             =head1 NAME
195              
196             Database::Migrator::Pg - Database::Migrator implementation for Postgres
197              
198             =head1 VERSION
199              
200             version 0.06
201              
202             =head1 SYNOPSIS
203              
204             package MyApp::Migrator;
205              
206             use Moose;
207              
208             extends 'Database::Migrator::Pg';
209              
210             has '+database' => (
211             required => 0,
212             default => 'MyApp',
213             );
214              
215             =head1 DESCRIPTION
216              
217             This module provides a L<Database::Migrator> implementation for Postgres. See
218             L<Database::Migrator> and L<Database::Migrator::Core> for more documentation.
219              
220             =head1 ATTRIBUTES
221              
222             This class adds several attributes in addition to those implemented by
223             L<Database::Migrator::Core>. All of these attributes are optional.
224              
225             =over 4
226              
227             =item * encoding
228              
229             The encoding of the database. This is only used when creating a new
230             database.
231              
232             =item * locale
233              
234             The locale of the database. This is only used when creating a new
235             database.
236              
237             =item * lc_collate
238              
239             The LC_COLLATE setting for the database. This is only used when creating a new
240             database.
241              
242             =item * lc_ctype
243              
244             The LC_CTYPE setting for the database. This is only used when creating a new
245             database.
246              
247             =item * owner
248              
249             The owner of the database. This is only used when creating a new
250             database.
251              
252             =item * tablespace
253              
254             The tablespace for the database. This is only used when creating a new
255             database.
256              
257             =item * template
258              
259             The template for the database. This is only used when creating a new database.
260              
261             =back
262              
263             =head1 SUPPORT
264              
265             Please submit bugs to the CPAN RT system at
266             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Database-Migrator-Pg or via email at
267             bug-database-migrator-pg@rt.cpan.org.
268              
269             Bugs may be submitted through L<https://github.com/maxmind/Database-Migrator-Pg/issues>.
270              
271             =head1 AUTHOR
272              
273             Dave Rolsky <autarch@urth.org>
274              
275             =head1 CONTRIBUTORS
276              
277             =for stopwords Gregory Oschwald Kevin Phair
278              
279             =over 4
280              
281             =item *
282              
283             Gregory Oschwald <goschwald@maxmind.com>
284              
285             =item *
286              
287             Kevin Phair <phair.kevin@gmail.com>
288              
289             =back
290              
291             =head1 COPYRIGHT AND LICENSE
292              
293             This software is Copyright (c) 2013 - 2017 by MaxMind, Inc.
294              
295             This is free software, licensed under:
296              
297             The Artistic License 2.0 (GPL Compatible)
298              
299             =cut