| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::PostgreSqlProvider; | ||
| 5 | $Footprintless::Plugin::Database::PostgreSqlProvider::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: A PostgreSql provider implementation | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::AbstractProvider | ||
| 8 | |||
| 9 | use parent qw(Footprintless::Plugin::Database::AbstractProvider); | ||
| 10 | |||
| 11 | use overload q{""} => 'to_string', fallback => 1; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use File::Temp; | ||
| 15 | use Footprintless::Command qw( | ||
| 16 | command | ||
| 17 | pipe_command | ||
| 18 | sed_command | ||
| 19 | ); | ||
| 20 | use Footprintless::Mixins qw( | ||
| 21 | _run_or_die | ||
| 22 | ); | ||
| 23 | use Log::Any; | ||
| 24 | |||
| 25 | my $logger = Log::Any->get_logger(); | ||
| 26 | |||
| 27 | sub backup { | ||
| 28 | my ( $self, $to, %options ) = @_; | ||
| 29 | |||
| 30 | croak("not connected backup") | ||
| 31 | unless ( $self->{connection} | ||
| 32 | || ( !$options{live} && defined( $self->{backup} ) ) ); | ||
| 33 | |||
| 34 | $logger->info('backing up database'); | ||
| 35 | if ( $options{ignore_all_views} ) { | ||
| 36 | my @views = (); | ||
| 37 | $self->query( | ||
| 38 | { sql => 'select table_name ' | ||
| 39 | . 'from information_schema.tables ' | ||
| 40 | . 'where table_type like \'VIEW\' and table_schema = ?', | ||
| 41 | parameters => [ $self->{schema} ] | ||
| 42 | }, | ||
| 43 | sub { | ||
| 44 | push( @views, $_[0] ); | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | |||
| 48 | if (@views) { | ||
| 49 | if ( !$options{ignore_tables} ) { | ||
| 50 | $options{ignore_tables} = \@views; | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | push( @{ $options{ignore_tables} }, @views ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | my $command = $self->_dump_command(%options); | ||
| 59 | |||
| 60 | if ( eval { $to->isa('Footprintless::Plugin::Database::PostgreSqlProvider') } ) { | ||
| 61 | $to->restore( | ||
| 62 | $self, | ||
| 63 | 'clean' => $options{clean}, | ||
| 64 | 'backup' => { | ||
| 65 | command => $command, | ||
| 66 | options => \%options | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | elsif ( ref($to) eq 'CODE' ) { | ||
| 71 | $logger->debug('sending to a callback'); | ||
| 72 | $self->_run_or_die( $command, { out_callback => $to, err_handle => \*STDERR } ); | ||
| 73 | } | ||
| 74 | elsif ( ref($to) eq 'GLOB' ) { | ||
| 75 | $logger->debug('writing to GLOB'); | ||
| 76 | $self->_run_or_die( $command, { out_handle => $to, err_handle => \*STDERR } ); | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | $logger->debug( 'writing to file ', $to ); | ||
| 80 | open( my $filehandle, '>', $to ); | ||
| 81 | $self->_run_or_die( $command, { out_handle => $filehandle, err_handle => \*STDERR } ); | ||
| 82 | close($filehandle); | ||
| 83 | } | ||
| 84 | $logger->info('finished backing up database'); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _client_command { | ||
| 88 | my ( $self, $command, $database, @additional_options ) = @_; | ||
| 89 | |||
| 90 | my $cnf = $self->_cnf(); | ||
| 91 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 92 | |||
| 93 | $ENV{PGPASSFILE} = $cnf; | ||
| 94 | if ( $self->{schema} ) { | ||
| 95 | $ENV{PGOPTIONS} = "-c search_path=$self->{schema}"; | ||
| 96 | } | ||
| 97 | return join( ' ', | ||
| 98 | $command, "-h $hostname", "-p $port", "-U $self->{username}", | ||
| 99 | @additional_options, $database ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub client { | ||
| 103 | my ( $self, %options ) = @_; | ||
| 104 | |||
| 105 | my $in_file; | ||
| 106 | eval { | ||
| 107 | my $in_handle = delete( $options{in_handle} ); | ||
| 108 | if ( $options{in_file} ) { | ||
| 109 | open( $in_file, '<', delete( $options{in_file} ) ) | ||
| 110 | || croak("invalid in_file: $!"); | ||
| 111 | } | ||
| 112 | if ( $options{in_string} ) { | ||
| 113 | my $string = delete( $options{in_string} ); | ||
| 114 | open( $in_file, '<', \$string ) | ||
| 115 | || croak("invalid in_string: $!"); | ||
| 116 | } | ||
| 117 | $self->_connect_tunnel(); | ||
| 118 | |||
| 119 | my $command = | ||
| 120 | $self->_client_command( 'psql', $self->{database}, @{ $options{client_options} } ); | ||
| 121 | $self->_run_or_die( | ||
| 122 | $command, | ||
| 123 | { in_handle => $in_file || $in_handle || \*STDIN, | ||
| 124 | out_handle => \*STDOUT, | ||
| 125 | err_handle => \*STDERR | ||
| 126 | } | ||
| 127 | ); | ||
| 128 | }; | ||
| 129 | my $error = $@; | ||
| 130 | $self->disconnect(); | ||
| 131 | if ($in_file) { | ||
| 132 | close($in_file); | ||
| 133 | } | ||
| 134 | |||
| 135 | croak($error) if ($error); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _cnf { | ||
| 139 | my ($self) = @_; | ||
| 140 | |||
| 141 | unless ( $self->{cnf} ) { | ||
| 142 | File::Temp->safe_level(File::Temp::HIGH); | ||
| 143 | my $cnf = File::Temp->new(); | ||
| 144 | |||
| 145 | if ( !chmod( 0600, $cnf ) ) { | ||
| 146 | croak("unable to create secure temp file"); | ||
| 147 | } | ||
| 148 | print( $cnf "*:*:*:*:$self->{password}\n" ); | ||
| 149 | close($cnf); | ||
| 150 | |||
| 151 | $self->{cnf} = $cnf; | ||
| 152 | } | ||
| 153 | |||
| 154 | return $self->{cnf}; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _connection_string { | ||
| 158 | my ($self) = @_; | ||
| 159 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 160 | |||
| 161 | my @pg_options = (); | ||
| 162 | if ( $self->{schema} ) { | ||
| 163 | push( @pg_options, "-c search_path=$self->{schema}" ); | ||
| 164 | } | ||
| 165 | |||
| 166 | return join( '', | ||
| 167 | 'DBI:Pg:', 'dbname=', $self->{database}, ';', 'host=', $hostname, ';', 'port=', $port, | ||
| 168 | ( @pg_options ? ( ";options=\'", join( ' ', @pg_options ), "\'" ) : () ) ); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _dump_command { | ||
| 172 | my ( $self, %options ) = @_; | ||
| 173 | |||
| 174 | my $dump_command; | ||
| 175 | if ( $options{live} || !defined( $self->{backup} ) ) { | ||
| 176 | my $cnf = $self->_cnf(); | ||
| 177 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 178 | |||
| 179 | my $size = $self->query_for_scalar( | ||
| 180 | { sql => "select pg_database_size(?)", | ||
| 181 | parameters => [ $self->{database} ] | ||
| 182 | } | ||
| 183 | ); | ||
| 184 | |||
| 185 | $dump_command = pipe_command( | ||
| 186 | $self->_client_command( 'pg_dump', $self->{database}, '--create', '--clean' ), | ||
| 187 | "pv -f " . ( $size ? "-s $size" : "-b" ) ); | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | die("dump from backup not yet implemented"); | ||
| 191 | } | ||
| 192 | |||
| 193 | if ( $options{pipeline} ) { | ||
| 194 | $dump_command = pipe_command( $dump_command, @{ $options{pipeline} } ); | ||
| 195 | } | ||
| 196 | |||
| 197 | return $dump_command; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub _init { | ||
| 201 | my ( $self, %options ) = @_; | ||
| 202 | $self->Footprintless::Plugin::Database::AbstractProvider::_init(%options); | ||
| 203 | |||
| 204 | $self->{port} = 5432 unless ( $self->{port} ); | ||
| 205 | $self->{schema} = 'public' unless ( $self->{schema} ); | ||
| 206 | |||
| 207 | return $self; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub restore { | ||
| 211 | my ( $self, $from, %options ) = @_; | ||
| 212 | |||
| 213 | croak('not connected restore') unless ( $self->{connection} ); | ||
| 214 | |||
| 215 | if ( $options{clean} ) { | ||
| 216 | my @tables = (); | ||
| 217 | my @views = (); | ||
| 218 | $self->query( | ||
| 219 | { sql => 'select table_name, table_type ' | ||
| 220 | . 'from information_schema.tables ' | ||
| 221 | . 'where table_schema = ?', | ||
| 222 | parameters => [ $self->{schema} ] | ||
| 223 | }, | ||
| 224 | sub { | ||
| 225 | my ( $table_name, $table_type ) = @_; | ||
| 226 | if ( uc($table_type) eq 'VIEW' ) { | ||
| 227 | push( @views, $table_name ); | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | push( @tables, $table_name ); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | ); | ||
| 234 | |||
| 235 | if (@views) { | ||
| 236 | my $drop_views_sql = 'drop view `' . join( "`,`", @views ) . '`'; | ||
| 237 | $logger->info( $self->{hostname}, ': ', $drop_views_sql ); | ||
| 238 | $self->execute( { sql => $drop_views_sql } ); | ||
| 239 | } | ||
| 240 | |||
| 241 | if (@tables) { | ||
| 242 | my $drop_tables_sql = 'drop table `' . join( "`,`", @tables ) . '`'; | ||
| 243 | $logger->info( $self->{hostname}, ': ', $drop_tables_sql ); | ||
| 244 | $self->execute( { sql => $drop_tables_sql } ); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | eval { | ||
| 249 | $logger->debug("Forcibly disconnecting all clients"); | ||
| 250 | $self->execute( | ||
| 251 | { sql => "select pg_terminate_backend(pid) from pg_stat_activity where datname = ?", | ||
| 252 | parameters => [ $self->{database} ] | ||
| 253 | } | ||
| 254 | ); | ||
| 255 | }; | ||
| 256 | |||
| 257 | my $command = $self->_client_command( 'psql', $self->{database} ); | ||
| 258 | |||
| 259 | if ( eval { $from->isa('Footprintless::Plugin::Database::PostgreSqlProvider') } ) { | ||
| 260 | $logger->debug("Restoring from another postgres instance"); | ||
| 261 | $self->_run_or_die( pipe_command( $options{backup}{command}, $command ), | ||
| 262 | { err_handle => \*STDERR } ); | ||
| 263 | if ( $options{backup}{options} | ||
| 264 | && $options{backup}{options}{post_restore} ) | ||
| 265 | { | ||
| 266 | $self->_run_or_die( | ||
| 267 | pipe_command( "cat \"$options{backup}{options}{post_restore}\"", $command ) ); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | elsif ( ref($from) eq 'HASH' ) { | ||
| 271 | $logger->debug("Restoring from HASH"); | ||
| 272 | $self->_run_or_die( pipe_command( $from->{command}, $command ), | ||
| 273 | { err_handle => \*STDERR } ); | ||
| 274 | if ( $from->{options} | ||
| 275 | && $from->{options}{post_restore} ) | ||
| 276 | { | ||
| 277 | $self->_run_or_die( | ||
| 278 | pipe_command( "cat \"$from->{options}{post_restore}\"", $command ) ); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | elsif ( ref($from) eq 'GLOB' ) { | ||
| 282 | $logger->debug('Restoring from GLOB'); | ||
| 283 | $self->_run_or_die( | ||
| 284 | pipe_command( "pv -b", $command ), | ||
| 285 | { in_handle => $from, | ||
| 286 | err_handle => \*STDERR | ||
| 287 | } | ||
| 288 | ); | ||
| 289 | if ( $options{post_restore} ) { | ||
| 290 | $self->_run_or_die( pipe_command( "cat \"$options{post_restore}\"", $command ) ); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | else { | ||
| 294 | $logger->debug( 'Restoring from ', $from ); | ||
| 295 | $self->_run_or_die( pipe_command( "pv $from", $command ), { err_handle => \*STDERR } ); | ||
| 296 | if ( $options{post_restore} ) { | ||
| 297 | $self->_run_or_die( pipe_command( "cat \"$options{post_restore}\"", $command ) ); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | $logger->info("finished restoring database"); | ||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | __END__ |