Jifty/Handle.pm

filter: Statementstmt 8.0% 11 / 137 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 20.0% 3 / 15 Podpod 100% 6 / 6 totaltotal 8.6% 20 / 231 CC 35 SCAR 46.1
CC
35
Cov
5%
check_schema_version
272.0
connect
30.0
create_database
30.0
CRAP 100.7
« Jifty/Web.pm ↑ Summary Jifty/Script/Adopt.pm »
1 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Handle;
5  
6 =head1 NAME
7  
8 Jifty::Handle -- A database handle class for Jifty
9  
10 =head1 DESCRIPTION
11  
12 A wrapper around Jifty::DBI::Handle which is aware of versions in the
13 database
14  
15 =cut
16  
17 113 ▶use Jifty::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 113 our @ISA;
19  
20 =head1 METHODS
21  
22 =head2 new PARAMHASH
23  
24 This class method instantiates a new L<Jifty::Handle> object. This
25 object deals with database handles for the system. After it is
26 created, it will be a subclass of L<Jifty::DBI::Handle>.
27  
28 =cut
29  
30 # Setup database handle based on config data
31 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
32 0 my $class = shift;
33  
34 0 my $driver = Jifty->config->framework('Database')->{'Driver'};
35 0 ▶ if ( $driver eq 'Oracle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver eq "Oracle") 0 0 0
36 0 $ENV{'NLS_LANG'} = "AMERICAN_AMERICA.AL32UTF8";
37 0 $ENV{'NLS_NCHAR'} = "AL32UTF8";
38 }
39  
40 # We do this to avoid Jifty::DBI::Handle's magic reblessing, because
41 # it breaks subclass methods.
42 0 my $driver_class = "Jifty::DBI::Handle::" . $driver;
43 0 Jifty::Util->require($driver_class);
44  
45 0 ▶ die "No such handle class as $driver_class. ",
Branch: 1 branch
BranchTrueFalseTotal
unless $driver_class->can("isa") 0 0 0
46 "Check your spelling and check that your Jifty installation and ",
47 "related modules (especially Jifty::DBI) are up to date."
48 unless $driver_class->can('isa');
49  
50 0 unshift @ISA, $driver_class;
51 0 return $class->SUPER::new();
52 }
53  
54 =head2 canonical_database_name
55  
56 Returns the canonical name of the application's database (the actual name that will
57 be given to the database driver). This name is a lower-case version of the C<Database>
58 argument in the C<Database> section of the framework config.
59  
60 For SQLite databases (where the database name is actually a filename), this also converts
61 a relative path into an absolute path based at the application root.
62  
63 =cut
64  
65 0 ▶sub canonical_database_name {
Subroutine canonical_database_name: not called CC=3 SCAR=24.8
Pod documented
66 0 my $self_or_class = shift;
67 0 my $db_config = Jifty->config->framework('Database');
68  
69 # XXX TODO consider canonicalizing to all-lowercase, once there are no
70 # legacy databases
71 0 my $db = $db_config->{'Database'};
72  
73 0 ▶ if ( $db_config->{'Driver'} =~ /SQLite/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_config->{"Driver"} =~ /SQLite/) 0 0 0
74 0 $db = Jifty::Util->absolute_path($db);
75 # ':' is not allowed in a (esp.) Win32 path.
76 # however, this may break someone's existing setup, so
77 # added existence check (Shawn's advice)
78 0 ▶ $db =~ s{::}{-}g unless -e $db;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $db 0 0 0
79 }
80  
81 0 return $db;
82 }
83  
84 =head2 connect ARGS
85  
86 Like L<Jifty::DBI>'s connect method but pulls the name of the database
87 from the current L<Jifty::Config>.
88  
89 =cut
90  
91 0 ▶sub connect {
Subroutine connect: not called CC=5 SCAR=34.0
Pod documented
92 0 my $self = shift;
93 0 my %args = (@_);
94 my %db_config = (
95 0 %{ Jifty->config->framework('Database') },
96 Database => $self->canonical_database_name
97 );
98  
99 0 my %lc_db_config;
100  
101 # Skip the non-dsn keys, but not anything else
102 0 for (
103 grep {
104 0 !/^autoupgrade|checkschema|version|forwardcompatible|recordbaseclass|attributes$/i
105 } keys %db_config
106 )
107 {
108 0 $lc_db_config{ lc($_) } = $db_config{$_};
109 }
110 0 $self->SUPER::connect( %lc_db_config, %args );
111 0 $self->{db_config} = { %lc_db_config, %args };
112 0 ▶ $self->dbh->{LongReadLen} = Jifty->config->framework('MaxAttachmentSize')
Condition0%0/2
'Jifty'->config->framework('MaxAttachmentSize') || '10000000'
l!l
00
MC/DC0%0/1
'Jifty'->config->framework('MaxAttachmentSize') || '10000000'
'Jifty'->config->framework('MaxAttachmentSize')
Truth table0%0/2
A
A= 'Jifty'->config->framework('MaxAttachmentSize')
Aresult
11
00
113 || '10000000';
114  
115 # setup attributes
116 0 ▶ my $attributes = Jifty->config->framework('Database')->{Attributes} || {};
Condition0%0/2
'Jifty'->config->framework('Database')->{'Attributes'} || {}
l!l
00
MC/DC0%0/1
'Jifty'->config->framework('Database')->{'Attributes'} || {}
'Jifty'->config->framework('Database')->{'Attributes'}
Truth table0%0/2
A
A= 'Jifty'->config->framework('Database')->{'Attributes'}
Aresult
11
00
117 0 for ( keys %$attributes ) {
118 0 $self->dbh->{ lc($_) } = $attributes->{$_};
119 }
120 }
121  
122 =head2 check_schema_version [pretend => 0|1]
123  
124 Make sure that we have a recent enough database schema. If we don't,
125 then error out.
126  
127 If C<pretend => 1> is passed, then any auto-upgrade action this might take is
128 dry-run only.
129  
130 =cut
131  
132 0 ▶sub check_schema_version {
Subroutine check_schema_version: not called CC=16 SCAR=56.1
Pod documented
133 0 my $self = shift;
134 0 my %opts = ( pretend => 0, @_ );
135  
136 0 require Jifty::Model::Metadata;
137 0 my $autoup = delete Jifty->config->framework('Database')->{'AutoUpgrade'};
138  
139 # Application db version check
140 {
141 0 my $appv = Jifty->config->framework('Database')->{'Version'};
142 0 my $dbv = $self->_fetch_dbv;
143  
144 0 ▶ unless (defined $dbv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dbv) 0 0 0
145 0 my $error = "Application schema has no version in the database.\n";
146  
147 # we can just create it and we'll be up to date
148 0 ▶ if ( $autoup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoup) 0 0 0
149 0 warn $error;
150 0 warn "Automatically creating your database.\n";
151 0 $self->_create_original_database();
152 0 return 1;
153 }
154  
155 0 die "${error}Please run `bin/jifty schema --setup` to create the database.\n";
156 }
157  
158 0 ▶ unless ( version->new($appv) == version->new($dbv) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("version"->new($appv) == "version"->new($dbv)) 0 0 0
159  
160 # if app version is older than db version, but we are still compatible
161 my $compat = delete Jifty->config->framework('Database')
162 0 ▶ ->{'ForwardCompatible'} || $appv;
Condition0%0/3
delete 'Jifty'->config->framework('Database')->{'ForwardCompatible'} || $appv
l!l&&r!l&&!r
000
MC/DC0%0/2
delete 'Jifty'->config->framework('Database')->{'ForwardCompatible'} || $appv
delete 'Jifty'->config->framework('Database')->{'ForwardCompatible'} $appv
Truth table0%0/3
A || B
A= delete 'Jifty'->config->framework('Database')->{'ForwardCompatible'}B= $appv
ABresult
1X1
011
000
163 0 ▶ if ( version->new($appv) > version->new($dbv)
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->new($appv) > "version"->new($dbv) or "version"->new($compat) < "version"->new($dbv)) 0 0 0
Condition0%0/3
"version"->new($appv) > "version"->new($dbv) or "version"->new($compat) < "version"->new($dbv)
l!l&&r!l&&!r
000
MC/DC0%0/2
"version"->new($appv) > "version"->new($dbv) or "version"->new($compat) < "version"->new($dbv)
"version"->new($appv) > "version"->new($dbv) "version"->new($compat) < "version"->new($dbv)
Truth table0%0/3
A or B
A= "version"->new($appv) > "version"->new($dbv)B= "version"->new($compat) < "version"->new($dbv)
ABresult
1X1
011
000
164 || version->new($compat) < version->new($dbv) )
165 {
166 0 my $error =
167 "Application schema version in database ($dbv) doesn't match application schema version ($appv)\n";
168 0 ▶ if ( $autoup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoup) { } 0 0 0
169 0 warn $error;
170 0 warn
171 "Automatically upgrading your database to match the current application schema.\n";
172 0 $self->_upgrade_schema('print' => $opts{pretend});
173 } else {
174 0 die "${error}Please run `bin/jifty schema --setup` to upgrade the database.\n";
175 }
176 }
177 }
178 }
179  
180 # Jifty db version check
181 {
182  
183 # If we got here, the application had a version (somehow) so
184 # this is an upgrade. If $dbv is undef, it's because it's
185 # from before when the _jifty_metadata table existed.
186 0 ▶ my $dbv
Condition0%0/2
'Jifty::Model::Metadata'->load('jifty_db_version') || '0.60426'
l!l
00
MC/DC0%0/1
'Jifty::Model::Metadata'->load('jifty_db_version') || '0.60426'
'Jifty::Model::Metadata'->load('jifty_db_version')
Truth table0%0/2
A
A= 'Jifty::Model::Metadata'->load('jifty_db_version')
Aresult
11
00
187 = version->new( Jifty::Model::Metadata->load("jifty_db_version")
188 || '0.60426' );
189 0 my $appv = version->new($Jifty::VERSION);
190 0 ▶ unless ( $appv == $dbv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($appv == $dbv) 0 0 0
191 0 my $error =
192 "Internal jifty schema version in database ($dbv) doesn't match running jifty version ($appv)\n";
193 0 ▶ if ($autoup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoup) { } 0 0 0
194 0 warn $error;
195 0 warn
196 "Automatically upgrading your database to match the current Jifty schema\n";
197 0 $self->_upgrade_schema('print' => $opts{pretend});
198 } else {
199 0 die "${error}Please run `bin/jifty schema --setup` to upgrade the database.\n";
200 }
201 }
202 }
203  
204 # Plugin version check
205 0 for my $plugin ( Jifty->plugins ) {
206 0 my $plugin_class = ref $plugin;
207  
208 0 my $dbv
209 = Jifty::Model::Metadata->load( $plugin_class . '_db_version' );
210 0 my $appv = version->new( $plugin->version );
211  
212 0 ▶ if ( not defined $dbv ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $dbv) { } 0 0 0
elsif ('version'->new($dbv) < $appv) { } 0 0 0
213 0 my $error =
214 "$plugin_class plugin isn't installed in database\n";
215 0 ▶ if ($autoup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoup) { } 0 0 0
216 0 warn $error;
217 0 warn
218 "Automatically upgrading your database to match the current plugin schema\n";
219 0 $self->_upgrade_schema('print' => $opts{pretend});
220 } else {
221 0 die "${error}Please run `bin/jifty schema --setup` to upgrade the database.\n";
222 }
223 } elsif (version->new($dbv) < $appv) {
224 0 my $error =
225 "$plugin_class plugin version in database ($dbv) doesn't match running plugin version ($appv)\n";
226 0 ▶ if ($autoup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoup) { } 0 0 0
227 0 warn $error;
228 0 warn
229 "Automatically upgrading your database to match the current plugin schema\n";
230 0 $self->_upgrade_schema('print' => $opts{pretend});
231 } else {
232 0 die "${error}Please run `bin/jifty schema --setup` to upgrade the database.\n";
233 }
234 }
235 }
236  
237 }
238  
239 =head2 create_database MODE
240  
241 C<MODE> is either "print" or "execute".
242  
243 This method either prints the commands necessary to create the
244 database or actually creates it, depending on the value of MODE.
245 Returns undef on failure.
246  
247 =cut
248  
249 0 ▶sub create_database {
Subroutine create_database: not called CC=5 SCAR=34.0
Pod documented
250 0 my $self = shift;
251 0 ▶ my $mode = shift || 'execute';
Condition0%0/2
shift() || 'execute'
l!l
00
MC/DC0%0/1
shift() || 'execute'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
252 0 my $database = $self->canonical_database_name;
253 0 my $driver = Jifty->config->framework('Database')->{'Driver'};
254 0 my $query = "CREATE DATABASE $database";
255 0 ▶ $query .= " TEMPLATE template0" if $driver =~ /Pg/;
Branch: 1 branch
BranchTrueFalseTotal
if $driver =~ /Pg/ 0 0 0
256 0 ▶ if ( $mode eq 'print' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'print') { } 0 0 0
elsif ($driver =~ /SQLite/) { } 0 0 0
257 0 print "$query;\n";
258 } elsif ( $driver =~ /SQLite/ ) {
259 0 return 1; # Kinda an optimistic hack
260 } else {
261 0 return $self->simple_query($query);
262 }
263 }
264  
265 =head2 drop_database MODE
266  
267 C<MODE> is either "print" or "execute".
268  
269 This method either prints the commands necessary to drop the database
270 or actually drops it, depending on the value of MODE. Returns undef
271 on failure.
272  
273 =cut
274  
275 0 ▶sub drop_database {
Subroutine drop_database: not called CC=5 SCAR=34.0
Pod documented
276 0 my $self = shift;
277 0 ▶ my $mode = shift || 'execute';
Condition0%0/2
shift() || 'execute'
l!l
00
MC/DC0%0/1
shift() || 'execute'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
278 0 my $database = $self->canonical_database_name;
279 0 my $driver = Jifty->config->framework('Database')->{'Driver'};
280 0 ▶ if ( $mode eq 'print' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'print') { } 0 0 0
elsif ($driver =~ /SQLite/) { } 0 0 0
281 0 print "DROP DATABASE $database;\n";
282 } elsif ( $driver =~ /SQLite/ ) {
283  
284 # Win32 complains when you try to unlink open DB
285 0 ▶ $self->disconnect if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
286 0 return unlink($database);
287 } else {
288 local $SIG{__WARN__}
289 0 ▶ = sub { warn $_[0] unless $_[0] =~ /exist|couldn't execute/i };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /exist|couldn't execute/i 0 0 0
290 0 return $self->simple_query("DROP DATABASE $database");
291 }
292 }
293  
294 0 ▶sub _create_original_database {
Subroutine _create_original_database: not called CC=1 SCAR=6.9
295 0 my $self = shift;
296  
297 0 my $hack = {};
298 0 require Jifty::Script::Schema;
299 0 bless $hack, "Jifty::Script::Schema";
300 0 $hack->create_all_tables;
301  
302 # reconnect for consistency
303 # SQLite complains about the schema being changed
304 0 $self->disconnect;
305 0 $self->connect;
306 }
307  
308 0 ▶sub _upgrade_schema {
Subroutine _upgrade_schema: not called CC=1 SCAR=6.9
309 0 my $self = shift;
310 0 my $hack = { @_ };
311 0 require Jifty::Script::Schema;
312 0 bless $hack, "Jifty::Script::Schema";
313 0 $hack->run_upgrades;
314 }
315  
316 0 ▶sub _fetch_dbv {
Subroutine _fetch_dbv: not called CC=3 SCAR=24.8
317 0 my $self = shift;
318  
319 0 my $dbv = Jifty::Model::Metadata->load("application_db_version");
320 0 ▶ return $dbv if defined $dbv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbv 0 0 0
321  
322 # First layer of backwards compatibility -- it used to be in _db_version
323 0 eval {
324 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 0 my @v = Jifty->handle->fetch_result(
326 "SELECT major, minor, rev FROM _db_version");
327 0 ▶ return join( ".", @v ) if @v == 3;
Branch: 1 branch
BranchTrueFalseTotal
if @v == 3 0 0 0
328 };
329  
330 # Second layer -- it was also called the 'key' column, not the data_key column
331 0 eval {
332 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
333 0 return Jifty->handle->fetch_result(
334 "SELECT value FROM _jifty_metadata WHERE key = 'application_db_version'"
335 );
336 };
337  
338 # most likely no database exists
339 0 return undef;
340 }
341  
342 =head1 AUTHOR
343  
344 Various folks at BestPractical Solutions, LLC.
345  
346 =cut
347  
348 113 1;
« Jifty/Web.pm ↑ Summary Jifty/Script/Adopt.pm »