MigrationDBIx.pm

filter: Statementstmt 83.0% 98 / 118 Branchbran 56.2% 36 / 64 Conditioncond 42.8% 12 / 28 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 100% 12 / 12 Podpod 33.3% 1 / 3 totaltotal 66.5% 161 / 242 CC 52 SCAR 32.8
CC
52
Cov
70%
_eq_deep
100.0
_sig_diff_source
17.1
_sig_diff
10.4
CRAP 26.7
« MigrationDBIx/Command/db.pm ↑ Summary
1 package Mojolicious::Plugin::Fondation::MigrationDBIx;
2 8 $Mojolicious::Plugin::Fondation::MigrationDBIx::VERSION = '0.06';
3 # ABSTRACT: Migration and fixture management for DBIx::Class backends
4  
5 8 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 ▶sub fondation_meta {
Subroutine fondation_meta: called CC=1 SCAR=0.0
Pod undocumented
11 return {
12 28 dependencies => ['Fondation::Model::DBIx::Async'],
13 defaults => {
14 title => 'DBIx Database Migration',
15 fondation_init => [
16 ['db', 'prepare', '-y'],
17 ['db', 'install'],
18 ['db', 'populate'],
19 ],
20 fondation_upgrade => [
21 ['db', 'prepare', '-y', '-a'],
22 ['db', 'upgrade'],
23 ['db', 'populate'],
24 ],
25 fondation_clean => [
26 'share/fixtures',
27 'share/migrations',
28 ],
29 },
30 };
31 }
32  
33 28 ▶sub register ($self, $app, $config) {
Subroutine register: called CC=2 SCAR=7.0
Pod documented
34  
35 28 my $backend_name = $config->{backend};
36  
37 my $migrations_dir = $config->{migrations_dir}
38 28 ▶ // $app->home->child('share', 'migrations')->to_string;
Condition33%1/3
$config->{"migrations_dir"} // $app->home->child("share", "migrations")->to_string
l!l&&r!l&&!r
0280
MC/DC0%0/2
$config->{"migrations_dir"} // $app->home->child("share", "migrations")->to_string
$config->{"migrations_dir"} $app->home->child("share", "migrations")->to_string
Truth table33%1/3
A // B
A= $config->{"migrations_dir"}B= $app->home->child("share", "migrations")->to_string
ABresult
1X1
011
000
39  
40 28 my $sig_file = path($migrations_dir)->child('.schema-sig.json')->to_string;
41  
42 28 $app->defaults('migration_dbix.config' => {
43 backend => $backend_name,
44 migrations_dir => $migrations_dir,
45 sig_file => $sig_file,
46 });
47  
48 28 push @{$app->commands->namespaces},
49 'Mojolicious::Plugin::Fondation::MigrationDBIx::Command';
50  
51 # Helper: schema_drift() -- detect schema changes since last prepare
52 48 ▶ $app->helper(schema_drift => sub ($c) {
Subroutine __ANON__: called CC=6 SCAR=18.1
53 48 my $cfg = $app->defaults->{'migration_dbix.config'};
54  
55 # Build a native schema (no async workers) for inspection
56 48 require Mojolicious::Plugin::Fondation::MigrationDBIx::Command::db;
57 48 my $native = Mojolicious::Plugin::Fondation::MigrationDBIx::Command::db
58 ->_build_native_schema($app, $cfg);
59 44 ▶ return unless $native;
Branch: 1 branch
BranchTrueFalseTotal
unless $native 1 43 44
60  
61 43 my $live_sig = $c->schema_sig($native);
62 43 my $stored = $self->_load_sig($cfg->{sig_file});
63  
64 # Disconnect the native schema -- we only needed it for inspection
65 43 ▶ $native->storage->disconnect if $native->storage;
Branch: 1 branch
BranchTrueFalseTotal
if $native->storage 43 0 43
66  
67 43 ▶ return { has_drift => 0, version => $stored->{version} // '?' }
Branch: 1 branch
BranchTrueFalseTotal
unless $stored 36 7 43
Condition50%1/2
$stored->{"version"} // "?"
l!l
036
MC/DC0%0/1
$stored->{"version"} // "?"
$stored->{"version"}
Truth table50%1/2
A
A= $stored->{"version"}
Aresult
11
00
68 unless $stored;
69  
70 7 my $changes = $self->_sig_diff($stored->{sources}, $live_sig);
71  
72 return {
73 has_drift => !!(%$changes),
74 7 ▶ version => $stored->{version} // '?',
Condition50%1/2
$stored->{"version"} // "?"
l!l
70
MC/DC0%0/1
$stored->{"version"} // "?"
$stored->{"version"}
Truth table50%1/2
A
A= $stored->{"version"}
Aresult
11
00
75 changes => $changes,
76 };
77 28 });
78  
79 28 return $self;
80 }
81  
82 25 ▶sub fondation_finalyze ($self, $app, $long_name) {
Subroutine fondation_finalyze: called CC=4 SCAR=17.3
Pod undocumented
83 25 ▶ return 1 unless $app->has_helper('schema_drift');
Branch: 1 branch
BranchTrueFalseTotal
unless $app->has_helper("schema_drift") 0 25 25
84  
85 25 my $c = $app->build_controller;
86 25 my $drift = eval { $c->schema_drift };
87 25 ▶ return 1 unless $drift && $drift->{has_drift};
Branch: 1 branch
BranchTrueFalseTotal
unless $drift and $drift->{"has_drift"} 25 0 25
Condition66%2/3
$drift and $drift->{"has_drift"}
!ll&&!rl&&r
5200
MC/DC0%0/2
$drift and $drift->{"has_drift"}
$drift $drift->{"has_drift"}
Truth table66%2/3
A and B
A= $driftB= $drift->{"has_drift"}
ABresult
0X0
100
111
88  
89 0 my @changed = sort keys %{$drift->{changes}};
90 0 $app->log->warn(sprintf(
91 '[Fondation::MigrationDBIx] Schema drifted in sources: %s',
92 join(', ', @changed)));
93 $app->log->warn(sprintf(
94 '[Fondation::MigrationDBIx] Version %s. Run: db prepare -a && db upgrade',
95 0 $drift->{version}));
96 0 return 1;
97 }
98  
99 # Load the stored schema signature from disk
100 43 ▶sub _load_sig ($self, $sig_file) {
Subroutine _load_sig: called CC=5 SCAR=16.7
101 43 ▶ return undef unless -f $sig_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $sig_file 36 7 43
102 7 my $data = eval { path($sig_file)->slurp };
103 7 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 7 7
104 7 my $sig = eval { decode_json($data) };
105 7 ▶ return undef unless $sig && $sig->{sources};
Branch: 1 branch
BranchTrueFalseTotal
unless $sig and $sig->{"sources"} 0 7 7
Condition33%1/3
$sig and $sig->{"sources"}
!ll&&!rl&&r
007
MC/DC0%0/2
$sig and $sig->{"sources"}
$sig $sig->{"sources"}
Truth table33%1/3
A and B
A= $sigB= $sig->{"sources"}
ABresult
0X0
100
111
106 7 return $sig;
107 }
108  
109 # Compare two schema signatures and return changed sources
110 7 ▶sub _sig_diff ($self, $stored_sources, $live_sources) {
Subroutine _sig_diff: called CC=10 SCAR=23.4
111 7 my $changes = {};
112  
113 # Sources in live but not stored -> new tables
114 7 for my $name (keys %$live_sources) {
115 8 ▶ unless (exists $stored_sources->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stored_sources->{$name}) 1 7 8
116 1 $changes->{$name} = 'new';
117 }
118 }
119  
120 # Sources in stored but not live -> removed tables
121 7 for my $name (keys %$stored_sources) {
122 7 ▶ unless (exists $live_sources->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $live_sources->{$name}) 0 7 7
123 0 $changes->{$name} = 'removed';
124 }
125 }
126  
127 # Sources in both -> check column-level changes
128 7 for my $name (keys %$live_sources) {
129 8 ▶ next if $changes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $changes->{$name} 1 7 8
130 7 ▶ next unless $stored_sources->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $stored_sources->{$name} 0 7 7
131 my $diff = $self->_sig_diff_source(
132 7 $stored_sources->{$name}, $live_sources->{$name});
133 7 ▶ $changes->{$name} = $diff if $diff && _source_diff_has_changes($diff);
Branch: 1 branch
BranchTrueFalseTotal
if $diff and _source_diff_has_changes($diff) 3 4 7
Condition66%2/3
$diff and _source_diff_has_changes($diff)
!ll&&!rl&&r
043
MC/DC50%1/2
$diff and _source_diff_has_changes($diff)
$diff _source_diff_has_changes($diff)
Truth table66%2/3
A and B
A= $diffB= _source_diff_has_changes($diff)
ABresult
0X0
100
111
134 }
135  
136 7 return $changes;
137 }
138  
139 # Compare a single source between stored and live
140 7 ▶sub _sig_diff_source ($self, $stored, $live) {
Subroutine _sig_diff_source: called CC=15 SCAR=28.4
141 7 my $diff = { added => [], removed => [], modified => {} };
142  
143 7 my %stored_cols = map { $_->{name} => $_ } @{$stored->{columns}};
144 7 my %live_cols = map { $_->{name} => $_ } @{$live->{columns}};
145  
146 # Added columns
147 7 for my $name (sort keys %live_cols) {
148 17 ▶ push @{$diff->{added}}, $name unless exists $stored_cols{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stored_cols{$name} 3 14 17
149 }
150  
151 # Removed columns
152 7 for my $name (sort keys %stored_cols) {
153 0 ▶ push @{$diff->{removed}}, $name unless exists $live_cols{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $live_cols{$name} 0 14 14
154 }
155  
156 # Modified columns
157 7 for my $name (sort keys %live_cols) {
158 17 ▶ next unless exists $stored_cols{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stored_cols{$name} 3 14 17
159 14 my $s = $stored_cols{$name};
160 14 my $l = $live_cols{$name};
161 14 my %mod;
162 14 for my $attr (qw(data_type is_nullable is_auto_increment
163 default_value size is_foreign_key))
164 {
165 84 my $sv = $s->{$attr};
166 84 my $lv = $l->{$attr};
167 84 ▶ next if !defined($sv) && !defined($lv);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sv or defined $lv 42 42 84
Condition66%2/3
defined $sv or defined $lv
l!l&&r!l&&!r
42042
MC/DC50%1/2
defined $sv or defined $lv
defined $sv defined $lv
Truth table66%2/3
A or B
A= defined $svB= defined $lv
ABresult
1X1
011
000
168 42 ▶ next if defined($sv) && defined($lv) && _eq_deep($sv, $lv);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sv and defined $lv and _eq_deep($sv, $lv) 42 0 42
Condition33%2/6
defined $sv and defined $lv and _eq_deep($sv, $lv)
!ll&&!rl&&r
0042
defined $sv and defined $lv
!ll&&!rl&&r
0042
MC/DC0%0/3
defined $sv and defined $lv and _eq_deep($sv, $lv)
defined $sv defined $lv _eq_deep($sv, $lv)
Truth table25%1/4
A and B and C
A= defined $svB= defined $lvC= _eq_deep($sv, $lv)
ABCresult
0XX0
10X0
1100
1111
169 0 $mod{$attr} = [$sv, $lv];
170 }
171 14 ▶ $diff->{modified}{$name} = \%mod if %mod;
Branch: 1 branch
BranchTrueFalseTotal
if %mod 0 14 14
172 }
173  
174 # PK changes
175 7 my %stored_pk = map { $_ => 1 } @{$stored->{primary_keys}};
176 7 my %live_pk = map { $_ => 1 } @{$live->{primary_keys}};
177 7 ▶ if (join(',', sort keys %stored_pk) ne join(',', sort keys %live_pk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (join(",", sort(keys %stored_pk)) ne join(",", sort(keys %live_pk))) 0 7 7
178 $diff->{primary_keys} = [
179 0 [sort keys %stored_pk],
180 [sort keys %live_pk],
181 ];
182 }
183  
184 7 return $diff;
185 }
186  
187 # Deep equality for nested structures (arrays, hashes, scalars)
188 42 ▶sub _eq_deep {
Subroutine _eq_deep: called CC=12 SCAR=46.0
189 42 my ($a, $b) = @_;
190 42 ▶ return 0 if ref $a ne ref $b;
Branch: 1 branch
BranchTrueFalseTotal
if ref $a ne ref $b 0 42 42
191 42 ▶ return $a eq $b if !ref $a;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $a 42 0 42
192 0 ▶ if (ref $a eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "ARRAY") 0 0 0
193 0 ▶ return 0 if @$a != @$b;
Branch: 1 branch
BranchTrueFalseTotal
if @$a != @$b 0 0 0
194 0 for my $i (0 .. $#$a) {
195 0 ▶ return 0 unless _eq_deep($a->[$i], $b->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless _eq_deep($a->[$i], $b->[$i]) 0 0 0
196 }
197 0 return 1;
198 }
199 0 ▶ if (ref $a eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "HASH") 0 0 0
200 0 ▶ return 0 if keys %$a != keys %$b;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$a != keys %$b 0 0 0
201 0 for my $k (keys %$a) {
202 0 ▶ return 0 unless exists $b->{$k} && _eq_deep($a->{$k}, $b->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $b->{$k} and _eq_deep($a->{$k}, $b->{$k}) 0 0 0
Condition0%0/3
exists $b->{$k} and _eq_deep($a->{$k}, $b->{$k})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $b->{$k} and _eq_deep($a->{$k}, $b->{$k})
exists $b->{$k} _eq_deep($a->{$k}, $b->{$k})
Truth table0%0/3
A and B
A= exists $b->{$k}B= _eq_deep($a->{$k}, $b->{$k})
ABresult
0X0
100
111
203 }
204 0 return 1;
205 }
206 0 return 0;
207 }
208  
209 # Check if a source diff has any actual changes
210 7 ▶sub _source_diff_has_changes {
Subroutine _source_diff_has_changes: called CC=5 SCAR=16.4
211 7 my ($diff) = @_;
212 7 ▶ return 1 if @{$diff->{added}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$diff->{"added"};} 3 4 7
213 4 ▶ return 1 if @{$diff->{removed}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$diff->{"removed"};} 0 4 4
214 4 ▶ return 1 if %{$diff->{modified}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$diff->{"modified"};} 0 4 4
215 4 ▶ return 1 if $diff->{primary_keys};
Branch: 1 branch
BranchTrueFalseTotal
if $diff->{"primary_keys"} 0 4 4
216 4 return 0;
217 }
218  
219 8 1;
220  
221 __END__
« MigrationDBIx/Command/db.pm ↑ Summary