| 1 | package DBIO::MSSQL::Diff::Column; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL 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::MSSQL::DDL qw(_mssql_column_type); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use DBIO::Diff::Compare qw(changed_column_fields norm_type); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 3 | __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/); | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | sub diff { | ||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 18 | 9 | my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | return $class->diff_nested($source_cols, $target_cols, | ||||||||||||||||||||||||||||||
| 21 | index_by => 'column_name', | ||||||||||||||||||||||||||||||
| 22 | scope => 'both', | ||||||||||||||||||||||||||||||
| 23 | source_tables => $source_tables, | ||||||||||||||||||||||||||||||
| 24 | target_tables => $target_tables, | ||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | changed_when => sub { scalar changed_column_fields($_[0], $_[1]) }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | on_new => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 27 | 2 | my ($table, $name, $new) = @_; | |||||||||||||||||||||||||||||
| 28 | 2 | $class->new(action => 'add', table_name => $table, column_name => $name, new_info => $new); | |||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 31 | 2 | my ($table, $name, $old, $new) = @_; | |||||||||||||||||||||||||||||
| 32 | 2 | $class->new(action => 'alter', table_name => $table, column_name => $name, | |||||||||||||||||||||||||||||
| 33 | old_info => $old, new_info => $new); | ||||||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 36 | 1 | my ($table, $name, $old) = @_; | |||||||||||||||||||||||||||||
| 37 | 1 | $class->new(action => 'drop', table_name => $table, column_name => $name, old_info => $old); | |||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||||
| 39 | 9 | ); | |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | 5 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=15 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||
| 44 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||
| 46 | 5 | my $tbl = _quote_ident($self->table_name); | |||||||||||||||||||||||||||||
| 47 | 5 | my $col = _quote_ident($self->column_name); | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 50 | 2 | my $info = $self->new_info; | |||||||||||||||||||||||||||||
| 51 | 2 | my $type = _mssql_column_type($info); | |||||||||||||||||||||||||||||
| 52 | 2 | my $sql = sprintf 'ALTER TABLE %s ADD %s %s', $tbl, $col, $type; | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | $sql .= ' NOT NULL' if $info->{not_null}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | if (defined $info->{default_value}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 55 | 0 | $sql .= " DEFAULT $info->{default_value}"; | |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||
| 57 | 2 | return "$sql;"; | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 61 | 1 | return sprintf 'ALTER TABLE %s DROP COLUMN %s;', $tbl, $col; | |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | if ($self->action eq 'alter') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 65 | 2 | my $old = $self->old_info; | |||||||||||||||||||||||||||||
| 66 | 2 | my $new = $self->new_info; | |||||||||||||||||||||||||||||
| 67 | 2 | my @stmts; | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | if (norm_type($old->{data_type}) ne norm_type($new->{data_type})) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 70 | 1 | push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s %s;', | |||||||||||||||||||||||||||||
| 71 | $tbl, $col, _mssql_column_type($new); | ||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | if (($old->{not_null} // 0) != ($new->{not_null} // 0)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$old->{"not_null"} // 0
$new->{"not_null"} // 0
MC/DC100%1/1
$old->{"not_null"} // 0
$old->{"not_null"}
MC/DC100%1/1
$new->{"not_null"} // 0
$new->{"not_null"}
Truth table100%2/2
A
A= $old->{"not_null"}
Truth table100%2/2
A
A= $new->{"not_null"}
| |||||||||||||||||||||||||||||||
| 74 | push @stmts, $new->{not_null} | ||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | ? sprintf('ALTER TABLE %s ALTER COLUMN %s NOT NULL;', $tbl, $col) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 76 | : sprintf('ALTER TABLE %s ALTER COLUMN %s NULL;', $tbl, $col); | ||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | my $old_d = defined $old->{default_value} ? $old->{default_value} : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 79 | 2 | ▶ | my $new_d = defined $new->{default_value} ? $new->{default_value} : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | if ($old_d ne $new_d) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | push @stmts, length $new_d | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', $tbl, $col, $new_d) | ||||||||||||||||||||||||||||||
| 83 | : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;', $tbl, $col); | ||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | 2 | return join "\n", @stmts; | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub summary { | ||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 91 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my $type = $self->new_info ? ' (' . $self->new_info->{data_type} . ')' : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | 1 | return sprintf ' %scolumn: %s.%s%s', | |||||||||||||||||||||||||||||
| 94 | $self->summary_prefix, $self->table_name, $self->column_name, $type; | ||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | 3 | 1; | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | =pod | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | =head1 NAME | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | DBIO::MSSQL::Diff::Column - Diff operations for MSSQL columns | ||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | version 0.900001 | ||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||
| 115 | Column-level diff operations for MSSQL. MSSQL supports C<ALTER TABLE ADD/DROP COLUMN>, | ||||||||||||||||||||||||||||||
| 116 | C<ALTER COLUMN> for type/nullability/default changes. Built on | ||||||||||||||||||||||||||||||
| 117 | L<DBIO::Diff::Op> (the nested create/change/drop walk) and | ||||||||||||||||||||||||||||||
| 118 | L<DBIO::Diff::Compare> (the C<changed_column_fields> sameness predicate). | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | =head2 diff | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | =head2 as_sql | ||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | =head2 summary | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||||
| 135 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||||
| 136 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||
| 139 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||