blib/lib/DBIx/VersionedSchema.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 64 CC 4 SCAR 17.6
CC
4
Cov
0%
run_updates
12.0
current_version
6.0
add_version
2.0
CRAP 5.8
1 =head1 NAME
2  
3 DBIx::VersionedSchema - helps to manage database schema versions
4  
5 =head1 SYNOPSIS
6
7 package MyVersions;
8 use base 'DBIx::VersionedSchema';
9  
10 # Set the name of the table
11 __PACKAGE__->Name('my_schema_versions');
12  
13 # Add first version
14 __PACKAGE__->add_version(sub {
15 my $dbh = shift;
16 $dbh->do(...);
17 });
18  
19 # Add another one
20 __PACKAGE__->add_version(...);
21  
22 package main;
23  
24 my $vs = MyVersions->new($dbh);
25  
26 # recreates / adds versions
27 $vs->run_updates;
28  
29 =head1 DESCRIPTION
30  
31 This module helps to evolve database schema in small increments called
32 schema versions henceforth.
33  
34 Each of those versions is perl function that you pass to add_version function.
35 Those versions are replayed by calling run_updates function.
36  
37 DBIx::VersionedSchema keeps track of applied versions by using database table.
38 You should provide the name of this table by using Name property of your
39 inheriting class.
40  
41 The tracking table will be created on the first call to run_updates function.
42  
43 =head1 METHODS
44  
45 =cut
46  
47 use strict;
48 use warnings FATAL => 'all';
49  
50 package DBIx::VersionedSchema;
51 use base qw(Class::Data::Inheritable);
52 our $VERSION = 0.03;
53  
54 __PACKAGE__->mk_classdata('Versions');
55  
56 =head2 Name
57  
58 This is per class method. It should be used to set the name of the tracking
59 table.
60  
61 =cut
62  
63 __PACKAGE__->mk_classdata('Name');
64  
65 =head2 add_version(function)
66  
67 Adds another schema version as perl function. This function will run during
68 run_updates call. Database handle will be provided to the
69 function as an argument.
70  
71 =cut
72  
73 sub add_version {
74 my ($self, $func) = @_;
75 my $versions = $self->Versions || [];
76 push @$versions, $func;
77 $self->Versions($versions);
78 }
79  
80 =head2 new(dbh)
81  
82 Creates new instance. $dbh is the database handle which will be passed
83 down to the schema changing functions.
84  
85 =cut
86 sub new {
87 my ($class, $dbh) = @_;
88 my $self = bless { dbh => $dbh }, $class;
89 return $self;
90 }
91  
92 =head2 current_version
93  
94 Returns current schema version. Returns C<undef> if no version tracking table
95 has been found. Returns 0 if the table exists but no updates have been run yet.
96  
97 =cut
98 sub current_version {
99 my $self = shift;
100 my @res = $self->{dbh}->selectrow_array(q{ select *
101 from information_schema.tables
102 where table_name = ? }, undef, $self->Name)
103 or return undef;
104 @res = $self->{dbh}->selectrow_array(sprintf(q{ select version
105 from %s
106 order by version desc limit 1 }, $self->Name));
107 return @res ? $res[0] : 0;
108 }
109  
110 =head2 run_updates
111  
112 Runs the updates starting from the current schema version.
113  
114 =cut
115 sub run_updates {
116 my $self = shift;
117 my $dbh = $self->{dbh};
118 $dbh->do(q{ set client_min_messages to warning });
119 my $cur_version = $self->current_version;
120 local $dbh->{AutoCommit};
121 unless (defined($cur_version)) {
122 $dbh->do(sprintf(q{ create table %s (
123 version smallint primary key,
124 release_date timestamp default current_timestamp
125 ) }, $self->Name));
126 $cur_version = 0;
127 }
128 my $versions = $self->Versions || [];
129 my $i = $cur_version;
130 for (; $i < scalar(@$versions); $i++) {
131 $versions->[$i]->($dbh);
132 }
133 $dbh->do(sprintf(q{ insert into %s (version)
134 values (?) }, $self->Name), undef, scalar(@$versions))
135 if $i > $cur_version;
136 }
137  
138 1;
139  
140 =head1 AUTHOR
141  
142 Boris Sukholitko
143 boriss@gmail.com
144  
145 =head1 COPYRIGHT
146  
147 This program is free software; you can redistribute
148 it and/or modify it under the same terms as Perl itself.
149  
150 The full text of the license can be found in the
151 LICENSE file included with this module.
152  
153  
154 =head1 SEE ALSO
155  
156 Test::TempDatabase - for creation of temporary databases.
157  
158 =cut
159  
160