SQLite/Diff.pm

filter: Statementstmt 100% 70 / 70 Branchbran 90.0% 9 / 10 Conditioncond 58.8% 10 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 87.0% 101 / 116 CC 19 SCAR 20.9
CC
19
Cov
92%
_build_operations
12.2
target_from_compiled
8.0
BEGIN
1.0
CRAP 8.1
« SQLite/Storage.pm ↑ Summary SQLite/Introspect/Indexes.pm »
1 package DBIO::SQLite::Diff;
2 # ABSTRACT: Compare two introspected SQLite models
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::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 197 ▶use DBIO::SQLite::Diff::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 197 ▶use DBIO::SQLite::Diff::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 197 ▶use DBIO::SQLite::Diff::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 197 ▶use DBIO::SQLite::Diff::Rebuild;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 1 ▶sub target_from_compiled {
Subroutine target_from_compiled: called CC=8 SCAR=20.8
Pod documented
17 1 my ($class, $compiled) = @_;
18 1 my (%tables, %columns);
19 1 ▶ for my $tname (keys %{ $compiled->{tables} // {} }) {
Condition50%1/2
$compiled->{"tables"} // {}
l!l
10
MC/DC0%0/1
$compiled->{"tables"} // {}
$compiled->{"tables"}
Truth table50%1/2
A
A= $compiled->{"tables"}
Aresult
11
00
20 1 my $t = $compiled->{tables}{$tname};
21 1 $tables{$tname} = { table_name => $tname };
22 1 my @cols;
23 1 ▶ for my $c (@{ $t->{columns} // [] }) {
Condition50%1/2
$t->{"columns"} // []
l!l
10
MC/DC0%0/1
$t->{"columns"} // []
$t->{"columns"}
Truth table50%1/2
A
A= $t->{"columns"}
Aresult
11
00
24 8 ▶ my $not_null = $c->{not_null} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c->{'not_null'} ? : 6 2 8
25 8 ▶ $not_null = 0 if $c->{is_pk}; # SQLite reports PK columns as notnull=0
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"is_pk"} 1 7 8
26 push @cols, {
27 column_name => $c->{column_name},
28 data_type => $c->{native_type},
29 not_null => $not_null,
30 default_value => $c->{default},
31 8 ▶ is_pk => ($c->{is_pk} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$c->{'is_pk'} ? : 1 7 8
32 pk_position => 0,
33 };
34 }
35 1 $columns{$tname} = \@cols;
36 }
37 1 return { tables => \%tables, columns => \%columns, indexes => {}, foreign_keys => {} };
38 }
39  
40 17 ▶sub _build_operations {
Subroutine _build_operations: called CC=12 SCAR=25.0
41 17 my ($self) = @_;
42 17 my @ops;
43  
44 push @ops, DBIO::SQLite::Diff::Table->diff(
45 $self->source->{tables}, $self->target->{tables},
46 $self->target->{columns}, $self->target->{foreign_keys},
47 17 );
48  
49 my @col_ops = DBIO::SQLite::Diff::Column->diff(
50 $self->source->{columns}, $self->target->{columns},
51 $self->source->{tables}, $self->target->{tables},
52 17 );
53  
54 # SQLite cannot ALTER a column's type / nullability / default in place. A
55 # table carrying any such change must be rebuilt wholesale (create-new /
56 # copy / drop / rename), which also subsumes that table's add / drop column
57 # ops. We can only rebuild faithfully when the target table's original
58 # CREATE statement is available (the introspect path provides it; the
59 # compiled-model path does not) -- otherwise the per-column ops stand and
60 # render their explanatory comment.
61 17 my %needs_rebuild;
62 17 for my $op (grep { $_->action eq 'alter' } @col_ops) {
63 4 my $table = $op->table_name;
64 4 my $tinfo = $self->target->{tables}{$table};
65 4 ▶ $needs_rebuild{$table} = 1 if $tinfo && $tinfo->{sql};
Branch: 1 branch
BranchTrueFalseTotal
if $tinfo and $tinfo->{"sql"} 3 1 4
Condition66%2/3
$tinfo and $tinfo->{"sql"}
!ll&&!rl&&r
013
MC/DC50%1/2
$tinfo and $tinfo->{"sql"}
$tinfo $tinfo->{"sql"}
Truth table66%2/3
A and B
A= $tinfoB= $tinfo->{"sql"}
ABresult
0X0
100
111
66 }
67  
68 17 push @ops, grep { !$needs_rebuild{ $_->table_name } } @col_ops;
69  
70 17 for my $table (sort keys %needs_rebuild) {
71 8 my %src_names = map { $_->{column_name} => 1 }
72 3 ▶ @{ $self->source->{columns}{$table} // [] };
Condition50%1/2
$self->source->{"columns"}{$table} // []
l!l
30
MC/DC0%0/1
$self->source->{"columns"}{$table} // []
$self->source->{"columns"}{$table}
Truth table50%1/2
A
A= $self->source->{"columns"}{$table}
Aresult
11
00
73 8 my @copy = grep { $src_names{$_} }
74 3 ▶ map { $_->{column_name} } @{ $self->target->{columns}{$table} // [] };
Condition50%1/2
$self->target->{"columns"}{$table} // []
l!l
30
MC/DC0%0/1
$self->target->{"columns"}{$table} // []
$self->target->{"columns"}{$table}
Truth table50%1/2
A
A= $self->target->{"columns"}{$table}
Aresult
11
00
75  
76 push @ops, DBIO::SQLite::Diff::Rebuild->new(
77 table_name => $table,
78 table_sql => $self->target->{tables}{$table}{sql},
79 3 copy_columns => \@copy,
80 );
81  
82 # The rebuild drops the table and with it every index; re-create the
83 # table's explicit (non-auto) target indexes afterwards.
84 3 ▶ my $tgt_idx = $self->target->{indexes}{$table} // {};
Condition100%2/2
$self->target->{"indexes"}{$table} // {}
l!l
12
MC/DC100%1/1
$self->target->{"indexes"}{$table} // {}
$self->target->{"indexes"}{$table}
Truth table100%2/2
A
A= $self->target->{"indexes"}{$table}
Aresult
11
00
85 3 for my $name (sort keys %$tgt_idx) {
86 1 my $info = $tgt_idx->{$name};
87 1 ▶ next if DBIO::SQLite::Diff::Index::_is_auto($info);
Branch: 1 branch
BranchTrueFalseTotal
if DBIO::SQLite::Diff::Index::_is_auto($info) 0 1 1
88 1 push @ops, DBIO::SQLite::Diff::Index->new(
89 action => 'create',
90 table_name => $table,
91 index_name => $name,
92 index_info => $info,
93 );
94 }
95 }
96  
97 # Normal index diff for every table that is NOT being rebuilt (rebuilt
98 # tables' indexes are handled above). The full tables sections are threaded
99 # through so Diff::Index can suppress a standalone DROP INDEX for any index
100 # whose owning table is itself being dropped this pass -- SQLite's DROP TABLE
101 # already removes those indexes (karr #14).
102 17 ▶ my %src_idx = %{ $self->source->{indexes} // {} };
Condition50%1/2
$self->source->{"indexes"} // {}
l!l
170
MC/DC0%0/1
$self->source->{"indexes"} // {}
$self->source->{"indexes"}
Truth table50%1/2
A
A= $self->source->{"indexes"}
Aresult
11
00
103 17 ▶ my %tgt_idx = %{ $self->target->{indexes} // {} };
Condition50%1/2
$self->target->{"indexes"} // {}
l!l
170
MC/DC0%0/1
$self->target->{"indexes"} // {}
$self->target->{"indexes"}
Truth table50%1/2
A
A= $self->target->{"indexes"}
Aresult
11
00
104 17 delete @src_idx{ keys %needs_rebuild };
105 17 delete @tgt_idx{ keys %needs_rebuild };
106 push @ops, DBIO::SQLite::Diff::Index->diff(
107 \%src_idx, \%tgt_idx,
108 $self->source->{tables}, $self->target->{tables},
109 17 );
110  
111 17 return \@ops;
112 }
113  
114 197 1;
115  
116 __END__
117  
118 =pod
119  
120 =encoding UTF-8
121  
122 =head1 NAME
123  
124 DBIO::SQLite::Diff - Compare two introspected SQLite models
125  
126 =head1 VERSION
127  
128 version 0.900001
129  
130 =head1 DESCRIPTION
131  
132 C<DBIO::SQLite::Diff> compares two introspected SQLite database models
133 (as produced by L<DBIO::SQLite::Introspect>) and produces a list of
134 structured diff operations. These operations can then be rendered to SQL
135 or a human-readable summary.
136  
137 my $diff = DBIO::SQLite::Diff->new(
138 source => $current_model,
139 target => $desired_model,
140 );
141  
142 if ($diff->has_changes) {
143 print $diff->as_sql;
144 print $diff->summary;
145 }
146  
147 Operations are emitted in dependency order: tables first (so new tables
148 exist before columns/indexes reference them), then columns, then
149 indexes. Drop ops come last for each layer.
150  
151 =head1 METHODS
152  
153 =head2 target_from_compiled
154  
155 my $target = DBIO::SQLite::Diff->target_from_compiled($compiled_model);
156  
157 Translates the neutral model from L<DBIO::Schema::ModelCompiler> into the
158 SQLite introspect-shaped model that C<diff> consumes: native types land in
159 C<data_type>, and primary-key columns are reported as nullable to match how
160 SQLite's C<PRAGMA table_info> reports them (PK columns are notnull=0 unless
161 explicitly declared NOT NULL).
162  
163 =head1 AUTHOR
164  
165 DBIO & DBIx::Class Authors
166  
167 =head1 COPYRIGHT AND LICENSE
168  
169 Copyright (C) 2026 DBIO Authors
170 Portions Copyright (C) 2005-2025 DBIx::Class Authors
171 Based on DBIx::Class, heavily modified.
172  
173 This is free software; you can redistribute it and/or modify it under
174 the same terms as the Perl 5 programming language system itself.
175  
176 =cut
« SQLite/Storage.pm ↑ Summary SQLite/Introspect/Indexes.pm »