Make/DBI/Schema.pm

filter: Statementstmt 87.7% 50 / 57 Branchbran 25.0% 1 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 86.3% 63 / 73 CC 8 SCAR 13.7
CC
8
Cov
84%
make
8.3
_cache_hash
3.0
BEGIN
1.0
CRAP 3.9
« Make/Recipe.pm ↑ Summary Make/File.pm »
1 package Beam::Make::DBI::Schema;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: A Beam::Make recipe to build database schemas
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod ### container.yml
8 #pod # A Beam::Wire container to configure a database connection to use
9 #pod sqlite:
10 #pod $class: DBI
11 #pod $method: connect
12 #pod $args:
13 #pod - dbi:SQLite:conversion.db
14 #pod
15 #pod ### Beamfile
16 #pod conversion.db:
17 #pod $class: Beam::Wire::DBI::Schema
18 #pod dbh: { $ref: 'container.yml:sqlite' }
19 #pod schema:
20 #pod - table: accounts
21 #pod columns:
22 #pod - account_id: VARCHAR(255) NOT NULL PRIMARY KEY
23 #pod - address: TEXT NOT NULL
24 #pod
25 #pod =head1 DESCRIPTION
26 #pod
27 #pod This L<Beam::Make> recipe class builds a database schema.
28 #pod
29 #pod =head1 SEE ALSO
30 #pod
31 #pod L<Beam::Make>, L<Beam::Wire>, L<DBI>
32 #pod
33 #pod =cut
34  
35 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use List::Util qw( pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Digest::SHA qw( sha1_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use experimental qw( signatures postderef );
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 extends 'Beam::Make::Recipe';
44  
45 #pod =attr dbh
46 #pod
47 #pod Required. The L<DBI> database handle to use. Can be a reference to a service
48 #pod in a L<Beam::Wire> container using C<< { $ref: "<container>:<service>" } >>.
49 #pod
50 #pod =cut
51  
52 2 has dbh => ( is => 'ro', required => 1 );
53  
54 #pod =attr schema
55 #pod
56 #pod A list of tables to create. Each table is a mapping with the following keys:
57 #pod
58 #pod =over
59 #pod
60 #pod =item table
61 #pod
62 #pod The name of the table to create.
63 #pod
64 #pod =item columns
65 #pod
66 #pod A list of key/value pairs of columns. The key is the column name, the value
67 #pod is the SQL to use for the column definition.
68 #pod
69 #pod =back
70 #pod
71 #pod =cut
72  
73 2 has schema => ( is => 'ro', required => 1 );
74  
75 1 ▶sub make( $self, %vars ) {
Subroutine make: called CC=6 SCAR=21.2
Pod documented
76 1 my $dbh = $self->dbh;
77  
78 # Now, prepare the changes to be made
79 1 my @changes;
80 1 for my $table_schema ( $self->schema->@* ) {
81 2 my $table = $table_schema->{table};
82 2 my @columns = $table_schema->{columns}->@*;
83 2 my $table_info = $dbh->table_info( '', '%', qq{$table} )->fetchrow_arrayref;
84 2 ▶ if ( !$table_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $table_info) { } 2 0 2
85 push @changes, sprintf 'CREATE TABLE %s ( %s )', $dbh->quote_identifier( $table ),
86 5 join ', ', map { join ' ', $dbh->quote_identifier( $_->key ), $_->value }
87 2 map { pairs %$_ } @columns;
88 }
89 else {
90 0 my $column_info = $dbh->column_info( '', '%', $table, '%' )->fetchall_hashref( 'COLUMN_NAME' );
91 # Compare columns and add if needed
92 0 for my $pair ( map { pairs %$_ } @columns ) {
93 0 my $column_name = $pair->key;
94 0 my $column_type = $pair->value;
95 0 ▶ if ( !$column_info->{ $column_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column_info->{$column_name}) 0 0 0
96 0 push @changes, sprintf 'ALTER TABLE %s ADD COLUMN %s %s',
97 $table, $column_name, $column_type;
98 }
99 }
100 }
101 }
102  
103 # Now execute the changes
104 1 for my $change ( @changes ) {
105 2 $dbh->do( $change );
106 }
107  
108 1 $self->cache->set( $self->name, $self->_cache_hash );
109 1 return 0;
110 }
111  
112 10 ▶sub _cache_hash( $self ) {
Subroutine _cache_hash: called CC=3 SCAR=11.0
113 10 my $dbh = $self->dbh;
114 10 my %tables;
115 10 for my $table_info ( $dbh->table_info( '', '%', '%' )->fetchall_arrayref( {} )->@* ) {
116 47 my $table_name = $table_info->{TABLE_NAME};
117 47 for my $column_info ( $dbh->column_info( '', '%', $table_name, '%' )->fetchall_arrayref( {} )->@* ) {
118 163 my $column_name = $column_info->{COLUMN_NAME};
119 163 push $tables{ $table_name }->@*, $column_name;
120 }
121 }
122 my $content = join ';',
123 10 map { sprintf '%s=%s', $_, join ',', sort $tables{ $_ }->@* } sort keys %tables;
124 10 return sha1_base64( $content );
125 }
126  
127 9 ▶sub last_modified( $self ) {
Subroutine last_modified: called CC=1 SCAR=0.0
Pod documented
128 9 return $self->cache->last_modified( $self->name, $self->_cache_hash );
129 }
130  
131 2 1;
132  
133 __END__
« Make/Recipe.pm ↑ Summary Make/File.pm »