| 1 | package DBIO::DuckDB::Diff::Table; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for DuckDB tables | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 4 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 4 | ▶ | use DBIO::SQL::Util qw(_quote_ident); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 4 | ▶ | use DBIO::DuckDB::DDL::Emit qw( | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | column_def pk_clause fk_clause create_table create_sequence | ||||||||||||||||||||||
| 12 | 4 | ); | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 4 | __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns foreign_keys/); | |||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | 2 | ▶ | sub diff { | ||||||||||||||||||||
|
Subroutine
diff: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||
| 19 | 2 | my ($class, $source, $target, $target_columns, $target_fks) = @_; | |||||||||||||||||||||
| 20 | 2 | ▶ | $target_columns //= {}; | ||||||||||||||||||||
|
Condition50%1/2
$target_columns //= {}
MC/DC0%0/1
$target_columns //= {}
$target_columns
Truth table50%1/2
A
A= $target_columns
| |||||||||||||||||||||||
| 21 | 2 | ▶ | $target_fks //= {}; | ||||||||||||||||||||
|
Condition50%1/2
$target_fks //= {}
MC/DC0%0/1
$target_fks //= {}
$target_fks
Truth table50%1/2
A
A= $target_fks
| |||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | return $class->diff_toplevel($source, $target, | ||||||||||||||||||||||
| 24 | 0 | ▶ | create => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 25 | 0 | my ($name) = @_; | |||||||||||||||||||||
| 26 | $class->new( | ||||||||||||||||||||||
| 27 | action => 'create', | ||||||||||||||||||||||
| 28 | table_name => $name, | ||||||||||||||||||||||
| 29 | table_info => $target->{$name}, | ||||||||||||||||||||||
| 30 | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||
| 31 | 0 | ▶ | foreign_keys => $target_fks->{$name} // [], | ||||||||||||||||||||
|
Condition0%0/4
$target_columns->{$name} // []
$target_fks->{$name} // []
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC0%0/1
$target_fks->{$name} // []
$target_fks->{$name}
Truth table0%0/2
A
A= $target_columns->{$name}
Truth table0%0/2
A
A= $target_fks->{$name}
| |||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||
| 34 | 1 | ▶ | drop => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 35 | 1 | my ($name) = @_; | |||||||||||||||||||||
| 36 | $class->new( | ||||||||||||||||||||||
| 37 | action => 'drop', | ||||||||||||||||||||||
| 38 | table_name => $name, | ||||||||||||||||||||||
| 39 | 1 | table_info => $source->{$name}, | |||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||
| 42 | 2 | ); | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 1 | ▶ | sub as_sql { | ||||||||||||||||||||
|
Subroutine
as_sql: called CC=10 SCAR=43.8
Pod
documented
| |||||||||||||||||||||||
| 47 | 1 | my ($self) = @_; | |||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 1 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 1 | return sprintf 'DROP TABLE %s;', _quote_ident($self->table_name); | |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | 0 | my @col_defs; | |||||||||||||||||||||
| 54 | my @pk_cols; | ||||||||||||||||||||||
| 55 | 0 | my @sequences; # sequences referenced via nextval() in defaults | |||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | 0 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||
| 58 | 0 | ▶ | push @pk_cols, $col->{column_name} if $col->{is_pk}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | 0 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 61 | # Collect any sequences referenced by nextval() so we can ensure | ||||||||||||||||||||||
| 62 | # they exist before the CREATE TABLE runs. | ||||||||||||||||||||||
| 63 | 0 | ▶ | while ($col->{default_value} =~ /nextval\(\s*'([^']+)'\s*\)/g) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 64 | 0 | push @sequences, $1; | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | push @col_defs, column_def( | ||||||||||||||||||||||
| 69 | name => $col->{column_name}, | ||||||||||||||||||||||
| 70 | type => ($col->{data_type} || 'VARCHAR'), | ||||||||||||||||||||||
| 71 | not_null => $col->{not_null}, | ||||||||||||||||||||||
| 72 | default => $col->{default_value}, | ||||||||||||||||||||||
| 73 | 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'}
| |||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 0 | ▶ | push @col_defs, pk_clause(@pk_cols) if @pk_cols; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 0 | for my $fk (@{ $self->foreign_keys }) { | |||||||||||||||||||||
| 79 | push @col_defs, fk_clause( | ||||||||||||||||||||||
| 80 | from => $fk->{from_columns}, | ||||||||||||||||||||||
| 81 | to_table => $fk->{to_table}, | ||||||||||||||||||||||
| 82 | to => $fk->{to_columns}, | ||||||||||||||||||||||
| 83 | 0 | ); | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 0 | my $create = create_table($self->table_name, @col_defs); | |||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | 0 | ▶ | if (@sequences) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 89 | 0 | my $prefix = join "\n", map { create_sequence(name => $_) } @sequences; | |||||||||||||||||||||
| 90 | 0 | return "$prefix\n$create"; | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | 0 | return $create; | |||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | 0 | ▶ | sub summary { | ||||||||||||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 97 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 98 | 0 | return sprintf '%s table: %s', $self->summary_prefix, $self->table_name; | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 4 | 1; | |||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | =pod | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | =encoding UTF-8 | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | DBIO::DuckDB::Diff::Table - Diff operations for DuckDB tables | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | =head1 VERSION | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | version 0.900001 | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | Represents a table-level diff operation in DuckDB: C<CREATE TABLE> or | ||||||||||||||||||||||
| 120 | C<DROP TABLE>. C<create> ops capture the target columns and foreign keys | ||||||||||||||||||||||
| 121 | so C<as_sql> can emit the full inline definition in a single statement | ||||||||||||||||||||||
| 122 | (DuckDB is happiest when a table is created whole rather than built up | ||||||||||||||||||||||
| 123 | with ALTER TABLE ADD COLUMN). | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | =head1 METHODS | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | =head2 diff | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | my @ops = DBIO::DuckDB::Diff::Table->diff( | ||||||||||||||||||||||
| 130 | $source_tables, $target_tables, | ||||||||||||||||||||||
| 131 | $target_columns, $target_fks, | ||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | =head2 as_sql | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | =head2 summary | ||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | =head1 AUTHOR | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | DBIO Authors | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||
| 147 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||