blib/lib/DBIx/SimpleMigration/Client/Pg.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 59 CC 7 SCAR 21.2
CC
7
Cov
0%
_create_migrations_schema
12.0
_create_migrations_table
12.0
new
6.0
CRAP 8.3
1 package DBIx::SimpleMigration::Client::Pg;
2  
3 use parent qw(DBIx::SimpleMigration::Client);
4  
5 our $VERSION = '1.0.1';
6  
7 sub new {
8 my $class = shift;
9 my $self = $class->SUPER::new(@_);
10  
11 $self->{options}->{migrations_schema} = 'public' unless $self->{options}->{migrations_schema};
12 $self->{options}->{migrations_table} = $self->{options}->{migrations_schema} . '.' . $self->{options}->{migrations_table};
13  
14 return $self;
15 }
16  
17 sub _create_migrations_schema {
18 my ($self) = @_;
19 return unless $self->{options}->{migrations_schema};
20  
21 my $query = '
22 SET client_min_messages = error;
23 CREATE SCHEMA IF NOT EXISTS ' . $self->{options}->{migrations_schema};
24  
25 eval { $self->{dbh}->do($query) };
26 if ($@) {
27 die 'Error creating schema: ' . $self->{dbh}->errstr;
28 }
29 }
30  
31 sub _create_migrations_table {
32 my ($self) = @_;
33  
34 if ($self->{options}->{migrations_schema} ne 'public') {
35 $self->_create_migrations_schema;
36 }
37  
38 my $query = '
39 SET client_min_messages = error;
40 CREATE TABLE IF NOT EXISTS ' . $self->{options}->{migrations_table} . ' (
41 name text primary key not null,
42 applied timestamp not null
43 )
44 ';
45  
46 eval { $self->{dbh}->do($query) };
47 if ($@) {
48 die 'Error creating table: ' . $self->{dbh}->errstr;
49 }
50 }
51  
52 sub _migrations_table_exists {
53 my ($self) = @_;
54  
55 my $schema_where;
56 if ($self->{options}->{migrations_schema}) {
57 $schema_where = "AND table_schema = '$self->{options}->{migrations_schema}'";
58 }
59  
60 my $query = "
61 WITH tables AS (
62 SELECT table_schema::text || '.' || table_name::text as theTable
63 FROM information_schema.tables
64 )
65 SELECT EXISTS (
66 SELECT NULL
67 FROM tables
68 WHERE theTable = ?
69 )
70 ";
71  
72 my $row = $self->{dbh}->selectrow_hashref($query, {}, $self->{options}->{migrations_table}) or die 'Database error: ' . $self->{dbh}->errstr;
73  
74 return $row->{exists};
75 }
76  
77 sub _lock_migrations_table {
78 my ($self) = @_;
79  
80 my $query = 'LOCK TABLE ' . $self->{options}->{migrations_table} . ' IN EXCLUSIVE MODE';
81 return $self->{dbh}->do($query);
82 }
83  
84 1;