lib/App/DrivePlayer/Schema.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 87.5% 35 / 40 CC 6 SCAR 15.2
CC
6
Cov
86%
connect_and_deploy
6.3
BEGIN
1.0
BEGIN
1.0
CRAP 4.6
« lib/App/DrivePlayer/Config.pm ↑ Summary lib/App/DrivePlayer/Schema/Result/Folder.pm »
1 package App::DrivePlayer::Schema;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base 'DBIx::Class::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 __PACKAGE__->load_namespaces();
8  
9 # Connect to a SQLite database at $path, configure pragmas, and deploy
10 # the schema if the tables do not yet exist.
11 36 ▶sub connect_and_deploy {
Subroutine connect_and_deploy: called CC=6 SCAR=18.4
Pod documented
12 36 my ($class, $path) = @_;
13  
14 36 my $schema = $class->connect(
15 "dbi:SQLite:dbname=$path", '', '',
16 {
17 sqlite_unicode => 1,
18 on_connect_do => [
19 'PRAGMA journal_mode=WAL',
20 'PRAGMA foreign_keys=ON',
21 ],
22 },
23 );
24  
25 # Deploy only when the database is new (tracks table absent)
26 36 my $dbh = $schema->storage->dbh;
27 36 my @tables = $dbh->tables(undef, undef, 'tracks', 'TABLE');
28 36 ▶ unless (@tables) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tables) 35 1 36
29 35 $schema->deploy({ add_drop_table => 0 });
30 }
31  
32 # Migrate: add columns introduced after initial deploy
33 576 my %existing = map { $_->[1] => 1 }
34 36 @{ $dbh->selectall_arrayref('PRAGMA table_info(tracks)') };
35 36 for my $col (qw( genre comment )) {
36 72 ▶ next if $existing{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $existing{$col} 72 0 72
37 0 $dbh->do("ALTER TABLE tracks ADD COLUMN $col TEXT");
38 }
39 36 ▶ unless ($existing{metadata_fetched}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing{"metadata_fetched"}) 0 36 36
40 0 $dbh->do('ALTER TABLE tracks ADD COLUMN metadata_fetched INTEGER NOT NULL DEFAULT 0');
41 }
42  
43 # Migrate: drop the unused composer column (SQLite >= 3.35)
44 36 ▶ $dbh->do('ALTER TABLE tracks DROP COLUMN composer') if $existing{composer};
Branch: 1 branch
BranchTrueFalseTotal
if $existing{"composer"} 0 36 36
45  
46 36 return $schema;
47 }
48  
49 2 1;
50  
51 __END__
« lib/App/DrivePlayer/Config.pm ↑ Summary lib/App/DrivePlayer/Schema/Result/Folder.pm »