blib/lib/GitDDL.pm

filter: Statementstmt 35.1% 45 / 128 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 46.1% 12 / 26 Podpod 100% 6 / 6 totaltotal 30.8% 63 / 204 CC 25 SCAR 33.1
CC
25
Cov
26%
diff
72.0
_build_dbh
72.0
deploy
20.0
CRAP 27.3
↑ Summary
1 package GitDDL;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.03';
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use SQL::Translator;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use SQL::Translator::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has work_tree => (
19 is => 'ro',
20 required => 1,
21 );
22  
23 1 has ddl_file => (
24 is => 'ro',
25 required => 1,
26 );
27  
28 1 has dsn => (
29 is => 'ro',
30 required => 1,
31 );
32  
33 1 has sql_filter => (
34 is => 'rw',
35 lazy => 1,
36 builder => '_build_sql_filter',
37 );
38  
39 1 has version_table => (
40 is => 'rw',
41 default => 'git_ddl_version',
42 );
43  
44 1 has _dbh => (
45 is => 'rw',
46 lazy => 1,
47 builder => '_build_dbh',
48 );
49  
50 1 has _git => (
51 is => 'rw',,
52 lazy => 1,
53 builder => '_build_git',
54 );
55  
56 1 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 0 ▶sub check_version {
Subroutine check_version: not called CC=1 SCAR=6.9
Pod documented
59 0 my ($self) = @_;
60 0 $self->database_version eq $self->ddl_version;
61 }
62  
63 0 ▶sub database_version {
Subroutine database_version: not called CC=3 SCAR=24.8
Pod documented
64 0 my ($self) = @_;
65  
66 0 ▶ croak sprintf 'invalid version_table: %s', $self->version_table
Branch: 1 branch
BranchTrueFalseTotal
unless $self->version_table =~ /^[a-zA-Z_]+$/ 0 0 0
67 unless $self->version_table =~ /^[a-zA-Z_]+$/;
68  
69 0 my ($version) =
70 $self->_dbh->selectrow_array('SELECT version FROM ' . $self->version_table);
71  
72 0 ▶ if (defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) { } 0 0 0
73 0 return $version;
74 }
75 else {
76 0 croak "Failed to get database version, please deploy first";
77 }
78 }
79  
80 0 ▶sub ddl_version {
Subroutine ddl_version: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($self) = @_;
82 0 $self->_git->run('log', '-n', '1', '--pretty=format:%H', '--', $self->ddl_file);
83 }
84  
85 0 ▶sub deploy {
Subroutine deploy: not called CC=4 SCAR=30.0
Pod documented
86 0 my ($self) = @_;
87  
88 0 ▶ my $version = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 open my $fh, '>', \my $stderr;
90 0 local *STDERR = $fh;
91 0 $self->database_version;
92 0 close $fh;
93 0 };
94  
95 0 ▶ if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 0 0 0
96 0 croak "database already deployed, use upgrade_database instead";
97 }
98  
99 0 ▶ croak sprintf 'invalid version_table: %s', $self->version_table
Branch: 1 branch
BranchTrueFalseTotal
unless $self->version_table =~ /^[a-zA-Z_]+$/ 0 0 0
100 unless $self->version_table =~ /^[a-zA-Z_]+$/;
101  
102 0 $self->_do_sql($self->_slurp(File::Spec->catfile($self->work_tree, $self->ddl_file)));
103  
104 0 $self->_do_sql(<<"__SQL__");
105 0 CREATE TABLE @{[ $self->version_table ]} (
106 version VARCHAR(40) NOT NULL
107 );
108 __SQL__
109  
110 0 ▶ $self->_dbh->do(
Branch: 1 branch
BranchTrueFalseTotal
$self->_dbh->do("INSERT INTO @{[$self->version_table];} (version) VALUES (?)", {}, $self->ddl_version) or croak($self->_dbh->errstr) 0 0 0
111 0 "INSERT INTO @{[ $self->version_table ]} (version) VALUES (?)", {}, $self->ddl_version
112 ) or croak $self->_dbh->errstr;
113 }
114  
115 0 ▶sub diff {
Subroutine diff: not called CC=8 SCAR=42.8
Pod documented
116 0 my ($self) = @_;
117  
118 0 ▶ if ($self->check_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_version) 0 0 0
119 0 croak 'ddl_version == database_version, should no differences';
120 }
121  
122 0 my $dsn0 = $self->dsn->[0];
123 my $db
124 = $dsn0 =~ /:mysql:/ ? 'MySQL'
125 : $dsn0 =~ /:Pg:/ ? 'PostgreSQL'
126 0 ▶ : do { my ($d) = $dsn0 =~ /dbi:(.*?):/; $d };
Branch: 2 branches
BranchTrueFalseTotal
$dsn0 =~ /:mysql:/ ? : 0 0 0
$dsn0 =~ /:Pg:/ ? : 0 0 0
127  
128 0 my $tmp_fh = File::Temp->new;
129 0 $self->_dump_sql_for_specified_commit($self->database_version, $tmp_fh->filename);
130  
131 0 my $source_sql = $self->sql_filter->($self->_slurp($tmp_fh->filename));
132 0 my $source = SQL::Translator->new;
133 0 ▶ $source->parser($db) or croak $source->error;
Branch: 1 branch
BranchTrueFalseTotal
$source->parser($db) or croak($source->error) 0 0 0
134 0 ▶ $source->translate(\$source_sql) or croak $source->error;
Branch: 1 branch
BranchTrueFalseTotal
$source->translate(\$source_sql) or croak($source->error) 0 0 0
135  
136 0 my $target_sql = $self->sql_filter->(
137 $self->_slurp(File::Spec->catfile($self->work_tree, $self->ddl_file))
138 );
139 0 my $target = SQL::Translator->new;
140 0 ▶ $target->parser($db) or croak $target->error;
Branch: 1 branch
BranchTrueFalseTotal
$target->parser($db) or croak($target->error) 0 0 0
141 0 ▶ $target->translate(\$target_sql) or croak $target->error;
Branch: 1 branch
BranchTrueFalseTotal
$target->translate(\$target_sql) or croak($target->error) 0 0 0
142  
143 0 my $diff = SQL::Translator::Diff->new({
144 output_db => $db,
145 source_schema => $source->schema,
146 target_schema => $target->schema,
147 })->compute_differences->produce_diff_sql;
148  
149 # ignore first line
150 0 $diff =~ s/.*?\n//;
151  
152 0 $diff
153 }
154  
155 0 ▶sub upgrade_database {
Subroutine upgrade_database: not called CC=2 SCAR=17.9
Pod documented
156 0 my ($self) = @_;
157  
158 0 $self->_do_sql($self->diff);
159  
160 0 ▶ $self->_dbh->do(
Branch: 1 branch
BranchTrueFalseTotal
$self->_dbh->do("UPDATE @{[$self->version_table];} SET version = ?", {}, $self->ddl_version) or croak($self->_dbh->errstr) 0 0 0
161 0 "UPDATE @{[ $self->version_table ]} SET version = ?", {}, $self->ddl_version
162 ) or croak $self->_dbh->errstr;
163 }
164  
165 0 ▶sub _build_dbh {
Subroutine _build_dbh: not called CC=8 SCAR=42.8
166 0 my ($self) = @_;
167  
168 # support on_connect_do
169 0 my $on_connect_do;
170 0 ▶ if (ref $self->dsn->[-1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->dsn->[-1] eq "HASH") 0 0 0
171 0 $on_connect_do = delete $self->dsn->[-1]{on_connect_do};
172 }
173  
174 0 ▶ my $dbh = DBI->connect(@{ $self->dsn })
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect(@{$self->dsn;}) or croak($DBI::errstr) 0 0 0
175 or croak $DBI::errstr;
176  
177 0 ▶ if ($on_connect_do) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_connect_do) 0 0 0
178 0 ▶ if (ref $on_connect_do eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $on_connect_do eq 'ARRAY') { } 0 0 0
179 $dbh->do($_) || croak $dbh->errstr
180 0 ▶ for @$on_connect_do;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($_) or croak($dbh->errstr) 0 0 0
181 }
182 else {
183 0 ▶ $dbh->do($on_connect_do) or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($on_connect_do) or croak($dbh->errstr) 0 0 0
184 }
185 }
186  
187 0 $dbh;
188 }
189  
190 0 ▶sub _build_git {
Subroutine _build_git: not called CC=1 SCAR=6.9
191 0 my ($self) = @_;
192 0 Git::Repository->new( work_tree => $self->work_tree );
193 }
194  
195 0 ▶sub _build_sql_filter {
Subroutine _build_sql_filter: not called CC=1 SCAR=6.9
196 0 my ($self) = @_;
197 0 ▶ sub { shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 }
199  
200 0 ▶sub _do_sql {
Subroutine _do_sql: not called CC=3 SCAR=24.8
201 0 my ($self, $sql) = @_;
202  
203 0 my @statements = map { "$_;" } grep { /\S+/ } split ';', $sql;
204 0 for my $statement (@statements) {
205 0 ▶ $self->_dbh->do($statement)
Branch: 1 branch
BranchTrueFalseTotal
$self->_dbh->do($statement) or croak($self->_dbh->errstr) 0 0 0
206 or croak $self->_dbh->errstr;
207 }
208 }
209  
210 0 ▶sub _slurp {
Subroutine _slurp: not called CC=2 SCAR=17.9
211 0 my ($self, $file) = @_;
212  
213 0 ▶ open my $fh, '<', $file or croak sprintf 'Cannot open file: %s, %s', $file, $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak(sprintf('Cannot open file: %s, %s', $file, $!)) 0 0 0
214 0 my $data = do { local $/; <$fh> };
215 0 close $fh;
216  
217 0 $data;
218 }
219  
220 0 ▶sub _dump_sql_for_specified_commit {
Subroutine _dump_sql_for_specified_commit: not called CC=2 SCAR=17.9
221 0 my ($self, $commit_hash, $outfile) = @_;
222  
223 0 my ($mode, $type, $blob_hash) = split /\s+/, scalar $self->_git->run(
224 'ls-tree', $commit_hash, '--', $self->ddl_file,
225 );
226  
227 0 my $sql = $self->_git->run('cat-file', 'blob', $blob_hash);
228  
229 0 ▶ open my $fh, '>', $outfile or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $outfile or croak($!) 0 0 0
230 0 print $fh $sql;
231 0 close $fh;
232 }
233  
234 1 __PACKAGE__->meta->make_immutable;
235  
236 __END__
↑ Summary