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

filter: Statementstmt 100% 65 / 65 Branchbran 89.2% 25 / 28 Conditioncond 62.0% 18 / 29 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 85.1% 132 / 155 CC 28 SCAR 21.9
CC
28
Cov
89%
diff
15.1
as_sql
10.0
_refuse_name_held_by_identity
4.7
CRAP 8.9
« blib/lib/DBIO/PostgreSQL/Introspect/Normalize.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Util.pm »
1 package DBIO::PostgreSQL::Diff::Sequence;
2 # ABSTRACT: Diff operations for PostgreSQL sequences
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::NoStatement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 103 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 103 ▶use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 103 __PACKAGE__->mk_diff_accessors(qw(schema_name sequence_name sequence_info));
16  
17  
18  
19  
20  
21  
22 144 ▶sub is_own {
Subroutine is_own: called CC=3 SCAR=11.0
Pod documented
23 144 my ($class, $info) = @_;
24 144 ▶ return $info && !$info->{is_identity} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$info && !$info->{'is_identity'} ? : 119 25 144
Condition100%3/3
$info and not $info->{"is_identity"}
!ll&&!rl&&r
187119
MC/DC100%2/2
$info and not $info->{"is_identity"}
$info not $info->{"is_identity"}
Truth table100%3/3
A and not(B)
A= $infoB= not $info->{"is_identity"}
ABresult
0X0
100
111
25 }
26  
27 1 ▶sub _refuse_name_held_by_identity {
Subroutine _refuse_name_held_by_identity: called CC=4 SCAR=15.5
28 1 my ($key, $old) = @_;
29 1 my $schema = $old->{schema_name};
30 1 my ($table, $column) = @{$old}{qw( owned_by_table_name owned_by_column )};
31  
32 1 ▶ my ($of, @way_out) = defined $schema && defined $table && defined $column
Branch: 1 branch
BranchTrueFalseTotal
defined $schema && defined $table && defined $column ? : 1 0 1
Condition33%2/6
defined $schema and defined $table and defined $column
!ll&&!rl&&r
001
defined $schema and defined $table
!ll&&!rl&&r
001
MC/DC0%0/3
defined $schema and defined $table and defined $column
defined $schema defined $table defined $column
Truth table25%1/4
A and B and C
A= defined $schemaB= defined $tableC= defined $column
ABCresult
0XX0
10X0
1100
1111
33 ? ( 'of column ' . join('.', $schema, $table, $column),
34 'Take the identity from the column yourself,',
35 sprintf(' ALTER TABLE %s ALTER COLUMN %s DROP IDENTITY;',
36 quote_qualified($schema, $table), quote_ident($column)),
37 'and diff again: the diff then creates the sequence and makes of the column what',
38 'the target has. The sequence it creates starts where the target has it start,',
39 'not where the identity stood.' )
40 : ( 'of a column',
41 'Take the identity from that column yourself (ALTER TABLE ... ALTER COLUMN ...',
42 'DROP IDENTITY) and diff again.' );
43  
44 1 die join "\n",
45 "refusing to diff: sequence $key is a sequence of its own in the target, and in the database that name is the identity sequence $of",
46 '',
47 'The sequence of an identity column is a part of the column: DROP IDENTITY takes',
48 'it and nothing else does, and until then CREATE SEQUENCE finds the name taken',
49 '(relation already exists). The diff creates a sequence ahead of everything it',
50 'does to a column. No statement was generated.',
51 '',
52 @way_out,
53 '';
54 }
55  
56  
57 26 ▶sub diff {
Subroutine diff: called CC=14 SCAR=27.1
Pod documented
58 26 my ($class, $source, $target, $source_schemas, $target_schemas) = @_;
59 26 my @ops;
60  
61 26 for my $key (sort keys %$target) {
62 27 my $new = $target->{$key};
63 27 ▶ next unless $class->is_own($new);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_own($new) 2 25 27
64  
65 25 my $old = $source->{$key};
66 25 ▶ next if $class->is_own($old);
Branch: 1 branch
BranchTrueFalseTotal
if $class->is_own($old) 15 10 25
67 10 ▶ _refuse_name_held_by_identity($key, $old) if $old;
Branch: 1 branch
BranchTrueFalseTotal
if $old 1 9 10
68  
69 9 my ($schema_name, $sequence_name)
70 = DBIO::PostgreSQL::Introspect->key_names($key, sequence_name => $new);
71 9 push @ops, $class->new(
72 action => 'create',
73 schema_name => $schema_name,
74 sequence_name => $sequence_name,
75 sequence_info => $new,
76 );
77 }
78  
79 # A schema is dropped without CASCADE: the sequences no column owns that
80 # stand in it are dropped ahead of it. One a column owns goes with that
81 # column's table, which the schema loses first.
82 25 ▶ if ($source_schemas && $target_schemas) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_schemas and $target_schemas) 25 0 25
Condition33%1/3
$source_schemas and $target_schemas
!ll&&!rl&&r
0025
MC/DC0%0/2
$source_schemas and $target_schemas
$source_schemas $target_schemas
Truth table33%1/3
A and B
A= $source_schemasB= $target_schemas
ABresult
0X0
100
111
83 25 for my $key (sort keys %$source) {
84 20 my $old = $source->{$key};
85 20 ▶ next unless $class->is_own($old) && !defined $old->{owned_by_table_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_own($old) and not defined $old->{"owned_by_table_name"} 16 4 20
Condition100%3/3
$class->is_own($old) and not defined $old->{"owned_by_table_name"}
!ll&&!rl&&r
1154
MC/DC100%2/2
$class->is_own($old) and not defined $old->{"owned_by_table_name"}
$class->is_own($old) not defined $old->{"owned_by_table_name"}
Truth table100%3/3
A and not(B)
A= $class->is_own($old)B= not defined $old->{"owned_by_table_name"}
ABresult
0X0
100
111
86 4 ▶ next if exists $target->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$key} 3 1 4
87 1 my $schema = $old->{schema_name};
88 1 ▶ next unless defined $schema && exists $source_schemas->{$schema} && !exists $target_schemas->{$schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema and exists $source_schemas->{$schema} and not exists $target_schemas->{$schema} 0 1 1
Condition33%2/6
defined $schema and exists $source_schemas->{$schema} and not exists $target_schemas->{$schema}
!ll&&!rl&&r
001
defined $schema and exists $source_schemas->{$schema}
!ll&&!rl&&r
001
MC/DC0%0/3
defined $schema and exists $source_schemas->{$schema} and not exists $target_schemas->{$schema}
defined $schema exists $source_schemas->{$schema} not exists $target_schemas->{$schema}
Truth table25%1/4
A and B and not(C)
A= defined $schemaB= exists $source_schemas->{$schema}C= not exists $target_schemas->{$schema}
ABCresult
0XX0
10X0
1100
1111
89 push @ops, $class->new(
90 action => 'drop',
91 schema_name => $schema,
92 sequence_name => $old->{sequence_name},
93 1 sequence_info => $old,
94 );
95 }
96 }
97  
98 25 return @ops;
99 }
100  
101  
102 14 ▶sub qualified_name {
Subroutine qualified_name: called CC=1 SCAR=0.0
Pod documented
103 14 my ($self) = @_;
104 14 return join '.', grep { defined } $self->schema_name, $self->sequence_name;
105 }
106  
107  
108 # The parameters of CREATE SEQUENCE, in the order they are written, each with
109 # the key of the model that has its value.
110 103 my @PARAMETERS = (
111 [ 'AS' => 'data_type' ],
112 [ 'INCREMENT BY' => 'increment' ],
113 [ 'MINVALUE' => 'min_value' ],
114 [ 'MAXVALUE' => 'max_value' ],
115 [ 'START WITH' => 'start_value' ],
116 [ 'CACHE' => 'cache_size' ],
117 );
118  
119 12 ▶sub as_sql {
Subroutine as_sql: called CC=10 SCAR=23.0
Pod documented
120 12 my ($self) = @_;
121 12 ▶ my $info = $self->sequence_info // {};
Condition100%2/2
$self->sequence_info // {}
l!l
93
MC/DC100%1/1
$self->sequence_info // {}
$self->sequence_info
Truth table100%2/2
A
A= $self->sequence_info
Aresult
11
00
122 12 my $object = 'sequence ' . $self->qualified_name;
123  
124 12 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->action // "") =~ /\A(?:create|drop)\z/ 1 11 12
Condition50%1/2
$self->action // ""
l!l
120
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
Aresult
11
00
125 unless ($self->action // '') =~ /\A(?:create|drop)\z/;
126 11 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object, 'it has no sequence_name')
Branch: 1 branch
BranchTrueFalseTotal
unless length($self->sequence_name // "") 1 10 11
Condition100%2/2
$self->sequence_name // ""
l!l
101
MC/DC100%1/1
$self->sequence_name // ""
$self->sequence_name
Truth table100%2/2
A
A= $self->sequence_name
Aresult
11
00
127 unless length($self->sequence_name // '');
128  
129 10 ▶ return 'DROP SEQUENCE ' . quote_qualified(grep { defined } $self->schema_name, $self->sequence_name) . ';'
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 2 8 10
130 if $self->action eq 'drop';
131  
132 return join(' ',
133 'CREATE SEQUENCE',
134 16 quote_qualified(grep { defined } $self->schema_name, $self->sequence_name),
135 37 ( map { "$_->[0] $info->{ $_->[1] }" }
136 48 ▶ grep { length($info->{ $_->[1] } // '') } @PARAMETERS ),
Branch: 2 branches
BranchTrueFalseTotal
defined $info->{'is_cycle'} ? : 6 2 8
$info->{'is_cycle'} ? : 1 5 6
Condition100%2/2
$$info{$_->[1]} // ""
l!l
3711
MC/DC100%1/1
$$info{$_->[1]} // ""
$$info{$_->[1]}
Truth table100%2/2
A
A= $$info{$_->[1]}
Aresult
11
00
137 8 ( defined $info->{is_cycle} ? ( $info->{is_cycle} ? 'CYCLE' : 'NO CYCLE' ) : () ),
138 ) . ';';
139 }
140  
141  
142 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
143 2 my ($self) = @_;
144 2 return sprintf ' %ssequence: %s', $self->summary_prefix, $self->qualified_name;
145 }
146  
147  
148 103 1;
149  
150 __END__
« blib/lib/DBIO/PostgreSQL/Introspect/Normalize.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Util.pm »