MSSQL/Diff/Table.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 75.0% 9 / 12 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 93.3% 84 / 90 CC 13 SCAR 15.8
CC
13
Cov
93%
as_sql
9.1
__ANON__
3.0
diff
3.0
CRAP 4.9
« MSSQL/Diff/Column.pm ↑ Summary MSSQL/Diff/Index.pm »
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 //= {}
l!l
61
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
19 7 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
61
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
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} // []
l!l
20
$target_fks->{$name} // []
l!l
11
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
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
BranchTrueFalseTotal
if ($self->action eq "drop") 3 2 5
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
BranchTrueFalseTotal
if $col->{"is_pk"} 2 2 4
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
BranchTrueFalseTotal
if $col->{"not_null"} 4 0 4
60 4 ▶ if (defined $col->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"}) 0 4 4
61 0 $def .= " DEFAULT $col->{default_value}";
62 }
63 4 ▶ if ($col->{is_identity}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"is_identity"}) 1 3 4
64 1 $def .= ' IDENTITY(1,1)';
65 }
66 4 push @col_defs, $def;
67 }
68  
69 2 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 2 0 2
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
« MSSQL/Diff/Column.pm ↑ Summary MSSQL/Diff/Index.pm »