blib/lib/DB/SQL/Migrations.pm

filter: Statementstmt 28.2% 24 / 85 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.2% 6 / 17 Podpod 0.0% 0 / 2 totaltotal 26.7% 30 / 112 CC 8 SCAR 18.8
CC
8
Cov
26%
_pending_migrations
20.0
apply
12.0
__ANON__
6.0
CRAP 6.5
↑ Summary
1 package DB::SQL::Migrations;
2  
3 1 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBIx::MultiStatementDo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.07';
10  
11 1 has [qw( dbh migrations_directory )];
12 0 ▶has schema_migrations_table => sub { 'schema_migrations' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 0 ▶has schema_migrations_name_field => sub { 'name' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 0 ▶has schema_migrations_date_field => sub { 'date_applied' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15  
16 0 ▶has _applied_migrations => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
17 0 my $self = shift;
18 0 my %applied_migrations;
19  
20 0 my $sth = $self->dbh->prepare("SELECT " .$self->schema_migrations_name_field . ", ". $self->schema_migrations_date_field ." FROM ". $self->schema_migrations_table );
21 0 $sth->execute();
22 0 $sth->bind_columns( \my ( $name, $date_applied ) );
23 0 ▶ while ( $sth->fetch() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 0 0 0
24 0 $applied_migrations{$name} = $date_applied;
25 }
26 0 $sth->finish;
27  
28 0 return \%applied_migrations;
29 1 };
30  
31 0 ▶sub _pending_migrations {
Subroutine _pending_migrations: not called CC=4 SCAR=30.0
32 0 my $self = shift;
33 0 my @pending_migrations;
34  
35 0 foreach my $migration_file( $self->_migration_files_in_order ) {
36 0 my $migration_key = $self->_migration_key($migration_file);
37 0 ▶ push @pending_migrations, $migration_file unless exists $self->_applied_migrations->{$migration_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_applied_migrations->{$migration_key} 0 0 0
38 }
39  
40 0 foreach my $pending_migration(@pending_migrations) {
41 0 print "$pending_migration is pending\n";
42 }
43  
44 0 return @pending_migrations;
45 }
46  
47 0 ▶sub apply {
Subroutine apply: not called CC=3 SCAR=24.8
Pod undocumented
48 0 my $self = shift;
49  
50 0 my @pending_migrations = $self->_pending_migrations;
51  
52 0 ▶ if(scalar(@pending_migrations)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pending_migrations) { } 0 0 0
53  
54 0 foreach my $migration(@pending_migrations) {
55 0 $self->_apply_migration($migration);
56 }
57 }
58 else {
59 0 print "Up to date\n";
60 }
61 }
62  
63 0 ▶sub _apply_migration {
Subroutine _apply_migration: not called CC=2 SCAR=17.9
64 0 my $self = shift;
65 0 my $file_name = shift;
66  
67 0 my $sql = read_file($file_name);
68 0 my $batch = DBIx::MultiStatementDo->new(
69 dbh => $self->dbh,
70 rollback => 0
71 );
72 0 $batch->dbh->{AutoCommit} = 0;
73 0 $batch->dbh->{RaiseError} = 1;
74  
75 eval {
76 0 $batch->do( $sql );
77 0 $batch->dbh->commit;
78 0 1
79 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $batch->do($sql); $batch->dbh->commit; 1 } } or do { print "$@ \n"; eval { do { $batch->dbh->rollback } }; print "Failed to apply migration: $file_name\n"; die "Exiting due to failed migrations \n" } 0 0 0
80 0 print "$@ \n";
81 0 eval { $batch->dbh->rollback };
82 0 print "Failed to apply migration: $file_name\n";
83  
84 0 die "Exiting due to failed migrations \n";
85 };
86  
87 0 $self->_insert_into_schema_migrations($file_name);
88  
89 0 print "Applied migration $file_name \n";
90 }
91  
92 0 ▶sub _insert_into_schema_migrations {
Subroutine _insert_into_schema_migrations: not called CC=1 SCAR=6.9
93 0 my $self = shift;
94 0 my $migration = shift;
95 0 my $migration_key = $self->_migration_key($migration);
96  
97 0 $self->dbh->do("INSERT INTO ". $self->schema_migrations_table ." (". $self->schema_migrations_name_field .", ". $self->schema_migrations_date_field .") VALUES (?,NOW())", undef, $migration_key );
98 0 $self->dbh->commit;
99 }
100  
101 0 ▶sub _migration_files_in_order {
Subroutine _migration_files_in_order: not called CC=1 SCAR=1.2
102 0 my $self = shift;
103 0 my $dir = $self->migrations_directory;
104  
105 0 ▶ return sort <$dir/*.sql>;
Subroutine BEGIN: called CC=1 SCAR=0.2
106 }
107  
108 0 ▶sub create_migrations_table {
Subroutine create_migrations_table: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self = shift;
110 0 my $table_name = $self->schema_migrations_table;
111 0 my $name_field = $self->schema_migrations_name_field;
112 0 my $date_field = $self->schema_migrations_date_field;
113  
114 0 my $sql = "CREATE TABLE IF NOT EXISTS $table_name (
115 $name_field varchar(255) NOT NULL PRIMARY KEY,
116 $date_field datetime NOT NULL
117 );
118 ";
119  
120 0 $self->dbh->do($sql);
121 }
122  
123 0 ▶sub _migration_key {
Subroutine _migration_key: not called CC=1 SCAR=6.9
124 0 my $self = shift;
125 0 my $migration_file = shift;
126  
127 #Use filename for the key
128 0 my($filename, $directories, $suffix) = fileparse($migration_file);
129 0 return $filename;
130 }
131  
132 1 1;
133 __END__
↑ Summary