| 1 | package DBIO::PostgreSQL::Diff::SequenceOwner; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for the column a PostgreSQL sequence belongs to | ||||||||||||||||||||||||||||
| 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::Diff::Sequence (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 103 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 103 | ▶ | use DBIO::PostgreSQL::Util qw(quote_qualified); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 103 | __PACKAGE__->mk_diff_accessors(qw(schema_name sequence_name table_name column_name)); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # The column an entry of the model says the sequence belongs to, as a list | ||||||||||||||||||||||||||||
| 24 | # ( table, column ), or the empty list for none. The sequence of an identity | ||||||||||||||||||||||||||||
| 25 | # column is no sequence of its own and is not asked. | ||||||||||||||||||||||||||||
| 26 | 42 | ▶ | sub _owner { | ||||||||||||||||||||||||||
|
Subroutine
_owner: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 27 | 42 | my ($info) = @_; | |||||||||||||||||||||||||||
| 28 | 42 | ▶ | return unless DBIO::PostgreSQL::Diff::Sequence->is_own($info); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 34 | my ($table, $column) = @{$info}{qw( owned_by_table_name owned_by_column )}; | |||||||||||||||||||||||||||
| 30 | 34 | ▶ | return unless defined $table && defined $column; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $table and defined $column
MC/DC50%1/2
defined $table and defined $column
defined $table
defined $column
Truth table66%2/3
A and B
A= defined $tableB= defined $column
| |||||||||||||||||||||||||||||
| 31 | 27 | return ($table, $column); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 25 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 25 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||
| 37 | 25 | my @ops; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 25 | for my $key (sort keys %$target) { | |||||||||||||||||||||||||||
| 40 | 26 | my $new = $target->{$key}; | |||||||||||||||||||||||||||
| 41 | 26 | ▶ | next unless DBIO::PostgreSQL::Diff::Sequence->is_own($new); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 24 | ▶ | next unless exists $new->{owned_by_table_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 21 | my @is = _owner($new); | |||||||||||||||||||||||||||
| 45 | 21 | my @was = _owner($source->{$key}); | |||||||||||||||||||||||||||
| 46 | 21 | ▶ | next if @is == @was && !grep { $is[$_] ne $was[$_] } 0 .. $#is; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@is == @was and not grep({$is[$_] ne $was[$_];} 0 .. $#is)
MC/DC100%2/2
@is == @was and not grep({$is[$_] ne $was[$_];} 0 .. $#is)
@is == @was
not grep({$is[$_] ne $was[$_];} 0 .. $#is)
Truth table100%3/3
A and not(B)
A= @is == @wasB= not grep({$is[$_] ne $was[$_];} 0 .. $#is)
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 12 | my ($schema_name, $sequence_name) | |||||||||||||||||||||||||||
| 49 | = DBIO::PostgreSQL::Introspect->key_names($key, sequence_name => $new); | ||||||||||||||||||||||||||||
| 50 | 12 | push @ops, $class->new( | |||||||||||||||||||||||||||
| 51 | action => 'own', | ||||||||||||||||||||||||||||
| 52 | schema_name => $schema_name, | ||||||||||||||||||||||||||||
| 53 | sequence_name => $sequence_name, | ||||||||||||||||||||||||||||
| 54 | table_name => $is[0], | ||||||||||||||||||||||||||||
| 55 | column_name => $is[1], | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 25 | return @ops; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 16 | ▶ | sub qualified_name { | ||||||||||||||||||||||||||
|
Subroutine
qualified_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||
| 65 | 16 | return join '.', grep { defined } $self->schema_name, $self->sequence_name; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 14 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | 14 | my $object = 'sequence ' . $self->qualified_name; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 14 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->action // ""
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
| |||||||||||||||||||||||||||||
| 74 | unless ($self->action // '') eq 'own'; | ||||||||||||||||||||||||||||
| 75 | 13 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object, 'it has no sequence_name') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->sequence_name // ""
MC/DC100%1/1
$self->sequence_name // ""
$self->sequence_name
Truth table100%2/2
A
A= $self->sequence_name
| |||||||||||||||||||||||||||||
| 76 | unless length($self->sequence_name // ''); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 12 | ▶ | my @column = grep { length($_ // '') } $self->table_name, $self->column_name; | ||||||||||||||||||||||||||
|
Condition100%2/2
$_ // ""
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
| |||||||||||||||||||||||||||||
| 79 | 12 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | $self, $object, 'it names a table without a column or a column without a table', | ||||||||||||||||||||||||||||
| 81 | ) if @column == 1; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 11 | my @schema = grep { defined } $self->schema_name; | |||||||||||||||||||||||||||
| 84 | 11 | ▶ | return sprintf 'ALTER SEQUENCE %s OWNED BY %s;', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | quote_qualified(@schema, $self->sequence_name), | ||||||||||||||||||||||||||||
| 86 | ( @column ? quote_qualified(@schema, @column) : 'NONE' ); | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | 2 | my @column = grep { defined } $self->table_name, $self->column_name; | |||||||||||||||||||||||||||
| 93 | return sprintf ' ~sequence: %s (owned by %s)', $self->qualified_name, | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | ( @column ? join('.', grep { defined } $self->schema_name, @column) : 'none' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 103 | 1; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||