| 1 | package DBIO::MSSQL::Deploy; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Deploy and upgrade MSSQL schemas via test-deploy-and-compare | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 25 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use base 'DBIO::Deploy::Base::TempDatabase'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 25 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 25 | ▶ | use DBIO::MSSQL::DDL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 25 | ▶ | use DBIO::MSSQL::Diff::Column (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 25 | ▶ | use DBIO::MSSQL::Introspect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 25 | ▶ | use DBIO::MSSQL::Diff; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _ddl_class { 'DBIO::MSSQL::DDL' } | ||||||||||||||||||||||||||
|
Subroutine
_ddl_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub _introspect_class { 'DBIO::MSSQL::Introspect' } | ||||||||||||||||||||||||||
|
Subroutine
_introspect_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _diff_class { 'DBIO::MSSQL::Diff' } | ||||||||||||||||||||||||||
|
Subroutine
_diff_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub _new_introspect { | ||||||||||||||||||||||||||
|
Subroutine
_new_introspect: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||
| 25 | 0 | return $self->_introspect_class->new( | |||||||||||||||||||||||||||
| 26 | dbh => $dbh, | ||||||||||||||||||||||||||||
| 27 | schemas => [ DBIO::MSSQL::DDL::_schemas_of($self->schema) ], | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | sub _create_temp_db { | ||||||||||||||||||||||||||
|
Subroutine
_create_temp_db: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 33 | 3 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||
| 34 | 3 | my $name = $self->temp_db_prefix . $$ . '_' . time(); | |||||||||||||||||||||||||||
| 35 | 3 | $self->_require_autocommit($dbh, '_create_temp_db'); | |||||||||||||||||||||||||||
| 36 | 2 | my ($collation) = $dbh->selectrow_array( | |||||||||||||||||||||||||||
| 37 | q{SELECT CAST(DATABASEPROPERTYEX(DB_NAME(), 'Collation') AS nvarchar(128))}); | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $dbh->do("CREATE DATABASE $name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $collation and $collation =~ /\A\w+\z/
MC/DC0%0/2
defined $collation and $collation =~ /\A\w+\z/
defined $collation
$collation =~ /\A\w+\z/
Truth table33%1/3
A and B
A= defined $collationB= $collation =~ /\A\w+\z/
| |||||||||||||||||||||||||||||
| 39 | . (defined $collation && $collation =~ /\A\w+\z/ ? " COLLATE $collation" : '')); | ||||||||||||||||||||||||||||
| 40 | 2 | return $name; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | sub _drop_temp_db { | ||||||||||||||||||||||||||
|
Subroutine
_drop_temp_db: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 3 | my ($self, $dbh, $name) = @_; | |||||||||||||||||||||||||||
| 46 | 3 | $self->_require_autocommit($dbh, '_drop_temp_db'); | |||||||||||||||||||||||||||
| 47 | 2 | $dbh->do("DROP DATABASE $name"); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # CREATE/DROP DATABASE may not run in a transaction, and neither switching | ||||||||||||||||||||||||||||
| 51 | # AutoCommit on nor a COMMIT would leave the caller's transaction standing: | ||||||||||||||||||||||||||||
| 52 | # a handle with AutoCommit off is refused before any statement (karr k80). | ||||||||||||||||||||||||||||
| 53 | 6 | ▶ | sub _require_autocommit { | ||||||||||||||||||||||||||
|
Subroutine
_require_autocommit: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 54 | 6 | my ($self, $dbh, $method) = @_; | |||||||||||||||||||||||||||
| 55 | 6 | my $ac = $dbh->{AutoCommit}; | |||||||||||||||||||||||||||
| 56 | 6 | ▶ | return unless defined $ac && $ac == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ac and $ac == 0
MC/DC50%1/2
defined $ac and $ac == 0
defined $ac
$ac == 0
Truth table66%2/3
A and B
A= defined $acB= $ac == 0
| |||||||||||||||||||||||||||||
| 57 | 2 | require DBIO::Exception; | |||||||||||||||||||||||||||
| 58 | 2 | DBIO::Exception->throw( | |||||||||||||||||||||||||||
| 59 | ref($self) . "::$method: refused -- the handle has AutoCommit off (a " | ||||||||||||||||||||||||||||
| 60 | . "transaction may be open), and CREATE DATABASE and DROP DATABASE cannot run " | ||||||||||||||||||||||||||||
| 61 | . "inside a transaction; nothing was sent and the transaction is untouched. " | ||||||||||||||||||||||||||||
| 62 | . "Pass a handle with AutoCommit on." | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub _check_before_apply { | ||||||||||||||||||||||||||
|
Subroutine
_check_before_apply: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 1 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||
| 69 | 1 | $self->_refuse_unfillable($diff, $self->_dbh); | |||||||||||||||||||||||||||
| 70 | 1 | return $self->next::method($diff); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | ▶ | sub _refuse_unfillable { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_unfillable: called CC=15 SCAR=27.4
| |||||||||||||||||||||||||||||
| 75 | 7 | my ($self, $diff, $dbh) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 4 | ▶ | my $has_rows = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | 4 | my ($table, $where) = @_; | |||||||||||||||||||||||||||
| 79 | 4 | ▶ | return scalar $dbh->selectrow_array(sprintf 'SELECT TOP 1 1 FROM %s%s', | ||||||||||||||||||||||||||
|
Condition100%2/2
$where // ""
MC/DC100%1/1
$where // ""
$where
Truth table100%2/2
A
A= $where
| |||||||||||||||||||||||||||||
| 80 | DBIO::MSSQL::DDL::_quote_table($table), $where // ''); | ||||||||||||||||||||||||||||
| 81 | 7 | }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 7 | for my $op (@{ $diff->operations }) { | |||||||||||||||||||||||||||
| 84 | 8 | ▶ | next unless $op->isa('DBIO::MSSQL::Diff::Column'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 8 | my ($table, $column) = ($op->table_name, $op->column_name); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 8 | ▶ | if ($op->action eq 'add') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | my $new = $op->new_info || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$op->new_info || {}
MC/DC0%0/1
$op->new_info || {}
$op->new_info
Truth table50%1/2
A
A= $op->new_info
| |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | next unless $new->{not_null} && !defined $new->{default_value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$new->{"not_null"} and not defined $new->{"default_value"}
MC/DC100%2/2
$new->{"not_null"} and not defined $new->{"default_value"}
$new->{"not_null"}
not defined $new->{"default_value"}
Truth table100%3/3
A and not(B)
A= $new->{"not_null"}B= not defined $new->{"default_value"}
| |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | next unless $has_rows->($table); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | DBIO::Exception->throw(sprintf | |||||||||||||||||||||||||||
| 92 | 'SQL Server refuses to add the column %s.%s: it is NOT NULL without a ' | ||||||||||||||||||||||||||||
| 93 | . 'default, and table %s holds rows that would have no value for it', | ||||||||||||||||||||||||||||
| 94 | $table, $column, $table); | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | elsif ($op->action eq 'alter') { | ||||||||||||||||||||||||||||
| 97 | 4 | ▶ | my ($old, $new) = ($op->old_info || {}, $op->new_info || {}); | ||||||||||||||||||||||||||
|
Condition50%2/4
$op->old_info || {}
$op->new_info || {}
MC/DC0%0/1
$op->old_info || {}
$op->old_info
MC/DC0%0/1
$op->new_info || {}
$op->new_info
Truth table50%1/2
A
A= $op->old_info
Truth table50%1/2
A
A= $op->new_info
| |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | my $nn = defined $new->{not_null} ? $new->{not_null} : $old->{not_null}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | next unless $nn && !$old->{not_null}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$nn and not $old->{"not_null"}
MC/DC100%2/2
$nn and not $old->{"not_null"}
$nn
not $old->{"not_null"}
Truth table100%3/3
A and not(B)
A= $nnB= not $old->{"not_null"}
| |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | next unless $has_rows->($table, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | ' WHERE ' . DBIO::MSSQL::DDL::_quote_ident($column) . ' IS NULL'); | ||||||||||||||||||||||||||||
| 102 | 1 | DBIO::Exception->throw(sprintf | |||||||||||||||||||||||||||
| 103 | 'SQL Server refuses to make the column %s.%s NOT NULL: table %s holds ' | ||||||||||||||||||||||||||||
| 104 | . 'rows where it is NULL', $table, $column, $table); | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 5 | return; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 9 | ▶ | sub _temp_dsn { | ||||||||||||||||||||||||||
|
Subroutine
_temp_dsn: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 112 | 9 | my ($self, $dsn, $temp_db) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 9 | ▶ | return $dsn if $dsn =~ s/\b((?:Database|dbname)=)[^;]*/$1$temp_db/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | ▶ | my $key = $dsn =~ /^dbi:Sybase:/i ? 'database' : 'Database'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 4 | $dsn =~ s/;\z//; | |||||||||||||||||||||||||||
| 118 | 4 | return "$dsn;$key=$temp_db"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 25 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||