« blib/lib/DBIO/SQLite/Extension/VirtualTable.pm
↑ Summary
blib/lib/DBIO/SQLite/Introspect/Triggers.pm »
| 1 | package DBIO::SQLite::Diff::Rebuild; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operation that rebuilds a SQLite table | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 190 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 190 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 190 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 190 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 190 | ▶ | use DBIO::SQLite::Util qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | create_table_name_index quote_identifier sql_tokens token_identifier | ||||||||||||||||||||||||||||
| 13 | 190 | ); | |||||||||||||||||||||||||||
| 14 | 190 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 190 | __PACKAGE__->mk_diff_accessors(qw/ | |||||||||||||||||||||||||||
| 18 | table_name table_sql copy_columns triggers reasons data_checks lossy_columns | ||||||||||||||||||||||||||||
| 19 | /); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 71 | ▶ | sub lossy { return scalar @{ $_[0]->lossy_columns // [] } ? 1 : 0 } | ||||||||||||||||||||||||||
|
Subroutine
lossy: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
Condition100%2/2
$_[0]->lossy_columns // []
MC/DC100%1/1
$_[0]->lossy_columns // []
$_[0]->lossy_columns
Truth table100%2/2
A
A= $_[0]->lossy_columns
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 184 | ▶ | sub statements { | ||||||||||||||||||||||||||
|
Subroutine
statements: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 184 | my ($self) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 184 | my $name = $self->table_name; | |||||||||||||||||||||||||||
| 31 | 184 | my $t = quote_identifier($name); | |||||||||||||||||||||||||||
| 32 | 184 | my $tmp_name = $name . '__dbio_rebuild'; | |||||||||||||||||||||||||||
| 33 | 184 | my $tmp = quote_identifier($tmp_name); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 184 | my $create = $self->_rename_create($self->table_sql, $name, $tmp_name); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 184 | ▶ | my @copy = map { quote_identifier($_) } @{ $self->copy_columns // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->copy_columns // []
MC/DC0%0/1
$self->copy_columns // []
$self->copy_columns
Truth table50%1/2
A
A= $self->copy_columns
| |||||||||||||||||||||||||||||
| 38 | 184 | my $collist = join ', ', @copy; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 184 | my @stmts = ( | |||||||||||||||||||||||||||
| 41 | 'PRAGMA foreign_keys=OFF;', | ||||||||||||||||||||||||||||
| 42 | 'PRAGMA legacy_alter_table=ON;', | ||||||||||||||||||||||||||||
| 43 | 'SAVEPOINT dbio_rebuild;', | ||||||||||||||||||||||||||||
| 44 | "$create;", | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | 184 | ▶ | push @stmts, sprintf "INSERT INTO %s (%s)\nSELECT %s FROM %s;", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | $tmp, $collist, $collist, $t | ||||||||||||||||||||||||||||
| 48 | if @copy; | ||||||||||||||||||||||||||||
| 49 | push @stmts, | ||||||||||||||||||||||||||||
| 50 | sprintf('DROP TABLE %s;', $t), | ||||||||||||||||||||||||||||
| 51 | sprintf('ALTER TABLE %s RENAME TO %s;', $tmp, $t), | ||||||||||||||||||||||||||||
| 52 | 184 | ▶ | (map { "$_;" } @{ $self->triggers // [] }), | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->triggers // []
MC/DC100%1/1
$self->triggers // []
$self->triggers
Truth table100%2/2
A
A= $self->triggers
| |||||||||||||||||||||||||||||
| 53 | 'RELEASE dbio_rebuild;', | ||||||||||||||||||||||||||||
| 54 | 'PRAGMA legacy_alter_table=OFF;', | ||||||||||||||||||||||||||||
| 55 | 'PRAGMA foreign_keys=ON;'; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 184 | return @stmts; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 34 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | 34 | return join "\n", $self->statements; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Swap the leading table-name token in a CREATE TABLE statement for the | ||||||||||||||||||||||||||||
| 67 | # temporary name, leaving the rest of the captured DDL untouched. The token is | ||||||||||||||||||||||||||||
| 68 | # found by the lexer, so it is the name in whichever form sqlite_master stores | ||||||||||||||||||||||||||||
| 69 | # it -- bare, "double-quoted" (also with a doubled quote inside, "a""b"), | ||||||||||||||||||||||||||||
| 70 | # `back-ticked`, [bracketed] -- and never a longer name that begins with it. | ||||||||||||||||||||||||||||
| 71 | # A statement whose name token is not this table's is left as it is. | ||||||||||||||||||||||||||||
| 72 | 184 | ▶ | sub _rename_create { | ||||||||||||||||||||||||||
|
Subroutine
_rename_create: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 73 | 184 | my ($self, $sql, $name, $tmp_name) = @_; | |||||||||||||||||||||||||||
| 74 | 184 | my @tokens = sql_tokens($sql); | |||||||||||||||||||||||||||
| 75 | 184 | my $at = create_table_name_index(\@tokens); | |||||||||||||||||||||||||||
| 76 | return $sql | ||||||||||||||||||||||||||||
| 77 | 184 | ▶ | unless defined $at && lc token_identifier(@{ $tokens[$at] }) eq lc $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $at and lc token_identifier(@{$tokens[$at];}) eq lc $name
MC/DC50%1/2
defined $at and lc token_identifier(@{$tokens[$at];}) eq lc $name
defined $at
lc token_identifier(@{$tokens[$at];}) eq lc $name
Truth table66%2/3
A and B
A= defined $atB= lc token_identifier(@{$tokens[$at];}) eq lc $name
| |||||||||||||||||||||||||||||
| 78 | 183 | $tokens[$at][1] = quote_identifier($tmp_name); | |||||||||||||||||||||||||||
| 79 | 183 | return join '', map { $_->[1] } @tokens; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 26 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 26 | my ($self) = @_; | |||||||||||||||||||||||||||
| 85 | 26 | ▶ | my @reasons = @{ $self->reasons // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->reasons // []
MC/DC0%0/1
$self->reasons // []
$self->reasons
Truth table50%1/2
A
A= $self->reasons
| |||||||||||||||||||||||||||||
| 86 | 26 | ▶ | return sprintf '~ table rebuild: %s%s', $self->table_name, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | @reasons ? ' (' . join('; ', @reasons) . ')' : ''; | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 190 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||