Oracle/Diff/Column.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Oracle/Introspect.pm ↑ Summary Shortcut/ora.pm »
1 package DBIO::Oracle::Diff::Column;
2 # ABSTRACT: Diff operations for Oracle columns
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DBIO::Oracle::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 __PACKAGE__->mk_diff_accessors(qw(table_name column_name old_info new_info));
14  
15  
16 # Canonical comparison key for a column default_value. Introspection stores
17 # expression defaults as SCALAR refs (e.g. \'current_timestamp') and literal
18 # defaults as plain strings; a naive string compare stringifies the ref to
19 # SCALAR(0x...) and reports a phantom diff on every expression default. This
20 # mirrors the rendering in as_sql so comparison and emission cannot diverge.
21 #
22 # Returns undef for the "no default" cases (undef input, literal "null", or
23 # the \'null' expression ref), so the core's desired_state semantics still
24 # treat a target-undef as "don't care" and skip the field.
25 sub _default_key {
26 my ($dv) = @_;
27 return undef unless defined $dv;
28 if (ref $dv eq 'SCALAR') {
29 my $expr = lc $$dv;
30 $expr =~ s/^\s+//;
31 $expr =~ s/\s+\z//;
32 return undef if $expr eq 'null';
33 return $expr;
34 }
35 return undef if $dv eq 'null';
36 return "'$dv'";
37 }
38  
39  
40 sub diff {
41 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
42 my @ops;
43  
44 for my $table_name (sort keys %$target_cols) {
45 next unless exists $source_tables->{$table_name}
46 && exists $target_tables->{$table_name};
47  
48 my %src_by_name = map { $_->{column_name} => $_ } @{ $source_cols->{$table_name} // [] };
49 my %tgt_by_name = map { $_->{column_name} => $_ } @{ $target_cols->{$table_name} // [] };
50  
51 for my $col_name (sort keys %tgt_by_name) {
52 my $tgt = $tgt_by_name{$col_name};
53  
54 if (!exists $src_by_name{$col_name}) {
55 push @ops, $class->new(
56 action => 'add',
57 table_name => $table_name,
58 column_name => $col_name,
59 new_info => $tgt,
60 );
61 next;
62 }
63  
64 my $src = $src_by_name{$col_name};
65 my @changed = _changed_column_fields($src, $tgt);
66 next unless @changed;
67  
68 push @ops, $class->new(
69 action => 'alter',
70 table_name => $table_name,
71 column_name => $col_name,
72 old_info => $src,
73 new_info => $tgt,
74 );
75 }
76  
77 for my $col_name (sort keys %src_by_name) {
78 next if exists $tgt_by_name{$col_name};
79 push @ops, $class->new(
80 action => 'drop',
81 table_name => $table_name,
82 column_name => $col_name,
83 old_info => $src_by_name{$col_name},
84 );
85 }
86 }
87  
88 return @ops;
89 }
90  
91 # Run the canonical changed_fields over data_type / not_null / default_value.
92 # Pre-normalise default_value through _default_key so SCALAR-ref expression
93 # defaults compare by content (the core would stringify refs to SCALAR(0x..)).
94 sub _changed_column_fields {
95 my ($src, $tgt) = @_;
96 return changed_fields(
97 { %$src, default_value => _default_key($src->{default_value}) },
98 { %$tgt, default_value => _default_key($tgt->{default_value}) },
99 type => ['data_type'],
100 bool => ['not_null'],
101 scalar => ['default_value'],
102 desired_state => 1,
103 );
104 }
105  
106  
107 sub as_sql {
108 my ($self) = @_;
109  
110 my $tbl = _quote_ident($self->table_name);
111 my $col = _quote_ident($self->column_name);
112  
113 if ($self->action eq 'add') {
114 my $info = $self->new_info;
115 my $type = DBIO::Oracle::Type::map_dbio_type_to_oracle($info->{data_type}, size => $info->{size});
116 my $sql = sprintf 'ALTER TABLE %s ADD (%s %s', $tbl, $col, $type;
117 if (defined $info->{default_value}) {
118 my $dv = $info->{default_value};
119 if (ref $dv eq 'SCALAR') {
120 $sql .= " DEFAULT $$dv";
121 }
122 elsif (defined $dv && $dv ne 'null') {
123 $sql .= " DEFAULT '$dv'";
124 }
125 }
126 $sql .= ' NOT NULL' if $info->{not_null};
127 $sql .= ')';
128 return "$sql;";
129 }
130  
131 if ($self->action eq 'drop') {
132 return sprintf 'ALTER TABLE %s DROP COLUMN %s;', $tbl, $col;
133 }
134  
135 if ($self->action eq 'alter') {
136 my $old = $self->old_info;
137 my $new = $self->new_info;
138 my @stmts;
139  
140 my @mods;
141 if (DBIO::Oracle::Type::normalize_type($old->{data_type})
142 ne DBIO::Oracle::Type::normalize_type($new->{data_type})) {
143 push @mods, DBIO::Oracle::Type::map_dbio_type_to_oracle($new->{data_type}, size => $new->{size});
144 }
145 if (defined $new->{default_value}) {
146 my $dv = $new->{default_value};
147 if (ref $dv eq 'SCALAR') {
148 push @mods, "DEFAULT $$dv";
149 }
150 elsif (defined $dv && $dv ne 'null') {
151 push @mods, "DEFAULT '$dv'";
152 }
153 }
154 elsif (!defined $new->{default_value} && defined $old->{default_value}) {
155 push @mods, 'DEFAULT NULL';
156 }
157 if (($old->{not_null} // 0) != ($new->{not_null} // 0)) {
158 push @mods, $new->{not_null} ? 'NOT NULL' : 'NULL';
159 }
160  
161 if (@mods) {
162 push @stmts, sprintf 'ALTER TABLE %s MODIFY (%s %s);',
163 $tbl, $col, join(' ', @mods);
164 }
165  
166 return join "\n", @stmts;
167 }
168 }
169  
170  
171 sub summary {
172 my ($self) = @_;
173 my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
174 return sprintf ' %scolumn: %s.%s%s',
175 $self->summary_prefix, $self->table_name, $self->column_name, $type;
176 }
177  
178 1;
179  
180 __END__
181  
182 =pod
183  
184 =encoding UTF-8
185  
186 =head1 NAME
187  
188 DBIO::Oracle::Diff::Column - Diff operations for Oracle columns
189  
190 =head1 VERSION
191  
192 version 0.900001
193  
194 =head1 DESCRIPTION
195  
196 Column-level diff operations for Oracle. Oracle supports:
197  
198 =over 4
199  
200 =item * C<ALTER TABLE ... ADD column datatype [DEFAULT value]>
201  
202 =item * C<ALTER TABLE ... DROP COLUMN column>
203  
204 =item * C<ALTER TABLE ... MODIFY column datatype [DEFAULT value] [NULL|NOT NULL]>
205  
206 =back
207  
208 Note: Oracle does not support renaming columns via standard SQL. This class
209 emits ADD + DROP for rename operations.
210  
211 =head1 METHODS
212  
213 =head2 diff
214  
215 =head2 as_sql
216  
217 =head2 summary
218  
219 =head1 AUTHOR
220  
221 DBIO & DBIx::Class Authors
222  
223 =head1 COPYRIGHT AND LICENSE
224  
225 Copyright (C) 2026 DBIO Authors
226 Portions Copyright (C) 2005-2025 DBIx::Class Authors
227 Based on DBIx::Class, heavily modified.
228  
229 This is free software; you can redistribute it and/or modify it under
230 the same terms as the Perl 5 programming language system itself.
231  
232 =cut
« Oracle/Introspect.pm ↑ Summary Shortcut/ora.pm »