File Coverage

blib/lib/Database/Migrator/Pg.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package Database::Migrator::Pg;
2             {
3             $Database::Migrator::Pg::VERSION = '0.04';
4             }
5             BEGIN {
6 1     1   977 $Database::Migrator::Pg::AUTHORITY = 'cpan:DROLSKY';
7             }
8              
9 1     1   8 use strict;
  1         2  
  1         32  
10 1     1   5 use warnings;
  1         1  
  1         48  
11 1     1   367693 use namespace::autoclean;
  1         26278  
  1         9  
12              
13 1     1   578 use Database::Migrator 0.07;
  0            
  0            
14             use Database::Migrator::Types qw( HashRef Str );
15             use File::Slurp qw( read_file );
16             use Pg::CLI 0.11;
17             use Pg::CLI::createdb;
18             use Pg::CLI::dropdb;
19             use Pg::CLI::psql;
20              
21             use Moose;
22              
23             with 'Database::Migrator::Core';
24              
25             has encoding => (
26             is => 'ro',
27             isa => Str,
28             predicate => '_has_encoding',
29             );
30              
31             has owner => (
32             is => 'ro',
33             isa => Str,
34             predicate => '_has_owner',
35             );
36              
37             has _psql => (
38             is => 'ro',
39             isa => 'Pg::CLI::psql',
40             init_arg => undef,
41             lazy => 1,
42             builder => '_build_psql',
43             );
44              
45             has _cli_constructor_args => (
46             is => 'ro',
47             isa => HashRef,
48             init_arg => undef,
49             lazy => 1,
50             builder => '_build_cli_constructor_args',
51             );
52              
53             sub _create_database {
54             my $self = shift;
55              
56             my $database = $self->database();
57              
58             $self->logger()->info("Creating the $database database");
59              
60             my @opts;
61             push @opts, '-E', $self->encoding()
62             if $self->_has_encoding();
63             push @opts, '-O', $self->owner()
64             if $self->_has_owner();
65              
66             $self->_run_cli_or_die(
67             'createdb',
68             'run',
69             database => $self->database(),
70             options => \@opts,
71             );
72              
73             return;
74             }
75              
76             sub _run_ddl {
77             my $self = shift;
78             my $file = shift;
79              
80             $self->_run_cli_or_die(
81             'psql',
82             'execute_file',
83             database => $self->database(),
84             file => $file,
85             );
86              
87             return;
88             }
89              
90             sub _drop_database {
91             my $self = shift;
92              
93             my $database = $self->database();
94              
95             $self->logger()->info("Dropping the $database database");
96              
97             $self->_run_cli_or_die(
98             'dropdb',
99             'run',
100             database => $self->database(),
101             options => ['--if-exists'],
102             );
103              
104             return;
105             }
106              
107             sub _run_cli_or_die {
108             my $self = shift;
109             my $cli_obj = shift;
110             my $method = shift;
111             my %args = @_;
112              
113             my $cli_obj_method = q{_} . $cli_obj;
114              
115             my $stdout;
116             my $stderr;
117             $self->$cli_obj_method()->$method(
118             %args,
119             stdout => \$stdout,
120             stderr => \$stderr,
121             );
122              
123             die $stderr if $stderr;
124              
125             return $stdout;
126             }
127              
128             sub _createdb {
129             my $self = shift;
130              
131             return Pg::CLI::createdb->new( $self->_cli_constructor_args() );
132             }
133              
134             sub _dropdb {
135             my $self = shift;
136              
137             return Pg::CLI::dropdb->new( $self->_cli_constructor_args() );
138             }
139              
140             sub _build_psql {
141             my $self = shift;
142              
143             return Pg::CLI::psql->new(
144             %{ $self->_cli_constructor_args() },
145             quiet => 1,
146             );
147             }
148              
149             sub _build_cli_constructor_args {
150             my $self = shift;
151              
152             my %args;
153             for my $m (qw( username password host port )) {
154             $args{$m} = $self->$m()
155             if defined $self->$m();
156             }
157              
158             return \%args;
159             }
160              
161             around _build_dbh => sub {
162             my $orig = shift;
163             my $self = shift;
164              
165             my $dbh = $self->$orig(@_);
166              
167             $dbh->do('SET CLIENT_MIN_MESSAGES = ERROR');
168              
169             return $dbh;
170             };
171              
172             sub _driver_name { 'Pg' }
173              
174             __PACKAGE__->meta()->make_immutable();
175              
176             1;
177              
178             #ABSTRACT: Database::Migrator implementation for Postgres
179              
180             __END__
181              
182             =pod
183              
184             =head1 NAME
185              
186             Database::Migrator::Pg - Database::Migrator implementation for Postgres
187              
188             =head1 VERSION
189              
190             version 0.04
191              
192             =head1 SYNOPSIS
193              
194             package MyApp::Migrator;
195              
196             use Moose;
197              
198             extends 'Database::Migrator::Pg';
199              
200             has '+database' => (
201             required => 0,
202             default => 'MyApp',
203             );
204              
205             =head1 DESCRIPTION
206              
207             This module provides a L<Database::Migrator> implementation for Postgres. See
208             L<Database::Migrator> and L<Database::Migrator::Core> for more documentation.
209              
210             =head1 ATTRIBUTES
211              
212             This class adds several attributes in addition to those implemented by
213             L<Database::Migrator::Core>:
214              
215             =over 4
216              
217             =item * encoding
218              
219             The encoding of the database. This is only used when creating a new
220             database. This is optional.
221              
222             =item * owner
223              
224             The owner of the database. This is only used when creating a new
225             database. This is optional.
226              
227             =back
228              
229             =head1 AUTHOR
230              
231             Dave Rolsky <autarch@urth.org>
232              
233             =head1 COPYRIGHT AND LICENSE
234              
235             This software is Copyright (c) 2013 by MaxMind, Inc..
236              
237             This is free software, licensed under:
238              
239             The Artistic License 2.0 (GPL Compatible)
240              
241             =cut