Alzabo/BackCompat.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 198 CC 9 SCAR 36.4
CC
9
Cov
0%
update_schema
90.0
add_comment_fields
2.0
convert_pk_to_array
2.0
CRAP 38.0
1 package Alzabo::BackCompat;
2  
3 use strict;
4  
5 use Alzabo::Config;
6  
7 use File::Basename;
8 use File::Copy;
9 use File::Spec;
10 use Storable;
11 use Tie::IxHash;
12  
13 use Params::Validate qw( :all );
14 Params::Validate::validation_options( on_fail => sub { Alzabo::Exception::Params->throw( error => join '', @_ ) } );
15  
16 use vars qw($VERSION);
17  
18 $VERSION = 2.0;
19  
20 #
21 # Each pair represents a range of versions which are compatible with
22 # each other. The first one is not quite right but it has to start
23 # somewhere ;)
24 #
25 # Any extra elements are subroutines which should be run to update the
26 # schema, if it's version is lower than the first element of the
27 # version pair.
28 #
29 my @compat = ( [ 0, 0.64 ],
30 [ 0.65, 0.70,
31 \&add_comment_fields,
32 ],
33 [ 0.71, 0.73,
34 \&convert_pk_to_array,
35 ],
36 [ 0.79, $Alzabo::VERSION,
37 \&add_table_attributes,
38 ],
39 );
40  
41 sub update_schema
42 {
43 my %p = validate( @_, { name => { type => SCALAR },
44 version => { type => SCALAR },
45 } );
46  
47 my @cb;
48 foreach my $c (@compat)
49 {
50 return
51 if ( ( $p{version} >= $c->[0] &&
52 $p{version} <= $c->[1] ) &&
53  
54 ( $Alzabo::VERSION >= $c->[0] &&
55 $Alzabo::VERSION <= $c->[1] )
56 );
57  
58 if ( $p{version} < $c->[0] && @$c > 2 )
59 {
60 push @cb, @{$c}[2..$#$c];
61 }
62 }
63  
64 my $create_loaded;
65 unless ( $Alzabo::Create::Schema::VERSION )
66 {
67 require Alzabo::Create::Schema;
68 $create_loaded = 1;
69 }
70  
71 my $v = $p{version} = 0 ? '0.64 or earlier' : $p{version};
72  
73 my $c_file = Alzabo::Create::Schema->_schema_filename( $p{name} );
74 unless ( -w $c_file )
75 {
76 my $msg = <<"EOF";
77  
78 The '$p{name}' schema was created by an older version of Alzabo
79 ($v) than the one currently installed ($Alzabo::VERSION).
80  
81 Alzabo can update your schema objects but your schema file:
82  
83 $c_file
84  
85 is not writeable by this process. Loading this schema in a process
86 which can write to this file will cause the schema to be updated.
87  
88 EOF
89  
90 die $msg;
91 }
92  
93 my $dir = dirname($c_file);
94 unless ( -w $dir )
95 {
96 my $msg = <<"EOF";
97  
98 The '$p{name}' schema was created by an older version of Alzabo
99 ($v) than the one currently installed ($Alzabo::VERSION).
100  
101 Alzabo can update your schema objects but its director:
102  
103 $dir
104  
105 is not writeable by this process. Loading this schema in a process
106 which can write to this file will cause the schema to be updated.
107  
108 EOF
109  
110 die $msg;
111 }
112  
113 foreach my $file ( glob("$dir/*.alz"),
114 glob("$dir/*.rdbms"),
115 glob("$dir/*.version") )
116 {
117 my $backup = "$file.bak.v$p{version}";
118  
119 copy($file, $backup);
120 }
121  
122 my $fh = do { local *FH; *FH };
123 open $fh, "<$c_file"
124 or Alzabo::Exception::System->throw( error => "Unable to open $c_file: $!" );
125 my $raw = Storable::fd_retrieve($fh)
126 or Alzabo::Exception::System->throw( error => "Can't read filehandle" );
127 close $fh
128 or Alzabo::Exception::System->throw( error => "Unable to close $c_file: $!" );
129  
130 foreach (@cb)
131 {
132 $_->($raw);
133 $_->( $raw->{original} ) if $raw->{original};
134 }
135  
136 open $fh, ">$c_file"
137 or Alzabo::Exception::System->throw( error => "Unable to write to $c_file: $!" );
138 Storable::nstore_fd( $raw, $fh )
139 or Alzabo::Exception::System->throw( error => "Can't store to filehandle" );
140 close $fh
141 or Alzabo::Exception::System->throw( error => "Unable to close $c_file: $!" );
142  
143 my $version_file =
144 File::Spec->catfile( Alzabo::Config::schema_dir(),
145 $p{name}, "$p{name}.version" );
146  
147 open $fh, ">$version_file"
148 or Alzabo::Exception::System->throw( error => "Unable to write to $version_file: $!" );
149 print $fh $Alzabo::VERSION
150 or Alzabo::Exception::System->throw( error => "Can't write to $version_file: $!" );
151 close $fh
152 or Alzabo::Exception::System->throw( error => "Unable to close $version_file: $!" );
153  
154 Alzabo::Create::Schema->load_from_file( name => $p{name} )->save_to_file;
155  
156 if ($create_loaded)
157 {
158 warn <<"EOF"
159  
160 Your schema, $p{name}, has been updated to be compatible with the
161 installed version of Alzabo. This required that the Alzabo::Create::*
162 classes be loaded. If you were loading an Alzabo::Runtime::Schema
163 object, your running process is now somewhat larger than it has to be.
164  
165 If this is a long running process you may want to reload it.
166  
167 EOF
168 }
169 }
170  
171 sub add_comment_fields
172 {
173 my $s = shift;
174  
175 foreach my $table ( $s->{tables}->Values )
176 {
177 $table->{comment} = '';
178  
179 foreach my $thing ( $table->{columns}->Values,
180 values %{ $table->{fk} } )
181 {
182 $table->{comment} = '';
183 }
184 }
185 }
186  
187 sub convert_pk_to_array
188 {
189 my $s = shift;
190  
191 foreach my $table ( $s->tables )
192 {
193 my @names = map { $_->name } $table->{pk}->Values;
194 my $pk = [ $table->{columns}->Indices(@names) ];
195  
196 $table->{pk} = $pk;
197 }
198 }
199  
200 sub add_table_attributes
201 {
202 my $s = shift;
203  
204 foreach my $table ( $s->tables )
205 {
206 tie %{ $table->{attributes} }, 'Tie::IxHash';
207 }
208 }
209  
210  
211 __END__