blib/lib/DBIO/PostgreSQL/Diff/Schema.pm

filter: Statementstmt 100% 70 / 70 Branchbran 94.4% 17 / 18 Conditioncond 66.6% 10 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 91.4% 118 / 129 CC 19 SCAR 13.4
CC
19
Cov
94%
diff
7.0
as_sql
5.0
_kept_extensions_by_schema
5.0
CRAP 3.8
« blib/lib/DBIO/PostgreSQL/PgSchema.pm ↑ Summary blib/lib/DBIO/PostgreSQL/InflateColumn/Vector.pm »
1 package DBIO::PostgreSQL::Diff::Schema;
2 # ABSTRACT: Diff operations for PostgreSQL schemas
3  
4 103 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 103 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 103 our $VERSION = '0.900007';
8  
9 103 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 103 ▶use DBIO::PostgreSQL::Diff::CaseOnly ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 103 ▶use DBIO::PostgreSQL::Diff::Dependents ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 103 ▶use DBIO::PostgreSQL::Diff::NoStatement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 103 ▶use DBIO::PostgreSQL::Util qw(quote_ident create_schema_if_missing);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 103 __PACKAGE__->mk_diff_accessors(qw(schema_name if_missing));
17  
18  
19  
20  
21  
22 # The extensions the diff keeps, by the schema the source has them in.
23 75 ▶sub _kept_extensions_by_schema {
Subroutine _kept_extensions_by_schema: called CC=5 SCAR=16.1
24 75 my ($source_extensions, $target_extensions) = @_;
25 75 my %kept;
26 75 ▶ for my $name (sort keys %{ $source_extensions // {} }) {
Condition100%2/2
$source_extensions // {}
l!l
696
MC/DC100%1/1
$source_extensions // {}
$source_extensions
Truth table100%2/2
A
A= $source_extensions
Aresult
11
00
27 9 ▶ next unless exists $target_extensions->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $target_extensions->{$name} 3 6 9
28 6 my $schema = $source_extensions->{$name}{schema_name};
29 6 ▶ push @{ $kept{$schema} }, $name if defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema 6 0 6
30 }
31 75 return \%kept;
32 }
33  
34 1 ▶sub _refuse_drop_under_extension {
Subroutine _refuse_drop_under_extension: called CC=1 SCAR=0.0
35 1 my ($schema, @extensions) = @_;
36 die join "\n",
37 sprintf(
38 'refusing to diff: schema %s is not in the target and would be dropped, and it holds %s, which the target keeps',
39 1 $schema, join(', ', map { "extension $_" } @extensions),
40 ),
41 '',
42 'An extension depends on the schema it is installed in: the server refuses',
43 'DROP SCHEMA ' . quote_ident($schema) . ' while the extension is in it, and CASCADE would drop the',
44 'extension too, and with the extension every column, index and',
45 'function that uses what it brings -- in tables this diff does not name. No',
46 'statement was generated.',
47 '',
48 'Either keep the schema (declare it with pg_schemas), or take the extension out',
49 'of it before the diff -- a relocatable extension moves, any other has to be',
50 'dropped and created again:',
51 1 ( map { ' ALTER EXTENSION ' . quote_ident($_) . ' SET SCHEMA ...;' } @extensions ),
52 '';
53 }
54  
55 76 ▶sub _refuse_case_only {
Subroutine _refuse_case_only: called CC=2 SCAR=7.0
56 76 my ($source, $target) = @_;
57 DBIO::PostgreSQL::Diff::CaseOnly->refuse(
58 'DROP SCHEMA comes after the drop of everything that is in the schema, tables and their rows included.',
59 map {
60 1 my ($old, $new) = map { $_->{name} } @$_;
61 {
62 1 kind => 'schema',
63 database => $old,
64 target => $new,
65 rename => sprintf('ALTER SCHEMA %s RENAME TO %s;', quote_ident($old), quote_ident($new)),
66 }
67 } DBIO::PostgreSQL::Diff::CaseOnly->pairs(
68 76 ▶ map { [ map { { in => [], name => $_ } } keys %{ $_ // {} } ] } $source, $target,
Condition50%1/2
$_ // {}
l!l
1520
MC/DC0%0/1
$_ // {}
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
69 ),
70 );
71 }
72  
73 76 ▶sub diff {
Subroutine diff: called CC=7 SCAR=19.5
Pod documented
74 76 my ($class, $source, $target, $source_extensions, $target_extensions, $source_dependents) = @_;
75  
76 76 _refuse_case_only($source, $target);
77  
78 75 my $kept = _kept_extensions_by_schema($source_extensions, $target_extensions);
79 75 ▶ for my $name (sort keys %{ $source // {} }) {
Condition50%1/2
$source // {}
l!l
750
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
80 108 ▶ next if exists $target->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$name} 96 12 108
81 12 ▶ _refuse_drop_under_extension($name, @{ $kept->{$name} }) if $kept->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $kept->{$name} 1 11 12
82 }
83  
84 # What stands in a schema and is not in the model is named, not left to the server.
85 DBIO::PostgreSQL::Diff::Dependents->refuse(map { {
86 object => "schema $_",
87 11 ▶ dependents => ($source_dependents // {})->{schemas}{$_},
Condition100%2/2
$source_dependents // {}
l!l
74
MC/DC100%1/1
$source_dependents // {}
$source_dependents
Truth table100%2/2
A
A= $source_dependents
Aresult
11
00
88 74 ▶ } } grep { !exists $target->{$_} } sort keys %{ $source // {} });
Condition50%1/2
$source // {}
l!l
740
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
89  
90 return $class->diff_toplevel(
91 $source, $target,
92 10 ▶ create => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
93 10 my ($name) = @_;
94 $class->new(action => 'create', schema_name => $name,
95 10 ▶ if_missing => ($target->{$name} && $target->{$name}{create_if_missing} ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$target->{$name} && $target->{$name}{'create_if_missing'} ? : 1 9 10
Condition66%2/3
$target->{$name} and $target->{$name}{"create_if_missing"}
!ll&&!rl&&r
091
MC/DC50%1/2
$target->{$name} and $target->{$name}{"create_if_missing"}
$target->{$name} $target->{$name}{"create_if_missing"}
Truth table66%2/3
A and B
A= $target->{$name}B= $target->{$name}{"create_if_missing"}
ABresult
0X0
100
111
96 },
97 10 ▶ drop => sub { my ($name) = @_; $class->new(action => 'drop', schema_name => $name) },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 73 );
99 }
100  
101  
102 63 ▶sub as_sql {
Subroutine as_sql: called CC=5 SCAR=16.1
Pod documented
103 63 my ($self) = @_;
104 63 ▶ if ($self->action eq 'create') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->action eq 'create') { } 32 31 63
elsif ($self->action eq 'drop') { } 30 1 31
105 32 ▶ return create_schema_if_missing($self->schema_name) . ';'
Branch: 1 branch
BranchTrueFalseTotal
if $self->if_missing 1 31 32
106 if $self->if_missing;
107 31 return sprintf 'CREATE SCHEMA %s;',
108 quote_ident($self->schema_name);
109 }
110 elsif ($self->action eq 'drop') {
111 30 return sprintf 'DROP SCHEMA %s;',
112 quote_ident($self->schema_name);
113 }
114 1 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw($self, 'schema ' . ($self->schema_name // ''));
Condition50%1/2
$self->schema_name // ""
l!l
10
MC/DC0%0/1
$self->schema_name // ""
$self->schema_name
Truth table50%1/2
A
A= $self->schema_name
Aresult
11
00
115 }
116  
117  
118 3 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
119 3 my ($self) = @_;
120 3 ▶ return sprintf ' %sschema: %s', ($self->action eq 'create' ? '+' : '-'), $self->schema_name;
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 1 2 3
121 }
122  
123 103 1;
124  
125 __END__
« blib/lib/DBIO/PostgreSQL/PgSchema.pm ↑ Summary blib/lib/DBIO/PostgreSQL/InflateColumn/Vector.pm »