| 1 | package DBIO::Firebird::Diff::Column; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for Firebird columns | ||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use DBIO::SQL::Util qw(_quote_ident); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use DBIO::Diff::Compare qw(changed_fields norm_type); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | # new() and the action accessor come from DBIO::Diff::Op. | ||||||||||||||||||||||||||||||
| 14 | 3 | __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/); | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | sub diff { | ||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 18 | 10 | my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||
| 19 | 10 | my @ops; | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 10 | for my $table_name (sort keys %$target_cols) { | |||||||||||||||||||||||||||||
| 22 | next unless exists $source_tables->{$table_name} | ||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | && exists $target_tables->{$table_name}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $source_tables->{$table_name} and exists $target_tables->{$table_name}
MC/DC50%1/2
exists $source_tables->{$table_name} and exists $target_tables->{$table_name}
exists $source_tables->{$table_name}
exists $target_tables->{$table_name}
Truth table66%2/3
A and B
A= exists $source_tables->{$table_name}B= exists $target_tables->{$table_name}
| |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | my %src_by_name = map { $_->{column_name} => $_ } @{ $source_cols->{$table_name} // [] }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$source_cols->{$table_name} // []
MC/DC0%0/1
$source_cols->{$table_name} // []
$source_cols->{$table_name}
Truth table50%1/2
A
A= $source_cols->{$table_name}
| |||||||||||||||||||||||||||||||
| 26 | 8 | ▶ | my %tgt_by_name = map { $_->{column_name} => $_ } @{ $target_cols->{$table_name} // [] }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$target_cols->{$table_name} // []
MC/DC0%0/1
$target_cols->{$table_name} // []
$target_cols->{$table_name}
Truth table50%1/2
A
A= $target_cols->{$table_name}
| |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 8 | for my $col_name (sort keys %tgt_by_name) { | |||||||||||||||||||||||||||||
| 29 | 11 | my $tgt = $tgt_by_name{$col_name}; | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 11 | ▶ | if (!exists $src_by_name{$col_name}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | 3 | push @ops, $class->new( | |||||||||||||||||||||||||||||
| 33 | action => 'add', | ||||||||||||||||||||||||||||||
| 34 | table_name => $table_name, | ||||||||||||||||||||||||||||||
| 35 | column_name => $col_name, | ||||||||||||||||||||||||||||||
| 36 | new_info => $tgt, | ||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||
| 38 | 3 | next; | |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 8 | my $src = $src_by_name{$col_name}; | |||||||||||||||||||||||||||||
| 42 | # Target is the desired state: fields absent from it are left as-is | ||||||||||||||||||||||||||||||
| 43 | # (desired_state => 1). Firebird size may be a scalar length or a | ||||||||||||||||||||||||||||||
| 44 | # [precision, scale] pair -- the `dim` field handles both. | ||||||||||||||||||||||||||||||
| 45 | 8 | my $changed = changed_fields($src, $tgt, | |||||||||||||||||||||||||||||
| 46 | type => ['data_type'], | ||||||||||||||||||||||||||||||
| 47 | scalar => ['default_value'], | ||||||||||||||||||||||||||||||
| 48 | bool => ['not_null'], | ||||||||||||||||||||||||||||||
| 49 | dim => ['size'], | ||||||||||||||||||||||||||||||
| 50 | desired_state => 1, | ||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | if ($changed) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 54 | 2 | push @ops, $class->new( | |||||||||||||||||||||||||||||
| 55 | action => 'alter', | ||||||||||||||||||||||||||||||
| 56 | table_name => $table_name, | ||||||||||||||||||||||||||||||
| 57 | column_name => $col_name, | ||||||||||||||||||||||||||||||
| 58 | old_info => $src, | ||||||||||||||||||||||||||||||
| 59 | new_info => $tgt, | ||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 8 | for my $col_name (sort keys %src_by_name) { | |||||||||||||||||||||||||||||
| 65 | 9 | ▶ | next if exists $tgt_by_name{$col_name}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 66 | push @ops, $class->new( | ||||||||||||||||||||||||||||||
| 67 | action => 'drop', | ||||||||||||||||||||||||||||||
| 68 | table_name => $table_name, | ||||||||||||||||||||||||||||||
| 69 | column_name => $col_name, | ||||||||||||||||||||||||||||||
| 70 | 1 | old_info => $src_by_name{$col_name}, | |||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | 10 | return @ops; | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | 7 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=17 SCAR=28.6
Pod
documented
| |||||||||||||||||||||||||||||||
| 80 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | 7 | my $tbl = _quote_ident($self->table_name); | |||||||||||||||||||||||||||||
| 83 | 7 | my $col = _quote_ident($self->column_name); | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 7 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 86 | 4 | my $info = $self->new_info; | |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | my $type = $info->{data_type} || 'VARCHAR'; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{'data_type'} || 'VARCHAR'
MC/DC0%0/1
$info->{'data_type'} || 'VARCHAR'
$info->{'data_type'}
Truth table50%1/2
A
A= $info->{'data_type'}
| |||||||||||||||||||||||||||||||
| 88 | 4 | my $sql = sprintf 'ALTER TABLE %s ADD %s %s', $tbl, $col, $type; | |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | $sql .= ' NOT NULL' if $info->{not_null}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | if (defined $info->{default_value}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 91 | 1 | $sql .= " DEFAULT $info->{default_value}"; | |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||
| 93 | 4 | return "$sql;"; | |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | 1 | return sprintf 'ALTER TABLE %s DROP %s;', $tbl, $col; | |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if ($self->action eq 'alter') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 101 | 2 | my $old = $self->old_info; | |||||||||||||||||||||||||||||
| 102 | 2 | my $new = $self->new_info; | |||||||||||||||||||||||||||||
| 103 | 2 | my @stmts; | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | if (norm_type($old->{data_type}) ne norm_type($new->{data_type})) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 106 | push @stmts, sprintf 'ALTER TABLE %s ALTER %s TYPE %s;', | ||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | $tbl, $col, ($new->{data_type} || 'VARCHAR'); | ||||||||||||||||||||||||||||
|
Condition50%1/2
$new->{'data_type'} || 'VARCHAR'
MC/DC0%0/1
$new->{'data_type'} || 'VARCHAR'
$new->{'data_type'}
Truth table50%1/2
A
A= $new->{'data_type'}
| |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | if (($old->{not_null} // 0) != ($new->{not_null} // 0)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$old->{"not_null"} // 0
$new->{"not_null"} // 0
MC/DC0%0/1
$old->{"not_null"} // 0
$old->{"not_null"}
MC/DC0%0/1
$new->{"not_null"} // 0
$new->{"not_null"}
Truth table50%1/2
A
A= $old->{"not_null"}
Truth table50%1/2
A
A= $new->{"not_null"}
| |||||||||||||||||||||||||||||||
| 110 | push @stmts, $new->{not_null} | ||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | ? sprintf('ALTER TABLE %s ALTER %s SET NOT NULL;', $tbl, $col) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 112 | : sprintf('ALTER TABLE %s ALTER %s DROP NOT NULL;', $tbl, $col); | ||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||
| 114 | 2 | ▶ | my $old_d = defined $old->{default_value} ? $old->{default_value} : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | my $new_d = defined $new->{default_value} ? $new->{default_value} : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | if ($old_d ne $new_d) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 117 | 2 | ▶ | push @stmts, length $new_d | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 118 | ? sprintf('ALTER TABLE %s ALTER %s SET DEFAULT %s;', $tbl, $col, $new_d) | ||||||||||||||||||||||||||||||
| 119 | : sprintf('ALTER TABLE %s ALTER %s DROP DEFAULT;', $tbl, $col); | ||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||
| 121 | 2 | return join "\n", @stmts; | |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 127 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | my $type = $self->new_info ? " ($self->{new_info}{data_type})" : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 129 | 3 | return sprintf ' %scolumn: %s.%s%s', | |||||||||||||||||||||||||||||
| 130 | $self->summary_prefix, $self->table_name, $self->column_name, $type; | ||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | 3 | 1; | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | =pod | ||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | DBIO::Firebird::Diff::Column - Diff operations for Firebird columns | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | version 0.900001 | ||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||
| 149 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | Column-level diff operations for Firebird. Firebird supports: | ||||||||||||||||||||||||||||||
| 152 | C<ALTER TABLE ... ADD COLUMN>, C<DROP COLUMN>, and limited type/DEFAULT | ||||||||||||||||||||||||||||||
| 153 | changes via C<ALTER TABLE ... ALTER COLUMN>. | ||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||
| 155 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||
| 157 | =head2 diff | ||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | =head2 as_sql | ||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | =head2 summary | ||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||