| 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 || ''
MC/DC0%0/1
shift @args || ''
shift @args
Truth table50%1/2
A
A= shift @args
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 44 | ▶ | my $config = $app->defaults->{'migration_dbix.config'} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 38 | ▶ | /^install$/ and return $self->_install($app, $config, @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 28 | ▶ | /^upgrade$/ and return $self->_upgrade($app, $config, @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 26 | ▶ | /^downgrade$/ and return $self->_downgrade($app, $config, @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 26 | ▶ | /^status$/ and return $self->_status($app, $config, @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 24 | ▶ | /^prepare$/ and return $self->_prepare($app, $config, @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | /^populate$/ and return $self->_populate($app, $config, @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
lc $driver eq "mysql" or lc $driver eq "mariadb"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 37 | ▶ | $driver = 'PostgreSQL' if lc $driver eq 'pg' || lc $driver eq 'postgresql'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
lc $driver eq "pg" or lc $driver eq "postgresql"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | if ($args[$i] eq '--class' && defined $args[$i + 1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$args[$i] eq "--class" and defined $args[$i + 1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 9 | my $c = $app->build_controller; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 9 | ▶ | if ($c->has_helper('default_backend_name')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 8 | my $bdef = eval { $c->backend_config($backend_name) }; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 8 | ▶ | if ($bdef && $bdef->{schema_class}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$bdef and $bdef->{"schema_class"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | if ($class_name && $class_name ne $bdef->{schema_class}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class_name and $class_name ne $bdef->{"schema_class"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # Derive class name from moniker if not specified | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | ▶ | unless ($class_name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | ▶ | my $moniker = $app->moniker // 'MyApp'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$app->moniker // "MyApp"
MC/DC0%0/1
$app->moniker // "MyApp"
$app->moniker
Truth table50%1/2
A
A= $app->moniker
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 3 | ▶ | if (!$force) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 6 | my $bdef = eval { $c->backend_config($backend_name) }; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 6 | ▶ | if (!$bdef) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 20 | ▶ | if ($drift && $drift->{has_drift} && !$auto_bump) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$drift and $drift->{"has_drift"} and not $auto_bump
$drift and $drift->{"has_drift"}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | push @details, "-" . join(',-', @{$ch->{removed}}) | |||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | ▶ | if @{$ch->{removed}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | push @details, "~PK"; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 1 | ▶ | say " ~ $name (" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$auto_bump and $drift and $drift->{"has_drift"}
$auto_bump and $drift
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 19 | ▶ | push @existing, 'fixtures' if $self->_dir_has_content($fix_dir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 19 | ▶ | if (@existing && !$yes) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@existing and not $yes
MC/DC0%0/2
@existing and not $yes
@existing
not $yes
Truth table66%2/3
A and not(B)
A= @existingB= not $yes
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my $counts = ''; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | $counts .= sprintf " %-12s %d file(s)\n", 'migrations', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 19 | ▶ | my $force = $yes || (@existing > 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$yes || @existing > 0
MC/DC50%1/2
$yes || @existing > 0
$yes
@existing > 0
Truth table66%2/3
A || B
A= $yesB= @existing > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 17 | ▶ | $mig_dir->child('_source')->remove_tree if $force && -d $mig_dir->child('_source'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$force and -d $mig_dir->child("_source")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 17 | ▶ | $mig_dir->child('SQLite')->remove_tree if $force && -d $mig_dir->child('SQLite'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$force and -d $mig_dir->child("SQLite")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
eval {
do {
$dh->schema_version
}
} // 1
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
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$force and -d $target_dir
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
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 19 | ▶ | $target_dir->make_path unless -d $target_dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | my $load_order = $manager->load_order // [sort keys %{$manager->registry}]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$manager->load_order // [sort(keys %{$manager->registry;})]
MC/DC0%0/1
$manager->load_order // [sort(keys %{$manager->registry;})]
$manager->load_order
Truth table50%1/2
A
A= $manager->load_order
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 19 | for my $long (@$load_order) { | |||||||||||||||||||||||||||||||||||||||||
| 345 | 73 | ▶ | my $entry = $manager->registry->{$long} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 73 | ▶ | my $share = $entry->{share_dir} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 73 | my $src_root = $share->child($subdir); | |||||||||||||||||||||||||||||||||||||||||
| 348 | 73 | ▶ | next unless -d $src_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 16 | ▶ | my @all_files = @{ $src_root->list_tree({ hidden => 1 }) // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$src_root->list_tree({"hidden", 1}) // []
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})
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 16 | for my $src (@all_files) { | |||||||||||||||||||||||||||||||||||||||||
| 352 | 32 | ▶ | next unless -f $src; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
-e $target and not $force
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
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 32 | ▶ | $target->dirname->make_path unless -d $target->dirname; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | 32 | eval { $src->copy_to($target); 1 } | |||||||||||||||||||||||||||||||||||||||||
| 366 | 32 | ▶ | or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | 10 | my $mig_dir = path($config->{migrations_dir}); | |||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 10 | ▶ | unless (-d $mig_dir) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | ? $dh->database_version : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 10 | ▶ | if ($db_v && $db_v >= $schema_v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$db_v and $db_v >= $schema_v
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | 2 | ▶ | my $db_v = $dh->version_storage_is_installed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | my $db_v = $dh->version_storage_is_installed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | ? $dh->database_version : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | die "No version installed. Nothing to downgrade.\n" unless $db_v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | ? $c->schema_class : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | if ($schema_class) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | 2 | ▶ | my $schema_v = $dh->schema_version // 'unknown'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dh->schema_version // "unknown"
MC/DC0%0/1
$dh->schema_version // "unknown"
$dh->schema_version
Truth table50%1/2
A
A= $dh->schema_version
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | 2 | ▶ | my $db_v = $dh->version_storage_is_installed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$dh->version_storage_is_installed and $db_v >= $schema_v
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | 1 | ▶ | if ($args[$i] eq '--set' && defined $args[$i + 1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args[$i] eq "--set" and defined $args[$i + 1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 1 | $set_filter = $args[$i + 1]; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 1 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | if ($args[$i] eq '--force') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | $force = 1; | |||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | 4 | ▶ | my $dh = $self->_build_dh($app, $config) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
lc $driver eq "mysql" or lc $driver eq "mariadb"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | 4 | ▶ | $driver = 'PostgreSQL' if lc $driver eq 'pg' || lc $driver eq 'postgresql'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
lc $driver eq "pg" or lc $driver eq "postgresql"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | 4 | ▶ | my $set_version = $dh->version_storage_is_installed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | ? $dh->database_version : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 4 | ▶ | die "No database version. Run 'db install' or 'db upgrade' first.\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | : @all_sets; | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | 4 | ▶ | unless (@sets) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 // []
MC/DC0%0/1
$sth->fetchall_arrayref // []
$sth->fetchall_arrayref
Truth table50%1/2
A
A= $sth->fetchall_arrayref
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | 4 | my @to_load; | |||||||||||||||||||||||||||||||||||||||||
| 575 | my @skipped; | ||||||||||||||||||||||||||||||||||||||||||
| 576 | 4 | for my $set (@sets) { | |||||||||||||||||||||||||||||||||||||||||
| 577 | 5 | ▶ | if ($loaded{$set}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 584 | if @skipped; | ||||||||||||||||||||||||||||||||||||||||||
| 585 | 4 | ▶ | say " Use --force to reload them." if @skipped; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 586 | 4 | @sets = @to_load; | |||||||||||||||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | 4 | ▶ | unless (@sets) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | 4 | ▶ | my $table = $conf->{sets}[0]{class} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | my $source = eval { $native->source($source_name) }; | |||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | ▶ | next unless $source; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | ▶ | next if $parent_set eq $set; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | my %in_degree; | |||||||||||||||||||||||||||||||||||||||||
| 645 | my %children; | ||||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | ▶ | $in_degree{$_} //= 0 for @sets; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$in_degree{$_} //= 0
MC/DC0%0/1
$in_degree{$_} //= 0
$in_degree{$_}
Truth table0%0/2
A
A= $in_degree{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | my $node = shift @queue; | |||||||||||||||||||||||||||||||||||||||||
| 657 | 0 | push @sorted, $node; | |||||||||||||||||||||||||||||||||||||||||
| 658 | 0 | ▶ | for my $child (@{$children{$node} // []}) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$children{$node} // []
MC/DC0%0/1
$children{$node} // []
$children{$node}
Truth table0%0/2
A
A= $children{$node}
| |||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | $in_degree{$child}--; | |||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | ▶ | push @queue, $child if $in_degree{$child} == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||
| 734 | 80 | ▶ | eval "require $schema_class; 1" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 743 | } | ||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | 80 | my %extra; | |||||||||||||||||||||||||||||||||||||||||
| 746 | 80 | ▶ | $extra{quote_char} = $bdef->{quote_char} if $bdef->{quote_char}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
$bdef->{"pass"} // ""
$bdef->{"dbi_attrs"} // {}
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"}
Truth table50%1/2
A
A= $bdef->{"pass"}
Truth table50%1/2
A
A= $bdef->{"dbi_attrs"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 764 | 4 | ▶ | my @files = grep { -f $_ } @{ $dir->list_tree({ hidden => 1 }) // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dir->list_tree({"hidden", 1}) // []
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})
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 770 | 0 | ▶ | return scalar grep { -f $_ } @{ $dir->list_tree({ hidden => 1 }) // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$dir->list_tree({"hidden", 1}) // []
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})
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 805 | 2 | $schema_class = $c->schema_class; | |||||||||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | 2 | ▶ | unless ($schema_class) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||