lib/SchemaEvolution.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 169 CC 6 SCAR 16.0
CC
6
Cov
0%
run
12.0
_build__dbh
12.0
apply_evolution
6.0
CRAP 5.0
1 package SchemaEvolution;
2 our $VERSION = '0.03';
3  
4  
5 # ABSTRACT: SchemaEvolution - manage the evolution of a database with simple files
6  
7 use Moose;
8 use MooseX::Has::Sugar;
9 use MooseX::Types::Moose qw( ArrayRef Bool HashRef Str );
10  
11 use Config::Tiny;
12 use DBI;
13 use File::Find::Rule;
14 use File::Slurp;
15 use Path::Class qw( file );
16 use SchemaEvolution::Types qw( DBH );
17 use TryCatch;
18  
19 with 'MooseX::Getopt';
20  
21 has 'config_file' => (
22 traits => [qw( Getopt )],
23 isa => Str,
24 cmd_aliases => [qw( c config )],
25 ro,
26 default => 'evolution.ini',
27 required
28 );
29  
30 has 'initialize' => (
31 traits => [qw( Getopt )],
32 isa => Bool,
33 ro,
34 default => 0,
35 );
36  
37 sub run {
38 my $self = shift;
39  
40 $self->initialize_table if ($self->initialize);
41  
42  
43 my $dbh = $self->_dbh;
44 my ($column, $table) = ($self->_version_column, $self->_version_table);
45 my ($version) = $dbh->selectrow_array("SELECT $column FROM $table");
46 defined $version
47 or die "Could not select version column '$column' from meta table '$table'";
48 my @evolutions = $self->evolutions_after_version($version);
49 if (@evolutions == 0) {
50 print "Didn't find any schema evolutions, nothing to do\n";
51 return;
52 }
53  
54 my $new_version = $version;
55 for my $evolution (@evolutions) {
56 try {
57 $new_version = $self->apply_evolution($evolution);
58 } catch {
59 print "encountered an error, aborting.\n";
60 last;
61 }
62 }
63  
64 print "\nSchema evolution completed (or terminated)\n";
65 print "New version is: $new_version\n";
66 $self->_set_version($new_version);
67 }
68  
69 sub initialize_table {
70 my $self = shift;
71 my $dbh = $self->_dbh;
72 my ($column, $table) = ($self->_version_column, $self->_version_table);
73 $dbh->do("CREATE TABLE $table ( $column INTEGER NOT NULL DEFAULT 0 )");
74 $dbh->do("INSERT INTO $table ( $column) VALUES ( 0 )");
75 }
76  
77 sub apply_evolution {
78 my ($self, $filename) = @_;
79 print "Applying $filename... ";
80 my $dbh = $self->_dbh;
81 my $sql = read_file($filename) or die "Could read contents of $filename!";
82 my $res = $dbh->do($sql);
83 if (!defined $res) {
84 die "Could not apply $filename";
85 }
86 print "done!\n";
87 return _version_from_filename($filename);
88 }
89  
90 sub _set_version {
91 my ($self, $version) = @_;
92 my ($column, $table) = ($self->_version_column, $self->_version_table);
93 $self->_dbh->do("UPDATE $table SET $column = ?", {}, $version);
94 }
95  
96 sub _version_from_filename
97 {
98 my $filename = shift;
99 my ($v) = file($filename)->basename =~ /^(\d+)/;
100 return int($v);
101 }
102  
103 sub evolutions_after_version
104 {
105 my ($self, $version) = @_;
106 return sort {
107 _version_from_filename($a) <=> _version_from_filename($b)
108 } grep {
109 _version_from_filename($_) > $version;
110 } @{ $self->_evolutions };
111 }
112  
113 has '_evolutions' => (
114 isa => ArrayRef[Str],
115 lazy,
116 ro,
117 default => sub {
118 my $self = shift;
119 return [
120 File::Find::Rule->file()
121 ->name('*.sql')->in($self->_evolutions_dir)
122 ];
123 }
124 );
125  
126 has '_config' => (
127 ro,
128 lazy_build
129 );
130  
131 sub _build__config {
132 my $self = shift;
133 my $tiny = Config::Tiny->new;
134 my $config = $tiny->read($self->config_file)
135 or die 'Could not open ' . $self->config_file;
136 return $config;
137 }
138  
139 for my $param (qw(username password dsn)) {
140 has "_$param" => (
141 ro,
142 lazy,
143 default => sub { shift->_config->{_}->{$param} }
144 );
145 }
146  
147 has '_version_table' => (
148 ro,
149 isa => Str,
150 lazy => 1,
151 default => sub { shift->_config->{_}->{version_table} || 'schema_version' }
152 );
153  
154 has '_version_column' => (
155 ro,
156 isa => Str,
157 lazy => 1,
158 default => sub { shift->_config->{_}->{version_column} || 'version' }
159 );
160  
161 has '_evolutions_dir' => (
162 ro,
163 isa => Str,
164 lazy => 1,
165 default => sub { shift->_config->{_}->{evolutions} || 'evolutions' }
166 );
167  
168 has '_dbh' => (
169 isa => DBH,
170 ro,
171 lazy_build
172 );
173  
174 sub _build__dbh {
175 my $self = shift;
176 my $dsn = $self->_dsn or die 'Missing required option "dsn"';
177 my @args = ($dsn);
178 push @args, $self->_username if $self->_username;
179 push @args, $self->_password if $self->_password;
180 my $dbh = DBI->connect(@args)
181 or die "Could not connect to: " . $self->_dsn;
182 return $dbh;
183 }
184  
185 1;
186  
187  
188  
189 __END__