| 1 | package DBIO::DuckDB::Diff::Rebuild; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Rebuild a DuckDB table for what ALTER TABLE cannot do | ||||||||||||||||||||||||||||||
| 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::Exception; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use DBIO::DuckDB::DDL::Emit qw(quote_ident quote_table split_table); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use DBIO::DuckDB::Diff::Table; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 8 | __PACKAGE__->mk_diff_accessors(qw/ | |||||||||||||||||||||||||||||
| 16 | table_name columns foreign_keys unique_constraints copy_columns reasons column_ops | ||||||||||||||||||||||||||||||
| 17 | /); | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub tmp_name { | ||||||||||||||||||||||||||||
|
Subroutine
tmp_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 22 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 23 | 0 | my ($schema, $table) = split_table($self->table_name); | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | return (defined $schema ? "$schema." : '') . $table . '__dbio_rebuild'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||
|
Subroutine
as_sql: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 29 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my @copy = @{ $self->copy_columns || [] }; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$self->copy_columns || []
MC/DC0%0/1
$self->copy_columns || []
$self->copy_columns
Truth table0%0/2
A
A= $self->copy_columns
| |||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | "Cannot rebuild table '%s': the old and the new table have no column in common, no row could be carried over", | ||||||||||||||||||||||||||||||
| 33 | $self->table_name) unless @copy; | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 0 | my $create = DBIO::DuckDB::Diff::Table->new( | |||||||||||||||||||||||||||||
| 36 | action => 'create', | ||||||||||||||||||||||||||||||
| 37 | table_name => $self->tmp_name, | ||||||||||||||||||||||||||||||
| 38 | columns => $self->columns, | ||||||||||||||||||||||||||||||
| 39 | foreign_keys => $self->foreign_keys, | ||||||||||||||||||||||||||||||
| 40 | unique_constraints => $self->unique_constraints, | ||||||||||||||||||||||||||||||
| 41 | )->as_sql; | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | 0 | my $list = join ', ', map { quote_ident($_) } @copy; | |||||||||||||||||||||||||||||
| 44 | 0 | my ($schema, $table) = split_table($self->table_name); | |||||||||||||||||||||||||||||
| 45 | 0 | return join "\n", | |||||||||||||||||||||||||||||
| 46 | $create, | ||||||||||||||||||||||||||||||
| 47 | sprintf('INSERT INTO %s (%s) SELECT %s FROM %s;', | ||||||||||||||||||||||||||||||
| 48 | quote_table($self->tmp_name), $list, $list, quote_table($self->table_name)), | ||||||||||||||||||||||||||||||
| 49 | sprintf('DROP TABLE %s;', quote_table($self->table_name)), | ||||||||||||||||||||||||||||||
| 50 | sprintf('ALTER TABLE %s RENAME TO %s;', quote_table($self->tmp_name), quote_ident($table)); | ||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub lossy_columns { | ||||||||||||||||||||||||||||
|
Subroutine
lossy_columns: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 55 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 56 | 0 | return map { $_->column_name } | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | grep { $_->action eq 'drop' || $_->lossy } @{ $self->column_ops || [] }; | ||||||||||||||||||||||||||||
|
Condition0%0/5
$_->action eq 'drop' || $_->lossy
$self->column_ops || []
MC/DC0%0/2
$_->action eq 'drop' || $_->lossy
$_->action eq 'drop'
$_->lossy
MC/DC0%0/1
$self->column_ops || []
$self->column_ops
Truth table0%0/3
A || B
A= $_->action eq 'drop'B= $_->lossy
Truth table0%0/2
A
A= $self->column_ops
| |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub lossy { scalar(my @c = $_[0]->lossy_columns) ? 1 : 0 } | ||||||||||||||||||||||||||||
|
Subroutine
lossy: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 65 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 66 | 0 | my @lossy = map { $self->table_name . ".$_" } $self->lossy_columns; | |||||||||||||||||||||||||||||
| 67 | return sprintf '%s rebuild table: %s (%s)%s', $self->summary_prefix, | ||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $self->table_name, join('; ', @{ $self->reasons || [] }), | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->reasons || []
MC/DC0%0/1
$self->reasons || []
$self->reasons
Truth table0%0/2
A
A= $self->reasons
| |||||||||||||||||||||||||||||||
| 69 | (@lossy ? ' drops or may alter: ' . join(', ', @lossy) : ''); | ||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 8 | 1; | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | __END__ | ||||||||||||||||||||||||||||||