blib/lib/DBIO/DuckDB/Diff/Table.pm

filter: Statementstmt 55.7% 34 / 61 Branchbran 8.3% 1 / 12 Conditioncond 25.0% 5 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 44.9% 53 / 118 CC 20 SCAR 39.1
CC
20
Cov
43%
as_sql
117.8
__ANON__
20.0
diff
7.1
CRAP 49.8
« blib/lib/DBIO/DuckDB/Introspect/ForeignKeys.pm ↑ Summary blib/lib/DBIO/DuckDB/Introspect/Tables.pm »
1 package DBIO::DuckDB::Diff::Table;
2 # ABSTRACT: Diff operations for DuckDB tables
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.900007';
8  
9 8 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use DBIO::DuckDB::DDL::Emit qw(quote_ident quote_table);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use DBIO::DuckDB::DDL::Emit qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 column_def pk_clause unique_clause fk_clause create_table create_sequence
14 8 );
15  
16 8 __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns foreign_keys unique_constraints/);
17  
18  
19  
20 2 ▶sub diff {
Subroutine diff: called CC=5 SCAR=19.6
Pod documented
21 2 my ($class, $source, $target, $target_columns, $target_fks, $source_fks, $target_uniques) = @_;
22 2 ▶ $target_uniques //= {};
Condition50%1/2
$target_uniques //= {}
l!l
02
MC/DC0%0/1
$target_uniques //= {}
$target_uniques
Truth table50%1/2
A
A= $target_uniques
Aresult
11
00
23 2 ▶ $target_columns //= {};
Condition50%1/2
$target_columns //= {}
l!l
20
MC/DC0%0/1
$target_columns //= {}
$target_columns
Truth table50%1/2
A
A= $target_columns
Aresult
11
00
24 2 ▶ $target_fks //= {};
Condition50%1/2
$target_fks //= {}
l!l
02
MC/DC0%0/1
$target_fks //= {}
$target_fks
Truth table50%1/2
A
A= $target_fks
Aresult
11
00
25 2 ▶ $source_fks //= {};
Condition50%1/2
$source_fks //= {}
l!l
02
MC/DC0%0/1
$source_fks //= {}
$source_fks
Truth table50%1/2
A
A= $source_fks
Aresult
11
00
26  
27 # DuckDB enforces foreign keys: a table is created after the tables its keys
28 # point to, and dropped before them (karr k49)
29 4 ▶ my $reads = sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
30 4 my ($fks) = @_;
31 0 ▶ return sub { [ map { $_->{to_table} } @{ $fks->{ $_[0] } || [] } ] };
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$$fks{$_[0]} || []
l!l
01
MC/DC0%0/1
$$fks{$_[0]} || []
$$fks{$_[0]}
Truth table50%1/2
A
A= $$fks{$_[0]}
Aresult
11
00
32 2 };
33  
34 return $class->diff_toplevel($source, $target,
35 depends => $reads->($target_fks),
36 source_depends => $reads->($source_fks),
37 0 ▶ create => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
38 0 my ($name) = @_;
39 $class->new(
40 action => 'create',
41 table_name => $name,
42 table_info => $target->{$name},
43 columns => $target_columns->{$name} // [],
44 foreign_keys => $target_fks->{$name} // [],
45 0 ▶ unique_constraints => $target_uniques->{$name} // {},
Condition0%0/6
$target_columns->{$name} // []
l!l
00
$target_fks->{$name} // []
l!l
00
$target_uniques->{$name} // {}
l!l
00
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC0%0/1
$target_fks->{$name} // []
$target_fks->{$name}
MC/DC0%0/1
$target_uniques->{$name} // {}
$target_uniques->{$name}
Truth table0%0/2
A
A= $target_columns->{$name}
Aresult
11
00
Truth table0%0/2
A
A= $target_fks->{$name}
Aresult
11
00
Truth table0%0/2
A
A= $target_uniques->{$name}
Aresult
11
00
46 );
47 },
48 1 ▶ drop => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 my ($name) = @_;
50 $class->new(
51 action => 'drop',
52 table_name => $name,
53 1 table_info => $source->{$name},
54 );
55 },
56 2 );
57 }
58  
59  
60 1 ▶sub as_sql {
Subroutine as_sql: called CC=12 SCAR=47.7
Pod documented
61 1 my ($self) = @_;
62  
63 1 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 1 0 1
64 1 return sprintf 'DROP TABLE %s;', quote_table($self->table_name);
65 }
66  
67 0 my @col_defs;
68 my @pk_cols;
69 0 my @sequences; # sequences referenced via nextval() in defaults
70  
71 0 for my $col (@{ $self->columns }) {
72 0 ▶ push @pk_cols, $col->{column_name} if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 0 0 0
73  
74 0 ▶ if (defined $col->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"}) 0 0 0
75 # Collect any sequences referenced by nextval() so we can ensure
76 # they exist before the CREATE TABLE runs.
77 0 ▶ while ($col->{default_value} =~ /nextval\(\s*'([^']+)'\s*\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"default_value"} =~ /nextval\(\s*'([^']+)'\s*\)/g) 0 0 0
78 0 push @sequences, $1;
79 }
80 }
81  
82 push @col_defs, column_def(
83 name => $col->{column_name},
84 type => ($col->{data_type} || 'VARCHAR'),
85 not_null => $col->{not_null},
86 default => $col->{default_value},
87 0 ▶ );
Condition0%0/2
$col->{'data_type'} || 'VARCHAR'
l!l
00
MC/DC0%0/1
$col->{'data_type'} || 'VARCHAR'
$col->{'data_type'}
Truth table0%0/2
A
A= $col->{'data_type'}
Aresult
11
00
88 }
89  
90 0 ▶ push @col_defs, pk_clause(@pk_cols) if @pk_cols;
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols 0 0 0
91  
92 # the UNIQUE constraints the table has in the target, in the order of
93 # their names (karr k49)
94 0 ▶ my $uniques = $self->unique_constraints || {};
Condition0%0/2
$self->unique_constraints || {}
l!l
00
MC/DC0%0/1
$self->unique_constraints || {}
$self->unique_constraints
Truth table0%0/2
A
A= $self->unique_constraints
Aresult
11
00
95 0 push @col_defs, unique_clause(@{ $uniques->{$_}{columns} }) for sort keys %$uniques;
96  
97 0 for my $fk (@{ $self->foreign_keys }) {
98 push @col_defs, fk_clause(
99 from => $fk->{from_columns},
100 to_table => $fk->{to_table},
101 to => $fk->{to_columns},
102 0 );
103 }
104  
105 0 my $create = create_table($self->table_name, @col_defs);
106  
107 0 ▶ if (@sequences) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sequences) 0 0 0
108 0 my $prefix = join "\n", map { create_sequence(name => $_) } @sequences;
109 0 return "$prefix\n$create";
110 }
111 0 return $create;
112 }
113  
114  
115 0 ▶sub summary {
Subroutine summary: not called CC=1 SCAR=6.9
Pod documented
116 0 my ($self) = @_;
117 0 return sprintf '%s table: %s', $self->summary_prefix, $self->table_name;
118 }
119  
120 8 1;
121  
122 __END__
« blib/lib/DBIO/DuckDB/Introspect/ForeignKeys.pm ↑ Summary blib/lib/DBIO/DuckDB/Introspect/Tables.pm »