| 1 | package DBIO::MSSQL::Diff::Table; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL tables | ||||||||||||||||||||||
| 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 | |||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | 3 | __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns foreign_keys/); | |||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 7 | ▶ | sub diff { | ||||||||||||||||||||
|
Subroutine
diff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 17 | 7 | my ($class, $source, $target, $target_columns, $target_fks) = @_; | |||||||||||||||||||||
| 18 | 7 | ▶ | $target_columns //= {}; | ||||||||||||||||||||
|
Condition100%2/2
$target_columns //= {}
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||
| 19 | 7 | ▶ | $target_fks //= {}; | ||||||||||||||||||||
|
Condition100%2/2
$target_fks //= {}
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
| |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | return $class->diff_toplevel($source, $target, | ||||||||||||||||||||||
| 22 | 2 | ▶ | create => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||
| 23 | 2 | my ($name) = @_; | |||||||||||||||||||||
| 24 | $class->new( | ||||||||||||||||||||||
| 25 | action => 'create', | ||||||||||||||||||||||
| 26 | table_name => $name, | ||||||||||||||||||||||
| 27 | table_info => $target->{$name}, | ||||||||||||||||||||||
| 28 | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||
| 29 | 2 | ▶ | foreign_keys => $target_fks->{$name} // [], | ||||||||||||||||||||
|
Condition75%3/4
$target_columns->{$name} // []
$target_fks->{$name} // []
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}
Truth table100%2/2
A
A= $target_fks->{$name}
| |||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||
| 32 | 3 | ▶ | drop => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 33 | 3 | my ($name) = @_; | |||||||||||||||||||||
| 34 | $class->new( | ||||||||||||||||||||||
| 35 | action => 'drop', | ||||||||||||||||||||||
| 36 | table_name => $name, | ||||||||||||||||||||||
| 37 | 3 | table_info => $source->{$name}, | |||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||
| 40 | 7 | ); | |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | 5 | ▶ | sub as_sql { | ||||||||||||||||||||
|
Subroutine
as_sql: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||
| 45 | 5 | my ($self) = @_; | |||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | 5 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 48 | 3 | return sprintf 'DROP TABLE %s;', _quote_ident($self->table_name); | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | 2 | my @col_defs; | |||||||||||||||||||||
| 52 | my @pk_cols; | ||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 2 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||
| 55 | 4 | ▶ | push @pk_cols, $col->{column_name} if $col->{is_pk}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | 4 | my $type = _mssql_column_type($col); | |||||||||||||||||||||
| 58 | 4 | my $def = sprintf ' %s %s', _quote_ident($col->{column_name}), $type; | |||||||||||||||||||||
| 59 | 4 | ▶ | $def .= ' NOT NULL' if $col->{not_null}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 60 | 4 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 61 | 0 | $def .= " DEFAULT $col->{default_value}"; | |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | 4 | ▶ | if ($col->{is_identity}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 64 | 1 | $def .= ' IDENTITY(1,1)'; | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | 4 | push @col_defs, $def; | |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 2 | ▶ | if (@pk_cols) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 70 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||
| 71 | 2 | join(', ', map { _quote_ident($_) } @pk_cols); | |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 2 | for my $fk (@{ $self->foreign_keys }) { | |||||||||||||||||||||
| 75 | push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||
| 76 | 1 | join(', ', map { _quote_ident($_) } @{ $fk->{from_columns} }), | |||||||||||||||||||||
| 77 | _quote_ident($fk->{to_table}), | ||||||||||||||||||||||
| 78 | 1 | join(', ', map { _quote_ident($_) } @{ $fk->{to_columns} }); | |||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 2 | return sprintf "CREATE TABLE %s (\n%s\n);", | |||||||||||||||||||||
| 82 | _quote_ident($self->table_name), join(",\n", @col_defs); | ||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 4 | ▶ | sub summary { | ||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 87 | 4 | my ($self) = @_; | |||||||||||||||||||||
| 88 | 4 | return sprintf '%s table: %s', $self->summary_prefix, $self->table_name; | |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 3 | 1; | |||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | =pod | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | =encoding UTF-8 | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | =head1 NAME | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | DBIO::MSSQL::Diff::Table - Diff operations for MSSQL tables | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | =head1 VERSION | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | version 0.900001 | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | Table-level diff operations for MSSQL. Handles C<CREATE TABLE>, C<DROP TABLE>. | ||||||||||||||||||||||
| 110 | C<create> ops capture the target columns and foreign keys so C<as_sql> can emit | ||||||||||||||||||||||
| 111 | the full inline definition. Built on L<DBIO::Diff::Op> (the C<create>/C<drop> | ||||||||||||||||||||||
| 112 | walk and accessor/summary plumbing). | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | =head1 METHODS | ||||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | =head2 diff | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | =head2 as_sql | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | =head2 summary | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | =head1 AUTHOR | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||
| 129 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||
| 130 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||
| 133 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||