| 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"} // []
MC/DC0%0/1
$config->{"backends"} // []
$config->{"backends"}
Truth table50%1/2
A
A= $config->{"backends"}
| |||||||||||||||||||||||||||||||||
| 60 | 9 | ▶ | die "backends must be an arrayref, got " . ref($backends_input) . "\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 66 | 10 | my $name = $backends_input->[$i]; | |||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | die "Backend name must be a string at index $i\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | if ref $name; | ||||||||||||||||||||||||||||||||
| 69 | 10 | ▶ | my $cfg = $backends_input->[$i+1] // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$$backends_input[$i + 1] // {}
MC/DC0%0/1
$$backends_input[$i + 1] // {}
$$backends_input[$i + 1]
Truth table50%1/2
A
A= $$backends_input[$i + 1]
| |||||||||||||||||||||||||||||||||
| 70 | 10 | ▶ | $cfg = { dsn => $cfg } unless ref $cfg; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | die "Duplicate backend '$name'\n" if exists $backends->{$name}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$config->{"models"} // {}
$config->{"models"}
Truth table50%1/2
A
A= $config->{"models"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $bdef = $self->{_backends}{$backend_name} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 90 | 34 | my $name = $self->{_backends_list}[$i]; | |||||||||||||||||||||||||||||||
| 91 | 34 | my $b = $self->{_backends}{$name}; | |||||||||||||||||||||||||||||||
| 92 | 34 | ▶ | if ($b->{schema_class}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 34 | $bdef = $b; | |||||||||||||||||||||||||||||||
| 94 | 34 | last; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | 34 | ▶ | return $bdef ? $bdef->{schema_class} : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 104 | $name = $self->{_backends_list}[0] | ||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if @{$self->{_backends_list}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $bdef = $self->{_backends}{$name} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | return $self->{_default_backend} if $self->{_default_backend}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | return $self->{_backends_list}[0] if @{$self->{_backends_list}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$spec->{"source"} // $name
$spec->{"source"}
$name
Truth table0%0/3
A // B
A= $spec->{"source"}B= $name
| |||||||||||||||||||||||||||||||||
| 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"} //= {}
MC/DC100%1/1
$self->{"_schemas"} //= {}
$self->{"_schemas"}
Truth table100%2/2
A
A= $self->{"_schemas"}
| |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 28 | my $bname = $backend_name; | |||||||||||||||||||||||||||||||
| 142 | 28 | ▶ | unless ($bname) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | $bname = $self->{_backends_list}[0] | ||||||||||||||||||||||||||||||||
| 144 | 8 | ▶ | if @{$self->{_backends_list}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | 28 | ▶ | return undef unless $bname; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 28 | ▶ | return $self->{_schemas}{$bname} if exists $self->{_schemas}{$bname}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 7 | ▶ | my $bdef = $self->{_backends}{$bname} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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"} // ""
$bdef->{"pass"} // ""
$bdef->{"workers"} // 2
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"}
Truth table50%1/2
A
A= $bdef->{"pass"}
Truth table100%2/2
A
A= $bdef->{"workers"}
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$schema //= $c->schema
$schema
$c->schema
Truth table0%0/3
A //= B
A= $schemaB= $c->schema
| |||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | return undef unless $schema; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | next if $seen{$table}++; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 218 | or die "Model '$name' is not configured\n"; | ||||||||||||||||||||||||||||||||
| 219 | 20 | ▶ | my $source = $spec->{source} // $name; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$spec->{"source"} // $name
MC/DC0%0/2
$spec->{"source"} // $name
$spec->{"source"}
$name
Truth table33%1/3
A // B
A= $spec->{"source"}B= $name
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 230 | 7 | for my $bname (keys %{$self->{_schemas}}) { | |||||||||||||||||||||||||||||||
| 231 | 6 | ▶ | if (my $schema = delete $self->{_schemas}{$bname}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 255 | 8 | ▶ | my $sc = eval { $c->schema_class } or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | # guard against non-DBIC schema class in source re-registration | ||||||||||||||||||||||||||||||||
| 258 | 8 | ▶ | return unless $sc->can('sources'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$entry->{"dbic"} and $entry->{"dbic"}{"result_classes"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 269 | 19 | ▶ | eval { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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 // {}
MC/DC0%0/1
$api->registry // {}
$api->registry
Truth table50%1/2
A
A= $api->registry
| |||||||||||||||||||||||||||||||||
| 284 | 8 | ▶ | return 1 unless %$registry; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 290 | 16 | my $entry = $registry->{$long}; | |||||||||||||||||||||||||||||||
| 291 | 16 | ▶ | my $plugin_config = $entry->{config} // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"config"} // {}
MC/DC0%0/1
$entry->{"config"} // {}
$entry->{"config"}
Truth table50%1/2
A
A= $entry->{"config"}
| |||||||||||||||||||||||||||||||||
| 292 | 16 | ▶ | my $plugin_models = $plugin_config->{models} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$plugin_config->{"models"} // {}
MC/DC100%1/1
$plugin_config->{"models"} // {}
$plugin_config->{"models"}
Truth table100%2/2
A
A= $plugin_config->{"models"}
| |||||||||||||||||||||||||||||||||
| 293 | 16 | ▶ | next unless %$plugin_models; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 8 | for my $model_name (keys %$plugin_models) { | |||||||||||||||||||||||||||||||
| 296 | 19 | ▶ | next if exists $models->{$model_name}; # app config wins | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
$plugin_def->{"backend"} // undef
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
Truth table50%1/2
A
A= $plugin_def->{"backend"}
| |||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||
| 309 | my $bname = $models->{$model_name}{backend} | ||||||||||||||||||||||||||||||||
| 310 | 19 | ▶ | or die "Fondation::Model::DBIx::Async: no backend for model '$model_name'" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 321 | 6 | ▶ | return $self->{_backends_list}[0] if @{$self->{_backends_list}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 322 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | 7 | 1; | |||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | __END__ | ||||||||||||||||||||||||||||||||