| 1 | package Database::Migrator::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 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.06'; | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Database::Migrator 0.07; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Database::Migrator::Types qw( HashRef Str ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Pg::CLI 0.11; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Pg::CLI::createdb; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Pg::CLI::dropdb; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use Pg::CLI::psql; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | with 'Database::Migrator::Core'; | |||||||||
| 19 | |||||||||||
| 20 | 1 | for my $create_flag ( | |||||||||
| 21 | qw( encoding locale lc_collate lc_ctype owner tablespace template )) { | ||||||||||
| 22 | |||||||||||
| 23 | 7 | has $create_flag => ( | |||||||||
| 24 | is => 'ro', | ||||||||||
| 25 | isa => Str, | ||||||||||
| 26 | predicate => '_has_' . $create_flag, | ||||||||||
| 27 | ); | ||||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 1 | has _psql => ( | |||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | isa => 'Pg::CLI::psql', | ||||||||||
| 33 | init_arg => undef, | ||||||||||
| 34 | lazy => 1, | ||||||||||
| 35 | builder => '_build_psql', | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 1 | 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 | 0 | ▶ | sub _create_database { | ||||||||
|
Subroutine
_create_database: not called CC=8 SCAR=42.8
| |||||||||||
| 48 | 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 | ▶ | push @opts, '--encoding', $self->encoding | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | if $self->_has_encoding; | ||||||||||
| 57 | 0 | ▶ | push @opts, '--locale', $self->locale | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | if $self->_has_locale; | ||||||||||
| 59 | 0 | ▶ | push @opts, '--lc-collate', $self->lc_collate | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | if $self->_has_lc_collate; | ||||||||||
| 61 | 0 | ▶ | push @opts, '--lc-ctype', $self->lc_ctype | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | if $self->_has_lc_ctype; | ||||||||||
| 63 | 0 | ▶ | push @opts, '--owner', $self->owner | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | if $self->_has_owner; | ||||||||||
| 65 | 0 | ▶ | push @opts, '--tablespace', $self->tablespace | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | if $self->_has_tablespace; | ||||||||||
| 67 | 0 | ▶ | push @opts, '--template', $self->template | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub _run_ddl { | ||||||||
|
Subroutine
_run_ddl: not called CC=1 SCAR=6.9
| |||||||||||
| 81 | 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 | 0 | ▶ | sub _drop_database { | ||||||||
|
Subroutine
_drop_database: not called CC=1 SCAR=6.9
| |||||||||||
| 96 | 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 | 0 | ▶ | sub _run_cli_or_die { | ||||||||
|
Subroutine
_run_cli_or_die: not called CC=2 SCAR=17.9
| |||||||||||
| 113 | 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 | ▶ | die $stderr if $stderr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | return $stdout; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub _createdb { | ||||||||
|
Subroutine
_createdb: not called CC=1 SCAR=6.9
| |||||||||||
| 134 | 0 | my $self = shift; | |||||||||
| 135 | |||||||||||
| 136 | 0 | return Pg::CLI::createdb->new( $self->_cli_constructor_args ); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 0 | ▶ | sub _dropdb { | ||||||||
|
Subroutine
_dropdb: not called CC=1 SCAR=6.9
| |||||||||||
| 140 | 0 | my $self = shift; | |||||||||
| 141 | |||||||||||
| 142 | 0 | return Pg::CLI::dropdb->new( $self->_cli_constructor_args ); | |||||||||
| 143 | } | ||||||||||
| 144 | ## use critic | ||||||||||
| 145 | |||||||||||
| 146 | 0 | ▶ | sub _build_psql { | ||||||||
|
Subroutine
_build_psql: not called CC=1 SCAR=6.9
| |||||||||||
| 147 | 0 | my $self = shift; | |||||||||
| 148 | |||||||||||
| 149 | return Pg::CLI::psql->new( | ||||||||||
| 150 | 0 | %{ $self->_cli_constructor_args }, | |||||||||
| 151 | quiet => 1, | ||||||||||
| 152 | ); | ||||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 0 | ▶ | sub _build_cli_constructor_args { | ||||||||
|
Subroutine
_build_cli_constructor_args: not called CC=3 SCAR=24.8
| |||||||||||
| 156 | 0 | my $self = shift; | |||||||||
| 157 | |||||||||||
| 158 | 0 | my %args; | |||||||||
| 159 | 0 | for my $m (qw( username password host port )) { | |||||||||
| 160 | 0 | ▶ | $args{$m} = $self->$m | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | if defined $self->$m; | ||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | return \%args; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | around _build_dbh => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 168 | 0 | my $orig = shift; | |||||||||
| 169 | 0 | my $self = shift; | |||||||||
| 170 | |||||||||||
| 171 | 0 | my $dbh = $self->$orig(@_); | |||||||||
| 172 | |||||||||||
| 173 | 0 | $dbh->do('SET CLIENT_MIN_MESSAGES = ERROR'); | |||||||||
| 174 | |||||||||||
| 175 | 0 | return $dbh; | |||||||||
| 176 | 1 | }; | |||||||||
| 177 | |||||||||||
| 178 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||
| 179 | 0 | ▶ | sub _driver_name {'Pg'} | ||||||||
|
Subroutine
_driver_name: not called CC=1 SCAR=6.9
| |||||||||||
| 180 | ## use critic | ||||||||||
| 181 | |||||||||||
| 182 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 183 | |||||||||||
| 184 | 1 | 1; | |||||||||
| 185 | |||||||||||
| 186 | #ABSTRACT: Database::Migrator implementation for Postgres | ||||||||||
| 187 | |||||||||||
| 188 | __END__ | ||||||||||