File Coverage

blib/lib/DBIx/Changeset/Loader/Pg.pm
Criterion Covered Total %
statement 25 38 65.7
branch 4 18 22.2
condition 0 3 0.0
subroutine 10 10 100.0
pod 4 4 100.0
total 43 73 58.9


line stmt bran cond sub pod time code
1             package DBIx::Changeset::Loader::Pg;
2              
3 2     2   6422 use warnings;
  2         5  
  2         68  
4 2     2   12 use strict;
  2         4  
  2         59  
5 2     2   11 use File::Spec;
  2         4  
  2         51  
6              
7 2     2   34 use base qw/DBIx::Changeset::Loader/;
  2         10  
  2         257  
8              
9 2     2   12 use vars qw{$VERSION};
  2         5  
  2         123  
10             BEGIN {
11 2     2   804 $VERSION = '1.11';
12             }
13              
14             =head1 NAME
15              
16             DBIx::Changeset::Loader::Pg - factory object for loading changesets into a PostgreSQL database
17              
18             =head1 SYNOPSIS
19              
20             Perhaps a little code snippet.
21              
22             use DBIx::Changeset::Loader;
23              
24             my $foo = DBIx::Changeset::Loader->new('Pg', $opts);
25             ...
26             $foo->apply_changeset($record);
27              
28             =head1 METHODS
29              
30             =head2 start_transaction
31             This is the start_transaction interface to implement in your own class
32             =cut
33 1     1 1 47 sub start_transaction {
34             }
35              
36             =head2 commit_transaction
37             This is the commit_transaction interface to implement in your own class
38             =cut
39 1     1 1 423 sub commit_transaction {
40             }
41              
42             =head2 rollback_transaction
43             This is the rollback_transaction interface to implement in your own class
44             =cut
45 1     1 1 637 sub rollback_transaction {
46             }
47              
48             =head2 apply_changeset
49             This is the apply_changeset interface to implement in your own class
50             =cut
51             sub apply_changeset {
52 4     4 1 3493 my ($self, $record) = @_;
53              
54 4 100       14 unless ( defined $record ) { DBIx::Changeset::Exception::LoaderException->throw(error => 'Missing a DBIx::Changeset::Record'); }
  2         25  
55 2 50       17 unless ( $record->valid() ) { DBIx::Changeset::Exception::LoaderException->throw(error => 'Passed an Invalid DBIx::Changeset::Record'); }
  0         0  
56 2 50       27 unless ( defined $self->db_name ) { DBIx::Changeset::Exception::LoaderException->throw(error => 'need a db_name parameter'); }
  2         52  
57            
58 0           my $perms = "";
59 0 0         $perms = "-U".$self->db_user() if ($self->db_user);
60 0 0         $perms .= " -h".$self->db_host() if ($self->db_host);
61              
62             ## set the connection password if supplied in PGPASSWORD
63 0 0         $ENV{'PGPASSWORD'} = $self->db_pass() if ($self->db_pass);
64              
65 0           my $DB = "";
66 0 0         $DB = $self->db_name if ($self->db_name);
67              
68 0           my $sql = File::Spec->catfile($record->changeset_location, $record->uri);
69            
70 0           my $result = qx/psql $perms $DB < $sql 2>&1/;
71            
72 0 0         delete $ENV{'PGPASSWORD'} if ($self->db_pass);
73              
74 0 0 0       if ($? or ($result =~ m#ERROR#xmgs)) {
75 0           DBIx::Changeset::Exception::LoaderException->throw(error => $result);
76             }
77              
78 0           return;
79             }
80              
81             =head1 COPYRIGHT & LICENSE
82              
83             Copyright 2004-2008 Grox Pty Ltd.
84              
85             This program is free software; you can redistribute it and/or modify it
86             under the same terms as Perl itself.
87              
88             The full text of the license can be found in the LICENSE file included with this module.
89              
90             =cut
91              
92             1; # End of DBIx::Changeset