File Coverage

blib/lib/DBIx/SimpleMigration.pm
Criterion Covered Total %
statement 46 50 92.0
branch 8 16 50.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 0 2 0.0
total 65 83 78.3


line stmt bran cond sub pod time code
1             package DBIx::SimpleMigration;
2              
3 2     2   13550 use 5.10.0;
  2         4  
4 2     2   6 use strict;
  2         2  
  2         30  
5 2     2   12 use warnings;
  2         2  
  2         38  
6              
7 2     2   11 use Carp;
  2         2  
  2         102  
8 2     2   6 use File::Basename;
  2         2  
  2         120  
9              
10 2     2   1235 use DBIx::SimpleMigration::Client;
  2         2  
  2         56  
11 2     2   621 use DBIx::SimpleMigration::Migration;
  2         6  
  2         592  
12              
13             our $VERSION = '1.0.1';
14              
15             sub new {
16 1     1 0 9045 my $self = bless {}, shift;
17 1 50       5 return unless @_ % 2 == 0;
18 1         4 my %args = @_;
19              
20             croak __PACKAGE__ . '->new: dbh option missing or not a DBI object'
21 1 50 33     13 unless ($args{dbh} && ref($args{dbh}) eq 'DBI::db');
22              
23             croak __PACKAGE__ . '->new: source dir missing or does not exist'
24 1 50 33     15 unless ($args{source} && -d $args{source});
25              
26 1         6 $self->{_source} = $args{source};
27              
28 1         4 my %options = (
29             migrations_table => 'migrations',
30             migrations_schema => 'simplemigration',
31             );
32              
33 1 50       3 if ($args{options}) {
34             # http://stackoverflow.com/questions/350018/how-can-i-combine-hashes-in-perl
35 0         0 @options{keys %{$args{options}}} = values %{$args{options}};
  0         0  
  0         0  
36             }
37 1         3 $self->{_options} = \%options;
38              
39 1         20 my $class = 'DBIx::SimpleMigration::Client::' . $args{dbh}->{Driver}->{Name};
40 1         65 eval "require $class; $class->import";
41 1 50       4 if ($@) {
42 0         0 $class = 'DBIx::SimpleMigration::Client';
43             }
44              
45             $self->{_client} = $class->new(
46             dbh => $args{dbh}->clone({
47 1         11 AutoCommit => 0,
48             RaiseError => 1
49             }), # new handle with AutoCommit off
50             options => \%options
51             );
52              
53 1         3 return $self;
54             }
55              
56             sub apply {
57 1     1 0 327 my ($self) = @_;
58              
59 1 50       5 if (!$self->{_client}->_migrations_table_exists) {
60 1         7 $self->{_client}->_create_migrations_table;
61             }
62              
63 1         2 my $dir = $self->{_source};
64 1         97 my @files = sort <$dir/*.sql>;
65              
66 1         11 my $applied_migrations = $self->{_client}->_applied_migrations;
67              
68 1         2 foreach my $file (@files) {
69 2         96 my ($filename) = fileparse($file);
70 2 50       13 next if exists $applied_migrations->{$filename};
71              
72             my $migration = DBIx::SimpleMigration::Migration->new(
73             client => $self->{_client},
74 2         16 file => $file
75             );
76              
77 2         9 $migration->apply;
78             }
79              
80 1 50       94 $self->{_client}->{dbh}->disconnect if $self->{_client}->{dbh};
81             }
82              
83             1;
84              
85             __END__