| 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
| |||||||||||
| 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
| |||||||||||
| 37 | 0 | $dbh->do("ALTER TABLE tracks ADD COLUMN $col TEXT"); | |||||||||
| 38 | } | ||||||||||
| 39 | 36 | ▶ | unless ($existing{metadata_fetched}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 45 | |||||||||||
| 46 | 36 | return $schema; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 2 | 1; | |||||||||
| 50 | |||||||||||
| 51 | __END__ | ||||||||||