File Coverage

blib/lib/DBIx/SimpleMigration/Client.pm
Criterion Covered Total %
statement 26 31 83.8
branch 3 6 50.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 34 44 77.2


line stmt bran cond sub pod time code
1             package DBIx::SimpleMigration::Client;
2              
3             our $VERSION = '1.0.1';
4              
5             sub new {
6 1     1 0 260 my $self = bless {}, shift;
7 1 50       4 return unless @_ % 2 == 0;
8 1         3 my %args = @_;
9              
10 1         4 $self->{dbh} = $args{dbh};
11 1         2 $self->{options} = $args{options};
12              
13 1         3 return $self;
14             }
15              
16             sub _create_migrations_table {
17 1     1   2 my ($self) = @_;
18              
19             my $query = '
20 1         4 CREATE TABLE IF NOT EXISTS ' . $self->{options}->{migrations_table} . ' (
21             name varchar(255) primary key not null,
22             applied datetime not null
23             )
24             ';
25              
26 1 50       6 $self->{dbh}->do($query) or die 'Error creating table: ' . $self->{dbh}->errstr;
27              
28 1         177 return $self;
29             }
30              
31             sub _migrations_table_exists {
32 0     0   0 my ($self) = @_;
33              
34 0         0 return 0; # checking seems to be largely driver-dependent
35             }
36              
37             sub _lock_migrations_table {
38 2     2   3 my ($self) = @_;
39              
40 2         9 return 1; # to be overloaded per-DB
41             }
42              
43             sub _applied_migrations {
44 1     1   2 my ($self) = @_;
45              
46             my $query = '
47             SELECT name, applied
48 1         3 FROM ' . $self->{options}->{migrations_table};
49              
50 1         13 my $result = $self->{dbh}->selectall_arrayref($query, {Slice => {}});
51              
52 1         128 my %applied_migrations;
53 1         1 foreach my $row (@{$result}) {
  1         3  
54 0         0 $applied_migrations{$row->{name}} = $row->{applied};
55             }
56              
57 1         3 return \%applied_migrations;
58             }
59              
60             sub _insert_migration {
61 2     2   4 my ($self, $key) = @_;
62              
63 2         3 eval {
64 2         7 my $query = 'INSERT INTO ' . $self->{options}->{migrations_table} . ' (name, applied) VALUES (?, CURRENT_TIMESTAMP)';
65 2         11 my $sth = $self->{dbh}->prepare($query);
66 2         226 $sth->execute($key);
67             };
68              
69 2 50       6 if ($@) {
70 0         0 warn 'Error recording migration: ' . $self->{dbh}->errstr;
71 0         0 return;
72             }
73              
74 2         8 return 1;
75             }
76              
77             1;
78              
79             __END__