Async.pm

filter: Statementstmt 80.3% 127 / 158 Branchbran 55.5% 50 / 90 Conditioncond 50.0% 20 / 40 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 77.7% 14 / 18 Podpod 33.3% 1 / 3 totaltotal 65.0% 216 / 332 CC 78 SCAR 29.6
CC
78
Cov
68%
__ANON__
90.0
__ANON__
30.0
fondation_finalyze
18.0
CRAP 19.4
« Async/Action/DBIx.pm ↑ Summary Async/ResultSet.pm »
1 package Mojolicious::Plugin::Fondation::Model::DBIx::Async;
2 7 $Mojolicious::Plugin::Fondation::Model::DBIx::Async::VERSION = '0.06';
3 # ABSTRACT: Fondation plugin exposing DBIx::Class::Async natively
4  
5 7 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Mojolicious::Plugin::Fondation::Model::DBIx::Async::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 9 ▶sub fondation_meta {
Subroutine fondation_meta: called CC=1 SCAR=0.0
Pod undocumented
10 return {
11 9 dependencies => [],
12 provides_actions => ['DBIx'],
13 defaults => {
14 backends => [],
15 models => {},
16 default_backend => undef,
17 },
18 setup => {
19 label => 'Database',
20 description => 'Main database connection settings',
21 parameters => [
22 {
23 key => '+backends.main.dsn',
24 label => 'DSN',
25 type => 'string',
26 default => 'dbi:SQLite:dbname=data/app.db',
27 required => 1,
28 placeholder => 'dbi:Pg:dbname=mydb;host=localhost',
29 },
30 {
31 key => '+backends.main.workers',
32 label => 'Workers',
33 type => 'integer',
34 default => 2,
35 min => 1,
36 max => 10,
37 },
38 {
39 key => '+backends.main.schema_class',
40 label => 'Schema class',
41 type => 'string',
42 default => 'MySchema',
43 required => 1,
44 },
45 {
46 key => '+backends.main.quote_char',
47 label => 'Quote character',
48 type => 'string',
49 default => '"',
50 },
51 ],
52 },
53 };
54 }
55  
56 9 ▶sub register ($self, $app, $config) {
Subroutine register: called CC=9 SCAR=25.3
Pod documented
57  
58 # Normalize backends — plain DSN string → { dsn => $dsn }
59 9 ▶ my $backends_input = $config->{backends} // [];
Condition50%1/2
$config->{"backends"} // []
l!l
90
MC/DC0%0/1
$config->{"backends"} // []
$config->{"backends"}
Truth table50%1/2
A
A= $config->{"backends"}
Aresult
11
00
60 9 ▶ die "backends must be an arrayref, got " . ref($backends_input) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $backends_input eq "ARRAY" 0 9 9
61 unless ref $backends_input eq 'ARRAY';
62  
63 9 my $backends = {};
64 9 my @list;
65 9 ▶ for (my $i = 0; $i < @$backends_input; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$backends_input) 10 9 19
66 10 my $name = $backends_input->[$i];
67 10 ▶ die "Backend name must be a string at index $i\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 10 10
68 if ref $name;
69 10 ▶ my $cfg = $backends_input->[$i+1] // {};
Condition50%1/2
$$backends_input[$i + 1] // {}
l!l
100
MC/DC0%0/1
$$backends_input[$i + 1] // {}
$$backends_input[$i + 1]
Truth table50%1/2
A
A= $$backends_input[$i + 1]
Aresult
11
00
70 10 ▶ $cfg = { dsn => $cfg } unless ref $cfg;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg 0 10 10
71 10 ▶ die "Duplicate backend '$name'\n" if exists $backends->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $backends->{$name} 0 10 10
72 10 $backends->{$name} = $cfg;
73 10 push @list, $name, $cfg;
74 }
75  
76 9 $self->{_backends_list} = \@list;
77 9 $self->{_backends} = $backends;
78 9 ▶ $self->{_models} = $config->{models} // {};
Condition50%1/2
$config->{"models"} // {}
l!l
90
MC/DC0%0/1
$config->{"models"} // {}
$config->{"models"}
Truth table50%1/2
A
A= $config->{"models"}
Aresult
11
00
79 9 $self->{_default_backend} = $config->{default_backend};
80  
81 # Helper: schema_class($backend_name?)
82 34 ▶ $app->helper(schema_class => sub ($c, $backend_name = undef) {
Subroutine __ANON__: called CC=7 SCAR=21.5
83 34 my $bdef;
84 34 ▶ if ($backend_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend_name) { } 0 34 34
85 0 ▶ $bdef = $self->{_backends}{$backend_name}
Branch: 1 branch
BranchTrueFalseTotal
$bdef = $self->{"_backends"}{$backend_name} or die "Backend '${backend_name}' not configured\n" 0 0 0
86 or die "Backend '$backend_name' not configured\n";
87 }
88 else {
89 34 ▶ for (my $i = 0; $i < @{$self->{_backends_list}}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"_backends_list"};}) 34 0 34
90 34 my $name = $self->{_backends_list}[$i];
91 34 my $b = $self->{_backends}{$name};
92 34 ▶ if ($b->{schema_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->{"schema_class"}) 34 0 34
93 34 $bdef = $b;
94 34 last;
95 }
96 }
97 }
98 34 ▶ return $bdef ? $bdef->{schema_class} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$bdef ? : 34 0 34
99 9 });
100  
101 # Helper: backend_config($name?)
102 0 ▶ $app->helper(backend_config => sub ($c, $name = undef) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
103 0 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
104 $name = $self->{_backends_list}[0]
105 0 ▶ if @{$self->{_backends_list}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_backends_list"};} 0 0 0
106 }
107 0 ▶ my $bdef = $self->{_backends}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $bdef = $self->{"_backends"}{$name} or die "Backend '${name}' not configured\n" 0 0 0
108 or die "Backend '$name' not configured\n";
109 0 return { %$bdef, name => $name };
110 9 });
111  
112 # Helper: default_backend_name($explicit?)
113 # Cascade: explicit param → $config->{default_backend} → first backend → undef
114 4 ▶ $app->helper(default_backend_name => sub ($c, $explicit = undef) {
Subroutine __ANON__: called CC=5 SCAR=16.3
115 4 ▶ return $explicit if $explicit;
Branch: 1 branch
BranchTrueFalseTotal
if $explicit 2 2 4
116 2 ▶ return $self->{_default_backend} if $self->{_default_backend};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_default_backend"} 1 1 2
117 1 ▶ return $self->{_backends_list}[0] if @{$self->{_backends_list}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_backends_list"};} 1 0 1
118 0 return undef;
119 9 });
120  
121 # Helper: model_config($name)
122 0 ▶ $app->helper(model_config => sub ($c, $name) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
123 0 ▶ my $spec = $self->{_models}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $self->{"_models"}{$name} or die "Model '${name}' is not configured\n" 0 0 0
124 or die "Model '$name' is not configured\n";
125 return {
126 name => $name,
127 source => $spec->{source} // $name,
128 backend => $spec->{backend},
129 0 ▶ };
Condition0%0/3
$spec->{"source"} // $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec->{"source"} // $name
$spec->{"source"} $name
Truth table0%0/3
A // B
A= $spec->{"source"}B= $name
ABresult
1X1
011
000
130 9 });
131  
132 # Helper: model_list()
133 0 ▶ $app->helper(model_list => sub ($c) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 return [ sort keys %{$self->{_models}} ];
135 9 });
136  
137 # Helper: schema($backend_name?)
138 28 ▶ $app->helper(schema => sub ($c, $backend_name = undef) {
Subroutine __ANON__: called CC=14 SCAR=26.8
139 28 ▶ $self->{_schemas} //= {};
Condition100%2/2
$self->{"_schemas"} //= {}
l!l
217
MC/DC100%1/1
$self->{"_schemas"} //= {}
$self->{"_schemas"}
Truth table100%2/2
A
A= $self->{"_schemas"}
Aresult
11
00
140  
141 28 my $bname = $backend_name;
142 28 ▶ unless ($bname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bname) 8 20 28
143 $bname = $self->{_backends_list}[0]
144 8 ▶ if @{$self->{_backends_list}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_backends_list"};} 8 0 8
145 }
146 28 ▶ return undef unless $bname;
Branch: 1 branch
BranchTrueFalseTotal
unless $bname 0 28 28
147 28 ▶ return $self->{_schemas}{$bname} if exists $self->{_schemas}{$bname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_schemas"}{$bname} 21 7 28
148  
149 7 ▶ my $bdef = $self->{_backends}{$bname}
Branch: 1 branch
BranchTrueFalseTotal
my $bdef = $self->{"_backends"}{$bname} or die "Backend '${bname}' not configured\n" 7 0 7
150 or die "Backend '$bname' not configured\n";
151 die "schema_class is required for backend '$bname'\n"
152 7 ▶ unless $bdef->{schema_class};
Branch: 1 branch
BranchTrueFalseTotal
unless $bdef->{"schema_class"} 0 7 7
153  
154 7 require DBIx::Class::Async::Schema;
155 7 require IO::Async::Loop::Mojo;
156  
157 7 my $loop = IO::Async::Loop::Mojo->new;
158 # Pass all backend keys except those consumed here or by
159 # DBIx::Class::Async::Schema as DBIC connect attributes.
160 7 my %connect_attrs;
161 7 for my $k (keys %$bdef) {
162 23 ▶ next if $k =~ /^(?:dsn|user|pass|schema_class|workers|name)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(?:dsn|user|pass|schema_class|workers|name)$/u 20 3 23
163 3 $connect_attrs{$k} = $bdef->{$k};
164 }
165 $self->{_schemas}{$bname} = DBIx::Class::Async::Schema->connect(
166 $bdef->{dsn},
167 $bdef->{user} // '',
168 $bdef->{pass} // '',
169 \%connect_attrs,
170 {
171 schema_class => $bdef->{schema_class},
172 7 ▶ workers => $bdef->{workers} // 2,
Condition66%4/6
$bdef->{"user"} // ""
l!l
07
$bdef->{"pass"} // ""
l!l
07
$bdef->{"workers"} // 2
l!l
61
MC/DC0%0/1
$bdef->{"user"} // ""
$bdef->{"user"}
MC/DC0%0/1
$bdef->{"pass"} // ""
$bdef->{"pass"}
MC/DC100%1/1
$bdef->{"workers"} // 2
$bdef->{"workers"}
Truth table50%1/2
A
A= $bdef->{"user"}
Aresult
11
00
Truth table50%1/2
A
A= $bdef->{"pass"}
Aresult
11
00
Truth table100%2/2
A
A= $bdef->{"workers"}
Aresult
11
00
173 loop => $loop,
174 }
175 );
176 7 return $self->{_schemas}{$bname};
177 9 });
178  
179 # Helper: schema_sig($schema?)
180 # Returns canonical signature of schema structure for drift detection.
181 # Deduplicates by real table name, skips __VERSION, only DDL attributes.
182 0 ▶ $app->helper(schema_sig => sub ($c, $schema = undef) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
183 0 ▶ $schema //= $c->schema;
Condition0%0/3
$schema //= $c->schema
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema //= $c->schema
$schema $c->schema
Truth table0%0/3
A //= B
A= $schemaB= $c->schema
ABresult
1X1
011
000
184 0 ▶ return undef unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
185  
186 0 my %sig;
187 my %seen;
188 0 for my $source_name ($schema->sources) {
189 0 my $source = $schema->source($source_name);
190 0 my $table = $source->name;
191 0 ▶ next if $table eq 'dbix_class_deploymenthandler_versions';
Branch: 1 branch
BranchTrueFalseTotal
if $table eq "dbix_class_deploymenthandler_versions" 0 0 0
192 0 ▶ next if $seen{$table}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$table}++ 0 0 0
193  
194 0 my @columns;
195 0 for my $col ($source->columns) {
196 0 my $info = $source->column_info($col);
197 0 my %ddl;
198 0 for my $attr (qw(data_type is_nullable is_auto_increment
199 default_value size is_foreign_key)) {
200 0 $ddl{$attr} = $info->{$attr};
201 }
202 0 push @columns, { name => $col, %ddl };
203 }
204  
205 0 $sig{$table} = {
206 columns => \@columns,
207 primary_keys => [ $source->primary_columns ],
208 };
209 }
210  
211 0 return \%sig;
212 9 });
213  
214 # Helper: model($name)
215 21 ▶ $app->helper(model => sub ($c, $name) {
Subroutine __ANON__: called CC=3 SCAR=11.2
216 21 my $models = $self->{_models};
217 21 ▶ my $spec = $models->{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $models->{$name} or die "Model '${name}' is not configured\n" 20 1 21
218 or die "Model '$name' is not configured\n";
219 20 ▶ my $source = $spec->{source} // $name;
Condition33%1/3
$spec->{"source"} // $name
l!l&&r!l&&!r
2000
MC/DC0%0/2
$spec->{"source"} // $name
$spec->{"source"} $name
Truth table33%1/3
A // B
A= $spec->{"source"}B= $name
ABresult
1X1
011
000
220 20 my $backend = $spec->{backend};
221 20 my $rs = $c->schema($backend)->resultset($source);
222 20 return bless $rs, 'Mojolicious::Plugin::Fondation::Model::DBIx::Async::ResultSet';
223 9 });
224  
225 # Shutdown cleanup: disconnect all DBIC::Async schemas on process exit.
226 # END block catches every clean exit (Ctrl-C, kill, hypnotoad stop,
227 # systemctl stop). Only SIGKILL bypasses it — no hook can help there.
228 8 ▶ $self->{_shutdown} = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
229 8 ▶ return unless $self->{_schemas};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_schemas"} 1 7 8
230 7 for my $bname (keys %{$self->{_schemas}}) {
231 6 ▶ if (my $schema = delete $self->{_schemas}{$bname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = delete $self->{"_schemas"}{$bname}) 6 0 6
232 6 DBIx::Class::Async->disconnect($schema);
233 6 $app->log->info(
234 "DBIx::Class::Async workers stopped for backend '$bname'");
235 }
236 }
237 9 };
238 7 ▶ { no warnings 'closure';
Subroutine BEGIN: called CC=1 SCAR=0.0
239 7 ▶ END {
Subroutine END: called CC=2 SCAR=7.2
240 7 $app->plugins->emit_hook('before_server_stop' => $app);
241 7 ▶ $self->{_shutdown}->() if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 7 0 7
242 }
243 }
244  
245 # ── Global DBIC source re-registration after all plugins finalize ──
246 # Plugins add relationships (has_many, belongs_to, many_to_many_async)
247 # during fondation_finalyze. DBIx::Class clones sources on
248 # register_source(), so the initial Action::DBIx registration stores
249 # a snapshot. We re-register every source once after all plugins have
250 # finalized, so schema instances (including async workers) see the
251 # complete relationship graph.
252 9 ▶ $app->plugins->on(fondation_after_finalyze => sub ($app, $manager) {
Subroutine __ANON__: called CC=10 SCAR=23.7
253 8 my $c = $app->build_controller;
254 8 ▶ return unless $c->has_helper('schema_class');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->has_helper("schema_class") 0 8 8
255 8 ▶ my $sc = eval { $c->schema_class } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sc = eval { do { $c->schema_class } } or return 8 0 8
256  
257 # guard against non-DBIC schema class in source re-registration
258 8 ▶ return unless $sc->can('sources');
Branch: 1 branch
BranchTrueFalseTotal
unless $sc->can("sources") 0 8 8
259  
260 # Collect moniker → Result class from all plugins' dbic metadata
261 8 my %result_classes;
262 8 for my $entry (values %{$manager->registry}) {
263 24 ▶ next unless $entry->{dbic} && $entry->{dbic}{result_classes};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"dbic"} and $entry->{"dbic"}{"result_classes"} 8 16 24
Condition66%2/3
$entry->{"dbic"} and $entry->{"dbic"}{"result_classes"}
!ll&&!rl&&r
8016
MC/DC50%1/2
$entry->{"dbic"} and $entry->{"dbic"}{"result_classes"}
$entry->{"dbic"} $entry->{"dbic"}{"result_classes"}
Truth table66%2/3
A and B
A= $entry->{"dbic"}B= $entry->{"dbic"}{"result_classes"}
ABresult
0X0
100
111
264 16 %result_classes = (%result_classes, %{$entry->{dbic}{result_classes}});
265 }
266  
267 8 for my $source_name ($sc->sources) {
268 19 ▶ my $result_class = $result_classes{$source_name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $result_class = $result_classes{$source_name} or next 19 0 19
269 19 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sc->register_source($source_name, $result_class->result_source_instance); 1 } } or $self->log->warn("Failed to re-register source '${source_name}': $@") 19 0 19
270 19 $sc->register_source($source_name,
271 $result_class->result_source_instance);
272 19 1;
273 } or $self->log->warn(
274 "Failed to re-register source '$source_name': $@");
275 }
276 9 });
277  
278 9 return $self;
279 }
280  
281 8 ▶sub fondation_finalyze ($self, $app, $long_name) {
Subroutine fondation_finalyze: called CC=16 SCAR=28.9
Pod undocumented
282 8 my $api = $app->fondation;
283 8 ▶ my $registry = $api->registry // {};
Condition50%1/2
$api->registry // {}
l!l
80
MC/DC0%0/1
$api->registry // {}
$api->registry
Truth table50%1/2
A
A= $api->registry
Aresult
11
00
284 8 ▶ return 1 unless %$registry;
Branch: 1 branch
BranchTrueFalseTotal
unless %$registry 0 8 8
285  
286 8 my $models = $self->{_models};
287  
288 8 for my $long (keys %$registry) {
289 24 ▶ next if $long eq $long_name;
Branch: 1 branch
BranchTrueFalseTotal
if $long eq $long_name 8 16 24
290 16 my $entry = $registry->{$long};
291 16 ▶ my $plugin_config = $entry->{config} // {};
Condition50%1/2
$entry->{"config"} // {}
l!l
160
MC/DC0%0/1
$entry->{"config"} // {}
$entry->{"config"}
Truth table50%1/2
A
A= $entry->{"config"}
Aresult
11
00
292 16 ▶ my $plugin_models = $plugin_config->{models} // {};
Condition100%2/2
$plugin_config->{"models"} // {}
l!l
88
MC/DC100%1/1
$plugin_config->{"models"} // {}
$plugin_config->{"models"}
Truth table100%2/2
A
A= $plugin_config->{"models"}
Aresult
11
00
293 16 ▶ next unless %$plugin_models;
Branch: 1 branch
BranchTrueFalseTotal
unless %$plugin_models 8 8 16
294  
295 8 for my $model_name (keys %$plugin_models) {
296 19 ▶ next if exists $models->{$model_name}; # app config wins
Branch: 1 branch
BranchTrueFalseTotal
if exists $models->{$model_name} 12 7 19
297 7 my $plugin_def = $plugin_models->{$model_name};
298 $models->{$model_name} = {
299 source => $plugin_def->{source} // $model_name,
300 backend => $plugin_def->{backend} // undef,
301 7 ▶ };
Condition40%2/5
$plugin_def->{"source"} // $model_name
l!l&&r!l&&!r
700
$plugin_def->{"backend"} // undef
l!l
07
MC/DC0%0/2
$plugin_def->{"source"} // $model_name
$plugin_def->{"source"} $model_name
MC/DC0%0/1
$plugin_def->{"backend"} // undef
$plugin_def->{"backend"}
Truth table33%1/3
A // B
A= $plugin_def->{"source"}B= $model_name
ABresult
1X1
011
000
Truth table50%1/2
A
A= $plugin_def->{"backend"}
Aresult
11
00
302 }
303 }
304  
305 # Validate: every model must have a backend
306 8 for my $model_name (sort keys %$models) {
307 $models->{$model_name}{backend} //=
308 19 ▶ $self->_resolve_default_backend;
Condition66%2/3
$models->{$model_name}{"backend"} //= $self->_resolve_default_backend
l!l&&r!l&&!r
1180
MC/DC0%0/2
$models->{$model_name}{"backend"} //= $self->_resolve_default_backend
$models->{$model_name}{"backend"} $self->_resolve_default_backend
Truth table66%2/3
A //= B
A= $models->{$model_name}{"backend"}B= $self->_resolve_default_backend
ABresult
1X1
011
000
309 my $bname = $models->{$model_name}{backend}
310 19 ▶ or die "Fondation::Model::DBIx::Async: no backend for model '$model_name'"
Branch: 1 branch
BranchTrueFalseTotal
my $bname = $models->{$model_name}{"backend"} or die "Fondation::Model::DBIx::Async: no backend for model '${model_name}'" . " and no default_backend configured\n" 19 0 19
311 . " and no default_backend configured\n";
312 die "Fondation::Model::DBIx::Async: backend '$bname' not found for model '$model_name'\n"
313 19 ▶ unless $self->{_backends}{$bname};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_backends"}{$bname} 0 19 19
314 }
315  
316 8 return 1;
317 }
318  
319 8 ▶sub _resolve_default_backend ($self) {
Subroutine _resolve_default_backend: called CC=3 SCAR=11.4
320 8 ▶ return $self->{_default_backend} if $self->{_default_backend};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_default_backend"} 2 6 8
321 6 ▶ return $self->{_backends_list}[0] if @{$self->{_backends_list}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_backends_list"};} 6 0 6
322 0 return undef;
323 }
324  
325 7 1;
326  
327 __END__
« Async/Action/DBIx.pm ↑ Summary Async/ResultSet.pm »