« 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 //= {}
MC/DC0%0/1
$target_uniques //= {}
$target_uniques
Truth table50%1/2
A
A= $target_uniques
| |||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_columns //= {}
MC/DC0%0/1
$target_columns //= {}
$target_columns
Truth table50%1/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | $target_fks //= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_fks //= {}
MC/DC0%0/1
$target_fks //= {}
$target_fks
Truth table50%1/2
A
A= $target_fks
| |||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | $source_fks //= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$source_fks //= {}
MC/DC0%0/1
$source_fks //= {}
$source_fks
Truth table50%1/2
A
A= $source_fks
| |||||||||||||||||||||||||||||||||
| 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]} || []
MC/DC0%0/1
$$fks{$_[0]} || []
$$fks{$_[0]}
Truth table50%1/2
A
A= $$fks{$_[0]}
| |||||||||||||||||||||||||||||||||
| 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} // []
$target_fks->{$name} // []
$target_uniques->{$name} // {}
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}
Truth table0%0/2
A
A= $target_fks->{$name}
Truth table0%0/2
A
A= $target_uniques->{$name}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$col->{'data_type'} || 'VARCHAR'
$col->{'data_type'}
Truth table0%0/2
A
A= $col->{'data_type'}
| |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | push @col_defs, pk_clause(@pk_cols) if @pk_cols; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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 || {}
MC/DC0%0/1
$self->unique_constraints || {}
$self->unique_constraints
Truth table0%0/2
A
A= $self->unique_constraints
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||