| 1 | package DBIO::Sybase::Deploy; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Deploy and upgrade Sybase ASE 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 Scope::Guard (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 25 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 25 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 25 | ▶ | use DBIO::Sybase::Diff; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 25 | ▶ | use DBIO::Sybase::Introspect; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 25 | ▶ | use DBIO::Sybase::DDL; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 25 | ▶ | use DBIO::Sybase::Quote (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | # Class-name hooks consumed by DBIO::Deploy::Base. | ||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub _ddl_class { 'DBIO::Sybase::DDL' } | ||||||||||||||||||||||||||||||
|
Subroutine
_ddl_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub _introspect_class { 'DBIO::Sybase::Introspect' } | ||||||||||||||||||||||||||||||
|
Subroutine
_introspect_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _diff_class { 'DBIO::Sybase::Diff' } | ||||||||||||||||||||||||||||||
|
Subroutine
_diff_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | # --- temp-database glue (engine-specific) --------------------------------- | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # The n of the name: two temp databases of one process in the same minute | ||||||||||||||||||||||||||||||||
| 30 | # (one left behind by a refused DROP, karr k75) must not share a name. | ||||||||||||||||||||||||||||||||
| 31 | 25 | my $temp_db_serial = 0; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | # Create a uniquely-named temp database and return its name. CREATE DATABASE | ||||||||||||||||||||||||||||||||
| 34 | # cannot run inside a transaction, so the handle must have AutoCommit; one | ||||||||||||||||||||||||||||||||
| 35 | # without it is refused before anything runs, and its transaction is left alone | ||||||||||||||||||||||||||||||||
| 36 | # (karr k96, _require_autocommit). | ||||||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||||||
| 38 | # It is created at the size the schema to be deployed needs, or at temp_db_size | ||||||||||||||||||||||||||||||||
| 39 | # (karr k69, see "The size of the temporary database" above); a server without | ||||||||||||||||||||||||||||||||
| 40 | # the room for it (Msg 1810) is reported by that size. | ||||||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||||||
| 42 | # The database is handed on only with 'abort tran on log full' set (karr k69, | ||||||||||||||||||||||||||||||||
| 43 | # see "A schema too large for the temporary database" above). One the option | ||||||||||||||||||||||||||||||||
| 44 | # could not be set on is dropped here -- core has not heard its name yet and | ||||||||||||||||||||||||||||||||
| 45 | # would never drop it -- and the error raised; if the drop fails too, the error | ||||||||||||||||||||||||||||||||
| 46 | # says which database stayed, since nothing else will. | ||||||||||||||||||||||||||||||||
| 47 | 44 | ▶ | sub _create_temp_db { | ||||||||||||||||||||||||||||||
|
Subroutine
_create_temp_db: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | 44 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 49 | # At most 30 characters, ASE's limit for a database name (Msg 103): the time | ||||||||||||||||||||||||||||||||
| 50 | # is in minutes, not seconds, so that a seven-digit pid and a serial up to | ||||||||||||||||||||||||||||||||
| 51 | # 999 still fit (karr k97). The serial is what makes the name unique. | ||||||||||||||||||||||||||||||||
| 52 | 44 | my $name = $self->temp_db_prefix . $$ . '_' . int(time() / 60) . '_' . ++$temp_db_serial; | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 44 | $self->_require_autocommit($dbh, '_create_temp_db'); | |||||||||||||||||||||||||||||||
| 55 | 43 | my $size = $self->_temp_db_size_to_create($dbh); | |||||||||||||||||||||||||||||||
| 56 | 37 | ▶ | $self->_in_master($dbh, sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 57 | 37 | ▶ | unless (eval { $dbh->do("CREATE DATABASE $name ON default = '$size'"); 1 }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 3 | my $error = $@; | |||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | die $error unless ( $dbh->err // 0 ) == 1810 or $error =~ /\bnumber=1810\b|\bMsg 1810\b/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
($dbh->err // 0) == 1810 or $error =~ /\bnumber=1810\b|\bMsg 1810\b/
$dbh->err // 0
MC/DC0%0/2
($dbh->err // 0) == 1810 or $error =~ /\bnumber=1810\b|\bMsg 1810\b/
$dbh->err
$error =~ /\bnumber=1810\b|\bMsg 1810\b/
Truth table66%2/3
A or B
A= $dbh->errB= $error =~ /\bnumber=1810\b|\bMsg 1810\b/
| |||||||||||||||||||||||||||||||||
| 60 | 3 | die ref($self) . ": the server has no room for the temporary database $name " | |||||||||||||||||||||||||||||||
| 61 | . "at the size of $size asked for it (Msg 1810) -- make room on the default " | ||||||||||||||||||||||||||||||||
| 62 | . "device(s), or set temp_db_size to a size that fits:\n$error"; | ||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | 34 | ▶ | return if eval { $self->_abort_tran_on_log_full($dbh, $name); 1 }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 6 | my $error = $@; | |||||||||||||||||||||||||||||||
| 66 | 6 | ▶ | eval { $self->_drop_temp_db($dbh, $name); 1 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | or $error = "$error\nThe temporary database $name was left on the server: $@"; | ||||||||||||||||||||||||||||||||
| 68 | 6 | die $error; | |||||||||||||||||||||||||||||||
| 69 | 37 | }); | |||||||||||||||||||||||||||||||
| 70 | 28 | return $name; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 45 | ▶ | sub temp_db_size { $_[0]->{temp_db_size} } | ||||||||||||||||||||||||||||||
|
Subroutine
temp_db_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # The size CREATE DATABASE is sent, as 'NNM' text: temp_db_size as given, or | ||||||||||||||||||||||||||||||||
| 77 | # the calculation. The server is asked only for the calculation. | ||||||||||||||||||||||||||||||||
| 78 | 43 | ▶ | sub _temp_db_size_to_create { | ||||||||||||||||||||||||||||||
|
Subroutine
_temp_db_size_to_create: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||
| 79 | 43 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 43 | ▶ | if (defined( my $given = $self->temp_db_size )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 11 | ▶ | $given =~ /\A([0-9]+)([kmgt]?)\z/i | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | or die ref($self) . ": temp_db_size '$given' is not a size: a whole number " | ||||||||||||||||||||||||||||||||
| 84 | . "and optionally K, M, G or T (28M, 1G; a number alone is megabytes)\n"; | ||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | return $1 . ( $2 ? uc $2 : 'M' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # The page size of the server and the pages model has, in all and in use: | ||||||||||||||||||||||||||||||||
| 89 | # model is what a new database is copied from, and no database is smaller. | ||||||||||||||||||||||||||||||||
| 90 | 32 | my ($page, $model_total, $model_used) = $dbh->selectrow_array( | |||||||||||||||||||||||||||||||
| 91 | 'SELECT @@maxpagesize, SUM(size), ' | ||||||||||||||||||||||||||||||||
| 92 | . 'SUM(size - curunreservedpgs(dbid, lstart, unreservedpgs)) ' | ||||||||||||||||||||||||||||||||
| 93 | . "FROM master..sysusages WHERE dbid = db_id('model')" | ||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||
| 95 | 32 | ▶ | die ref($self) . ": could not read the page size and the size of model from the server\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$page and $model_total
MC/DC0%0/2
$page and $model_total
$page
$model_total
Truth table33%1/3
A and B
A= $pageB= $model_total
| |||||||||||||||||||||||||||||||||
| 96 | unless $page && $model_total; | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 32 | my ($tables, $indexes) = $self->_temp_db_objects($self->schema); | |||||||||||||||||||||||||||||||
| 99 | 32 | ▶ | return $self->_temp_db_megabytes($tables, $indexes, $page, $model_total, $model_used // 0) . 'M'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$model_used // 0
MC/DC0%0/1
$model_used // 0
$model_used
Truth table50%1/2
A
A= $model_used
| |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | # (tables, other indexes) the DDL of $schema writes: the objects that take | ||||||||||||||||||||||||||||||||
| 103 | # space. What DBIO::Sybase::DDL leaves out is left out here -- a view, a source | ||||||||||||||||||||||||||||||||
| 104 | # whose name is not a plain table, a table a second source maps to again -- and | ||||||||||||||||||||||||||||||||
| 105 | # the indexes are those it writes: a unique column, a unique constraint, a | ||||||||||||||||||||||||||||||||
| 106 | # declared index, none of them a second time over the columns of one written. | ||||||||||||||||||||||||||||||||
| 107 | # The primary key is not among them; a table is counted with its key (below). | ||||||||||||||||||||||||||||||||
| 108 | # Over-counting is safe, under-counting is the hang this class was fixed of. | ||||||||||||||||||||||||||||||||
| 109 | 33 | ▶ | sub _temp_db_objects { | ||||||||||||||||||||||||||||||
|
Subroutine
_temp_db_objects: called CC=20 SCAR=30.9
| |||||||||||||||||||||||||||||||||
| 110 | 33 | my ($class, $schema) = @_; | |||||||||||||||||||||||||||||||
| 111 | 33 | my ($tables, $indexes, %seen_table) = (0, 0); | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 33 | for my $source_name (sort $schema->sources) { | |||||||||||||||||||||||||||||||
| 114 | 112 | my $source = $schema->source($source_name); | |||||||||||||||||||||||||||||||
| 115 | 112 | ▶ | next if $source->isa('DBIO::ResultSource::View'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 111 | my $table_name = $source->name; | |||||||||||||||||||||||||||||||
| 117 | 111 | ▶ | if (ref $table_name eq 'SCALAR') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 0 | $table_name = $$table_name; | |||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | next if $table_name =~ /\s|\(/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 111 | ▶ | next if ref $table_name || $seen_table{$table_name}++; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $table_name or $seen_table{$table_name}++
MC/DC50%1/2
ref $table_name or $seen_table{$table_name}++
ref $table_name
$seen_table{$table_name}++
Truth table66%2/3
A or B
A= ref $table_nameB= $seen_table{$table_name}++
| |||||||||||||||||||||||||||||||||
| 122 | 110 | $tables++; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 110 | my @pk_cols = $source->primary_columns; | |||||||||||||||||||||||||||||||
| 125 | 110 | ▶ | my %written = @pk_cols ? ( join("\0", @pk_cols) => 1 ) : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 110 | ▶ | my %unique = @pk_cols ? ( join("\0", sort @pk_cols) => 1 ) : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 110 | for my $col_name ($source->columns) { | |||||||||||||||||||||||||||||||
| 129 | 221 | my $info = $source->column_info($col_name); | |||||||||||||||||||||||||||||||
| 130 | 221 | ▶ | next unless $info->{is_unique} || $info->{is_single_unique_key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
MC/DC50%1/2
$info->{"is_unique"} or $info->{"is_single_unique_key"}
$info->{"is_unique"}
$info->{"is_single_unique_key"}
Truth table66%2/3
A or B
A= $info->{"is_unique"}B= $info->{"is_single_unique_key"}
| |||||||||||||||||||||||||||||||||
| 131 | 1 | $unique{$col_name} = $written{$col_name} = 1; | |||||||||||||||||||||||||||||||
| 132 | 1 | $indexes++; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 110 | my %uniques = $source->unique_constraints; | |||||||||||||||||||||||||||||||
| 136 | 110 | for my $uname (sort keys %uniques) { | |||||||||||||||||||||||||||||||
| 137 | 155 | ▶ | next if $uname eq 'primary'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 46 | my @cols = @{ $uniques{$uname} }; | |||||||||||||||||||||||||||||||
| 139 | 46 | ▶ | next if !@cols || $unique{ join "\0", sort @cols }++; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not @cols or $unique{join "\0", sort(@cols)}++
MC/DC0%0/2
not @cols or $unique{join "\0", sort(@cols)}++
not @cols
$unique{join "\0", sort(@cols)}++
Truth table33%1/3
not(A) or B
A= not @colsB= $unique{join "\0", sort(@cols)}++
| |||||||||||||||||||||||||||||||||
| 140 | 46 | $written{ join "\0", @cols } = 1; | |||||||||||||||||||||||||||||||
| 141 | 46 | $indexes++; | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 110 | my %named = map { $_ => 1 } keys %uniques; | |||||||||||||||||||||||||||||||
| 145 | 110 | for my $decl ($source->index_declarations_for('sybase', [])) { | |||||||||||||||||||||||||||||||
| 146 | 6 | my @cols = @{ $decl->{columns} }; | |||||||||||||||||||||||||||||||
| 147 | 6 | ▶ | next if $written{ join "\0", @cols }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 2 | ▶ | next if $named{ $decl->{name} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | $written{ join "\0", @cols } = 1 if $decl->{unique}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 2 | $indexes++; | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | 33 | return ($tables, $indexes); | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | # The size in MB (a multiple of 4, the allocation unit of the server) of a | ||||||||||||||||||||||||||||||||
| 157 | # database that holds $tables tables and $indexes further indexes, on a server | ||||||||||||||||||||||||||||||||
| 158 | # with pages of $page bytes whose model has $model_total pages, $model_used of | ||||||||||||||||||||||||||||||||
| 159 | # them in use. Measured on ASE 16.0 SP02 (16K pages): a table, its primary key | ||||||||||||||||||||||||||||||||
| 160 | # and every further index take an extent of 8 pages each, and what is left of | ||||||||||||||||||||||||||||||||
| 161 | # the schema (system tables, the log) took 45 more pages for 36 tables; the | ||||||||||||||||||||||||||||||||
| 162 | # calculation (2 * tables + indexes) * 8 hit 688 of the 733 pages that schema | ||||||||||||||||||||||||||||||||
| 163 | # needed, so a quarter and 64 pages are added to it. A database is copied from | ||||||||||||||||||||||||||||||||
| 164 | # model, which is therefore in use already, and is never smaller than it. | ||||||||||||||||||||||||||||||||
| 165 | 43 | ▶ | sub _temp_db_megabytes { | ||||||||||||||||||||||||||||||
|
Subroutine
_temp_db_megabytes: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 166 | 43 | my ($class, $tables, $indexes, $page, $model_total, $model_used) = @_; | |||||||||||||||||||||||||||||||
| 167 | 43 | my $schema_pages = ( 2 * $tables + $indexes ) * 8; | |||||||||||||||||||||||||||||||
| 168 | 43 | my $pages = $schema_pages + int( $schema_pages / 4 + 0.999 ) + 64 + $model_used; | |||||||||||||||||||||||||||||||
| 169 | 43 | ▶ | $pages = $model_total if $pages < $model_total; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 43 | my $unit = 4 * 1024 * 1024; | |||||||||||||||||||||||||||||||
| 172 | 43 | return int( ( $pages * $page + $unit - 1 ) / $unit ) * 4; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | # Set 'abort tran on log full' on the database $name; $dbh is in master and | ||||||||||||||||||||||||||||||||
| 176 | # outside a transaction, which sp_dboption insists on (Msg 17428, Msg 17260). | ||||||||||||||||||||||||||||||||
| 177 | # The procedure runs the checkpoint that puts the option into effect itself. | ||||||||||||||||||||||||||||||||
| 178 | # It reports that and the option in two messages of severity 10, which | ||||||||||||||||||||||||||||||||
| 179 | # DBD::Sybase prints as warnings unless the handle's syb_err_handler takes | ||||||||||||||||||||||||||||||||
| 180 | # them: the slot is borrowed for the statement, everything above severity 10 | ||||||||||||||||||||||||||||||||
| 181 | # keeps going to the handler it displaced, and the Scope::Guard puts that one | ||||||||||||||||||||||||||||||||
| 182 | # back also when the statement dies (the pattern of Storage::ASE::_execute_sth). | ||||||||||||||||||||||||||||||||
| 183 | # Then the option is looked at (status2 & 1 in sysdatabases): a database | ||||||||||||||||||||||||||||||||
| 184 | # without it is one a deploy can hang in for good, so a procedure that merely | ||||||||||||||||||||||||||||||||
| 185 | # returned is not taken for an answer. | ||||||||||||||||||||||||||||||||
| 186 | 34 | ▶ | sub _abort_tran_on_log_full { | ||||||||||||||||||||||||||||||
|
Subroutine
_abort_tran_on_log_full: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 187 | 34 | my ($self, $dbh, $name) = @_; | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 34 | my $old = $dbh->{syb_err_handler}; | |||||||||||||||||||||||||||||||
| 190 | 34 | ▶ | $dbh->{syb_err_handler} = sub { $_[1] > 10 ? ( $old ? $old->(@_) : 1 ) : 0 }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 191 | 34 | ▶ | my $restore = Scope::Guard->new(sub { $dbh->{syb_err_handler} = $old }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 34 | $dbh->do("EXEC sp_dboption '$name', 'abort tran on log full', true"); | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 30 | my ($set) = $dbh->selectrow_array( | |||||||||||||||||||||||||||||||
| 196 | "SELECT status2 & 1 FROM master..sysdatabases WHERE name = '$name'" | ||||||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||||||
| 198 | 30 | ▶ | die ref($self) . ": could not set 'abort tran on log full' on the temporary database $name" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | unless $set; | ||||||||||||||||||||||||||||||||
| 200 | 28 | return 1; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | # Drop the temp database. ASE refuses that with Msg 3702 while anything is in | ||||||||||||||||||||||||||||||||
| 204 | # it, which is over by itself (see "The temporary database is dropped more than | ||||||||||||||||||||||||||||||||
| 205 | # once if need be" above), and core catches whatever this dies of: one refusal | ||||||||||||||||||||||||||||||||
| 206 | # would leave the database on the server and nobody would hear of it. So Msg | ||||||||||||||||||||||||||||||||
| 207 | # 3702, and no other error, is answered by trying again -- _drop_in_use_tries | ||||||||||||||||||||||||||||||||
| 208 | # times in all, _drop_in_use_wait seconds apart, from master throughout | ||||||||||||||||||||||||||||||||
| 209 | # (karr k74). After the last try the server's error propagates as before. | ||||||||||||||||||||||||||||||||
| 210 | 16 | ▶ | sub _drop_temp_db { | ||||||||||||||||||||||||||||||
|
Subroutine
_drop_temp_db: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 211 | 16 | my ($self, $dbh, $name) = @_; | |||||||||||||||||||||||||||||||
| 212 | 16 | $self->_require_autocommit($dbh, '_drop_temp_db'); | |||||||||||||||||||||||||||||||
| 213 | 15 | ▶ | $self->_in_master($dbh, sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 214 | 15 | my $tries = $self->_drop_in_use_tries; | |||||||||||||||||||||||||||||||
| 215 | 15 | while (1) { | |||||||||||||||||||||||||||||||
| 216 | 59 | ▶ | return if eval { $dbh->do("DROP DATABASE $name"); 1 }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 217 | 48 | my $error = $@; | |||||||||||||||||||||||||||||||
| 218 | 48 | ▶ | die $error unless ( $dbh->err // 0 ) == 3702 and --$tries > 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
($dbh->err // 0) == 3702 and --$tries > 0
$dbh->err // 0
MC/DC100%2/2
($dbh->err // 0) == 3702 and --$tries > 0
$dbh->err
--$tries > 0
Truth table100%3/3
A and B
A= $dbh->errB= --$tries > 0
| |||||||||||||||||||||||||||||||||
| 219 | 44 | Time::HiRes::sleep($self->_drop_in_use_wait); | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | 15 | }); | |||||||||||||||||||||||||||||||
| 222 | 11 | return 1; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | # CREATE/DROP DATABASE do not run in a transaction on ASE, and ending the | ||||||||||||||||||||||||||||||||
| 226 | # caller's transaction (COMMIT, or switching AutoCommit on, which DBI commits | ||||||||||||||||||||||||||||||||
| 227 | # for) is not these methods' to do (karr k96). So: refuse, first thing. | ||||||||||||||||||||||||||||||||
| 228 | 60 | ▶ | sub _require_autocommit { | ||||||||||||||||||||||||||||||
|
Subroutine
_require_autocommit: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 229 | 60 | my ($self, $dbh, $method) = @_; | |||||||||||||||||||||||||||||||
| 230 | 60 | ▶ | return 1 if $dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | 2 | DBIO::Exception->throw(ref($self) . "::$method needs a handle with AutoCommit: " | |||||||||||||||||||||||||||||||
| 232 | . "CREATE DATABASE and DROP DATABASE do not run in a transaction on ASE, and " | ||||||||||||||||||||||||||||||||
| 233 | . "$method does not commit a transaction that is not its own"); | ||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | # Ten seconds in all. | ||||||||||||||||||||||||||||||||
| 237 | 15 | ▶ | sub _drop_in_use_tries { 40 } | ||||||||||||||||||||||||||||||
|
Subroutine
_drop_in_use_tries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 238 | 44 | ▶ | sub _drop_in_use_wait { 0.25 } | ||||||||||||||||||||||||||||||
|
Subroutine
_drop_in_use_wait: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | # Run $code with $dbh's current database switched to master, then switch it | ||||||||||||||||||||||||||||||||
| 241 | # back -- also when $code dies (karr k46). ASE refuses CREATE DATABASE from any | ||||||||||||||||||||||||||||||||
| 242 | # other database (Msg 1809 "CREATE DATABASE must be preceded by a 'USE master' | ||||||||||||||||||||||||||||||||
| 243 | # command", met live on ASE 16.0 SP02), and a database cannot be dropped from | ||||||||||||||||||||||||||||||||
| 244 | # inside itself. $dbh is the live storage's handle: after the temp database is | ||||||||||||||||||||||||||||||||
| 245 | # dropped, core's _build_target_model hands it on to introspect the live side | ||||||||||||||||||||||||||||||||
| 246 | # and apply() runs through it, so leaving it in master would read and alter | ||||||||||||||||||||||||||||||||
| 247 | # master instead of the target. The guard is created only after the switch | ||||||||||||||||||||||||||||||||
| 248 | # succeeded, so a failed USE master restores nothing it did not change. | ||||||||||||||||||||||||||||||||
| 249 | 52 | ▶ | sub _in_master { | ||||||||||||||||||||||||||||||
|
Subroutine
_in_master: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 250 | 52 | my ($self, $dbh, $code) = @_; | |||||||||||||||||||||||||||||||
| 251 | 52 | my ($orig) = $dbh->selectrow_array('SELECT db_name()'); | |||||||||||||||||||||||||||||||
| 252 | 52 | $dbh->do('USE master'); | |||||||||||||||||||||||||||||||
| 253 | 52 | ▶ | my $restore = Scope::Guard->new(sub { $dbh->do("USE $orig") }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 254 | 52 | return $code->(); | |||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | # DBD::Sybase names the database with database=; the core fallback appends | ||||||||||||||||||||||||||||||||
| 258 | # dbname=, which DBD::Sybase does not read, so a DSN without a database would | ||||||||||||||||||||||||||||||||
| 259 | # connect the temp deploy to the login's default database and create the | ||||||||||||||||||||||||||||||||
| 260 | # desired schema THERE. Replace database= in place, or append it. | ||||||||||||||||||||||||||||||||
| 261 | 7 | ▶ | sub _temp_dsn { | ||||||||||||||||||||||||||||||
|
Subroutine
_temp_dsn: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 262 | 7 | my ($self, $dsn, $temp_db) = @_; | |||||||||||||||||||||||||||||||
| 263 | 7 | ▶ | return $dsn if $dsn =~ s/\bdatabase=[^;]*/database=$temp_db/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | 2 | ▶ | return $dsn =~ /:$/ ? "${dsn}database=$temp_db" : "$dsn;database=$temp_db"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 6 | ▶ | sub _check_before_apply { | ||||||||||||||||||||||||||||||
|
Subroutine
_check_before_apply: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 271 | 6 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||||
| 272 | 6 | $self->SUPER::_check_before_apply($diff); | |||||||||||||||||||||||||||||||
| 273 | 6 | $self->_refuse_nulls($diff, $self->_dbh); | |||||||||||||||||||||||||||||||
| 274 | 4 | return; | |||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | # A column that becomes NOT NULL in a table that holds a NULL in it is Msg 515 | ||||||||||||||||||||||||||||||||
| 278 | # half way through the batch, after the statements before it were applied and | ||||||||||||||||||||||||||||||||
| 279 | # with no way back (the DDL is not transactional, see above). Refuse it before | ||||||||||||||||||||||||||||||||
| 280 | # the first statement, naming table and column (karr k97). A table without a | ||||||||||||||||||||||||||||||||
| 281 | # NULL in the column passes. | ||||||||||||||||||||||||||||||||
| 282 | 6 | ▶ | sub _refuse_nulls { | ||||||||||||||||||||||||||||||
|
Subroutine
_refuse_nulls: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 283 | 6 | my ($self, $diff, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 284 | 6 | for my $op (@{ $diff->operations }) { | |||||||||||||||||||||||||||||||
| 285 | 6 | ▶ | next unless $op->isa('DBIO::Sybase::Diff::Column::Alter'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 286 | 6 | ▶ | my ($from, $to) = ($op->from // {}, $op->to); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$op->from // {}
MC/DC0%0/1
$op->from // {}
$op->from
Truth table50%1/2
A
A= $op->from
| |||||||||||||||||||||||||||||||||
| 287 | 6 | ▶ | next unless $to->{not_null} && !$from->{not_null}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$to->{"not_null"} and not $from->{"not_null"}
MC/DC100%2/2
$to->{"not_null"} and not $from->{"not_null"}
$to->{"not_null"}
not $from->{"not_null"}
Truth table100%3/3
A and not(B)
A= $to->{"not_null"}B= not $from->{"not_null"}
| |||||||||||||||||||||||||||||||||
| 288 | 3 | my ($table, $column) = ($op->table, $to->{column_name}); | |||||||||||||||||||||||||||||||
| 289 | 3 | my ($nulls) = $dbh->selectrow_array(sprintf | |||||||||||||||||||||||||||||||
| 290 | 'SELECT COUNT(*) FROM %s WHERE %s IS NULL', | ||||||||||||||||||||||||||||||||
| 291 | DBIO::Sybase::Quote::quote_ident($table), DBIO::Sybase::Quote::quote_ident($column)); | ||||||||||||||||||||||||||||||||
| 292 | 3 | ▶ | next unless $nulls; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 293 | 2 | DBIO::Exception->throw(sprintf | |||||||||||||||||||||||||||||||
| 294 | 'DBIO::Sybase: table %s, column %s: making the column NOT NULL is refused: ' | ||||||||||||||||||||||||||||||||
| 295 | . 'table %s holds rows where it is NULL (Msg 515), and nothing was sent', | ||||||||||||||||||||||||||||||||
| 296 | $table, $column, $table); | ||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | 4 | return; | |||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | 25 | 1; | |||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | __END__ | ||||||||||||||||||||||||||||||||