MSSQL/Deploy.pm

filter: Statementstmt 39.5% 19 / 48 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 6 / 12 Podpod n/a totaltotal 32.0% 25 / 78 CC 7 SCAR 24.1
CC
7
Cov
31%
_temp_connect_info
30.0
_create_temp_db
12.0
_ddl_class
2.0
CRAP 11.1
« MSSQL/Introspect/Tables.pm ↑ Summary MSSQL/Diff/Column.pm »
1 package DBIO::MSSQL::Deploy;
2 # ABSTRACT: Deploy and upgrade MSSQL schemas via test-deploy-and-compare
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use base 'DBIO::Deploy::Base::TempDatabase';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use DBIO::MSSQL::DDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBIO::MSSQL::Introspect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DBIO::MSSQL::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 0 ▶sub _ddl_class { 'DBIO::MSSQL::DDL' }
Subroutine _ddl_class: not called CC=1 SCAR=6.9
15 0 ▶sub _introspect_class { 'DBIO::MSSQL::Introspect' }
Subroutine _introspect_class: not called CC=1 SCAR=6.9
16 0 ▶sub _diff_class { 'DBIO::MSSQL::Diff' }
Subroutine _diff_class: not called CC=1 SCAR=6.9
17  
18  
19 0 ▶sub _create_temp_db {
Subroutine _create_temp_db: not called CC=3 SCAR=24.8
20 0 my ($self, $dbh) = @_;
21 0 my $name = $self->temp_db_prefix . $$ . '_' . time();
22 0 ▶ $dbh->do("COMMIT") if defined $dbh->{AutoCommit} && $dbh->{AutoCommit} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh->{"AutoCommit"} and $dbh->{"AutoCommit"} == 0 0 0 0
Condition0%0/3
defined $dbh->{"AutoCommit"} and $dbh->{"AutoCommit"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dbh->{"AutoCommit"} and $dbh->{"AutoCommit"} == 0
defined $dbh->{"AutoCommit"} $dbh->{"AutoCommit"} == 0
Truth table0%0/3
A and B
A= defined $dbh->{"AutoCommit"}B= $dbh->{"AutoCommit"} == 0
ABresult
0X0
100
111
23 0 local $dbh->{AutoCommit} = 1;
24 0 $dbh->do("CREATE DATABASE $name");
25 0 return $name;
26 }
27  
28  
29 0 ▶sub _drop_temp_db {
Subroutine _drop_temp_db: not called CC=1 SCAR=6.9
30 0 my ($self, $dbh, $name) = @_;
31 0 local $dbh->{AutoCommit} = 1;
32 0 $dbh->do("DROP DATABASE $name");
33 }
34  
35  
36 0 ▶sub _temp_connect_info {
Subroutine _temp_connect_info: not called CC=5 SCAR=34.0
37 0 my ($self, $temp_db) = @_;
38 0 my $storage = $self->schema->storage;
39 0 my @connect_info = @{ $storage->connect_info };
40  
41 0 my ($dsn, $user, $pass);
42 0 ▶ if (ref $connect_info[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $connect_info[0] eq 'HASH') { } 0 0 0
43 0 my $h = $connect_info[0];
44 0 $dsn = $h->{dsn};
45 0 $user = $h->{user};
46 0 ▶ $pass = $h->{password} // $h->{pass};
Condition0%0/3
$h->{"password"} // $h->{"pass"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$h->{"password"} // $h->{"pass"}
$h->{"password"} $h->{"pass"}
Truth table0%0/3
A // B
A= $h->{"password"}B= $h->{"pass"}
ABresult
1X1
011
000
47 } else {
48 0 ($dsn, $user, $pass) = @connect_info;
49 }
50  
51 0 ▶ if (ref $dsn eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dsn eq "CODE") 0 0 0
52 0 die ref($self) . ' does not support coderef DSN for temp database connections';
53 }
54  
55 0 ▶ if ($dsn =~ /(?:Database|dbname)=/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /(?:Database|dbname)=/i) { } 0 0 0
56 0 $dsn =~ s/(?:Database|dbname)=[^;]*/Database=$temp_db/i;
57 } else {
58 0 $dsn .= ";Database=$temp_db";
59 }
60  
61 0 return ($dsn, $user, $pass);
62 }
63  
64  
65 1 1;
66  
67 __END__
68  
69 =pod
70  
71 =encoding UTF-8
72  
73 =head1 NAME
74  
75 DBIO::MSSQL::Deploy - Deploy and upgrade MSSQL schemas via test-deploy-and-compare
76  
77 =head1 VERSION
78  
79 version 0.900001
80  
81 =head1 DESCRIPTION
82  
83 C<DBIO::MSSQL::Deploy> orchestrates schema deployment and upgrades for
84 Microsoft SQL Server using the test-deploy-and-compare strategy.
85  
86 The orchestration (C<install>, C<diff>, C<apply>, C<upgrade>) and the
87 temp-database lifecycle are inherited from
88 L<DBIO::Deploy::Base::TempDatabase>: C<diff> introspects the live database,
89 deploys the desired DDL into a throwaway database, introspects that, and
90 diffs the two models. This class supplies only the MSSQL-specific seams: the
91 three class-name hooks and the C<CREATE>/C<DROP DATABASE> dialect.
92  
93 my $deploy = DBIO::MSSQL::Deploy->new(
94 schema => MyApp::DB->connect($dsn),
95 );
96 $deploy->install; # fresh
97 my $diff = $deploy->diff; # or step-by-step
98 $deploy->apply($diff) if $diff->has_changes;
99 $deploy->upgrade; # convenience
100  
101 =head1 METHODS
102  
103 =head2 _create_temp_db
104  
105 my $name = $self->_create_temp_db($dbh);
106  
107 Creates a uniquely-named throwaway database with T-SQL C<CREATE DATABASE>.
108 MSSQL cannot run C<CREATE DATABASE> inside a transaction, so any open
109 transaction is committed and the statement runs with autocommit on.
110  
111 =head2 _drop_temp_db
112  
113 $self->_drop_temp_db($dbh, $name);
114  
115 Drops the throwaway database with T-SQL C<DROP DATABASE>.
116  
117 =head2 _temp_connect_info
118  
119 Overrides the base derivation: MSSQL DSNs use the C<Database=> attribute, so
120 both the rewrite and the append-when-absent case emit C<Database=> (the base
121 default appends C<dbname=>, which an ODBC/Sybase MSSQL DSN does not honour).
122  
123 =seealso
124  
125 =over
126  
127 =item * L<DBIO::Deploy::Base::TempDatabase> - shared temp-database orchestration
128  
129 =item * L<DBIO::MSSQL> - schema component
130  
131 =item * L<DBIO::MSSQL::DDL> - generates DDL
132  
133 =item * L<DBIO::MSSQL::Introspect> - reads live database state
134  
135 =item * L<DBIO::MSSQL::Diff> - compares two introspected models
136  
137 =back
138  
139 =head1 AUTHOR
140  
141 DBIO & DBIx::Class Authors
142  
143 =head1 COPYRIGHT AND LICENSE
144  
145 Copyright (C) 2026 DBIO Authors
146 Portions Copyright (C) 2005-2025 DBIx::Class Authors
147 Based on DBIx::Class, heavily modified.
148  
149 This is free software; you can redistribute it and/or modify it under
150 the same terms as the Perl 5 programming language system itself.
151  
152 =cut
« MSSQL/Introspect/Tables.pm ↑ Summary MSSQL/Diff/Column.pm »