Sybase/Diff/Table.pm

filter: Statementstmt 94.5% 104 / 110 Branchbran 100% 26 / 26 Conditioncond 65.3% 17 / 26 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 90.9% 20 / 22 Podpod 50.0% 1 / 2 totaltotal 86.5% 173 / 200 CC 31 SCAR 17.7
CC
31
Cov
91%
diff_primary_keys
11.0
diff
10.0
as_sql
6.0
CRAP 5.9
« Sybase/Storage.pm ↑ Summary Sybase/Util.pm »
1 package DBIO::Sybase::Diff::Table;
2 # ABSTRACT: Diff Sybase ASE tables
3  
4 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 our $VERSION = '0.900007';
8  
9 31 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use DBIO::Diff::Op ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use DBIO::Sybase::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use DBIO::Sybase::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 76 ▶sub diff {
Subroutine diff: called CC=10 SCAR=23.1
Pod undocumented
16 76 my ($class, $source, $target, $target_columns, $target_fks) = @_;
17 76 ▶ $target_columns //= {};
Condition50%1/2
$target_columns //= {}
l!l
760
MC/DC0%0/1
$target_columns //= {}
$target_columns
Truth table50%1/2
A
A= $target_columns
Aresult
11
00
18 76 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
742
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
19 76 my @ops;
20  
21 76 for my $name (sort keys %$target) {
22 # A view is DBIO::Sybase::Diff::View's (karr k98): CREATE TABLE for it
23 # would be a table of the same name, and the columns of a view are the
24 # query's.
25 84 ▶ next if _is_view($target->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if _is_view($target->{$name}) 4 80 84
26 # Existing tables are reconciled column-by-column and index-by-index by
27 # DBIO::Sybase::Diff::Column / ::Index. There is no table-level ALTER op:
28 # Sybase ASE cannot convert a table to a view (or vice versa) in place, so
29 # a kind change is left for a human rather than emitting a broken stub.
30 80 ▶ next if exists $source->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source->{$name} 53 27 80
31 push @ops, DBIO::Sybase::Diff::Table::Create->new(
32 action => 'create',
33 table => $target->{$name},
34 27 ▶ columns => $target_columns->{$name} // [],
Condition50%1/2
$target_columns->{$name} // []
l!l
270
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Aresult
11
00
35 );
36 }
37  
38 76 for my $name (sort keys %$source) {
39 62 ▶ next if _is_view($source->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if _is_view($source->{$name}) 4 58 62
40 push @ops, DBIO::Sybase::Diff::Table::Drop->new(
41 action => 'drop',
42 table => $source->{$name},
43 58 ▶ ) unless exists $target->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $target->{$name} 5 53 58
44 }
45  
46 76 return @ops;
47 }
48  
49 # A view is listed with the tables of a model (kind => 'view'), and is the
50 # business of DBIO::Sybase::Diff::View.
51 146 ▶sub _is_view { ($_[0]{kind} // '') eq 'view' }
Subroutine _is_view: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]{"kind"} // ""
l!l
1460
MC/DC0%0/1
$_[0]{"kind"} // ""
$_[0]{"kind"}
Truth table50%1/2
A
A= $_[0]{"kind"}
Aresult
11
00
52  
53  
54 51 ▶sub diff_primary_keys {
Subroutine diff_primary_keys: called CC=11 SCAR=24.0
Pod documented
55 51 my ($class, $s_tables, $t_tables, $s_cols, $t_cols, $s_indexes) = @_;
56 51 ▶ $_ //= {} for $s_tables, $t_tables, $s_cols, $t_cols, $s_indexes;
Condition100%2/2
$_ //= {}
l!l
2541
MC/DC100%1/1
$_ //= {}
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
57 51 my @ops;
58  
59 51 for my $name (sort keys %$t_tables) {
60 59 ▶ next unless exists $s_tables->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s_tables->{$name} 5 54 59
61 54 my @from = _pk_columns($s_cols->{$name});
62 54 my @to = _pk_columns($t_cols->{$name});
63 54 ▶ next if join("\0", @from) eq join("\0", @to);
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", @from) eq join("\0", @to) 48 6 54
64  
65 6 ▶ if (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 5 1 6
66 4 ▶ my ($pk) = grep { ($_->{origin} // '') eq 'pk' }
Condition50%1/2
$_->{"origin"} // ""
l!l
40
MC/DC0%0/1
$_->{"origin"} // ""
$_->{"origin"}
Truth table50%1/2
A
A= $_->{"origin"}
Aresult
11
00
67 5 ▶ values %{ $s_indexes->{$name} // {} };
Condition100%2/2
$s_indexes->{$name} // {}
l!l
41
MC/DC100%1/1
$s_indexes->{$name} // {}
$s_indexes->{$name}
Truth table100%2/2
A
A= $s_indexes->{$name}
Aresult
11
00
68 push @ops, DBIO::Sybase::Diff::Table::PrimaryKey::Drop->new(
69 action => 'drop',
70 table_name => $name,
71 constraint_name => $pk ? $pk->{index_name} : undef,
72 5 ▶ columns => \@from,
Branch: 1 branch
BranchTrueFalseTotal
$pk ? : 4 1 5
73 );
74 }
75 6 ▶ push @ops, DBIO::Sybase::Diff::Table::PrimaryKey::Add->new(
Branch: 1 branch
BranchTrueFalseTotal
if @to 4 2 6
76 action => 'create',
77 table_name => $name,
78 columns => \@to,
79 ) if @to;
80 }
81  
82 51 return @ops;
83 }
84  
85 # The key columns of one table's column list, in key order. pk_position is
86 # what the catalog reports; a column marked is_pk without one (a hand-written
87 # model) keeps its place in the column list.
88 125 ▶sub _pk_columns {
Subroutine _pk_columns: called CC=4 SCAR=14.1
89 125 my ($cols) = @_;
90 125 my $i = 0;
91 38 ▶ my @pk = map { [ $_->{pk_position} || 1e9, $i++, $_->{column_name} ] }
Condition100%2/2
$_->{'pk_position'} || 1000000000.0
l!l
2315
MC/DC100%1/1
$_->{'pk_position'} || 1000000000.0
$_->{'pk_position'}
Truth table100%2/2
A
A= $_->{'pk_position'}
Aresult
11
00
92 125 ▶ grep { $_->{is_pk} } @{ $cols // [] };
Condition50%1/2
$cols // []
l!l
1250
MC/DC0%0/1
$cols // []
$cols
Truth table50%1/2
A
A= $cols
Aresult
11
00
93 125 ▶ return map { $_->[2] } sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @pk;
Condition33%1/3
$DBIO::Sybase::Diff::Table::a->[0] <=> $DBIO::Sybase::Diff::Table::b->[0] || $DBIO::Sybase::Diff::Table::a->[1] <=> $DBIO::Sybase::Diff::Table::b->[1]
l!l&&r!l&&!r
700
MC/DC0%0/2
$DBIO::Sybase::Diff::Table::a->[0] <=> $DBIO::Sybase::Diff::Table::b->[0] || $DBIO::Sybase::Diff::Table::a->[1] <=> $DBIO::Sybase::Diff::Table::b->[1]
$DBIO::Sybase::Diff::Table::a->[0] <=> $DBIO::Sybase::Diff::Table::b->[0] $DBIO::Sybase::Diff::Table::a->[1] <=> $DBIO::Sybase::Diff::Table::b->[1]
Truth table33%1/3
A || B
A= $DBIO::Sybase::Diff::Table::a->[0] <=> $DBIO::Sybase::Diff::Table::b->[0]B= $DBIO::Sybase::Diff::Table::a->[1] <=> $DBIO::Sybase::Diff::Table::b->[1]
ABresult
1X1
011
000
94 }
95  
96 package DBIO::Sybase::Diff::Table::Create;
97 31 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 31 __PACKAGE__->mk_diff_accessors(qw(table columns));
100  
101 29 ▶sub as_sql {
Subroutine as_sql: called CC=6 SCAR=17.9
102 29 my $self = shift;
103 29 my $tbl = DBIO::Sybase::Quote::quote_ident($self->table->{table_name});
104 29 my @col_defs;
105  
106 29 ▶ my $cols = $self->columns // [];
Condition50%1/2
$self->columns // []
l!l
290
MC/DC0%0/1
$self->columns // []
$self->columns
Truth table50%1/2
A
A= $self->columns
Aresult
11
00
107 29 for my $col (@$cols) {
108 38 my $type = DBIO::Sybase::DDL::sybase_column_type($col->{data_type}, $col->{size});
109 my $def = sprintf ' %s %s',
110 38 DBIO::Sybase::Quote::quote_ident($col->{column_name}), $type;
111 # Same column grammar and same rules as DBIO::Sybase::DDL::install_ddl
112 # (karr k47): DEFAULT before the nullability, IDENTITY as the lone
113 # attribute, and a nullable column spelled NULL rather than left to the
114 # database's "allow nulls by default" option. IDENTITY on an integer
115 # or NUMERIC(p,0) column only (karr k52) -- any other known type croaks, see
116 # DBIO::Sybase::DDL::_identity_clause.
117 38 ▶ if ($col->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"is_auto_increment"}) 17 21 38
118 push @col_defs, $def . DBIO::Sybase::DDL::_identity_clause(
119 17 $type, $col->{data_type}, $col->{column_name}, $self->table->{table_name});
120 5 next;
121 }
122 $def .= DBIO::Sybase::DDL::sybase_default_clause(
123 21 $col->{default_value}, $col->{data_type}, $col->{column_name});
124 21 ▶ $def .= $col->{not_null} ? ' NOT NULL' : ' NULL';
Branch: 1 branch
BranchTrueFalseTotal
$col->{'not_null'} ? : 17 4 21
125 21 push @col_defs, $def;
126 }
127  
128 # The key, in key order (karr k53) -- same clause install_ddl writes.
129 17 my @pk = DBIO::Sybase::Diff::Table::_pk_columns($cols);
130 push @col_defs, sprintf ' PRIMARY KEY (%s)',
131 17 ▶ join(', ', map { DBIO::Sybase::Quote::quote_ident($_) } @pk)
Branch: 1 branch
BranchTrueFalseTotal
if @pk 14 3 17
132 if @pk;
133  
134 17 sprintf "CREATE TABLE %s (\n%s\n);", $tbl, join(",\n", @col_defs);
135 }
136 3 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
137 3 my $self = shift;
138 3 "CREATE TABLE " . $self->table->{table_name};
139 }
140  
141 package DBIO::Sybase::Diff::Table::Drop;
142 31 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 31 __PACKAGE__->mk_diff_accessors(qw(table));
145  
146 6 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
147 6 my $self = shift;
148 6 "DROP TABLE " . DBIO::Sybase::Quote::quote_ident($self->table->{table_name}) . ';';
149 }
150 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
151 2 my $self = shift;
152 2 "DROP TABLE " . $self->table->{table_name};
153 }
154  
155 package DBIO::Sybase::Diff::Table::PrimaryKey::Drop;
156 31 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 31 __PACKAGE__->mk_diff_accessors(qw(table_name constraint_name columns));
159  
160 6 ▶sub as_sql {
Subroutine as_sql: called CC=3 SCAR=11.0
161 6 my $self = shift;
162 6 my $tbl = $self->table_name;
163 # The name comes from the source side's PK index -- the introspected live
164 # database always has one. Without it there is nothing ASE would accept.
165 6 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->constraint_name and length $self->constraint_name 1 5 6
Condition66%2/3
defined $self->constraint_name and length $self->constraint_name
!ll&&!rl&&r
105
MC/DC50%1/2
defined $self->constraint_name and length $self->constraint_name
defined $self->constraint_name length $self->constraint_name
Truth table66%2/3
A and B
A= defined $self->constraint_nameB= length $self->constraint_name
ABresult
0X0
100
111
166 "DBIO::Sybase: no primary-key constraint name for table $tbl in the "
167 . 'source model (its origin => pk index is missing), cannot drop the key')
168 unless defined $self->constraint_name && length $self->constraint_name;
169 5 sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;',
170 DBIO::Sybase::Quote::quote_ident($tbl),
171 DBIO::Sybase::Quote::quote_ident($self->constraint_name);
172 }
173 0 ▶sub summary {
Subroutine summary: not called CC=1 SCAR=6.9
174 0 my $self = shift;
175 0 sprintf 'DROP PRIMARY KEY ON %s (%s)', $self->table_name, join(', ', @{ $self->columns });
176 }
177  
178 package DBIO::Sybase::Diff::Table::PrimaryKey::Add;
179 31 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 31 __PACKAGE__->mk_diff_accessors(qw(table_name columns));
182  
183 5 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
184 5 my $self = shift;
185 sprintf 'ALTER TABLE %s ADD PRIMARY KEY (%s);',
186 DBIO::Sybase::Quote::quote_ident($self->table_name),
187 5 join(', ', map { DBIO::Sybase::Quote::quote_ident($_) } @{ $self->columns });
188 }
189 0 ▶sub summary {
Subroutine summary: not called CC=1 SCAR=6.9
190 0 my $self = shift;
191 0 sprintf 'ADD PRIMARY KEY ON %s (%s)', $self->table_name, join(', ', @{ $self->columns });
192 }
193  
194 31 1;
195  
196 __END__
« Sybase/Storage.pm ↑ Summary Sybase/Util.pm »