| 1 | package DBIO::MSSQL::Diff::Column; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL columns | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 40 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use DBIO::MSSQL::DDL qw(_mssql_column_type _quote_ident _quote_table _bare_table); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use DBIO::MSSQL::Diff::Table (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use DBIO::Diff::Compare qw(changed_column_fields column_change_lossy); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 40 | __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 130 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 130 | my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # a view has no ALTER TABLE ... ADD/DROP COLUMN; Diff::View replaces it (karr k82) | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 130 | my %is_view = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||
| 26 | DBIO::MSSQL::Diff::Table->view_names($source_tables, $target_tables); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 130 | ▶ | if (%is_view) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 17 | ▶ | $source_cols = { map { $_ => $source_cols->{$_} } grep { !$is_view{$_} } keys %{ $source_cols // {} } }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source_cols // {}
MC/DC0%0/1
$source_cols // {}
$source_cols
Truth table50%1/2
A
A= $source_cols
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 17 | ▶ | $target_cols = { map { $_ => $target_cols->{$_} } grep { !$is_view{$_} } keys %{ $target_cols // {} } }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_cols // {}
MC/DC0%0/1
$target_cols // {}
$target_cols
Truth table50%1/2
A
A= $target_cols
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | return $class->diff_nested($source_cols, $target_cols, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | index_by => 'column_name', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | scope => 'both', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | source_tables => $source_tables, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | target_tables => $target_tables, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 179 | ▶ | changed_when => sub { scalar changed_column_fields($_[0], $_[1]) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | on_new => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 8 | my ($table, $name, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 8 | $class->new(action => 'add', table_name => $table, column_name => $name, new_info => $new); | |||||||||||||||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 60 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 60 | my ($table, $name, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 60 | $class->new(action => 'alter', table_name => $table, column_name => $name, | |||||||||||||||||||||||||||||||||||||||||
| 45 | old_info => $old, new_info => $new); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | my ($table, $name, $old) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 4 | $class->new(action => 'drop', table_name => $table, column_name => $name, old_info => $old); | |||||||||||||||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 130 | ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 40 | our %LOSSLESS_PAIRS; | |||||||||||||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 40 | my @ladder = qw(tinyint smallint int bigint); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 40 | for my $from (0 .. $#ladder - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 59 | 120 | $LOSSLESS_PAIRS{"$ladder[$from]->$ladder[$_]"} = 1 for $from + 1 .. $#ladder; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 40 | $LOSSLESS_PAIRS{'date->datetime2'} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # string types that become another one of the same content without loss while | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # the length stays or grows | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 40 | my %SAME_FAMILY = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||||||||||||
| 67 | char->varchar char->nchar char->nvarchar varchar->nvarchar nchar->nvarchar | ||||||||||||||||||||||||||||||||||||||||||
| 68 | binary->varbinary | ||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 40 | my %ALIAS = (integer => 'int', numeric => 'decimal', bool => 'bit', boolean => 'bit'); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 17 | ▶ | sub lossy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lossy: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 17 | ▶ | return 0 unless $self->action eq 'alter'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 16 | ▶ | return 0 unless $self->retypes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 12 | ▶ | my ($old, $new) = ($self->old_info // {}, $self->new_info // {}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$self->old_info // {}
$self->new_info // {}
MC/DC0%0/1
$self->old_info // {}
$self->old_info
MC/DC0%0/1
$self->new_info // {}
$self->new_info
Truth table50%1/2
A
A= $self->old_info
Truth table50%1/2
A
A= $self->new_info
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 12 | ▶ | my $base = sub { my $t = lc($_[0] // ''); $t =~ s/\s.*\z//s; $ALIAS{$t} // $t }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
Condition40%2/5
$_[0] // ""
$ALIAS{$t} // $t
MC/DC0%0/1
$_[0] // ""
$_[0]
MC/DC0%0/2
$ALIAS{$t} // $t
$ALIAS{$t}
$t
Truth table50%1/2
A
A= $_[0]
Truth table33%1/3
A // B
A= $ALIAS{$t}B= $t
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 12 | my ($ot, $nt) = ($base->($old->{data_type}), $base->($new->{data_type})); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 12 | ▶ | $nt = $ot unless length $nt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # max is longer than any number | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 24 | ▶ | my $len = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 24 | my $s = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 24 | ▶ | return $s unless defined $s && !ref $s && $s =~ /\A\s*-1\s*\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $s and not ref $s and $s =~ /\A\s*-1\s*\z/
defined $s and not ref $s
MC/DC66%2/3
defined $s and not ref $s and $s =~ /\A\s*-1\s*\z/
defined $s
not ref $s
$s =~ /\A\s*-1\s*\z/
Truth table75%3/4
A and not(B) and C
A= defined $sB= not ref $sC= $s =~ /\A\s*-1\s*\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | return 2**31; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 12 | my %o = (data_type => $ot, size => $len->($old->{size})); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 12 | my %n = (data_type => $nt, size => $len->($new->{size})); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 12 | ▶ | $n{data_type} = $ot if $SAME_FAMILY{"$ot->$nt"}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 12 | ▶ | return column_change_lossy(\%o, \%n, \%LOSSLESS_PAIRS) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 48 | ▶ | sub retypes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retypes: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 48 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 48 | ▶ | return 0 unless $self->action eq 'alter'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 41 | ▶ | my $old = $self->old_info || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->old_info || {}
MC/DC0%0/1
$self->old_info || {}
$self->old_info
Truth table50%1/2
A
A= $self->old_info
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 41 | ▶ | my $new = $self->new_info || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->new_info || {}
MC/DC0%0/1
$self->new_info || {}
$self->new_info
Truth table50%1/2
A
A= $self->new_info
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 41 | ▶ | my $want = sub { defined $new->{$_[0]} ? $new->{$_[0]} : $old->{$_[0]} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 41 | my $dt = $want->('data_type'); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 41 | ▶ | return 0 unless defined $dt && defined $old->{data_type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $dt and defined $old->{"data_type"}
MC/DC0%0/2
defined $dt and defined $old->{"data_type"}
defined $dt
defined $old->{"data_type"}
Truth table33%1/3
A and B
A= defined $dtB= defined $old->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 41 | my $nt = $self->_alter_column_type({ data_type => $dt, size => $want->('size') }, 'desired'); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 41 | my $ot = $self->_alter_column_type($old, 'live'); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 41 | ▶ | return $nt ne $ot ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 38 | ▶ | sub unbinds_dependents { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unbinds_dependents: called CC=12 SCAR=25.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 38 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 38 | ▶ | return 0 unless $self->action eq 'alter'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 31 | ▶ | my $old = $self->old_info || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->old_info || {}
MC/DC0%0/1
$self->old_info || {}
$self->old_info
Truth table50%1/2
A
A= $self->old_info
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 31 | ▶ | my $new = $self->new_info || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->new_info || {}
MC/DC0%0/1
$self->new_info || {}
$self->new_info
Truth table50%1/2
A
A= $self->new_info
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 31 | ▶ | my $want = sub { defined $new->{$_[0]} ? $new->{$_[0]} : $old->{$_[0]} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 31 | ▶ | my $new_nn = $want->('not_null') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 31 | ▶ | return 1 if $new_nn && !$old->{not_null}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$new_nn and not $old->{"not_null"}
MC/DC100%2/2
$new_nn and not $old->{"not_null"}
$new_nn
not $old->{"not_null"}
Truth table100%3/3
A and not(B)
A= $new_nnB= not $old->{"not_null"}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 19 | my $dt = $want->('data_type'); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 19 | ▶ | return 0 unless defined $dt && defined $old->{data_type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $dt and defined $old->{"data_type"}
MC/DC0%0/2
defined $dt and defined $old->{"data_type"}
defined $dt
defined $old->{"data_type"}
Truth table33%1/3
A and B
A= defined $dtB= defined $old->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 19 | my $nt = $self->_alter_column_type({ data_type => $dt, size => $want->('size') }, 'desired'); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 19 | my $ot = $self->_alter_column_type($old, 'live'); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 19 | ▶ | return 0 if $nt eq $ot; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 12 | my ($nb) = lc($nt) =~ /\A(\w+)/; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 12 | my ($ob) = lc($ot) =~ /\A(\w+)/; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | ▶ | return 0 if $nb eq $ob && $nb =~ /\A(?:nvarchar|varchar|varbinary)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$nb eq $ob and $nb =~ /\A(?:nvarchar|varchar|varbinary)\z/
MC/DC50%1/2
$nb eq $ob and $nb =~ /\A(?:nvarchar|varchar|varbinary)\z/
$nb eq $ob
$nb =~ /\A(?:nvarchar|varchar|varbinary)\z/
Truth table66%2/3
A and B
A= $nb eq $obB= $nb =~ /\A(?:nvarchar|varchar|varbinary)\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 67 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=25 SCAR=32.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 67 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 67 | my $tbl = _quote_table($self->table_name); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 67 | my $col = _quote_ident($self->column_name); | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 67 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | my $info = $self->new_info; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | my $type = _mssql_column_type($info, $self->table_name, $self->column_name); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 6 | my $sql = sprintf 'ALTER TABLE %s ADD %s %s', $tbl, $col, $type; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 6 | ▶ | $sql .= ' NOT NULL' if $info->{not_null}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 6 | ▶ | if (defined $info->{default_value}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $sql .= " DEFAULT $info->{default_value}"; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 6 | return "$sql;"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 60 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 4 | return sprintf 'ALTER TABLE %s DROP COLUMN %s;', $tbl, $col; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 56 | ▶ | if ($self->action eq 'alter') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 56 | ▶ | my $old = $self->old_info || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->old_info || {}
MC/DC0%0/1
$self->old_info || {}
$self->old_info
Truth table50%1/2
A
A= $self->old_info
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 56 | ▶ | my $new = $self->new_info || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->new_info || {}
MC/DC0%0/1
$self->new_info || {}
$self->new_info
Truth table50%1/2
A
A= $self->new_info
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 56 | my @stmts; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | # The desired state of one field, read the way the comparator that decided | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # this op exists reads it: a field the target leaves undef was not | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # prescribed, so the live value stands (DBIO::Diff::Compare, desired-state | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # contract). Per FIELD, not per hashref -- a target naming a type but no | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # size must keep the live length, because a bare nvarchar in a column | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # definition is not "unbounded", it is nvarchar(1) (karr k28). | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 168 | ▶ | my $want = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 168 | my ($field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 168 | ▶ | return defined $new->{$field} ? $new->{$field} : $old->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 56 | }; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 56 | my %want_type = (data_type => $want->('data_type'), size => $want->('size')); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 56 | ▶ | my $old_nn = $old->{not_null} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 56 | ▶ | my $new_nn = $want->('not_null') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # The RENDERED type is the trigger, not the bare data_type string: | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # INFORMATION_SCHEMA keeps the width in CHARACTER_MAXIMUM_LENGTH, so | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # nvarchar(64) -> nvarchar(128) has the same data_type on both sides and | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # fell through every branch, leaving as_sql returning the empty string | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # while the diff still reported a change (karr k30). | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 56 | my ($retype, $new_type) = (0, undef); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 56 | ▶ | if (defined $want_type{data_type}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 54 | $new_type = $self->_alter_column_type(\%want_type, 'desired'); | |||||||||||||||||||||||||||||||||||||||||
| 181 | $retype = !defined $old->{data_type} | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 52 | ▶ | || $new_type ne $self->_alter_column_type($old, 'live'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
!defined($old->{'data_type'}) || $new_type ne $self->_alter_column_type($old, 'live')
MC/DC50%1/2
!defined($old->{'data_type'}) || $new_type ne $self->_alter_column_type($old, 'live')
!defined($old->{'data_type'})
$new_type ne $self->_alter_column_type($old, 'live')
Truth table66%2/3
not(A) || B
A= !defined($old->{'data_type'})B= $new_type ne $self->_alter_column_type($old, 'live')
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | # One statement, always carrying both -- see L</ALTER COLUMN IS ONE | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # STATEMENT>. Emitted only when something in it actually moved, so a | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # default-only change does not drag a pointless retype along. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 54 | ▶ | if ($retype || $old_nn != $new_nn) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$retype or $old_nn != $new_nn
MC/DC100%2/2
$retype or $old_nn != $new_nn
$retype
$old_nn != $new_nn
Truth table100%3/3
A or B
A= $retypeB= $old_nn != $new_nn
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 45 | ▶ | DBIO::Exception->throw(sprintf( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | "DBIO::MSSQL: ALTER COLUMN of column '%s' of table '%s' (%s.%s) is " | ||||||||||||||||||||||||||||||||||||||||||
| 191 | . 'refused: Transact-SQL requires a new_data_type there and neither ' | ||||||||||||||||||||||||||||||||||||||||||
| 192 | . 'the desired nor the live column info names one', | ||||||||||||||||||||||||||||||||||||||||||
| 193 | $self->column_name, $self->table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 194 | $self->table_name, $self->column_name, | ||||||||||||||||||||||||||||||||||||||||||
| 195 | )) unless defined $new_type; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 43 | ▶ | push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s %s %s;', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | $tbl, $col, $new_type, ($new_nn ? 'NOT NULL' : 'NULL'); | ||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | # A default is a named CONSTRAINT in this dialect, never a clause of ALTER | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # COLUMN (karr k31). The drop is unshifted ahead of any retype because SQL | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # Server refuses to change the data type of a column that is "associated | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # with a default definition". | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 52 | my $new_d = $new->{default_value}; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 52 | ▶ | if (defined $new_d && length $new_d) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $new_d and length $new_d
MC/DC50%1/2
defined $new_d and length $new_d
defined $new_d
length $new_d
Truth table66%2/3
A and B
A= defined $new_dB= length $new_d
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 6 | my $old_d = $old->{default_value}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 6 | ▶ | if (!defined $old_d || $old_d ne $new_d) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $old_d or $old_d ne $new_d
MC/DC0%0/2
not defined $old_d or $old_d ne $new_d
not defined $old_d
$old_d ne $new_d
Truth table66%2/3
not(A) or B
A= not defined $old_dB= $old_d ne $new_d
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 6 | ▶ | if (defined $old_d && length $old_d) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $old_d and length $old_d
MC/DC50%1/2
defined $old_d and length $old_d
defined $old_d
length $old_d
Truth table66%2/3
A and B
A= defined $old_dB= length $old_d
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | my $live_name = $old->{default_constraint_name} | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 4 | ▶ | or DBIO::Exception->throw(sprintf( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | "DBIO::MSSQL: replacing the DEFAULT of column '%s' of table '%s' " | ||||||||||||||||||||||||||||||||||||||||||
| 213 | . '(%s.%s) is refused: Transact-SQL drops a default by ' | ||||||||||||||||||||||||||||||||||||||||||
| 214 | . 'CONSTRAINT NAME and the live column info carries no ' | ||||||||||||||||||||||||||||||||||||||||||
| 215 | . 'default_constraint_name', | ||||||||||||||||||||||||||||||||||||||||||
| 216 | $self->column_name, $self->table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 217 | $self->table_name, $self->column_name, | ||||||||||||||||||||||||||||||||||||||||||
| 218 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | unshift @stmts, sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', | |||||||||||||||||||||||||||||||||||||||||
| 220 | $tbl, _quote_ident($live_name); | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 4 | push @stmts, sprintf 'ALTER TABLE %s ADD CONSTRAINT %s DEFAULT %s FOR %s;', | |||||||||||||||||||||||||||||||||||||||||
| 223 | $tbl, _quote_ident($self->_default_constraint_name($new)), $new_d, $col; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | # A default the target no longer declares is removed (core karr k367, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | # karr k83): the comparator reports it, the live name drops it. A live | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # DEFAULT NULL counts as none, as in the comparator. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | elsif (_is_real_default($old->{default_value})) { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | my $live_name = $old->{default_constraint_name} | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 3 | ▶ | or DBIO::Exception->throw(sprintf( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | "DBIO::MSSQL: removing the DEFAULT of column '%s' of table '%s' " | ||||||||||||||||||||||||||||||||||||||||||
| 234 | . '(%s.%s) is refused: Transact-SQL drops a default by ' | ||||||||||||||||||||||||||||||||||||||||||
| 235 | . 'CONSTRAINT NAME and the live column info carries no ' | ||||||||||||||||||||||||||||||||||||||||||
| 236 | . 'default_constraint_name', | ||||||||||||||||||||||||||||||||||||||||||
| 237 | $self->column_name, $self->table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 238 | $self->table_name, $self->column_name, | ||||||||||||||||||||||||||||||||||||||||||
| 239 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | unshift @stmts, sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', | |||||||||||||||||||||||||||||||||||||||||
| 241 | $tbl, _quote_ident($live_name); | ||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 49 | return join "\n", @stmts; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # True for a live default that is one: not absent, not a literal NULL | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # ("(NULL)", "((NULL))", \'NULL'), the way DBIO::Diff::Compare reads it. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 46 | ▶ | sub _is_real_default { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_real_default: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 46 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 46 | ▶ | $v = $$v if ref($v) eq 'SCALAR' || ref($v) eq 'REF'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $v eq "SCALAR" or ref $v eq "REF"
MC/DC0%0/2
ref $v eq "SCALAR" or ref $v eq "REF"
ref $v eq "SCALAR"
ref $v eq "REF"
Truth table33%1/3
A or B
A= ref $v eq "SCALAR"B= ref $v eq "REF"
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 46 | ▶ | return 0 unless defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | $v =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 4 | ▶ | 1 while $v =~ s/\A\(\s*(.*?)\s*\)\z/$1/s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 4 | ▶ | return length $v && uc($v) ne 'NULL' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $v and uc $v ne "NULL"
MC/DC50%1/2
length $v and uc $v ne "NULL"
length $v
uc $v ne "NULL"
Truth table66%2/3
A and B
A= length $vB= uc $v ne "NULL"
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # _mssql_column_type with the column named in whatever it refuses. A type the | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # renderer cannot produce has no silent fallback here: the bare type name it | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # would otherwise return is a one-character column in T-SQL, so refusing is the | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # only reading that does not quietly change the data model. | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 226 | ▶ | sub _alter_column_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_alter_column_type: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 226 | my ($self, $info, $side) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 226 | my $type = eval { _mssql_column_type($info) }; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 226 | ▶ | return $type if defined $type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | ▶ | my $err = $@ || "unknown error\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || "unknown error\n"
MC/DC0%0/1
$@ || "unknown error\n"
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 2 | $err =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | DBIO::Exception->throw(sprintf | |||||||||||||||||||||||||||||||||||||||||
| 270 | "DBIO::MSSQL: ALTER COLUMN of column '%s' of table '%s' (%s.%s) is refused: " | ||||||||||||||||||||||||||||||||||||||||||
| 271 | . 'cannot render the %s type: %s', | ||||||||||||||||||||||||||||||||||||||||||
| 272 | $self->column_name, $self->table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 273 | $self->table_name, $self->column_name, $side, $err); | ||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | # The name the new DEFAULT constraint is created under. Named rather than left | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # to the server so a later diff can drop it without introspection; a target | ||||||||||||||||||||||||||||||||||||||||||
| 278 | # that carries a name of its own keeps it. | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 4 | ▶ | sub _default_constraint_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_default_constraint_name: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 4 | my ($self, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 281 | return $info->{default_constraint_name} | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 4 | ▶ | if defined $info->{default_constraint_name} && length $info->{default_constraint_name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $info->{"default_constraint_name"} and length $info->{"default_constraint_name"}
MC/DC0%0/2
defined $info->{"default_constraint_name"} and length $info->{"default_constraint_name"}
defined $info->{"default_constraint_name"}
length $info->{"default_constraint_name"}
Truth table33%1/3
A and B
A= defined $info->{"default_constraint_name"}B= length $info->{"default_constraint_name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 4 | return sprintf 'DF_%s_%s', _bare_table($self->table_name), $self->column_name; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | ▶ | my $type = $self->new_info ? ' (' . $self->new_info->{data_type} . ')' : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | return sprintf ' %scolumn: %s.%s%s', | |||||||||||||||||||||||||||||||||||||||||
| 291 | $self->summary_prefix, $self->table_name, $self->column_name, $type; | ||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | __END__ | ||||||||||||||||||||||||||||||||||||||||||