SQLite/Diff/Rebuild.pm

filter: Statementstmt 100% 37 / 37 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 94.2% 49 / 52 CC 3 SCAR 4.7
CC
3
Cov
95%
as_sql
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
« SQLite/Introspect/ForeignKeys.pm ↑ Summary SQLite/Introspect.pm »
1 package DBIO::SQLite::Diff::Rebuild;
2 # ABSTRACT: Diff operation that rebuilds a SQLite table
3  
4 197 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 197 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 197 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 197 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 197 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 197 __PACKAGE__->mk_diff_accessors(qw/table_name table_sql copy_columns/);
14  
15  
16 5 ▶sub as_sql {
Subroutine as_sql: called CC=3 SCAR=11.0
Pod documented
17 5 my ($self) = @_;
18  
19 5 my $name = $self->table_name;
20 5 my $t = _quote_ident($name);
21 5 my $tmp_name = $name . '__dbio_rebuild';
22 5 my $tmp = _quote_ident($tmp_name);
23  
24 5 my $create = $self->_rename_create($self->table_sql, $name, $tmp_name);
25  
26 5 ▶ my @copy = map { _quote_ident($_) } @{ $self->copy_columns // [] };
Condition50%1/2
$self->copy_columns // []
l!l
50
MC/DC0%0/1
$self->copy_columns // []
$self->copy_columns
Truth table50%1/2
A
A= $self->copy_columns
Aresult
11
00
27 5 my $collist = join ', ', @copy;
28  
29 5 my @stmts = (
30 'PRAGMA foreign_keys=OFF;',
31 "$create;",
32 );
33 5 ▶ push @stmts, sprintf "INSERT INTO %s (%s)\nSELECT %s FROM %s;",
Branch: 1 branch
BranchTrueFalseTotal
if @copy 5 0 5
34 $tmp, $collist, $collist, $t
35 if @copy;
36 5 push @stmts,
37 sprintf('DROP TABLE %s;', $t),
38 sprintf('ALTER TABLE %s RENAME TO %s;', $tmp, $t),
39 'PRAGMA foreign_keys=ON;';
40  
41 5 return join "\n", @stmts;
42 }
43  
44 # Swap the leading table-name token in a CREATE TABLE statement for the
45 # temporary name, leaving the rest of the captured DDL untouched. Handles the
46 # bare, "double-quoted", `back-ticked`, and [bracketed] identifier forms that
47 # sqlite_master may store.
48 5 ▶sub _rename_create {
Subroutine _rename_create: called CC=1 SCAR=0.0
49 5 my ($self, $sql, $name, $tmp_name) = @_;
50 5 my $repl = _quote_ident($tmp_name);
51 5 $sql =~ s{
52 ^(\s*CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?)
53 (?: "\Q$name\E" | `\Q$name\E` | \[\Q$name\E\] | \Q$name\E )
54 }{$1$repl}xi;
55 5 return $sql;
56 }
57  
58  
59 1 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
60 1 my ($self) = @_;
61 1 return sprintf '~ table rebuild: %s', $self->table_name;
62 }
63  
64 197 1;
65  
66 __END__
67  
68 =pod
69  
70 =encoding UTF-8
71  
72 =head1 NAME
73  
74 DBIO::SQLite::Diff::Rebuild - Diff operation that rebuilds a SQLite table
75  
76 =head1 VERSION
77  
78 version 0.900001
79  
80 =head1 DESCRIPTION
81  
82 SQLite's C<ALTER TABLE> cannot change a column's type, nullability, or
83 default in place. The only way to apply such a change is the documented
84 rebuild dance: create a new table with the desired shape under a temporary
85 name, copy the surviving data across, drop the original, and rename the new
86 table into its place.
87  
88 The new table is created from C<table_sql> -- the I<target> table's original
89 C<CREATE TABLE> statement as captured from C<sqlite_master> during
90 introspection -- with only the leading table-name token swapped for the
91 temporary name. Reusing the captured DDL keeps the rebuild faithful: PRIMARY
92 KEY, AUTOINCREMENT, inline foreign keys, C<WITHOUT ROWID>, C<STRICT>, and
93 column defaults all survive exactly as the desired schema declared them
94 (reconstructing them from the column model would be lossy).
95  
96 C<copy_columns> are the column names present in B<both> the old and new
97 table, so only those carry data over (added columns take their default,
98 dropped columns are left behind).
99  
100 The rebuild brackets itself with C<PRAGMA foreign_keys=OFF> / C<ON>: the
101 statements run one at a time with autocommit on (see
102 L<DBIO::Deploy::Base/_execute_ddl>), so dropping a table that other tables
103 reference is safe within the bracketed window.
104  
105 Indexes on the rebuilt table are dropped with it; L<DBIO::SQLite::Diff>
106 re-emits C<CREATE INDEX> ops for the table's target indexes after this op.
107  
108 =head1 METHODS
109  
110 =head2 as_sql
111  
112 Returns the multi-statement rebuild SQL (semicolon-terminated statements, as
113 expected by C<_split_statements>).
114  
115 =head2 summary
116  
117 =head1 AUTHOR
118  
119 DBIO & DBIx::Class Authors
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 Copyright (C) 2026 DBIO Authors
124 Portions Copyright (C) 2005-2025 DBIx::Class Authors
125 Based on DBIx::Class, heavily modified.
126  
127 This is free software; you can redistribute it and/or modify it under
128 the same terms as the Perl 5 programming language system itself.
129  
130 =cut
« SQLite/Introspect/ForeignKeys.pm ↑ Summary SQLite/Introspect.pm »