| 1 | package DBIO::MSSQL::Diff::Table; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL tables | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 42 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 42 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 42 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 42 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 42 | ▶ | use DBIO::MSSQL::DDL qw(_mssql_column_type _quote_ident _quote_table _constraint_name); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 42 | __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns/); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 422 | ▶ | sub view_names { | ||||||||||||||||||||||||||
|
Subroutine
view_names: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 422 | my ($class, @sections) = @_; | |||||||||||||||||||||||||||
| 19 | 422 | my %seen; | |||||||||||||||||||||||||||
| 20 | 422 | for my $tables (@sections) { | |||||||||||||||||||||||||||
| 21 | 844 | ▶ | for my $name (keys %{ $tables // {} }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$tables // {}
MC/DC0%0/1
$tables // {}
$tables
Truth table50%1/2
A
A= $tables
| |||||||||||||||||||||||||||||
| 22 | 1174 | ▶ | my $kind = ref $tables->{$name} eq 'HASH' ? $tables->{$name}{kind} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 1174 | ▶ | $seen{$name} = 1 if defined $kind && $kind eq 'view'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $kind and $kind eq "view"
MC/DC100%2/2
defined $kind and $kind eq "view"
defined $kind
$kind eq "view"
Truth table100%3/3
A and B
A= defined $kindB= $kind eq "view"
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 422 | return sort keys %seen; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 100 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 100 | my ($class, $source, $target, $target_columns) = @_; | |||||||||||||||||||||||||||
| 32 | 100 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns //= {}
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # a view is not a table: no CREATE TABLE for a declared one, no DROP TABLE | ||||||||||||||||||||||||||||
| 35 | # for a live one (a view is Diff::View's) | ||||||||||||||||||||||||||||
| 36 | 100 | my %is_view = map { $_ => 1 } $class->view_names($source, $target); | |||||||||||||||||||||||||||
| 37 | 100 | $source = { map { $_ => $source->{$_} } grep { !$is_view{$_} } keys %$source }; | |||||||||||||||||||||||||||
| 38 | 100 | $target = { map { $_ => $target->{$_} } grep { !$is_view{$_} } keys %$target }; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | return $class->diff_toplevel($source, $target, | ||||||||||||||||||||||||||||
| 41 | 32 | ▶ | create => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 32 | my ($name) = @_; | |||||||||||||||||||||||||||
| 43 | $class->new( | ||||||||||||||||||||||||||||
| 44 | action => 'create', | ||||||||||||||||||||||||||||
| 45 | table_name => $name, | ||||||||||||||||||||||||||||
| 46 | table_info => $target->{$name}, | ||||||||||||||||||||||||||||
| 47 | 32 | ▶ | 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}
| |||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | 14 | ▶ | drop => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 14 | my ($name) = @_; | |||||||||||||||||||||||||||
| 52 | $class->new( | ||||||||||||||||||||||||||||
| 53 | action => 'drop', | ||||||||||||||||||||||||||||
| 54 | table_name => $name, | ||||||||||||||||||||||||||||
| 55 | 14 | table_info => $source->{$name}, | |||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | 100 | ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 41 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=9 SCAR=22.6
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 41 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 41 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 16 | return sprintf 'DROP TABLE %s;', _quote_table($self->table_name); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 25 | my @col_defs; | |||||||||||||||||||||||||||
| 70 | my @pk_cols; | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 25 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 43 | my $type = _mssql_column_type($col, $self->table_name, $col->{column_name}); | |||||||||||||||||||||||||||
| 75 | 42 | my $def = sprintf ' %s %s', _quote_ident($col->{column_name}), $type; | |||||||||||||||||||||||||||
| 76 | 42 | ▶ | $def .= ' NOT NULL' if $col->{not_null}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 42 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $def .= " DEFAULT $col->{default_value}"; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 42 | ▶ | if ($col->{is_identity}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | $def .= ' IDENTITY(1,1)'; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 42 | push @col_defs, $def; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 15 | @pk_cols = map { $_->{column_name} } | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | sort { ($a->{pk_position} || 0) <=> ($b->{pk_position} || 0) } | ||||||||||||||||||||||||||
|
Condition0%0/4
$DBIO::MSSQL::Diff::Table::a->{'pk_position'} || 0
$DBIO::MSSQL::Diff::Table::b->{'pk_position'} || 0
MC/DC0%0/1
$DBIO::MSSQL::Diff::Table::a->{'pk_position'} || 0
$DBIO::MSSQL::Diff::Table::a->{'pk_position'}
MC/DC0%0/1
$DBIO::MSSQL::Diff::Table::b->{'pk_position'} || 0
$DBIO::MSSQL::Diff::Table::b->{'pk_position'}
Truth table0%0/2
A
A= $DBIO::MSSQL::Diff::Table::a->{'pk_position'}
Truth table0%0/2
A
A= $DBIO::MSSQL::Diff::Table::b->{'pk_position'}
| |||||||||||||||||||||||||||||
| 88 | 24 | grep { $_->{is_pk} } @{ $self->columns }; | |||||||||||||||||||||||||||
| 89 | 24 | ▶ | if (@pk_cols) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | push @col_defs, sprintf ' CONSTRAINT %s PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||
| 91 | _quote_ident(_constraint_name('PK', $self->table_name)), | ||||||||||||||||||||||||||||
| 92 | 15 | join(', ', map { _quote_ident($_) } @pk_cols); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 24 | return sprintf "CREATE TABLE %s (\n%s\n);", | |||||||||||||||||||||||||||
| 96 | _quote_table($self->table_name), join(",\n", @col_defs); | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 5 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 102 | 5 | return sprintf '%s table: %s', $self->summary_prefix, $self->table_name; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 42 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||