| 1 | package DBIO::Informix::Diff::Table; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for Informix tables | ||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 25 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 25 | ▶ | use DBIO::Exception; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | 25 | ▶ | use DBIO::Informix::Util qw(_quote_ident); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 25 | ▶ | use DBIO::Informix::ColumnDef qw(column_def_sql); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 25 | __PACKAGE__->mk_diff_accessors(qw(table_name table_info columns replaced)); | |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | 59 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 21 | 59 | my ($class, $source, $target, $target_columns) = @_; | |||||||||||||||||||||||||||||||||||||
| 22 | 59 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns //= {}
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | 59 | my @ops; | |||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | 59 | for my $name (sort keys %$target) { | |||||||||||||||||||||||||||||||||||||
| 27 | # A view that is in both with another definition is dropped and created. | ||||||||||||||||||||||||||||||||||||||
| 28 | 94 | ▶ | if (exists $source->{$name}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 29 | 71 | my ($old, $new) = ($source->{$name}, $target->{$name}); | |||||||||||||||||||||||||||||||||||||
| 30 | 71 | ▶ | if (($old->{kind} // 'table') ne ($new->{kind} // 'table')) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%4/4
$old->{"kind"} // "table"
$new->{"kind"} // "table"
MC/DC100%1/1
$old->{"kind"} // "table"
$old->{"kind"}
MC/DC100%1/1
$new->{"kind"} // "table"
$new->{"kind"}
Truth table100%2/2
A
A= $old->{"kind"}
Truth table100%2/2
A
A= $new->{"kind"}
| |||||||||||||||||||||||||||||||||||||||
| 31 | # DROP TABLE would take the rows, DROP VIEW is not what the table is: | ||||||||||||||||||||||||||||||||||||||
| 32 | # refused when the statement is asked for (as_sql), not simplified. | ||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my $op = $class->new(action => 'change', table_name => $name, table_info => $new); | |||||||||||||||||||||||||||||||||||||
| 34 | 2 | $op->{old_info} = $old; | |||||||||||||||||||||||||||||||||||||
| 35 | 2 | push @ops, $op; | |||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||
| 37 | ▶ | elsif ( (($new->{kind} // '') eq 'view' and ($old->{definition} // '') ne ($new->{definition} // ''))) { | |||||||||||||||||||||||||||||||||||||
|
Condition77%7/9
($new->{"kind"} // "") eq "view" and ($old->{"definition"} // "") ne ($new->{"definition"} // "")
$new->{"kind"} // ""
$old->{"definition"} // ""
$new->{"definition"} // ""
MC/DC50%1/2
($new->{"kind"} // "") eq "view" and ($old->{"definition"} // "") ne ($new->{"definition"} // "")
$new->{"kind"}
$old->{"definition"}
MC/DC0%0/1
$new->{"definition"} // ""
$new->{"definition"}
Truth table66%2/3
A and B
A= $new->{"kind"}B= $old->{"definition"}
Truth table50%1/2
A
A= $new->{"definition"}
| |||||||||||||||||||||||||||||||||||||||
| 38 | 2 | push @ops, $class->new(action => 'drop', table_name => $name, table_info => $old, replaced => 1); | |||||||||||||||||||||||||||||||||||||
| 39 | push @ops, $class->new(action => 'create', table_name => $name, table_info => $new, | ||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | columns => $target_columns->{$name} // []); | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns->{$name} // []
MC/DC100%1/1
$target_columns->{$name} // []
$target_columns->{$name}
Truth table100%2/2
A
A= $target_columns->{$name}
| |||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||
| 42 | 71 | next; | |||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||
| 44 | push @ops, $class->new( | ||||||||||||||||||||||||||||||||||||||
| 45 | action => 'create', | ||||||||||||||||||||||||||||||||||||||
| 46 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||
| 47 | table_info => $target->{$name}, | ||||||||||||||||||||||||||||||||||||||
| 48 | 23 | ▶ | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_columns->{$name} // []
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
| |||||||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | 59 | for my $name (sort keys %$source) { | |||||||||||||||||||||||||||||||||||||
| 53 | 82 | ▶ | next if exists $target->{$name}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 54 | push @ops, $class->new( | ||||||||||||||||||||||||||||||||||||||
| 55 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||
| 56 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||
| 57 | 11 | table_info => $source->{$name}, | |||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | 59 | return @ops; | |||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | 125 | ▶ | sub is_view { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
is_view: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 66 | 125 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 67 | 125 | ▶ | return 0 if $self->action eq 'change'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 68 | 119 | my $i = $self->table_info; | |||||||||||||||||||||||||||||||||||||
| 69 | 119 | ▶ | return (ref $i eq 'HASH' && ($i->{kind} // '') eq 'view') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
ref $i eq "HASH" and ($i->{"kind"} // "") eq "view"
$i->{"kind"} // ""
MC/DC50%1/2
ref $i eq "HASH" and ($i->{"kind"} // "") eq "view"
ref $i eq "HASH"
$i->{"kind"}
Truth table66%2/3
A and B
A= ref $i eq "HASH"B= $i->{"kind"}
| |||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | 31 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 74 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | 31 | ▶ | if ($self->action eq 'change') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | my $old = $self->{old_info}{kind} // 'table'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"old_info"}{"kind"} // "table"
MC/DC0%0/1
$self->{"old_info"}{"kind"} // "table"
$self->{"old_info"}{"kind"}
Truth table50%1/2
A
A= $self->{"old_info"}{"kind"}
| |||||||||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 79 | "%s: the database has a %s and the schema a %s (a table and view of one name); " | ||||||||||||||||||||||||||||||||||||||
| 80 | . "the rows of a table are not dropped to make a view, nor a view replaced by a table here", | ||||||||||||||||||||||||||||||||||||||
| 81 | $self->table_name, $old, $old eq 'view' ? 'table' : 'view'); | ||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | 29 | ▶ | if ($self->is_view) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 85 | 7 | ▶ | return sprintf 'DROP VIEW %s;', _quote_ident($self->table_name) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 86 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||
| 87 | # the definition is the CREATE VIEW the server keeps (owner-qualified) | ||||||||||||||||||||||||||||||||||||||
| 88 | 4 | my $def = $self->table_info->{definition}; | |||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | DBIO::Exception->throw(sprintf 'view %s: the model holds no definition to write it from', | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $def and length $def
MC/DC50%1/2
defined $def and length $def
defined $def
length $def
Truth table66%2/3
A and B
A= defined $defB= length $def
| |||||||||||||||||||||||||||||||||||||||
| 90 | $self->table_name) unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||||||
| 91 | 3 | return $def . ';'; | |||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | 22 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 95 | 6 | return sprintf 'DROP TABLE %s;', _quote_ident($self->table_name); | |||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||
| 98 | 16 | my @col_defs; | |||||||||||||||||||||||||||||||||||||
| 99 | my @pk_cols; | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 16 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||||||||||||||||||
| 102 | 28 | ▶ | push @pk_cols, $col if $col->{is_pk}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 103 | push @col_defs, ' ' . column_def_sql( | ||||||||||||||||||||||||||||||||||||||
| 104 | name => $col->{column_name}, | ||||||||||||||||||||||||||||||||||||||
| 105 | type => $col->{data_type}, | ||||||||||||||||||||||||||||||||||||||
| 106 | size => $col->{size}, | ||||||||||||||||||||||||||||||||||||||
| 107 | not_null => $col->{not_null}, | ||||||||||||||||||||||||||||||||||||||
| 108 | default_value => $col->{default_value}, | ||||||||||||||||||||||||||||||||||||||
| 109 | is_auto_increment => $col->{is_auto_increment}, | ||||||||||||||||||||||||||||||||||||||
| 110 | 28 | ); | |||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | # The PK is a table-level constraint, in key order (pk_position); | ||||||||||||||||||||||||||||||||||||||
| 114 | # columns without a position keep their table order. | ||||||||||||||||||||||||||||||||||||||
| 115 | 16 | ▶ | if (@pk_cols) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 116 | 11 | my @ordered = map { $_->{column_name} } | |||||||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | sort { ($a->{pk_position} || 0) <=> ($b->{pk_position} || 0) } @pk_cols; | ||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
$DBIO::Informix::Diff::Table::a->{'pk_position'} || 0
$DBIO::Informix::Diff::Table::b->{'pk_position'} || 0
MC/DC100%1/1
$DBIO::Informix::Diff::Table::a->{'pk_position'} || 0
$DBIO::Informix::Diff::Table::a->{'pk_position'}
MC/DC100%1/1
$DBIO::Informix::Diff::Table::b->{'pk_position'} || 0
$DBIO::Informix::Diff::Table::b->{'pk_position'}
Truth table100%2/2
A
A= $DBIO::Informix::Diff::Table::a->{'pk_position'}
Truth table100%2/2
A
A= $DBIO::Informix::Diff::Table::b->{'pk_position'}
| |||||||||||||||||||||||||||||||||||||||
| 118 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||||||||||||
| 119 | 9 | join(', ', map { _quote_ident($_) } @ordered); | |||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | 16 | return sprintf "CREATE TABLE %s (\n%s\n);", | |||||||||||||||||||||||||||||||||||||
| 123 | _quote_ident($self->table_name), join(",\n", @col_defs); | ||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||
| 127 | 9 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 128 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 129 | 9 | ▶ | return sprintf '%s %s: %s', $self->summary_prefix, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 130 | ($self->is_view ? 'view' : 'table'), $self->table_name; | ||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 25 | 1; | |||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||||||||||||