MigrationDBIx/Command/db.pm

filter: Statementstmt 73.0% 325 / 445 Branchbran 56.7% 143 / 252 Conditioncond 51.5% 50 / 97 MC/DCmcdc 15.5% 9 / 58 Subroutinesub 88.0% 22 / 25 Podpod 100% 1 / 1 totaltotal 62.6% 550 / 878 CC 180 SCAR 43.2
CC
180
Cov
65%
_populate
229.3
_prepare
69.0
_bootstrap_schema
40.0
CRAP 74.9
↑ Summary MigrationDBIx.pm »
1 package Mojolicious::Plugin::Fondation::MigrationDBIx::Command::db;
2 8 $Mojolicious::Plugin::Fondation::MigrationDBIx::Command::db::VERSION = '0.06';
3 # ABSTRACT: Database migration and fixture commands for DBIx::Class backends
4  
5 8 ▶use Mojo::Base 'Mojolicious::Command', -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 8 has description => 'Manage database migrations and fixtures for DBIx backends';
11 0 ▶has usage => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 <<"USAGE";
13 Usage: APPLICATION db COMMAND [OPTIONS]
14  
15 myapp.pl db bootstrap-schema [--class ClassName] [--backend name] [--force]
16 Create a minimal DBIx::Class::Schema class
17 myapp.pl db prepare [-y] [-a] Generate SQL migrations + copy fixtures
18 -a Auto-bump schema version on drift
19 myapp.pl db install Run pending DBIx migrations
20 myapp.pl db upgrade Upgrade one version
21 myapp.pl db downgrade Downgrade one version
22 myapp.pl db status Show current migration version
23 myapp.pl db populate [--set SET] [--force]
24 Load DBIx fixture data (current DB version)
25 --force Reload already-loaded sets
26  
27 USAGE
28 8 };
29  
30 44 ▶sub run ($self, @args) {
Subroutine run: called CC=11 SCAR=24.5
Pod documented
31 44 my $app = $self->app;
32 44 ▶ my $subcommand = shift @args || '';
Condition50%1/2
shift @args || ''
l!l
440
MC/DC0%0/1
shift @args || ''
shift @args
Truth table50%1/2
A
A= shift @args
Aresult
11
00
33  
34 44 ▶ my $config = $app->defaults->{'migration_dbix.config'}
Branch: 1 branch
BranchTrueFalseTotal
my $config = $app->defaults->{"migration_dbix.config"} or die "MigrationDBIx not configured. Add Fondation::MigrationDBIx to your config.\n" 44 0 44
35 or die "MigrationDBIx not configured. Add Fondation::MigrationDBIx to your config.\n";
36  
37 44 for ($subcommand) {
38 44 ▶ /^bootstrap-schema$/ and return $self->_bootstrap_schema($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^bootstrap-schema$/u and return $self->_bootstrap_schema($app, $config, @args) 6 38 44
39 38 ▶ /^install$/ and return $self->_install($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^install$/u and return $self->_install($app, $config, @args) 10 28 38
40 28 ▶ /^upgrade$/ and return $self->_upgrade($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^upgrade$/u and return $self->_upgrade($app, $config, @args) 2 26 28
41 26 ▶ /^downgrade$/ and return $self->_downgrade($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^downgrade$/u and return $self->_downgrade($app, $config, @args) 0 26 26
42 26 ▶ /^status$/ and return $self->_status($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^status$/u and return $self->_status($app, $config, @args) 2 24 26
43 24 ▶ /^prepare$/ and return $self->_prepare($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^prepare$/u and return $self->_prepare($app, $config, @args) 20 4 24
44 4 ▶ /^populate$/ and return $self->_populate($app, $config, @args);
Branch: 1 branch
BranchTrueFalseTotal
/^populate$/u and return $self->_populate($app, $config, @args) 4 0 4
45 0 die $self->usage;
46 }
47 }
48  
49 # ---------------------------------------------------------------------------
50 # Build a DeploymentHandler with ignore_ddl => 1
51 # ---------------------------------------------------------------------------
52  
53 37 ▶sub _build_dh ($self, $app, $config) {
Subroutine _build_dh: called CC=7 SCAR=22.0
54 37 ▶ my $native = $self->_build_native_schema($app, $config)
Branch: 1 branch
BranchTrueFalseTotal
my $native = $self->_build_native_schema($app, $config) or return undef 37 0 37
55 or return undef;
56  
57 37 my $mig_dir = path($config->{migrations_dir});
58  
59 # Derive database type from DSN (e.g. dbi:SQLite:... -> SQLite, dbi:Pg:... -> PostgreSQL)
60 37 my $c = $app->build_controller;
61 37 my $bdef = $c->backend_config($config->{backend});
62 37 ▶ my ($driver) = $bdef->{dsn} =~ /^dbi:([^:]+):/i
Branch: 1 branch
BranchTrueFalseTotal
my($driver) = $bdef->{"dsn"} =~ /^dbi:([^:]+):/iu or die qq[Cannot parse DSN: $bdef->{"dsn"}\n] 37 0 37
63 or die "Cannot parse DSN: $bdef->{dsn}\n";
64 37 ▶ $driver = 'MySQL' if lc $driver eq 'mysql' || lc $driver eq 'mariadb';
Branch: 1 branch
BranchTrueFalseTotal
if lc $driver eq "mysql" or lc $driver eq "mariadb" 0 37 37
Condition33%1/3
lc $driver eq "mysql" or lc $driver eq "mariadb"
l!l&&r!l&&!r
0037
MC/DC0%0/2
lc $driver eq "mysql" or lc $driver eq "mariadb"
lc $driver eq "mysql" lc $driver eq "mariadb"
Truth table33%1/3
A or B
A= lc $driver eq "mysql"B= lc $driver eq "mariadb"
ABresult
1X1
011
000
65 37 ▶ $driver = 'PostgreSQL' if lc $driver eq 'pg' || lc $driver eq 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
if lc $driver eq "pg" or lc $driver eq "postgresql" 0 37 37
Condition33%1/3
lc $driver eq "pg" or lc $driver eq "postgresql"
l!l&&r!l&&!r
0037
MC/DC0%0/2
lc $driver eq "pg" or lc $driver eq "postgresql"
lc $driver eq "pg" lc $driver eq "postgresql"
Truth table33%1/3
A or B
A= lc $driver eq "pg"B= lc $driver eq "postgresql"
ABresult
1X1
011
000
66  
67 37 require DBIx::Class::DeploymentHandler;
68 37 return DBIx::Class::DeploymentHandler->new(
69 schema => $native,
70 script_directory => $mig_dir->to_string,
71 databases => [$driver],
72 sql_translator_args => { add_drop_table => 0 },
73 ignore_ddl => 1,
74 );
75 }
76  
77 # ---------------------------------------------------------------------------
78 # db bootstrap-schema -- create a minimal DBIx::Class::Schema class file
79 # ---------------------------------------------------------------------------
80  
81 9 ▶sub _bootstrap_schema ($self, $app, $config, @args) {
Subroutine _bootstrap_schema: called CC=25 SCAR=36.9
82 9 my $class_name;
83 my $backend_name;
84 9 my $force;
85  
86 # Parse options
87 9 ▶ for (my $i = 0; $i < @args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 3 9 12
88 3 ▶ if ($args[$i] eq '--class' && defined $args[$i + 1]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args[$i] eq '--class' and defined $args[$i + 1]) { } 1 2 3
elsif ($args[$i] eq '--backend' and defined $args[$i + 1]) { } 0 2 2
elsif ($args[$i] eq '--force') { } 2 0 2
Condition66%2/3
$args[$i] eq "--class" and defined $args[$i + 1]
!ll&&!rl&&r
201
MC/DC50%1/2
$args[$i] eq "--class" and defined $args[$i + 1]
$args[$i] eq "--class" defined $args[$i + 1]
Truth table66%2/3
A and B
A= $args[$i] eq "--class"B= defined $args[$i + 1]
ABresult
0X0
100
111
89 1 $class_name = $args[$i + 1];
90 1 $i++;
91 }
92 ▶ elsif ($args[$i] eq '--backend' && defined $args[$i + 1]) {
Condition33%1/3
$args[$i] eq "--backend" and defined $args[$i + 1]
!ll&&!rl&&r
200
MC/DC0%0/2
$args[$i] eq "--backend" and defined $args[$i + 1]
$args[$i] eq "--backend" defined $args[$i + 1]
Truth table33%1/3
A and B
A= $args[$i] eq "--backend"B= defined $args[$i + 1]
ABresult
0X0
100
111
93 0 $backend_name = $args[$i + 1];
94 0 $i++;
95 }
96 elsif ($args[$i] eq '--force') {
97 2 $force = 1;
98 }
99 }
100  
101 # Resolve backend name
102 9 ▶ unless ($backend_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($backend_name) 9 0 9
103 9 my $c = $app->build_controller;
104 9 ▶ if ($c->has_helper('default_backend_name')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->has_helper('default_backend_name')) { } 9 0 9
elsif ($config->{'backend'}) { } 0 0 0
105 9 $backend_name = $c->default_backend_name($config->{backend});
106 }
107 elsif ($config->{backend}) {
108 0 $backend_name = $config->{backend};
109 }
110 }
111  
112 9 ▶ unless ($backend_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($backend_name) 1 8 9
113 1 die "No backend specified. Use --backend <name> or configure one.\n";
114 }
115  
116 # Check if DBIx::Async is loaded and schema_class already configured
117 8 my $c = $app->build_controller;
118 8 ▶ if ($c->has_helper('backend_config')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_helper("backend_config")) 8 0 8
119 8 my $bdef = eval { $c->backend_config($backend_name) };
120 8 ▶ if ($bdef && $bdef->{schema_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bdef and $bdef->{"schema_class"}) 3 5 8
Condition66%2/3
$bdef and $bdef->{"schema_class"}
!ll&&!rl&&r
053
MC/DC50%1/2
$bdef and $bdef->{"schema_class"}
$bdef $bdef->{"schema_class"}
Truth table66%2/3
A and B
A= $bdefB= $bdef->{"schema_class"}
ABresult
0X0
100
111
121 3 ▶ if ($class_name && $class_name ne $bdef->{schema_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_name and $class_name ne $bdef->{"schema_class"}) 1 2 3
Condition66%2/3
$class_name and $class_name ne $bdef->{"schema_class"}
!ll&&!rl&&r
201
MC/DC50%1/2
$class_name and $class_name ne $bdef->{"schema_class"}
$class_name $class_name ne $bdef->{"schema_class"}
Truth table66%2/3
A and B
A= $class_nameB= $class_name ne $bdef->{"schema_class"}
ABresult
0X0
100
111
122 1 say "Note: backend '$backend_name' already has schema_class '"
123 . "$bdef->{schema_class}'. Generating '$class_name' anyway.";
124 }
125 # Use existing schema_class if no --class given
126 3 ▶ $class_name //= $bdef->{schema_class};
Condition66%2/3
$class_name //= $bdef->{"schema_class"}
l!l&&r!l&&!r
120
MC/DC0%0/2
$class_name //= $bdef->{"schema_class"}
$class_name $bdef->{"schema_class"}
Truth table66%2/3
A //= B
A= $class_nameB= $bdef->{"schema_class"}
ABresult
1X1
011
000
127 }
128 }
129  
130 # Derive class name from moniker if not specified
131 8 ▶ unless ($class_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class_name) 5 3 8
132 5 ▶ my $moniker = $app->moniker // 'MyApp';
Condition50%1/2
$app->moniker // "MyApp"
l!l
50
MC/DC0%0/1
$app->moniker // "MyApp"
$app->moniker
Truth table50%1/2
A
A= $app->moniker
Aresult
11
00
133 5 $class_name = ucfirst($moniker) . '::Schema';
134 }
135  
136 # Determine file path under lib/
137 8 (my $class_path = "$class_name.pm") =~ s{::}{/}g;
138 8 my $file = $app->home->child('lib', $class_path);
139  
140 # Check for existing file
141 8 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 3 5 8
142 3 ▶ if (!$force) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($force) 1 2 3
143 1 die "Schema file already exists: $file\n"
144 . "Use --force to overwrite.\n";
145 }
146  
147 # --force: only overwrite files that were generated by this tool.
148 # Refuse to touch a user-created schema class to avoid data loss.
149 2 my $existing = $file->slurp;
150 2 ▶ unless ($existing =~ /^# BOOTSTRAPPED BY Fondation::MigrationDBIx/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing =~ /^# BOOTSTRAPPED BY Fondation::MigrationDBIx/mu) 1 1 2
151 1 die "Schema file was NOT generated by db bootstrap-schema: $file\n"
152 . "Refusing to overwrite user-created schema class.\n";
153 }
154 }
155  
156 # Create parent directories
157 6 $file->dirname->make_path;
158  
159 # Write the minimal schema class
160 6 my $content = <<"SCHEMA";
161 package $class_name;
162  
163 # BOOTSTRAPPED BY Fondation::MigrationDBIx -- do not edit this marker.
164 # This file is managed by 'db bootstrap-schema'. Removing or changing
165 # the marker above will prevent --force from overwriting it, protecting
166 # user-created schema classes from accidental data loss.
167  
168 use base 'DBIx::Class::Schema';
169  
170 # Increment this version when you change the schema.
171 # Run 'db prepare' after altering tables, then 'db upgrade' to apply.
172 our \$VERSION = '1';
173  
174 # Auto-discovers Result classes under ${class_name}::Result::*
175 # Local classes take priority over plugin Result classes registered
176 # by the DBIx action (load_namespaces runs during connect(), after
177 # the action has registered plugin sources).
178 __PACKAGE__->load_namespaces;
179  
180 1;
181 SCHEMA
182  
183 6 $file->spurt($content);
184  
185 6 say "Created $file";
186 6 say "";
187  
188 # Show configuration instructions if schema_class is not yet configured
189 6 ▶ if ($c->has_helper('backend_config')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_helper('backend_config')) { } 6 0 6
190 6 my $bdef = eval { $c->backend_config($backend_name) };
191 6 ▶ if (!$bdef) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $bdef) { } 0 6 6
elsif (not $bdef->{'schema_class'}) { } 3 3 6
192 0 say "Backend '$backend_name' not found in DBIx::Async config.";
193 0 say "Make sure Fondation::Model::DBIx::Async is configured with a";
194 0 say "backend named '$backend_name'.";
195 0 say "";
196 0 say " 'Fondation::Model::DBIx::Async' => {";
197 0 say " backends => [";
198 0 say " $backend_name => {";
199 0 say " dsn => 'dbi:SQLite:dbname=data/app.db',";
200 0 say " schema_class => '$class_name',";
201 0 say " },";
202 0 say " ],";
203 0 say " },";
204 }
205 elsif (!$bdef->{schema_class}) {
206 3 say "Add this to your backend '$backend_name' config in myapp.conf:";
207 3 say "";
208 3 say " schema_class => '$class_name', # ← add this line";
209 3 say "";
210 3 say "Then run: myapp.pl db prepare";
211 }
212 else {
213 3 say "Schema class '$class_name' is ready.";
214 }
215 }
216 else {
217 0 say "Add 'schema_class => '$class_name'' to your backend";
218 0 say "'$backend_name' in Fondation::Model::DBIx::Async config.";
219 0 say "Then run: myapp.pl db prepare";
220 }
221 }
222  
223 # ---------------------------------------------------------------------------
224 # db prepare -- generate SQL from schema + copy fixtures from plugins
225 # ---------------------------------------------------------------------------
226  
227 20 ▶sub _prepare ($self, $app, $config, @args) {
Subroutine _prepare: called CC=33 SCAR=42.3
228 20 my $yes = grep { $_ eq '-y' } @args;
229 20 my $auto_bump = grep { $_ eq '-a' } @args;
230  
231 20 my $mig_dir = path($config->{migrations_dir});
232 20 my $fix_dir = $app->home->child('share', 'fixtures');
233  
234 # Detect schema drift (changes since last prepare)
235 20 my $c = $app->build_controller;
236 my $drift = $c->has_helper('schema_drift')
237 20 ▶ ? eval { $c->schema_drift } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$c->has_helper('schema_drift') ? : 20 0 20
238  
239 20 ▶ if ($drift && $drift->{has_drift} && !$auto_bump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drift and $drift->{"has_drift"} and not $auto_bump) 1 19 20
Condition83%5/6
$drift and $drift->{"has_drift"} and not $auto_bump
!ll&&!rl&&r
1721
$drift and $drift->{"has_drift"}
!ll&&!rl&&r
0173
MC/DC66%2/3
$drift and $drift->{"has_drift"} and not $auto_bump
$drift $drift->{"has_drift"} not $auto_bump
Truth table75%3/4
A and B and not(C)
A= $driftB= $drift->{"has_drift"}C= not $auto_bump
ABCresult
0XX0
10X0
1100
1111
240 1 say "\nSchema version $drift->{version} has drifted since last prepare:";
241 1 for my $name (sort keys %{$drift->{changes}}) {
242 1 my $ch = $drift->{changes}{$name};
243 1 ▶ if ($ch eq 'new') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq 'new') { } 0 1 1
elsif ($ch eq 'removed') { } 0 1 1
244 0 say " + $name (new table)";
245 }
246 elsif ($ch eq 'removed') {
247 0 say " - $name (removed)";
248 }
249 else {
250 1 my @details;
251 1 push @details, "+" . join(',+', @{$ch->{added}})
252 1 ▶ if @{$ch->{added}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$ch->{"added"};} 1 0 1
253 0 push @details, "-" . join(',-', @{$ch->{removed}})
254 1 ▶ if @{$ch->{removed}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$ch->{"removed"};} 0 1 1
255 1 for my $col (sort keys %{$ch->{modified}}) {
256 0 my $mod = $ch->{modified}{$col};
257 0 my @attrs = sort keys %$mod;
258 0 push @details, "~$col(" . join(',', @attrs) . ")";
259 }
260 1 ▶ if ($ch->{primary_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch->{"primary_keys"}) 0 1 1
261 0 push @details, "~PK";
262 }
263 1 ▶ say " ~ $name ("
Branch: 1 branch
BranchTrueFalseTotal
if @details 1 0 1
264 . join('; ', @details) . ")" if @details;
265 }
266 }
267 1 say "\nRun 'db prepare -a' to auto-bump the version and generate the";
268 1 say "migration, or bump \$VERSION in your schema class manually.";
269 1 return;
270 }
271  
272 # Auto-bump schema version if requested
273 19 ▶ if ($auto_bump && $drift && $drift->{has_drift}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_bump and $drift and $drift->{"has_drift"}) 2 17 19
Condition66%4/6
$auto_bump and $drift and $drift->{"has_drift"}
!ll&&!rl&&r
1702
$auto_bump and $drift
!ll&&!rl&&r
1702
MC/DC33%1/3
$auto_bump and $drift and $drift->{"has_drift"}
$auto_bump $drift $drift->{"has_drift"}
Truth table50%2/4
A and B and C
A= $auto_bumpB= $driftC= $drift->{"has_drift"}
ABCresult
0XX0
10X0
1100
1111
274 2 say "Schema drifted -- auto-bumping version...";
275 2 $self->_bump_schema_version($app, $config);
276 }
277  
278 # Check if target directories already have content
279 19 my @existing;
280 19 ▶ push @existing, 'migrations' if $self->_dir_has_content($mig_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dir_has_content($mig_dir) 2 17 19
281 19 ▶ push @existing, 'fixtures' if $self->_dir_has_content($fix_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dir_has_content($fix_dir) 1 18 19
282  
283 19 ▶ if (@existing && !$yes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@existing and not $yes) 0 19 19
Condition66%2/3
@existing and not $yes
!ll&&!rl&&r
1720
MC/DC0%0/2
@existing and not $yes
@existing not $yes
Truth table66%2/3
A and not(B)
A= @existingB= not $yes
ABresult
0X0
100
111
284 0 my $counts = '';
285 0 ▶ $counts .= sprintf " %-12s %d file(s)\n", 'migrations',
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dir_has_content($mig_dir) 0 0 0
286 $self->_file_count($mig_dir) if $self->_dir_has_content($mig_dir);
287 0 ▶ $counts .= sprintf " %-12s %d file(s)\n", 'fixtures',
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dir_has_content($fix_dir) 0 0 0
288 $self->_file_count($fix_dir) if $self->_dir_has_content($fix_dir);
289 0 say "\nTarget directories already have content:";
290 0 print $counts;
291 0 say "";
292 0 print "Overwrite existing files? [y/N] ";
293 0 my $answer = <STDIN>;
294 0 chomp $answer;
295 0 ▶ exit(0) unless $answer =~ /^y(es)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer =~ /^y(es)?$/iu 0 0 0
296 }
297  
298 19 ▶ my $force = $yes || (@existing > 0);
Condition66%2/3
$yes || @existing > 0
l!l&&r!l&&!r
1801
MC/DC50%1/2
$yes || @existing > 0
$yes @existing > 0
Truth table66%2/3
A || B
A= $yesB= @existing > 0
ABresult
1X1
011
000
299  
300 # Generate SQL from schema classes via DeploymentHandler
301 19 my $dh = $self->_build_dh($app, $config);
302 19 ▶ if ($dh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dh) 19 0 19
303 # Remove existing generated dirs so DeploymentHandler regenerates cleanly --
304 # skip this when auto-bumping (we're adding a version, not rebuilding)
305 19 ▶ unless ($auto_bump) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auto_bump) 17 2 19
306 17 ▶ $mig_dir->child('_source')->remove_tree if $force && -d $mig_dir->child('_source');
Branch: 1 branch
BranchTrueFalseTotal
if $force and -d $mig_dir->child("_source") 0 17 17
Condition66%2/3
$force and -d $mig_dir->child("_source")
!ll&&!rl&&r
1160
MC/DC0%0/2
$force and -d $mig_dir->child("_source")
$force -d $mig_dir->child("_source")
Truth table66%2/3
A and B
A= $forceB= -d $mig_dir->child("_source")
ABresult
0X0
100
111
307 17 ▶ $mig_dir->child('SQLite')->remove_tree if $force && -d $mig_dir->child('SQLite');
Branch: 1 branch
BranchTrueFalseTotal
if $force and -d $mig_dir->child("SQLite") 0 17 17
Condition66%2/3
$force and -d $mig_dir->child("SQLite")
!ll&&!rl&&r
1160
MC/DC0%0/2
$force and -d $mig_dir->child("SQLite")
$force -d $mig_dir->child("SQLite")
Truth table66%2/3
A and B
A= $forceB= -d $mig_dir->child("SQLite")
ABresult
0X0
100
111
308 }
309  
310 19 $dh->prepare_install;
311 19 say "Done.";
312  
313 # Save schema signature for future drift detection
314 19 $self->_save_sig($app, $config, $dh);
315 }
316  
317 # Copy fixture tree from all plugins to the versioned directory.
318 # Fixtures live under share/fixtures/<schema_version>/ — the version
319 # comes from the schema class $VERSION (reflected by DeploymentHandler).
320 # This ensures db populate always loads fixtures for the current schema
321 # version, even after a version bump via db prepare -a.
322 19 ▶ my $version = $dh ? eval { $dh->schema_version } // '1' : '1';
Branch: 1 branch
BranchTrueFalseTotal
$dh ? : 19 0 19
Condition50%1/2
eval { do { $dh->schema_version } } // 1
l!l
190
MC/DC0%0/1
eval { do { $dh->schema_version } } // 1
eval { do { $dh->schema_version } }
Truth table50%1/2
A
A= eval { do { $dh->schema_version } }
Aresult
11
00
323 19 my $fix_version_dir = $fix_dir->child($version);
324 19 my $fix_copied = $self->_copy_tree_from_plugins(
325 $app, 'fixtures', $fix_version_dir, $force);
326 19 ▶ say sprintf "Fixtures: %d file(s) copied to v%s.", $fix_copied, $version
Branch: 1 branch
BranchTrueFalseTotal
if $fix_copied 15 4 19
327 if $fix_copied;
328 }
329  
330 # ---------------------------------------------------------------------------
331 # Copy a share subdirectory tree from all plugins to the app
332 # ---------------------------------------------------------------------------
333  
334 19 ▶sub _copy_tree_from_plugins ($self, $app, $subdir, $target_dir, $force) {
Subroutine _copy_tree_from_plugins: called CC=16 SCAR=30.5
335 19 my $manager = $app->manager;
336 19 my $copied = 0;
337  
338 # Clean target if forcing overwrite
339 19 ▶ $target_dir->remove_tree({ keep_root => 1 }) if $force && -d $target_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $force and -d $target_dir 0 19 19
Condition66%2/3
$force and -d $target_dir
!ll&&!rl&&r
1180
MC/DC0%0/2
$force and -d $target_dir
$force -d $target_dir
Truth table66%2/3
A and B
A= $forceB= -d $target_dir
ABresult
0X0
100
111
340 19 ▶ $target_dir->make_path unless -d $target_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $target_dir 19 0 19
341  
342 0 ▶ my $load_order = $manager->load_order // [sort keys %{$manager->registry}];
Condition50%1/2
$manager->load_order // [sort(keys %{$manager->registry;})]
l!l
190
MC/DC0%0/1
$manager->load_order // [sort(keys %{$manager->registry;})]
$manager->load_order
Truth table50%1/2
A
A= $manager->load_order
Aresult
11
00
343  
344 19 for my $long (@$load_order) {
345 73 ▶ my $entry = $manager->registry->{$long} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $manager->registry->{$long} or next 73 0 73
346 73 ▶ my $share = $entry->{share_dir} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $share = $entry->{"share_dir"} or next 73 0 73
347 73 my $src_root = $share->child($subdir);
348 73 ▶ next unless -d $src_root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $src_root 57 16 73
349  
350 16 ▶ my @all_files = @{ $src_root->list_tree({ hidden => 1 }) // [] };
Condition50%1/2
$src_root->list_tree({"hidden", 1}) // []
l!l
160
MC/DC0%0/1
$src_root->list_tree({"hidden", 1}) // []
$src_root->list_tree({"hidden", 1})
Truth table50%1/2
A
A= $src_root->list_tree({"hidden", 1})
Aresult
11
00
351 16 for my $src (@all_files) {
352 32 ▶ next unless -f $src;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $src 0 32 32
353  
354 32 my $rel_path = $src->to_rel($src_root);
355  
356 # Strip the plugin's fixture version prefix (e.g. "1/" or "1\")
357 # so that fixtures land in the target version directory
358 # determined by the current schema version.
359 32 $rel_path =~ s{^[^/\\]+[/\\]}{};
360 32 my $target = $target_dir->child($rel_path);
361  
362 32 ▶ next if -e $target && !$force;
Branch: 1 branch
BranchTrueFalseTotal
if -e $target and not $force 0 32 32
Condition33%1/3
-e $target and not $force
!ll&&!rl&&r
3200
MC/DC0%0/2
-e $target and not $force
-e $target not $force
Truth table33%1/3
A and not(B)
A= -e $targetB= not $force
ABresult
0X0
100
111
363  
364 32 ▶ $target->dirname->make_path unless -d $target->dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $target->dirname 31 1 32
365 32 eval { $src->copy_to($target); 1 }
366 32 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $src->copy_to($target); 1 } } or do { warn " Failed to copy ${rel_path}: $@\n"; next } 32 0 32
367 0 warn " Failed to copy $rel_path: $@\n";
368 0 next;
369 };
370 32 $copied++;
371 }
372 }
373  
374 19 return $copied;
375 }
376  
377 # ---------------------------------------------------------------------------
378 # db install -- run DeploymentHandler install
379 # ---------------------------------------------------------------------------
380  
381 10 ▶sub _install ($self, $app, $config, @args) {
Subroutine _install: called CC=7 SCAR=20.7
382 10 ▶ my $dh = $self->_build_dh($app, $config)
Branch: 1 branch
BranchTrueFalseTotal
my $dh = $self->_build_dh($app, $config) or return 10 0 10
383 or return;
384  
385 10 my $mig_dir = path($config->{migrations_dir});
386  
387 10 ▶ unless (-d $mig_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $mig_dir) 0 10 10
388 0 say "No migrations directory: $mig_dir";
389 0 say "Run 'db prepare' first to generate migration files from schema.";
390 0 return;
391 }
392  
393 10 my $schema_v = $dh->schema_version;
394 10 ▶ my $db_v = $dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
$dh->version_storage_is_installed ? : 1 9 10
395 ? $dh->database_version : 0;
396  
397 10 ▶ if ($db_v && $db_v >= $schema_v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_v and $db_v >= $schema_v) 1 9 10
Condition66%2/3
$db_v and $db_v >= $schema_v
!ll&&!rl&&r
901
MC/DC50%1/2
$db_v and $db_v >= $schema_v
$db_v $db_v >= $schema_v
Truth table66%2/3
A and B
A= $db_vB= $db_v >= $schema_v
ABresult
0X0
100
111
398 1 say "Already at version $db_v (schema: $schema_v). Nothing to migrate.";
399 1 return;
400 }
401  
402 9 say "Installing schema (version $schema_v)...";
403 9 $dh->install;
404 9 ▶ my $active = eval { $dh->database_version } // $schema_v;
Condition33%1/3
eval { do { $dh->database_version } } // $schema_v
l!l&&r!l&&!r
900
MC/DC0%0/2
eval { do { $dh->database_version } } // $schema_v
eval { do { $dh->database_version } } $schema_v
Truth table33%1/3
A // B
A= eval { do { $dh->database_version } }B= $schema_v
ABresult
1X1
011
000
405 9 say "Done. Active version: $active";
406 }
407  
408 # ---------------------------------------------------------------------------
409 # db upgrade -- run pending upgrades
410 # ---------------------------------------------------------------------------
411  
412 2 ▶sub _upgrade ($self, $app, $config, @args) {
Subroutine _upgrade: called CC=4 SCAR=14.0
413 2 ▶ my $dh = $self->_build_dh($app, $config)
Branch: 1 branch
BranchTrueFalseTotal
my $dh = $self->_build_dh($app, $config) or return 2 0 2
414 or return;
415  
416 2 ▶ my $db_v = $dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
$dh->version_storage_is_installed ? : 2 0 2
417 ? $dh->database_version : 0;
418 2 my $schema_v = $dh->schema_version;
419  
420 2 ▶ return say "Already at latest version $db_v." if $db_v >= $schema_v;
Branch: 1 branch
BranchTrueFalseTotal
if $db_v >= $schema_v 1 1 2
421  
422 1 say "Upgrading from version $db_v to " . ($db_v + 1) . "...";
423 1 $dh->upgrade;
424 1 say "Done. Active version: " . $dh->database_version;
425 }
426  
427 # ---------------------------------------------------------------------------
428 # db downgrade -- rollback one version
429 # ---------------------------------------------------------------------------
430  
431 0 ▶sub _downgrade ($self, $app, $config, @args) {
Subroutine _downgrade: not called CC=7 SCAR=35.2
432 0 ▶ my $dh = $self->_build_dh($app, $config)
Branch: 1 branch
BranchTrueFalseTotal
my $dh = $self->_build_dh($app, $config) or return 0 0 0
433 or return;
434  
435 0 ▶ my $db_v = $dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
$dh->version_storage_is_installed ? : 0 0 0
436 ? $dh->database_version : 0;
437 0 ▶ die "No version installed. Nothing to downgrade.\n" unless $db_v;
Branch: 1 branch
BranchTrueFalseTotal
unless $db_v 0 0 0
438  
439 0 my $target_v = $db_v - 1;
440  
441 # Temporarily set the schema $VERSION one lower so DeploymentHandler
442 # sees schema_version < database_version and triggers the downgrade.
443 0 my $c = $app->build_controller;
444 0 ▶ my $schema_class = $c->has_helper('schema_class')
Branch: 1 branch
BranchTrueFalseTotal
$c->has_helper('schema_class') ? : 0 0 0
445 ? $c->schema_class : undef;
446 0 ▶ if ($schema_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_class) 0 0 0
447 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
448 0 ${"${schema_class}::VERSION"} = $target_v;
449 }
450  
451 0 say "Downgrading from version $db_v to $target_v...";
452 0 $dh->downgrade;
453 0 say "Done. Active version: " . $dh->database_version;
454  
455 # Restore the original version
456 0 ▶ if ($schema_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_class) 0 0 0
457 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
458 0 ${"${schema_class}::VERSION"} = $db_v;
459 }
460 }
461  
462 # ---------------------------------------------------------------------------
463 # db status -- show current vs latest migration version
464 # ---------------------------------------------------------------------------
465  
466 2 ▶sub _status ($self, $app, $config, @args) {
Subroutine _status: called CC=6 SCAR=18.2
467 2 ▶ my $dh = $self->_build_dh($app, $config)
Branch: 1 branch
BranchTrueFalseTotal
my $dh = $self->_build_dh($app, $config) or return 2 0 2
468 or return;
469  
470 2 ▶ my $schema_v = $dh->schema_version // 'unknown';
Condition50%1/2
$dh->schema_version // "unknown"
l!l
20
MC/DC0%0/1
$dh->schema_version // "unknown"
$dh->schema_version
Truth table50%1/2
A
A= $dh->schema_version
Aresult
11
00
471 2 ▶ my $db_v = $dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
$dh->version_storage_is_installed ? : 1 1 2
472 ? $dh->database_version : 'none';
473  
474 2 say "Schema version : $schema_v";
475 2 say "Active version : $db_v";
476 2 ▶ say "Status : "
Branch: 1 branch
BranchTrueFalseTotal
$dh->version_storage_is_installed && $db_v >= $schema_v ? : 1 1 2
Condition66%2/3
$dh->version_storage_is_installed and $db_v >= $schema_v
!ll&&!rl&&r
101
MC/DC50%1/2
$dh->version_storage_is_installed and $db_v >= $schema_v
$dh->version_storage_is_installed $db_v >= $schema_v
Truth table66%2/3
A and B
A= $dh->version_storage_is_installedB= $db_v >= $schema_v
ABresult
0X0
100
111
477 . ($dh->version_storage_is_installed && $db_v >= $schema_v
478 ? "up to date" : "migrations pending");
479 }
480  
481 # ---------------------------------------------------------------------------
482 # db populate -- load DBIx fixtures
483 # ---------------------------------------------------------------------------
484  
485 4 ▶sub _populate ($self, $app, $config, @args) {
Subroutine _populate: called CC=47 SCAR=54.4
486 4 my $set_filter; # undef = all sets
487 my $force;
488  
489 # Parse options
490 4 ▶ for (my $i = 0; $i < @args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 1 4 5
491 1 ▶ if ($args[$i] eq '--set' && defined $args[$i + 1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[$i] eq "--set" and defined $args[$i + 1]) 1 0 1
Condition33%1/3
$args[$i] eq "--set" and defined $args[$i + 1]
!ll&&!rl&&r
001
MC/DC0%0/2
$args[$i] eq "--set" and defined $args[$i + 1]
$args[$i] eq "--set" defined $args[$i + 1]
Truth table33%1/3
A and B
A= $args[$i] eq "--set"B= defined $args[$i + 1]
ABresult
0X0
100
111
492 1 $set_filter = $args[$i + 1];
493 1 $i++;
494 1 next;
495 }
496 0 ▶ if ($args[$i] eq '--force') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[$i] eq "--force") 0 0 0
497 0 $force = 1;
498 }
499 }
500  
501 4 ▶ my $dh = $self->_build_dh($app, $config)
Branch: 1 branch
BranchTrueFalseTotal
my $dh = $self->_build_dh($app, $config) or return 4 0 4
502 or return;
503  
504 4 my $c = $app->build_controller;
505 4 my $bdef = $c->backend_config($config->{backend});
506 4 my ($driver) = $bdef->{dsn} =~ /^dbi:([^:]+):/i;
507 4 ▶ $driver = 'MySQL' if lc $driver eq 'mysql' || lc $driver eq 'mariadb';
Branch: 1 branch
BranchTrueFalseTotal
if lc $driver eq "mysql" or lc $driver eq "mariadb" 0 4 4
Condition33%1/3
lc $driver eq "mysql" or lc $driver eq "mariadb"
l!l&&r!l&&!r
004
MC/DC0%0/2
lc $driver eq "mysql" or lc $driver eq "mariadb"
lc $driver eq "mysql" lc $driver eq "mariadb"
Truth table33%1/3
A or B
A= lc $driver eq "mysql"B= lc $driver eq "mariadb"
ABresult
1X1
011
000
508 4 ▶ $driver = 'PostgreSQL' if lc $driver eq 'pg' || lc $driver eq 'postgresql';
Branch: 1 branch
BranchTrueFalseTotal
if lc $driver eq "pg" or lc $driver eq "postgresql" 0 4 4
Condition33%1/3
lc $driver eq "pg" or lc $driver eq "postgresql"
l!l&&r!l&&!r
004
MC/DC0%0/2
lc $driver eq "pg" or lc $driver eq "postgresql"
lc $driver eq "pg" lc $driver eq "postgresql"
Truth table33%1/3
A or B
A= lc $driver eq "pg"B= lc $driver eq "postgresql"
ABresult
1X1
011
000
509  
510 4 ▶ my $set_version = $dh->version_storage_is_installed
Branch: 1 branch
BranchTrueFalseTotal
$dh->version_storage_is_installed ? : 4 0 4
511 ? $dh->database_version : 0;
512 4 ▶ die "No database version. Run 'db install' or 'db upgrade' first.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $set_version 0 4 4
513 unless $set_version;
514  
515 4 my $conf_dir = $app->home->child('share', 'fixtures', $set_version, 'conf');
516  
517 4 ▶ unless (-d $conf_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $conf_dir) 0 4 4
518 0 say "Fixture config directory not found: $conf_dir";
519 0 return;
520 }
521  
522 # Discover available set names from conf/*.json
523 4 my @all_sets;
524 4 for my $file (sort $conf_dir->list({ file => 1 })->each) {
525 5 ▶ next unless $file->basename =~ /\.json$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->basename =~ /\.json$/iu 0 5 5
526 5 (my $set_name = $file->basename) =~ s/\.json$//i;
527 5 push @all_sets, $set_name;
528 }
529  
530 4 ▶ unless (@all_sets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@all_sets) 0 4 4
531 0 say "No fixture sets found in $conf_dir";
532 0 return;
533 }
534  
535 # Filter by --set or use all
536 my @sets = $set_filter
537 4 ▶ ? grep { $_ eq $set_filter } @all_sets
Branch: 1 branch
BranchTrueFalseTotal
$set_filter ? : 1 3 4
538 : @all_sets;
539  
540 4 ▶ unless (@sets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@sets) 0 4 4
541 0 die "Fixture set '$set_filter' not found. Available: "
542 . join(', ', @all_sets) . "\n";
543 }
544  
545 # Ensure the fixtures_loaded tracking table exists
546 4 my $native = $dh->schema;
547 4 my $storage = $native->storage;
548 4 $storage->ensure_connected;
549 4 ▶ $storage->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
550 4 my ($storage, $dbh) = @_;
551 4 $dbh->do(<<'SQL');
552 CREATE TABLE IF NOT EXISTS fixtures_loaded (
553 set_name VARCHAR(255) NOT NULL PRIMARY KEY,
554 loaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
555 )
556 SQL
557 4 });
558  
559 # Check which sets have already been loaded
560 4 my %loaded;
561 {
562 4 my ($sth, @rows);
563 4 ▶ $storage->dbh_do(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
564 4 my ($storage, $dbh) = @_;
565 4 $sth = $dbh->prepare('SELECT set_name FROM fixtures_loaded');
566 4 $sth->execute;
567 4 ▶ @rows = @{$sth->fetchall_arrayref // []};
Condition50%1/2
$sth->fetchall_arrayref // []
l!l
40
MC/DC0%0/1
$sth->fetchall_arrayref // []
$sth->fetchall_arrayref
Truth table50%1/2
A
A= $sth->fetchall_arrayref
Aresult
11
00
568 4 });
569 4 %loaded = map { $_->[0] => 1 } @rows;
570 }
571  
572 # Filter out already-loaded sets (unless --force)
573 4 ▶ if (!$force) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($force) 4 0 4
574 4 my @to_load;
575 my @skipped;
576 4 for my $set (@sets) {
577 5 ▶ if ($loaded{$set}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded{$set}) { } 1 4 5
578 1 push @skipped, $set;
579 } else {
580 4 push @to_load, $set;
581 }
582 }
583 4 ▶ say "Skipping already-loaded set(s): " . join(', ', @skipped)
Branch: 1 branch
BranchTrueFalseTotal
if @skipped 1 3 4
584 if @skipped;
585 4 ▶ say " Use --force to reload them." if @skipped;
Branch: 1 branch
BranchTrueFalseTotal
if @skipped 1 3 4
586 4 @sets = @to_load;
587 }
588  
589 4 ▶ unless (@sets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@sets) 0 4 4
590 0 say "All sets already loaded. Nothing to do.";
591 0 return;
592 }
593  
594 4 require DBIx::Class::Fixtures;
595 4 my $fixtures = DBIx::Class::Fixtures->new({
596 config_dir => $conf_dir->to_string,
597 });
598  
599 # Build set → DBIC class mapping from conf JSON files.
600 # The 'class' field in conf JSON is a TABLE name (e.g. 'api_tokens'),
601 # not a DBIC source name (e.g. 'ApiToken'). We build a bidirectional
602 # map so we can resolve belongs_to relationships between sets.
603 4 my %set_table; # set → table name
604 my %table_set; # table name → set
605 4 for my $set (@sets) {
606 4 my $conf_file = $conf_dir->child("$set.json");
607 4 ▶ my $conf = eval { decode_json($conf_file->slurp) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $conf = eval { do { &decode_json(scalar $conf_file->slurp) } } or next 4 0 4
608 4 ▶ my $table = $conf->{sets}[0]{class} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table = $conf->{"sets"}[0]{"class"} or next 4 0 4
609 4 $set_table{$set} = $table;
610 4 $table_set{$table} = $set;
611 }
612  
613 # Build table → source mapping from the live schema
614 4 my %table_source;
615 4 for my $source_name ($native->sources) {
616 9 my $source = $native->source($source_name);
617 9 $table_source{$source->name} = $source_name;
618 }
619  
620 # Resolve belongs_to dependencies between sets
621 4 my %depends_on;
622 4 for my $set (keys %set_table) {
623 4 my $table = $set_table{$set};
624 4 my $source_name = $table_source{$table};
625 4 ▶ next unless $source_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_name 4 0 4
626 0 my $source = eval { $native->source($source_name) };
627 0 ▶ next unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 0 0
628 0 for my $rel_name ($source->relationships) {
629 0 my $rel = $source->relationship_info($rel_name);
630 0 ▶ next unless $rel->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel->{"attrs"}{"is_foreign_key_constraint"} 0 0 0
631 # $rel->{class} is a DBIC Result class — find its table name
632 0 my $parent_source = eval { $native->source($rel->{class}) };
633 0 ▶ next unless $parent_source;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_source 0 0 0
634 0 my $parent_table = $parent_source->name;
635 0 my $parent_set = $table_set{$parent_table};
636 0 ▶ next unless $parent_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_set 0 0 0
637 0 ▶ next if $parent_set eq $set;
Branch: 1 branch
BranchTrueFalseTotal
if $parent_set eq $set 0 0 0
638 0 push @{$depends_on{$set}}, $parent_set;
639 }
640 }
641  
642 # Topological sort (Kahn's algorithm) so dependencies load first
643 4 ▶ if (%depends_on) {
Branch: 1 branch
BranchTrueFalseTotal
if (%depends_on) 0 4 4
644 0 my %in_degree;
645 my %children;
646 0 ▶ $in_degree{$_} //= 0 for @sets;
Condition0%0/2
$in_degree{$_} //= 0
l!l
00
MC/DC0%0/1
$in_degree{$_} //= 0
$in_degree{$_}
Truth table0%0/2
A
A= $in_degree{$_}
Aresult
11
00
647 0 for my $set (keys %depends_on) {
648 0 for my $dep (@{$depends_on{$set}}) {
649 0 $in_degree{$set}++;
650 0 push @{$children{$dep}}, $set;
651 }
652 }
653 0 my @sorted;
654 0 my @queue = grep { $in_degree{$_} == 0 } @sets;
655 0 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 0 0
656 0 my $node = shift @queue;
657 0 push @sorted, $node;
658 0 ▶ for my $child (@{$children{$node} // []}) {
Condition0%0/2
$children{$node} // []
l!l
00
MC/DC0%0/1
$children{$node} // []
$children{$node}
Truth table0%0/2
A
A= $children{$node}
Aresult
11
00
659 0 $in_degree{$child}--;
660 0 ▶ push @queue, $child if $in_degree{$child} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $in_degree{$child} == 0 0 0 0
661 }
662 }
663 0 @sets = @sorted;
664 }
665  
666 4 say "Populating set(s): " . join(', ', @sets);
667  
668 4 for my $set (@sets) {
669 4 my $set_dir = $app->home->child(
670 'share', 'fixtures', $set_version, $set)->to_string;
671 4 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $fixtures->populate({"no_deploy", 1, "directory", $set_dir, "schema", $native}); 1 } } or die "Failed to populate set '${set}': $@" 4 0 4
672 4 $fixtures->populate({
673 no_deploy => 1,
674 directory => $set_dir,
675 schema => $native,
676 });
677 4 1;
678 } or die "Failed to populate set '$set': $@";
679  
680 # Record that this set has been loaded
681 4 ▶ $storage->dbh_do(sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
682 4 my ($storage, $dbh) = @_;
683 4 my $sql;
684 4 ▶ if ($driver eq 'MySQL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($driver eq 'MySQL') { } 0 4 4
elsif ($driver eq 'PostgreSQL') { } 0 4 4
685 0 $sql = 'REPLACE INTO fixtures_loaded (set_name) VALUES (?)';
686 } elsif ($driver eq 'PostgreSQL') {
687 0 $sql = 'INSERT INTO fixtures_loaded (set_name) VALUES (?)'
688 . ' ON CONFLICT (set_name) DO UPDATE SET set_name = EXCLUDED.set_name, loaded_at = CURRENT_TIMESTAMP';
689 } else {
690 4 $sql = 'INSERT OR REPLACE INTO fixtures_loaded (set_name) VALUES (?)';
691 }
692 4 $dbh->do($sql, undef, $set);
693 4 });
694 }
695  
696 4 say "Populate complete.";
697 }
698  
699 # ---------------------------------------------------------------------------
700 # Build a native DBIx::Class::Schema from backend config
701 # ---------------------------------------------------------------------------
702  
703 85 ▶sub _build_native_schema ($self, $app, $config) {
Subroutine _build_native_schema: called CC=13 SCAR=28.5
704 # Resolve backend: explicit config -> DBIx::Async default -> first backend -> undef
705 85 my $c = $app->build_controller;
706 85 my $backend_name;
707 85 ▶ if ($c->has_helper('default_backend_name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_helper('default_backend_name')) { } 85 0 85
708 85 $backend_name = $c->default_backend_name($config->{backend});
709 } else {
710 0 $backend_name = $config->{backend};
711 }
712  
713 85 ▶ unless ($backend_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($backend_name) 1 84 85
714 1 say "No backend configured. Set 'backend' in MigrationDBIx config"
715 . " or 'default_backend' in Fondation::Model::DBIx::Async.";
716 1 return undef;
717 }
718  
719 84 my $bdef;
720 84 ▶ unless ($app->has_helper('backend_config')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app->has_helper("backend_config")) 0 84 84
721 0 say "Fondation::Model::DBIx::Async is not loaded. No backend_config helper.";
722 0 return undef;
723 }
724  
725 84 $bdef = eval { $c->backend_config($backend_name) };
726 84 ▶ unless ($bdef) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bdef) 0 84 84
727 0 say "Backend '$backend_name' not found.";
728 0 return undef;
729 }
730  
731 my $schema_class = $bdef->{schema_class}
732 84 ▶ or die "No schema_class configured for backend '$backend_name'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $schema_class = $bdef->{"schema_class"} or die "No schema_class configured for backend '${backend_name}'\n" 80 4 84
733  
734 80 ▶ eval "require $schema_class; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $schema_class; 1" or die "Cannot load schema class ${schema_class}: $@\n" . "Run 'db bootstrap-schema' to create the file.\n" 80 0 80
735 or die "Cannot load schema class $schema_class: $@\n"
736 . "Run 'db bootstrap-schema' to create the file.\n";
737  
738 # Ensure parent directory exists for file-based DSNs (SQLite)
739 80 ▶ if ($bdef->{dsn} =~ /^dbi:SQLite:(?:dbname=)?(.+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bdef->{"dsn"} =~ /^dbi:SQLite:(?:dbname=)?(.+)$/iu) 80 0 80
740 80 my $db_path = $1;
741 80 my $dir = Mojo::File->new($db_path)->dirname;
742 80 ▶ $dir->make_path unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 80 80
743 }
744  
745 80 my %extra;
746 80 ▶ $extra{quote_char} = $bdef->{quote_char} if $bdef->{quote_char};
Branch: 1 branch
BranchTrueFalseTotal
if $bdef->{"quote_char"} 0 80 80
747 my $native = $schema_class->connect(
748 $bdef->{dsn},
749 $bdef->{user} // '',
750 $bdef->{pass} // '',
751 $bdef->{dbi_attrs} // {},
752 80 ▶ \%extra,
Condition50%3/6
$bdef->{"user"} // ""
l!l
080
$bdef->{"pass"} // ""
l!l
080
$bdef->{"dbi_attrs"} // {}
l!l
080
MC/DC0%0/1
$bdef->{"user"} // ""
$bdef->{"user"}
MC/DC0%0/1
$bdef->{"pass"} // ""
$bdef->{"pass"}
MC/DC0%0/1
$bdef->{"dbi_attrs"} // {}
$bdef->{"dbi_attrs"}
Truth table50%1/2
A
A= $bdef->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $bdef->{"pass"}
Aresult
11
00
Truth table50%1/2
A
A= $bdef->{"dbi_attrs"}
Aresult
11
00
753 );
754  
755 80 return $native;
756 }
757  
758 # ---------------------------------------------------------------------------
759 # Helpers for directory content detection
760 # ---------------------------------------------------------------------------
761  
762 38 ▶sub _dir_has_content ($self, $dir) {
Subroutine _dir_has_content: called CC=3 SCAR=11.0
763 38 ▶ return 0 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 34 4 38
764 4 ▶ my @files = grep { -f $_ } @{ $dir->list_tree({ hidden => 1 }) // [] };
Condition50%1/2
$dir->list_tree({"hidden", 1}) // []
l!l
40
MC/DC0%0/1
$dir->list_tree({"hidden", 1}) // []
$dir->list_tree({"hidden", 1})
Truth table50%1/2
A
A= $dir->list_tree({"hidden", 1})
Aresult
11
00
765 4 return scalar @files > 0;
766 }
767  
768 0 ▶sub _file_count ($self, $dir) {
Subroutine _file_count: not called CC=3 SCAR=24.8
769 0 ▶ return 0 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
770 0 ▶ return scalar grep { -f $_ } @{ $dir->list_tree({ hidden => 1 }) // [] };
Condition0%0/2
$dir->list_tree({"hidden", 1}) // []
l!l
00
MC/DC0%0/1
$dir->list_tree({"hidden", 1}) // []
$dir->list_tree({"hidden", 1})
Truth table0%0/2
A
A= $dir->list_tree({"hidden", 1})
Aresult
11
00
771 }
772  
773 # ---------------------------------------------------------------------------
774 # Save schema signature for future drift detection
775 # ---------------------------------------------------------------------------
776  
777 19 ▶sub _save_sig ($self, $app, $config, $dh) {
Subroutine _save_sig: called CC=3 SCAR=11.2
778 19 my $c = $app->build_controller;
779 19 my $live_sig = $c->schema_sig($dh->schema);
780  
781 19 ▶ my $version = $dh->schema_version
Branch: 1 branch
BranchTrueFalseTotal
my $version = $dh->schema_version or die "Cannot determine schema version after prepare\n" 19 0 19
782 or die "Cannot determine schema version after prepare\n";
783  
784 19 my $sig = {
785 version => $version,
786 sources => $live_sig,
787 };
788  
789 my $sig_file = $config->{sig_file}
790 19 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sig_file = $config->{"sig_file"} or return 19 0 19
791  
792 19 path($sig_file)->dirname->make_path;
793 19 path($sig_file)->spurt(encode_json($sig));
794 }
795  
796 # ---------------------------------------------------------------------------
797 # Auto-bump the schema version in the class file
798 # ---------------------------------------------------------------------------
799  
800 2 ▶sub _bump_schema_version ($self, $app, $config) {
Subroutine _bump_schema_version: called CC=6 SCAR=19.9
801 2 my $c = $app->build_controller;
802  
803 2 my $schema_class;
804 2 ▶ if ($c->has_helper('schema_class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_helper("schema_class")) 2 0 2
805 2 $schema_class = $c->schema_class;
806 }
807  
808 2 ▶ unless ($schema_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_class) 0 2 2
809 0 say "Cannot auto-bump: no schema_class configured.";
810 0 return;
811 }
812  
813 2 (my $class_path = "$schema_class.pm") =~ s{::}{/}g;
814 2 my $file = $app->home->child('lib', $class_path);
815  
816 2 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 2 2
817 0 say "Cannot auto-bump: schema file not found ($file).";
818 0 return;
819 }
820  
821 2 my $content = $file->slurp;
822 2 ▶ unless ($content =~ /^our\s+\$VERSION\s*=\s*['"]?(\d+)['"]?\s*;/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content =~ /^our\s+\$VERSION\s*=\s*['"]?(\d+)['"]?\s*;/mu) 0 2 2
823 0 say "Cannot auto-bump: no \$VERSION found in $schema_class.";
824 0 return;
825 }
826  
827 2 my $old_version = $1;
828 2 my $new_version = $old_version + 1;
829  
830 $content =~ s/^(our\s+\$VERSION\s*=\s*['"]?)$old_version(['"]?\s*;)/${1}${new_version}${2}/m
831 2 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$content =~ s/^(our\s+\$VERSION\s*=\s*['"]?)$old_version(['"]?\s*;)/$1$new_version$2/mu or do { say "Cannot auto-bump: failed to replace \$VERSION."; return } 2 0 2
832 0 say "Cannot auto-bump: failed to replace \$VERSION.";
833 0 return;
834 };
835  
836 2 $file->spurt($content);
837 2 say "Bumped \$VERSION from $old_version to $new_version in $file";
838  
839 # Update the in-memory $VERSION so the next _build_dh sees it.
840 # Direct assignment avoids reloading the class (which would lose
841 # all register_source calls from Action::DBIx).
842 {
843 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
844 2 ${"${schema_class}::VERSION"} = $new_version;
845 }
846 }
847  
848 8 1;
849  
850 __END__
↑ Summary MigrationDBIx.pm »