SQLite/Diff/Table.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 83.3% 10 / 12 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 92.3% 85 / 92 CC 14 SCAR 15.6
CC
14
Cov
93%
as_sql
9.0
__ANON__
3.0
diff
3.0
CRAP 4.8
« SQLite/Adapter.pm ↑ Summary SQLite/Introspect/Tables.pm »
1 package DBIO::SQLite::Diff::Table;
2 # ABSTRACT: Diff operations for SQLite tables
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_info columns foreign_keys/);
14  
15  
16 21 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.0
Pod documented
17 21 my ($class, $source, $target, $target_columns, $target_fks) = @_;
18 21 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
201
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
19 21 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
201
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
20  
21 # The generic create/drop walk lives in DBIO::Diff::Op->diff_toplevel; we
22 # only supply the SQLite-shaped ops (create captures target columns + FKs so
23 # as_sql can render the full inline CREATE TABLE).
24 return $class->diff_toplevel($source, $target,
25 4 ▶ create => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
26 4 my ($name) = @_;
27 $class->new(
28 action => 'create',
29 table_name => $name,
30 table_info => $target->{$name},
31 columns => $target_columns->{$name} // [],
32 4 ▶ foreign_keys => $target_fks->{$name} // [],
Condition75%3/4
$target_columns->{$name} // []
l!l
40
$target_fks->{$name} // []
l!l
22
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Aresult
11
00
Truth table100%2/2
A
A= $target_fks->{$name}
Aresult
11
00
33 );
34 },
35 2 ▶ drop => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 2 my ($name) = @_;
37 $class->new(
38 action => 'drop',
39 table_name => $name,
40 2 table_info => $source->{$name},
41 );
42 },
43 21 );
44 }
45  
46  
47 6 ▶sub as_sql {
Subroutine as_sql: called CC=9 SCAR=22.0
Pod documented
48 6 my ($self) = @_;
49  
50 6 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 2 4 6
51 2 return sprintf 'DROP TABLE %s;', _quote_ident($self->table_name);
52 }
53  
54 4 my @col_defs;
55 my @pk_cols;
56  
57 4 for my $col (@{ $self->columns }) {
58 7 ▶ push @pk_cols, $col->{column_name} if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 5 2 7
59  
60 7 ▶ my $type = $col->{data_type} // 'TEXT';
Condition50%1/2
$col->{"data_type"} // "TEXT"
l!l
70
MC/DC0%0/1
$col->{"data_type"} // "TEXT"
$col->{"data_type"}
Truth table50%1/2
A
A= $col->{"data_type"}
Aresult
11
00
61 7 my $def = sprintf ' %s %s', _quote_ident($col->{column_name}), $type;
62 7 ▶ $def .= ' NOT NULL' if $col->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"not_null"} 2 5 7
63 7 ▶ if (defined $col->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"}) 0 7 7
64 0 $def .= " DEFAULT $col->{default_value}";
65 }
66 7 push @col_defs, $def;
67 }
68  
69 # Multi-column PK becomes a table-level constraint. Single-column INTEGER
70 # PRIMARY KEY is left inline (it's already part of the column type
71 # roundtrip via introspection -- pk=1 in the column metadata).
72 4 ▶ if (@pk_cols > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols > 1) 1 3 4
73 push @col_defs, sprintf ' PRIMARY KEY (%s)',
74 1 join(', ', map { _quote_ident($_) } @pk_cols);
75 }
76  
77 4 for my $fk (@{ $self->foreign_keys }) {
78 push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES %s(%s)',
79 1 join(', ', map { _quote_ident($_) } @{ $fk->{from_columns} }),
80 _quote_ident($fk->{to_table}),
81 1 join(', ', map { _quote_ident($_) } @{ $fk->{to_columns} });
82 }
83  
84 4 return sprintf "CREATE TABLE %s (\n%s\n);",
85 _quote_ident($self->table_name), join(",\n", @col_defs);
86 }
87  
88  
89 1 ▶sub summary {
Subroutine summary: called CC=2 SCAR=7.1
Pod documented
90 1 my ($self) = @_;
91 1 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 1 0 1
92 1 return sprintf '%s table: %s', $prefix, $self->table_name;
93 }
94  
95 197 1;
96  
97 __END__
98  
99 =pod
100  
101 =encoding UTF-8
102  
103 =head1 NAME
104  
105 DBIO::SQLite::Diff::Table - Diff operations for SQLite tables
106  
107 =head1 VERSION
108  
109 version 0.900001
110  
111 =head1 DESCRIPTION
112  
113 Represents a table-level diff operation in SQLite: C<CREATE TABLE> or
114 C<DROP TABLE>. Unlike PostgreSQL, where the table shell can be created
115 empty and columns added one at a time, SQLite is much friendlier when
116 the full table definition is emitted at once -- so for C<create>
117 operations the SQL is generated directly from the introspected target
118 columns (and any FKs / PK constraints) inline.
119  
120 =head1 METHODS
121  
122 =head2 diff
123  
124 my @ops = DBIO::SQLite::Diff::Table->diff(
125 $source_tables, $target_tables,
126 $target_columns, $target_fks,
127 );
128  
129 Compares two table hashrefs (keyed by table name) and returns C<create>
130 ops for tables present only in target and C<drop> ops for tables only in
131 source. C<create> ops capture the target columns and FKs so C<as_sql>
132 can render the full inline definition.
133  
134 =head2 as_sql
135  
136 Returns the SQL for this operation. For C<create>, emits a full
137 C<CREATE TABLE> with columns, primary key, and foreign keys inline. For
138 C<drop>, emits C<DROP TABLE>.
139  
140 =head2 summary
141  
142 =head1 AUTHOR
143  
144 DBIO & DBIx::Class Authors
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 Copyright (C) 2026 DBIO Authors
149 Portions Copyright (C) 2005-2025 DBIx::Class Authors
150 Based on DBIx::Class, heavily modified.
151  
152 This is free software; you can redistribute it and/or modify it under
153 the same terms as the Perl 5 programming language system itself.
154  
155 =cut
« SQLite/Adapter.pm ↑ Summary SQLite/Introspect/Tables.pm »