blib/lib/Smolder/Upgrade.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 113 CC 8 SCAR 27.9
CC
8
Cov
0%
upgrade
42.0
version_upgrade
12.0
new
2.0
CRAP 16.2
1 package Smolder::Upgrade;
2 use warnings;
3 use strict;
4 use File::Spec::Functions qw(catfile);
5 use Smolder;
6 use Smolder::Conf qw(SQLDir);
7 use Smolder::DB;
8  
9 =head1 NAME
10  
11 Smolder::Upgrade - Base class for Smolder upgrade modules
12  
13 =head1 SYNOPSIS
14  
15 use base 'Smolder::Upgrade';
16 sub pre_db_upgrade {....}
17 sub post_db_upgrade { ... }
18  
19 =head1 DESCRIPTION
20  
21 This module is intended to be used as a parent class for Smolder upgrade
22 modules.
23  
24 =head2 METHODS
25  
26 =head3 new
27  
28 The new() method is a constructor which creates a trivial object from a
29 hash. Your upgrade modules may use this to store state information.
30  
31 =cut
32  
33 # Create a trivial object
34 sub new {
35 my $class = shift;
36 bless({}, $class);
37 }
38  
39 =head3 upgrade
40  
41 This method looks at the current db_version of the database being used
42 and then decides which upgrade modules should be run. Upgrade modules
43 are children of this module and have the following naming pattern:
44 C<Smolder::Upgrade::VX_YZ> where C<X.YZ> is the version number.
45  
46 So for example if the current version is at 1.35 and we are upgrading
47 to 1.67, then any C<Smolder::Upgrade::VX_YZ> modules between those 2
48 versions are run.
49  
50 =cut
51  
52 sub upgrade {
53 my $self = shift;
54 # don't do anything for dev releases
55 return if $Smolder::VERSION =~ /_/;
56 my $db_version = Smolder::DB->db_Main->selectall_arrayref('SELECT db_version FROM db_version');
57 $db_version = $db_version->[0]->[0];
58 if ($db_version < $Smolder::VERSION) {
59  
60 # find applicable upgrade modules
61 warn
62 "Your version of Smolder ($db_version) is out of date. Upgrading to $Smolder::VERSION...\n";
63 my $upgrade_dir = __FILE__;
64 $upgrade_dir =~ s/\.pm$//;
65  
66 # Find upgrade modules
67 opendir(DIR, $upgrade_dir) || die("Unable to open upgrade directory '$upgrade_dir': $!\n");
68  
69 my @up_modules;
70 foreach my $file (sort readdir(DIR)) {
71 if ( (-f catfile($upgrade_dir, $file))
72 && ($file =~ /^V(\d+)\_(\d+)\.pm$/)
73 && ("$1.$2" > $db_version))
74 {
75 push(@up_modules, $file);
76 }
77 }
78 closedir(DIR);
79 warn " Found " . scalar(@up_modules) . " applicable upgrade modules.\n";
80  
81 if (@up_modules) {
82 foreach my $mod (@up_modules) {
83 $mod =~ /(.*)\.pm$/;
84 my $class = $1;
85 my ($major, $minor) = ($class =~ /V(\d+)_(\d+)/);
86 $class = "Smolder::Upgrade::$class";
87 eval "require $class";
88 die "Can't load $class upgrade class: $@" if $@;
89 $class->new->version_upgrade("$major.$minor");
90 }
91 }
92  
93 # upgrade the db_version
94 Smolder::DB->db_Main->do('UPDATE db_version SET db_version = ?', undef, $Smolder::VERSION);
95 }
96 }
97  
98 =head3 version_upgrade
99  
100 This method is called by C<upgrade()> for each upgrade version module. It
101 shouldn't be called directly from anyway else except for testing.
102  
103 It performs the following steps:
104  
105 =over
106  
107 =item 1
108  
109 Call the L<pre_db_upgrade> method .
110  
111 =item 2
112  
113 Run the SQL upgrade file found in F<sql/upgrade/> that has the same version
114 which is named for this same version. So an upgrade module named F<V1_23>
115 will run the F<upgrade/V1_23.sql> file if it exists.
116  
117 =item 3
118  
119 Call the L<post_db_upgrade> method.
120  
121 =back
122  
123 =cut
124  
125 sub version_upgrade {
126 my ($self, $version) = @_;
127 $self->pre_db_upgrade();
128  
129 # find and run the SQL file
130 $version =~ /(\d+)\.(\d+)/;
131 my $file = catfile(SQLDir, 'upgrade', "V$1_$2.sql");
132 if (-e $file) {
133 warn " Upgrading DB with file '$file'.\n";
134 Smolder::DB->run_sql_file($file);
135 } else {
136 warn " No SQL file ($file) for version $version. Skipping DB upgrade.\n";
137 }
138 $self->post_db_upgrade();
139  
140 # add any new things to the config file
141 my $new_config_stuff = $self->add_to_config();
142 if ($new_config_stuff) {
143  
144 # write out the new lines
145 my $conf_file = catfile($ENV{SMOLDER_ROOT}, 'conf', 'smolder.conf');
146 open(CONF, '>>', $conf_file)
147 or die "Unable to open $conf_file: $!";
148 print CONF $new_config_stuff;
149 close(CONF);
150 }
151 }
152  
153 =head3 pre_db_upgrade
154  
155 This method must be implemented in your subclass. It is called before
156 the SQL upgrade file is run.
157 It receives the L<Smolder::Platform> class for the given platform.
158  
159 =cut
160  
161 sub pre_db_upgrade {
162 my $self = shift;
163 die "pre_db_upgrade() must be implemented in " . ref($self);
164 }
165  
166 =head3 post_db_upgrade
167  
168 This method must be implemented in your subclass. It is called after
169 the SQL upgrade file is run.
170 It receives the L<Smolder::Platform> class for the given platform.
171  
172 =cut
173  
174 sub post_db_upgrade {
175 my $self = shift;
176 die "post_db_upgrade() must be implemented in " . ref($self);
177 }
178  
179 =head3 add_to_config
180  
181 This method will take a given string and add it to the end of the
182 current configuration file. This is useful for adding new required
183 directives with a reasonable default.
184  
185 =cut
186  
187 sub add_to_config { }
188  
189 1;