| 1 | package DBIO::Firebird::Deploy; | ||
| 2 | # ABSTRACT: Deploy and upgrade Firebird schemas via test-deploy-and-compare | ||
| 3 | |||
| 4 | 26 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 26 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | our $VERSION = '0.900007'; | ||
| 8 | |||
| 9 | 26 | ▶ | use base 'DBIO::Deploy::Base::TempDatabase'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 26 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use DBD::Firebird; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use File::Basename (); | ||
| 14 | use DBIO::Util (); | ||
| 15 | use DBIO::Exception (); | ||
| 16 | use DBIO::Firebird::DDL; | ||
| 17 | use DBIO::Firebird::Introspect; | ||
| 18 | use DBIO::Firebird::Diff; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | sub _ddl_class { 'DBIO::Firebird::DDL' } | ||
| 23 | sub _introspect_class { 'DBIO::Firebird::Introspect' } | ||
| 24 | sub _diff_class { 'DBIO::Firebird::Diff' } | ||
| 25 | |||
| 26 | # --- Firebird-specific temp-database seam --- | ||
| 27 | |||
| 28 | # DSQL cannot prepare a CREATE DATABASE statement (SQLCODE -530), so the temp | ||
| 29 | # database is created out-of-band with DBD::Firebird->create_database -- a class | ||
| 30 | # method that croaks on error and runs no transaction. The temp file is placed | ||
| 31 | # server-side, co-located with the live database (same host[/port] and the same | ||
| 32 | # directory as the live db file), so it lands somewhere predictable and | ||
| 33 | # writable. The full host/port:/abs/path.fdb identifier is returned and is what | ||
| 34 | # _temp_dsn / _temp_connect_info consume, keeping create, connect and drop all | ||
| 35 | # referring to the same database. | ||
| 36 | sub _create_temp_db { | ||
| 37 | my ($self, $dbh) = @_; | ||
| 38 | |||
| 39 | my ($hostport, $dir) = $self->_live_db_location; | ||
| 40 | my $name = $self->temp_db_prefix . $$ . '_' . time() . '.fdb'; | ||
| 41 | my $id = ($hostport eq '' ? '' : "$hostport:") . "$dir/$name"; | ||
| 42 | |||
| 43 | my ($dsn, $user, $pass) = $self->_temp_connect_info($id); | ||
| 44 | |||
| 45 | # create_database puts the password into the text of a CREATE DATABASE | ||
| 46 | # statement and croaks with whatever the engine makes of it: never the | ||
| 47 | # password (karr k39, core k297) | ||
| 48 | eval { | ||
| 49 | DBD::Firebird->create_database({ | ||
| 50 | db_path => $id, | ||
| 51 | user => $user, | ||
| 52 | password => $pass, | ||
| 53 | dialect => 3, | ||
| 54 | page_size => 4096, | ||
| 55 | character_set => 'UTF8', | ||
| 56 | }); | ||
| 57 | 1; | ||
| 58 | } or die DBIO::Util::mask_connect_secrets($@, [ $dsn, $user, $pass ]); | ||
| 59 | |||
| 60 | return $id; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _drop_temp_db { | ||
| 64 | my ($self, $dbh, $name) = @_; | ||
| 65 | |||
| 66 | # DROP DATABASE in Firebird drops whatever database the handle is connected | ||
| 67 | # to -- it takes no name. Running it on the production $dbh would drop the | ||
| 68 | # live database. Connect a throw-away handle to the temp database and drop | ||
| 69 | # that instead. DSQL cannot prepare DROP DATABASE (SQLCODE -104), so the drop | ||
| 70 | # goes through $temp_dbh->func("ib_drop_database"); the handle is invalid | ||
| 71 | # afterwards, so it is not disconnected. | ||
| 72 | my ($dsn, $user, $pass) = $self->_temp_connect_info($name); | ||
| 73 | # DBI dies quoting the DSN it was given, and the DBD may repeat any of the | ||
| 74 | # rest: never the password (karr k39, core k297) | ||
| 75 | my $temp_dbh = eval { | ||
| 76 | DBI->connect($dsn, $user, $pass, { | ||
| 77 | RaiseError => 1, AutoCommit => 1, | ||
| 78 | }) | ||
| 79 | } or die DBIO::Util::mask_connect_secrets( | ||
| 80 | $@ || "Cannot connect to temp database for drop: $DBI::errstr", [ $dsn, $user, $pass ], | ||
| 81 | ); | ||
| 82 | |||
| 83 | $temp_dbh->func("ib_drop_database"); | ||
| 84 | } | ||
| 85 | |||
| 86 | # Resolve the live database's server-side location from the storage connect_info: | ||
| 87 | # the host[/port] and the directory of the live db file, so the temp database is | ||
| 88 | # co-located with it. Returns ($hostport, $dir); $hostport is '' for a host-less | ||
| 89 | # (purely local) DSN. | ||
| 90 | # The live connection's ($dsn, $user, $pass). With an AccessBroker the storage's | ||
| 91 | # connect_info is [ $broker ] and holds nothing of its answer; the answer of | ||
| 92 | # this moment is what _dbi_connect_info reads, normalised to | ||
| 93 | # [ $dsn, $user, $pass, \%attrs ] (karr k42). Without a broker, connect_info | ||
| 94 | # as given. | ||
| 95 | sub _live_connect_info { | ||
| 96 | my ($self) = @_; | ||
| 97 | my $storage = $self->schema->storage; | ||
| 98 | |||
| 99 | # A user or password the connect info does not give is '' (core karr k392: | ||
| 100 | # DBI_USER / DBI_PASS stand in for nothing). | ||
| 101 | my @live; | ||
| 102 | if ($storage->can('access_broker') && $storage->access_broker) { | ||
| 103 | @live = @{ $storage->_dbi_connect_info }[0 .. 2]; | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | my @info = @{ $storage->connect_info }; | ||
| 107 | @live = ref $info[0] eq 'HASH' | ||
| 108 | ? ( $info[0]{dsn}, $info[0]{user}, $info[0]{password} // $info[0]{pass} ) | ||
| 109 | : @info[0 .. 2]; | ||
| 110 | } | ||
| 111 | return ( $live[0], $live[1] // '', $live[2] // '' ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _live_db_location { | ||
| 115 | my ($self) = @_; | ||
| 116 | my ($dsn, $user, $pass) = $self->_live_connect_info; | ||
| 117 | |||
| 118 | die ref($self) . ' does not support coderef DSN for temp database connections' | ||
| 119 | if ref $dsn eq 'CODE'; | ||
| 120 | |||
| 121 | # The attribute names DBD::Firebird's own DSN parser accepts: db/dbname/ | ||
| 122 | # database for the path, host/hostname, port. The host may also ride in the | ||
| 123 | # path as host[/port]:/abs/path, or in host= as host/port. | ||
| 124 | my %attr; | ||
| 125 | for my $part (split /;/, $dsn =~ s/^dbi:[^:]*://ir) { | ||
| 126 | my ($k, $v) = split /=/, $part, 2; | ||
| 127 | $attr{lc $k} = $v if defined $v; | ||
| 128 | } | ||
| 129 | my $db = $attr{dbname} // $attr{db} // $attr{database}; | ||
| 130 | # the DSN is the caller's and may carry a password (karr k39, core k297) | ||
| 131 | die DBIO::Util::mask_connect_secrets( | ||
| 132 | ref($self) . ": cannot find dbname=/database= in DSN '$dsn'", [ $dsn, $user, $pass ], | ||
| 133 | ) unless defined $db && length $db; | ||
| 134 | my $host = $attr{host} // $attr{hostname}; | ||
| 135 | |||
| 136 | my ($hostport, $abs); | ||
| 137 | if ($db =~ m{^([^/].*?):(/.*)$}) { ($hostport, $abs) = ($1, $2) } | ||
| 138 | else { | ||
| 139 | $abs = $db; | ||
| 140 | $hostport = ''; | ||
| 141 | if (defined $host && length $host) { | ||
| 142 | $hostport = $host; | ||
| 143 | $hostport .= "/$attr{port}" | ||
| 144 | if defined $attr{port} && length $attr{port} && $host !~ m{/}; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | return ($hostport, File::Basename::dirname($abs)); | ||
| 149 | } | ||
| 150 | |||
| 151 | # $_[2] is the temp-db identifier passed by core's _temp_connect_info, which is | ||
| 152 | # already the full host/port:/abs/path.fdb spelling (built by _create_temp_db); | ||
| 153 | # connect to it with the same dbname= DSN form the live DSN uses. Only the DSN | ||
| 154 | # form differs from the base -- the connect-info shape handling (also an | ||
| 155 | # AccessBroker's answer, karr k42), user/password extraction and coderef-DSN | ||
| 156 | # guard are inherited from core's _temp_connect_info. | ||
| 157 | sub _temp_dsn { "dbi:Firebird:dbname=$_[2]" } | ||
| 158 | |||
| 159 | |||
| 160 | sub _check_before_apply { | ||
| 161 | my ($self, $diff) = @_; | ||
| 162 | $diff->as_sql; | ||
| 163 | $self->_refuse_unfillable($diff, $self->_dbh); | ||
| 164 | return $self->SUPER::_check_before_apply($diff); | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | sub _refuse_unfillable { | ||
| 169 | my ($self, $diff, $dbh) = @_; | ||
| 170 | |||
| 171 | my $q = \&DBIO::Firebird::DDL::_firebird_quote_ident; | ||
| 172 | my $has_rows = sub { | ||
| 173 | my ($table, $where) = @_; | ||
| 174 | return scalar $dbh->selectrow_array(sprintf 'SELECT FIRST 1 1 FROM %s%s', | ||
| 175 | $q->($table), $where // ''); | ||
| 176 | }; | ||
| 177 | |||
| 178 | my @found; | ||
| 179 | for my $op (@{ $diff->operations }) { | ||
| 180 | next unless $op->isa('DBIO::Firebird::Diff::Column'); | ||
| 181 | my ($table, $column) = ($op->table_name, $op->column_name); | ||
| 182 | my $new = $op->new_info // {}; | ||
| 183 | |||
| 184 | if ($op->action eq 'add') { | ||
| 185 | next unless $new->{not_null} && !$new->{is_auto_increment}; | ||
| 186 | my $default = $new->{default_value}; | ||
| 187 | next if ref $default | ||
| 188 | || (defined $default && $default =~ /\S/ && $default !~ /^\s*NULL\s*$/i); | ||
| 189 | next unless $has_rows->($table); | ||
| 190 | push @found, sprintf | ||
| 191 | ' %s.%s: added NOT NULL without a default, and the table holds rows that would have no value for it', | ||
| 192 | $q->($table), $q->($column); | ||
| 193 | } | ||
| 194 | elsif ($op->action eq 'alter') { | ||
| 195 | next unless $new->{not_null} && !($op->old_info // {})->{not_null}; | ||
| 196 | next unless $has_rows->($table, ' WHERE ' . $q->($column) . ' IS NULL'); | ||
| 197 | push @found, sprintf | ||
| 198 | ' %s.%s: becomes NOT NULL, and the table holds rows where it is NULL', | ||
| 199 | $q->($table), $q->($column); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | DBIO::Exception->throw(join "\n", | ||
| 204 | sprintf('refusing to apply the diff: the Firebird server would refuse %s half way, with its own error', | ||
| 205 | @found == 1 ? 'this change' : 'these changes'), | ||
| 206 | '', | ||
| 207 | @found, | ||
| 208 | '', | ||
| 209 | 'No statement was run. Fill the column (or give it a default) first, or empty the table.', | ||
| 210 | '', | ||
| 211 | ) if @found; | ||
| 212 | |||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | 1; | ||
| 218 | |||
| 219 | __END__ | ||