| 1 | package DBIO::Sybase::Diff::Column; | ||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff Sybase ASE columns | ||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 33 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 33 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 33 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 33 | ▶ | use DBIO::Diff::Compare qw(changed_column_fields); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 33 | ▶ | use DBIO::Sybase::DDL (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | 33 | ▶ | use DBIO::Sybase::Quote (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | 89 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=17 SCAR=28.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 18 | 89 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||
| 19 | 89 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||||||||||||||||||||||||||
| 20 | 89 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||||||||||||||||||||||||||
| 21 | 89 | my @ops; | |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | 89 | for my $table (sort keys %$target) { | |||||||||||||||||||||||||||||||||||
| 24 | # A table this diff is CREATING already carries every one of its columns in | ||||||||||||||||||||||||||||||||||||
| 25 | # the CREATE TABLE that DBIO::Sybase::Diff::Table emits (it is handed | ||||||||||||||||||||||||||||||||||||
| 26 | # $target->{columns} for exactly that). Adding them again here produced a | ||||||||||||||||||||||||||||||||||||
| 27 | # migration that created the column and then added it a second time -- | ||||||||||||||||||||||||||||||||||||
| 28 | # "CREATE TABLE fresh (id INT NOT NULL); ALTER TABLE fresh ADD id INT" -- | ||||||||||||||||||||||||||||||||||||
| 29 | # which ASE rejects as a duplicate column name. The two table models were | ||||||||||||||||||||||||||||||||||||
| 30 | # already being passed in for this guard and were accepted and ignored. | ||||||||||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||||||||||
| 32 | # The condition mirrors Diff::Table's own ("create when the table is not in | ||||||||||||||||||||||||||||||||||||
| 33 | # the source"), but is stated positively -- absent from source AND present | ||||||||||||||||||||||||||||||||||||
| 34 | # in target -- so that a caller supplying no table model at all (the | ||||||||||||||||||||||||||||||||||||
| 35 | # column-level unit tests pass {}, {}) knows nothing to be new and keeps | ||||||||||||||||||||||||||||||||||||
| 36 | # diffing every table, rather than silently skipping all of them. | ||||||||||||||||||||||||||||||||||||
| 37 | 97 | ▶ | next if !exists $source_tables->{$table} && exists $target_tables->{$table}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $source_tables->{$table} and exists $target_tables->{$table}
MC/DC100%2/2
not exists $source_tables->{$table} and exists $target_tables->{$table}
not exists $source_tables->{$table}
exists $target_tables->{$table}
Truth table100%3/3
not(A) and B
A= not exists $source_tables->{$table}B= exists $target_tables->{$table}
| |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | # The columns of a view are what its query selects: a changed view is | ||||||||||||||||||||||||||||||||||||
| 40 | # dropped and created again by DBIO::Sybase::Diff::View, ALTER TABLE on | ||||||||||||||||||||||||||||||||||||
| 41 | # a view is Msg 4909 (karr k98). | ||||||||||||||||||||||||||||||||||||
| 42 | next if ($source_tables->{$table}{kind} // '') eq 'view' | ||||||||||||||||||||||||||||||||||||
| 43 | 92 | ▶ | || ($target_tables->{$table}{kind} // '') eq 'view'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition85%6/7
($source_tables->{$table}{"kind"} // "") eq "view" or ($target_tables->{$table}{"kind"} // "") eq "view"
$source_tables->{$table}{"kind"} // ""
$target_tables->{$table}{"kind"} // ""
MC/DC50%1/2
($source_tables->{$table}{"kind"} // "") eq "view" or ($target_tables->{$table}{"kind"} // "") eq "view"
$source_tables->{$table}{"kind"}
$target_tables->{$table}{"kind"}
Truth table66%2/3
A or B
A= $source_tables->{$table}{"kind"}B= $target_tables->{$table}{"kind"}
| |||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | 89 | ▶ | my $s_cols = $source->{$table} // []; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{$table} // []
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
| |||||||||||||||||||||||||||||||||||||
| 46 | 89 | ▶ | my $t_cols = $target->{$table} // []; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target->{$table} // []
MC/DC0%0/1
$target->{$table} // []
$target->{$table}
Truth table50%1/2
A
A= $target->{$table}
| |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 89 | my %s_idx = map { $_->{column_name} => $_ } @$s_cols; | |||||||||||||||||||||||||||||||||||
| 49 | 89 | my %t_idx = map { $_->{column_name} => $_ } @$t_cols; | |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | 89 | for my $name (sort keys %t_idx) { | |||||||||||||||||||||||||||||||||||
| 52 | 161 | ▶ | if (exists $s_idx{$name}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | push @ops, DBIO::Sybase::Diff::Column::Alter->new( | ||||||||||||||||||||||||||||||||||||
| 54 | action => 'alter', | ||||||||||||||||||||||||||||||||||||
| 55 | table => $table, | ||||||||||||||||||||||||||||||||||||
| 56 | from => $s_idx{$name}, | ||||||||||||||||||||||||||||||||||||
| 57 | to => $t_idx{$name}, | ||||||||||||||||||||||||||||||||||||
| 58 | ) if changed_column_fields( | ||||||||||||||||||||||||||||||||||||
| 59 | 140 | ▶ | _comparable($s_idx{$name}), _comparable($t_idx{$name})); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||||||||||
| 62 | push @ops, DBIO::Sybase::Diff::Column::Add->new( | ||||||||||||||||||||||||||||||||||||
| 63 | action => 'add', | ||||||||||||||||||||||||||||||||||||
| 64 | table => $table, | ||||||||||||||||||||||||||||||||||||
| 65 | 21 | col => $t_idx{$name}, | |||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | 89 | for my $name (sort keys %s_idx) { | |||||||||||||||||||||||||||||||||||
| 71 | push @ops, DBIO::Sybase::Diff::Column::Drop->new( | ||||||||||||||||||||||||||||||||||||
| 72 | action => 'drop', | ||||||||||||||||||||||||||||||||||||
| 73 | table => $table, | ||||||||||||||||||||||||||||||||||||
| 74 | col => $s_idx{$name}, | ||||||||||||||||||||||||||||||||||||
| 75 | 147 | ▶ | ) unless exists $t_idx{$name}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | 89 | return @ops; | |||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||
| 82 | # A column as the comparator should see it. An expression default is a SCALAR | ||||||||||||||||||||||||||||||||||||
| 83 | # ref (DBIO::Sybase::Introspect::Columns reads `DEFAULT getdate()` that way), | ||||||||||||||||||||||||||||||||||||
| 84 | # and core compares default_value as a string -- a ref's string is its | ||||||||||||||||||||||||||||||||||||
| 85 | # address, so the live and the desired getdate() would differ on every diff. | ||||||||||||||||||||||||||||||||||||
| 86 | # Compare the SQL behind the ref instead, marked so it never equals the plain | ||||||||||||||||||||||||||||||||||||
| 87 | # literal of the same text (karr k48). | ||||||||||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||||||||||
| 89 | # One expression is not an expression at all: a bare number on a numeric | ||||||||||||||||||||||||||||||||||||
| 90 | # column. \'0' and the literal 0 render the same DEFAULT 0 there (karr k55), and | ||||||||||||||||||||||||||||||||||||
| 91 | # the catalog reads that text back as the literal, so the ref compares as the | ||||||||||||||||||||||||||||||||||||
| 92 | # literal it is equivalent to. | ||||||||||||||||||||||||||||||||||||
| 93 | 280 | ▶ | sub _comparable { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_comparable: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 94 | 280 | my ($col) = @_; | |||||||||||||||||||||||||||||||||||
| 95 | 280 | my $dv = $col->{default_value}; | |||||||||||||||||||||||||||||||||||
| 96 | 280 | ▶ | return $col unless ref $dv eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 97 | return { %$col, default_value => $$dv } | ||||||||||||||||||||||||||||||||||||
| 98 | if DBIO::Sybase::DDL::_is_numeric_type($col->{data_type}) | ||||||||||||||||||||||||||||||||||||
| 99 | 10 | ▶ | && DBIO::Sybase::DDL::_is_numeric_literal($$dv); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
DBIO::Sybase::DDL::_is_numeric_type($col->{"data_type"}) and DBIO::Sybase::DDL::_is_numeric_literal($$dv)
MC/DC100%2/2
DBIO::Sybase::DDL::_is_numeric_type($col->{"data_type"}) and DBIO::Sybase::DDL::_is_numeric_literal($$dv)
DBIO::Sybase::DDL::_is_numeric_type($col->{"data_type"})
DBIO::Sybase::DDL::_is_numeric_literal($$dv)
Truth table100%3/3
A and B
A= DBIO::Sybase::DDL::_is_numeric_type($col->{"data_type"})B= DBIO::Sybase::DDL::_is_numeric_literal($$dv)
| |||||||||||||||||||||||||||||||||||||
| 100 | 9 | return { %$col, default_value => "\0sql:$$dv" }; | |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||
| 103 | package DBIO::Sybase::Diff::Column::Add; | ||||||||||||||||||||||||||||||||||||
| 104 | 33 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 33 | __PACKAGE__->mk_diff_accessors(qw(table col)); | |||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | 23 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 109 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 110 | 23 | my $col = $self->col; | |||||||||||||||||||||||||||||||||||
| 111 | 23 | my $name = $col->{column_name}; | |||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | my $type = DBIO::Sybase::DDL::sybase_column_type( | ||||||||||||||||||||||||||||||||||||
| 114 | 23 | $col->{data_type}, $col->{size}); | |||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | # Clause order is the grammar's, not Transact-SQL's: ASE puts the default | ||||||||||||||||||||||||||||||||||||
| 117 | # BEFORE the nullability -- see L</THE ADD CLAUSE ORDER IS DEFAULT, THEN | ||||||||||||||||||||||||||||||||||||
| 118 | # NULLABILITY>. sybase_default_clause carries its own leading space and | ||||||||||||||||||||||||||||||||||||
| 119 | # renders '' for both an absent default and the literal 'null'. | ||||||||||||||||||||||||||||||||||||
| 120 | my $default = DBIO::Sybase::DDL::sybase_default_clause( | ||||||||||||||||||||||||||||||||||||
| 121 | 23 | $col->{default_value}, $col->{data_type}, $name); | |||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | # A not-null add without a default is not a statement ASE will run, so it is | ||||||||||||||||||||||||||||||||||||
| 124 | # refused here rather than sent -- see L</A NOT NULL ADD CARRIES ITS | ||||||||||||||||||||||||||||||||||||
| 125 | # DEFAULT>. The test is the RENDERED clause, not $col->{default_value}: a | ||||||||||||||||||||||||||||||||||||
| 126 | # default_value of 'null' renders nothing, which leaves exactly the statement | ||||||||||||||||||||||||||||||||||||
| 127 | # the server rejects, and a contradiction ("no nulls, default null") worth | ||||||||||||||||||||||||||||||||||||
| 128 | # naming rather than passing on. | ||||||||||||||||||||||||||||||||||||
| 129 | DBIO::Exception->throw(sprintf( | ||||||||||||||||||||||||||||||||||||
| 130 | 'DBIO::Sybase: table %s, column %s: cannot ALTER TABLE %s ADD %s -- the add ' | ||||||||||||||||||||||||||||||||||||
| 131 | . 'is refused: ASE requires a default ' | ||||||||||||||||||||||||||||||||||||
| 132 | . 'clause on a not-null add (Reference Manual: Commands, alter table: ' | ||||||||||||||||||||||||||||||||||||
| 133 | . '"If you specify (or imply) not null for the newly added column, a ' | ||||||||||||||||||||||||||||||||||||
| 134 | . 'default clause is required") and the desired column names no usable ' | ||||||||||||||||||||||||||||||||||||
| 135 | . 'default_value', | ||||||||||||||||||||||||||||||||||||
| 136 | $self->table, $name, $self->table, $name, | ||||||||||||||||||||||||||||||||||||
| 137 | 23 | ▶ | )) if $col->{not_null} && !length $default; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$col->{"not_null"} and not length $default
MC/DC100%2/2
$col->{"not_null"} and not length $default
$col->{"not_null"}
not length $default
Truth table100%3/3
A and not(B)
A= $col->{"not_null"}B= not length $default
| |||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||
| 139 | # ... and the default of a not-null add is a constant: a function such as | ||||||||||||||||||||||||||||||||||||
| 140 | # getdate() is Msg 13918 ("Default cannot be an expression when adding | ||||||||||||||||||||||||||||||||||||
| 141 | # non-NULL column"), after which Msg 11050 aborts the statement (karr k97). | ||||||||||||||||||||||||||||||||||||
| 142 | DBIO::Exception->throw(sprintf( | ||||||||||||||||||||||||||||||||||||
| 143 | 'DBIO::Sybase: table %s, column %s: ALTER TABLE ADD of a NOT NULL column ' | ||||||||||||||||||||||||||||||||||||
| 144 | . 'with an expression default is refused: ASE cannot add a NOT NULL column ' | ||||||||||||||||||||||||||||||||||||
| 145 | . 'with an expression default (Msg 13918). Give the column a constant ' | ||||||||||||||||||||||||||||||||||||
| 146 | . 'default, or add it as nullable', | ||||||||||||||||||||||||||||||||||||
| 147 | $self->table, $name, | ||||||||||||||||||||||||||||||||||||
| 148 | 20 | ▶ | )) if $col->{not_null} && ref $col->{default_value} eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$col->{"not_null"} and ref $col->{"default_value"} eq "SCALAR"
MC/DC100%2/2
$col->{"not_null"} and ref $col->{"default_value"} eq "SCALAR"
$col->{"not_null"}
ref $col->{"default_value"} eq "SCALAR"
Truth table100%3/3
A and B
A= $col->{"not_null"}B= ref $col->{"default_value"} eq "SCALAR"
| |||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||
| 150 | # Always spelled out, never omitted -- an add that says nothing is not | ||||||||||||||||||||||||||||||||||||
| 151 | # neutral, it inherits the database's null default (see L</NULLABILITY IS | ||||||||||||||||||||||||||||||||||||
| 152 | # ALWAYS SPELLED OUT>). Read straight from the target: an add has no live | ||||||||||||||||||||||||||||||||||||
| 153 | # column to fall back to, and this is the same truthiness | ||||||||||||||||||||||||||||||||||||
| 154 | # DBIO::Sybase::Diff::Table::Create reads the field with. | ||||||||||||||||||||||||||||||||||||
| 155 | sprintf 'ALTER TABLE %s ADD %s %s%s %s;', | ||||||||||||||||||||||||||||||||||||
| 156 | DBIO::Sybase::Quote::quote_ident($self->table), | ||||||||||||||||||||||||||||||||||||
| 157 | DBIO::Sybase::Quote::quote_ident($name), $type, $default, | ||||||||||||||||||||||||||||||||||||
| 158 | 19 | ▶ | ($col->{not_null} ? 'NOT NULL' : 'NULL'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||
| 160 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 161 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 162 | 3 | "ALTER TABLE $self->{table} ADD $self->{col}{column_name}"; | |||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | package DBIO::Sybase::Diff::Column::Alter; | ||||||||||||||||||||||||||||||||||||
| 166 | 33 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||
| 168 | 33 | __PACKAGE__->mk_diff_accessors(qw(table from to)); | |||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||
| 170 | 35 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||
| 171 | 35 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 172 | 35 | my $tbl = DBIO::Sybase::Quote::quote_ident($self->table); | |||||||||||||||||||||||||||||||||||
| 173 | 35 | ▶ | my $from = $self->from // {}; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->from // {}
MC/DC0%0/1
$self->from // {}
$self->from
Truth table50%1/2
A
A= $self->from
| |||||||||||||||||||||||||||||||||||||
| 174 | 35 | my $to = $self->to; | |||||||||||||||||||||||||||||||||||
| 175 | 35 | my $col_name = $to->{column_name}; | |||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | # The desired state of one field, read the way the comparator that decided | ||||||||||||||||||||||||||||||||||||
| 178 | # this op exists reads it: a field the target leaves undef was not | ||||||||||||||||||||||||||||||||||||
| 179 | # prescribed, so the live value stands (DBIO::Diff::Compare's desired-state | ||||||||||||||||||||||||||||||||||||
| 180 | # skip, see L</THE DESIRED-STATE CONTRACT APPLIES TO RENDERING TOO>). Per | ||||||||||||||||||||||||||||||||||||
| 181 | # FIELD, not per hashref -- a target naming a type but no size keeps the | ||||||||||||||||||||||||||||||||||||
| 182 | # live width, because a bare VARCHAR in an ASE column definition is not | ||||||||||||||||||||||||||||||||||||
| 183 | # "unbounded" (karr k35). | ||||||||||||||||||||||||||||||||||||
| 184 | 69 | ▶ | my $want = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 185 | 69 | my ($field) = @_; | |||||||||||||||||||||||||||||||||||
| 186 | 69 | ▶ | return defined $to->{$field} ? $to->{$field} : $from->{$field}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 187 | 35 | }; | |||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||
| 189 | 35 | my $replace = $self->_replace_sql; | |||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | # A default-only change is the replace and nothing else (karr k42): the | ||||||||||||||||||||||||||||||||||||
| 192 | # modify would restate a type, width and nullability the comparator found | ||||||||||||||||||||||||||||||||||||
| 193 | # unmoved, at the price of a data copy, and live on ASE 16.0 a replace takes | ||||||||||||||||||||||||||||||||||||
| 194 | # effect without one -- see L</A DEFAULT-ONLY CHANGE IS THE REPLACE ALONE>. | ||||||||||||||||||||||||||||||||||||
| 195 | 35 | ▶ | return $replace if $self->_default_only; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||
| 197 | 23 | my $type = DBIO::Sybase::DDL::sybase_column_type( | |||||||||||||||||||||||||||||||||||
| 198 | $want->('data_type'), $want->('size')); | ||||||||||||||||||||||||||||||||||||
| 199 | # MODIFY, not ALTER COLUMN: the latter is Transact-SQL and parses on no ASE | ||||||||||||||||||||||||||||||||||||
| 200 | # (karr k38). Nothing stands between the table name and the keyword -- see | ||||||||||||||||||||||||||||||||||||
| 201 | # L</THE ALTER IS A MODIFY, THE DEFAULT IS A SEPARATE REPLACE>. | ||||||||||||||||||||||||||||||||||||
| 202 | 23 | my $sql = sprintf 'ALTER TABLE %s MODIFY %s %s', $tbl, | |||||||||||||||||||||||||||||||||||
| 203 | DBIO::Sybase::Quote::quote_ident($col_name), $type; | ||||||||||||||||||||||||||||||||||||
| 204 | # Stated on every alter, never omitted -- see L</NULLABILITY IS ALWAYS | ||||||||||||||||||||||||||||||||||||
| 205 | # SPELLED OUT>. Omitting it does not preserve the column's nullability. | ||||||||||||||||||||||||||||||||||||
| 206 | 23 | ▶ | $sql .= $want->('not_null') ? ' NOT NULL;' : ' NULL;'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||
| 208 | # The default is NOT a clause of `modify`; it is its own subclause, and one | ||||||||||||||||||||||||||||||||||||
| 209 | # that cannot share a statement with the modify anyway (the manual allows | ||||||||||||||||||||||||||||||||||||
| 210 | # several subclauses per statement only for add/drop/modify/lock, and only | ||||||||||||||||||||||||||||||||||||
| 211 | # while no column is named twice). So it renders as a second statement, | ||||||||||||||||||||||||||||||||||||
| 212 | # separated by ';' -- what DBIO::Deploy::Base::_execute_ddl splits on, and | ||||||||||||||||||||||||||||||||||||
| 213 | # the same shape Diff::Index::Alter emits for its DROP/CREATE pair. | ||||||||||||||||||||||||||||||||||||
| 214 | 23 | ▶ | $sql .= " $replace" if length $replace; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||
| 216 | 23 | $sql; | |||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||
| 219 | # The replace statement, or '' when there is nothing to replace. | ||||||||||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||||||||||
| 221 | # Emitted only on an actual change, and read from the target only, never | ||||||||||||||||||||||||||||||||||||
| 222 | # through the desired-state fallback. Restating an unchanged default is not a | ||||||||||||||||||||||||||||||||||||
| 223 | # harmless no-op here: on a column carrying an sp_bindefault-bound default ASE | ||||||||||||||||||||||||||||||||||||
| 224 | # rejects the statement outright. An absent target default prescribes nothing | ||||||||||||||||||||||||||||||||||||
| 225 | # (core's comparator never reports a dropped default), and the compared value | ||||||||||||||||||||||||||||||||||||
| 226 | # is the RENDERED clause, so the literal 'null' -- which renders as nothing -- | ||||||||||||||||||||||||||||||||||||
| 227 | # emits nothing, and two expression defaults compare by their SQL rather than | ||||||||||||||||||||||||||||||||||||
| 228 | # by the address of their SCALAR refs. | ||||||||||||||||||||||||||||||||||||
| 229 | 50 | ▶ | sub _replace_sql { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_replace_sql: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||
| 230 | 50 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 231 | 50 | ▶ | my $from = $self->from // {}; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->from // {}
MC/DC0%0/1
$self->from // {}
$self->from
Truth table50%1/2
A
A= $self->from
| |||||||||||||||||||||||||||||||||||||
| 232 | 50 | my $to = $self->to; | |||||||||||||||||||||||||||||||||||
| 233 | # Each side renders under its OWN type (karr k55): a varchar column whose | ||||||||||||||||||||||||||||||||||||
| 234 | # default '0' is retyped to int keeps its default_value, but its live clause | ||||||||||||||||||||||||||||||||||||
| 235 | # DEFAULT '0' is the VARCHAR constant an INT column cannot insert, so the | ||||||||||||||||||||||||||||||||||||
| 236 | # replace must follow the modify. The target's type falls back to the live | ||||||||||||||||||||||||||||||||||||
| 237 | # one exactly as the modify reads it -- an unprescribed type did not move. | ||||||||||||||||||||||||||||||||||||
| 238 | 50 | ▶ | my $want_type = defined $to->{data_type} ? $to->{data_type} : $from->{data_type}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 239 | my $want_default = DBIO::Sybase::DDL::sybase_default_clause( | ||||||||||||||||||||||||||||||||||||
| 240 | 50 | $to->{default_value}, $want_type, $to->{column_name}); | |||||||||||||||||||||||||||||||||||
| 241 | my $live_default = DBIO::Sybase::DDL::sybase_default_clause( | ||||||||||||||||||||||||||||||||||||
| 242 | 50 | $from->{default_value}, $from->{data_type}, $to->{column_name}); | |||||||||||||||||||||||||||||||||||
| 243 | # A default the target no longer declares is removed, not kept (karr k97, | ||||||||||||||||||||||||||||||||||||
| 244 | # core karr k367: the comparator reports it, and the live DEFAULT NULL counts | ||||||||||||||||||||||||||||||||||||
| 245 | # as none). The Reference Manual's own example is | ||||||||||||||||||||||||||||||||||||
| 246 | # `alter table authors replace phone default null`. | ||||||||||||||||||||||||||||||||||||
| 247 | 50 | ▶ | $want_default = ' DEFAULT NULL' | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not length $want_default and length $live_default
MC/DC100%2/2
not length $want_default and length $live_default
not length $want_default
length $live_default
Truth table100%3/3
not(A) and B
A= not length $want_defaultB= length $live_default
| |||||||||||||||||||||||||||||||||||||
| 248 | if !length $want_default && length $live_default; | ||||||||||||||||||||||||||||||||||||
| 249 | 50 | ▶ | return '' unless length $want_default && $want_default ne $live_default; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $want_default and $want_default ne $live_default
MC/DC100%2/2
length $want_default and $want_default ne $live_default
length $want_default
$want_default ne $live_default
Truth table100%3/3
A and B
A= length $want_defaultB= $want_default ne $live_default
| |||||||||||||||||||||||||||||||||||||
| 250 | return sprintf 'ALTER TABLE %s REPLACE %s%s;', | ||||||||||||||||||||||||||||||||||||
| 251 | DBIO::Sybase::Quote::quote_ident($self->table), | ||||||||||||||||||||||||||||||||||||
| 252 | 31 | DBIO::Sybase::Quote::quote_ident($to->{column_name}), $want_default; | |||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | # True when the replace is the whole statement: the comparator reports nothing | ||||||||||||||||||||||||||||||||||||
| 256 | # but default_value as changed (the same exported call diff() makes, not a | ||||||||||||||||||||||||||||||||||||
| 257 | # re-implementation of it -- a set test, since the list comes in spec-group | ||||||||||||||||||||||||||||||||||||
| 258 | # order) AND there is a replace to emit. Without the second condition a | ||||||||||||||||||||||||||||||||||||
| 259 | # default-only change that renders no replace -- the literal 'null' -- would | ||||||||||||||||||||||||||||||||||||
| 260 | # leave an op with no SQL at all; it falls back to the modify it has always | ||||||||||||||||||||||||||||||||||||
| 261 | # rendered instead. | ||||||||||||||||||||||||||||||||||||
| 262 | 41 | ▶ | sub _default_only { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_default_only: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 263 | 41 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 264 | 41 | ▶ | my @moved = grep { $_ ne 'default_value' } | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->from // {}
MC/DC0%0/1
$self->from // {}
$self->from
Truth table50%1/2
A
A= $self->from
| |||||||||||||||||||||||||||||||||||||
| 265 | DBIO::Diff::Compare::changed_column_fields($self->from // {}, $self->to); | ||||||||||||||||||||||||||||||||||||
| 266 | 41 | ▶ | return !@moved && length $self->_replace_sql; | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
!@moved && length $self->_replace_sql
MC/DC100%2/2
!@moved && length $self->_replace_sql
!@moved
length $self->_replace_sql
Truth table100%3/3
not(A) && B
A= !@movedB= length $self->_replace_sql
| |||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | 33 | my %LOSSLESS_PAIRS = map { ($_ => 1) } ( | |||||||||||||||||||||||||||||||||||
| 271 | 'tinyint->smallint', 'tinyint->int', 'tinyint->bigint', | ||||||||||||||||||||||||||||||||||||
| 272 | 'smallint->int', 'smallint->bigint', 'int->bigint', | ||||||||||||||||||||||||||||||||||||
| 273 | ); | ||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||
| 275 | 33 | ▶ | sub lossy { | ||||||||||||||||||||||||||||||||||
|
Subroutine
lossy: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||
| 276 | 33 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 277 | 33 | ▶ | return 0 unless $self->action eq 'alter'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 278 | 33 | ▶ | my ($old, $new) = ($self->from // {}, $self->to); | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->from // {}
MC/DC0%0/1
$self->from // {}
$self->from
Truth table50%1/2
A
A= $self->from
| |||||||||||||||||||||||||||||||||||||
| 279 | return DBIO::Diff::Compare::column_change_lossy( | ||||||||||||||||||||||||||||||||||||
| 280 | { data_type => $old->{data_type}, size => $old->{size} }, | ||||||||||||||||||||||||||||||||||||
| 281 | { data_type => $new->{data_type}, size => $new->{size} }, | ||||||||||||||||||||||||||||||||||||
| 282 | 33 | ▶ | \%LOSSLESS_PAIRS, | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 283 | ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||
| 286 | 6 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 287 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 288 | # Names the clause the statement leads with, as the Add and Drop summaries | ||||||||||||||||||||||||||||||||||||
| 289 | # in this file do: REPLACE for a default-only change (karr k42), MODIFY | ||||||||||||||||||||||||||||||||||||
| 290 | # otherwise. A default change beside a modify adds a second statement that | ||||||||||||||||||||||||||||||||||||
| 291 | # the summary does not spell out -- Diff::Index::Alter's one-line summary | ||||||||||||||||||||||||||||||||||||
| 292 | # covers its DROP/CREATE pair the same way. | ||||||||||||||||||||||||||||||||||||
| 293 | 6 | ▶ | my $keyword = $self->_default_only ? 'REPLACE' : 'MODIFY'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 294 | 6 | "ALTER TABLE $self->{table} $keyword $self->{to}{column_name}"; | |||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | package DBIO::Sybase::Diff::Column::Drop; | ||||||||||||||||||||||||||||||||||||
| 298 | 33 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||
| 300 | 33 | __PACKAGE__->mk_diff_accessors(qw(table col)); | |||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||
| 302 | 8 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 303 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 304 | # DROP, not DROP COLUMN: the drop subclause keywords only its constraint | ||||||||||||||||||||||||||||||||||||
| 305 | # arm, and names a column bare -- see L</THE DROP NAMES THE COLUMN AND | ||||||||||||||||||||||||||||||||||||
| 306 | # NOTHING ELSE>. This is what summary has always said (karr k41). | ||||||||||||||||||||||||||||||||||||
| 307 | sprintf 'ALTER TABLE %s DROP %s;', | ||||||||||||||||||||||||||||||||||||
| 308 | DBIO::Sybase::Quote::quote_ident($self->table), | ||||||||||||||||||||||||||||||||||||
| 309 | 8 | DBIO::Sybase::Quote::quote_ident($self->col->{column_name}); | |||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||
| 311 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 312 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 313 | 2 | "ALTER TABLE $self->{table} DROP $self->{col}{column_name}"; | |||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||
| 316 | 33 | 1; | |||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||
| 318 | __END__ | ||||||||||||||||||||||||||||||||||||