Backend/SQL.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 93.6% 88 / 94 CC 4 SCAR 4.7
CC
4
Cov
94%
_deploy_schema
3.0
_build_dbis
2.0
__ANON__
2.0
CRAP 1.6
« Backend/SQLite.pm ↑ Summary Archive/SQL.pm »
1 9 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Backend::SQL;
6 # ABSTRACT: Metabase backend role for SQL-based backends
7  
8 9 our $VERSION = '1.001';
9  
10 9 ▶use Class::Load qw/load_class try_load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use SQL::Translator::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Storable qw/nfreeze/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 has [qw/dsn db_user db_pass db_type/] => (
17 is => 'ro',
18 isa => 'Str',
19 lazy_build => 1,
20 );
21  
22 9 has dbis => (
23 is => 'ro',
24 isa => 'DBIx::Simple',
25 lazy_build => 1,
26 handles => [qw/dbh/],
27 );
28  
29 9 has schema => (
30 is => 'ro',
31 isa => 'SQL::Translator::Schema',
32 lazy_build => 1,
33 );
34  
35 9 has '_blob_field_params' => (
36 is => 'ro',
37 isa => 'HashRef',
38 lazy_build => 1,
39 );
40  
41 9 has '_guid_field_params' => (
42 is => 'ro',
43 isa => 'HashRef',
44 lazy_build => 1,
45 );
46  
47 #--------------------------------------------------------------------------#
48 # to be implemented by Metabase::Backend::${DBNAME}
49 #--------------------------------------------------------------------------#
50  
51 9 requires '_build_dsn';
52 9 requires '_build_db_user';
53 9 requires '_build_db_pass';
54 9 requires '_build_db_type';
55 9 requires '_fixup_sql_diff';
56 9 requires '_build__blob_field_params';
57 9 requires '_build__guid_field_params';
58 9 requires '_munge_guid';
59 9 requires '_unmunge_guid';
60  
61  
62 #--------------------------------------------------------------------------#
63  
64 239 ▶sub _build_dbis {
Subroutine _build_dbis: called CC=2 SCAR=7.1
65 239 my ($self) = @_;
66 239 my @connect = map { $self->$_ } qw/dsn db_user db_pass/;
67 239 my $dbis = eval { DBIx::Simple->connect(@connect, {PrintWarn => 0}) }
68 0 ▶ or die "Could not connect via " . join(":",map { qq{'$_'} } @connect[0,1],"...")
Branch: 1 branch
BranchTrueFalseTotal
my $dbis = eval { do { "DBIx::Simple"->connect(@connect, {"PrintWarn", 0}) } } or die 'Could not connect via ' . join(":", map({"'${_}'";} @connect[0, 1], "...")) . " because: $@\n" 239 0 239
69 . " because: $@\n";
70 239 return $dbis;
71 }
72  
73 79 ▶sub _build_schema {
Subroutine _build_schema: called CC=1 SCAR=0.0
74 79 my $self = shift;
75 79 return SQL::Translator::Schema->new(
76 name => 'Metabase',
77 database => $self->db_type,
78 );
79 }
80  
81 80 ▶sub _deploy_schema {
Subroutine _deploy_schema: called CC=3 SCAR=11.0
82 80 my ($self) = @_;
83  
84 80 my $schema = $self->schema;
85  
86 # Blow up if this doesn't seem OK
87 80 ▶ $schema->is_valid or die "Could not validate schema: $schema->error";
Branch: 1 branch
BranchTrueFalseTotal
$schema->is_valid or die "Could not validate schema: $schema->error" 80 0 80
88 # use Data::Dumper;
89 # warn "Schema: " . Dumper($schema);
90  
91 80 my $db_type = $self->db_type;
92 # See what we already have
93 80 my $existing = SQL::Translator->new(
94 parser => 'DBI',
95 parser_args => {
96 dbh => $self->dbh,
97 },
98 producer => $db_type,
99 show_warnings => 0, # suppress warning from empty DB
100 );
101 {
102 # shut up P::RD when there is no text -- the SQL::Translator parser
103 # forces things on when loaded. Gross.
104 9 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 80 load_class( "SQL::Translator::Parser::" . $db_type );
106 80 load_class( "SQL::Translator::Producer::" . $db_type );
107 80 local *main::RD_ERRORS;
108 80 local *main::RD_WARN;
109 80 local *main::RD_HINT;
110 80 my $existing_sql = $existing->translate();
111 # warn "*** Existing schema: " . $existing_sql;
112 }
113  
114 # Convert our target schema
115 80 my $fake = SQL::Translator->new(
116 parser => 'Storable',
117 producer => $db_type,
118 );
119 80 my $fake_sql = $fake->translate( \( nfreeze($schema) ) );
120 # warn "*** Fake schema: $fake_sql";
121  
122 80 my $diff = SQL::Translator::Diff::schema_diff(
123 $existing->schema, $db_type, $fake->schema, $db_type
124 );
125  
126 80 $diff = $self->_fixup_sql_diff($diff);
127  
128 # DBIx::RunSQL requires a file (ugh)
129 80 my ($fh, $sqlfile) = File::Temp::tempfile();
130 80 print {$fh} $diff;
131 80 close $fh;
132 # warn "*** Schema Diff:\n$diff\n"; # XXX
133  
134 80 $self->clear_dbis; # ensure we re-initailize handle
135 80 ▶ unless ( $diff =~ /-- No differences found/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diff =~ /-- No differences found/i) 80 0 80
136 DBIx::RunSQL->create(
137 dbh => $self->dbh,
138 sql => $sqlfile,
139 0 ▶ verbose_handler => sub { return },
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 80 );
141 80 $self->dbh->disconnect;
142 }
143  
144 # must reset the connection
145 80 $self->clear_dbis;
146 80 $self->dbis; # rebuild
147  
148 # my ($count) = $self->dbis->query(qq{select count(*) from "core"})->list;
149 # warn "Initialized with $count records";
150 80 return;
151 }
152  
153 9 1;
154  
155  
156 # vim: ts=2 sts=2 sw=2 et:
157  
158 __END__
« Backend/SQLite.pm ↑ Summary Archive/SQL.pm »