Firebird/Deploy.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Firebird/Introspect.pm ↑ Summary Shortcut/fb.pm »
1 package DBIO::Firebird::Deploy;
2 # ABSTRACT: Deploy and upgrade Firebird schemas via test-deploy-and-compare
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base 'DBIO::Deploy::Base::TempDatabase';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DBD::Firebird;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use File::Basename ();
12 use DBIO::Firebird::DDL;
13 use DBIO::Firebird::Introspect;
14 use DBIO::Firebird::Diff;
15  
16  
17  
18 sub _ddl_class { 'DBIO::Firebird::DDL' }
19 sub _introspect_class { 'DBIO::Firebird::Introspect' }
20 sub _diff_class { 'DBIO::Firebird::Diff' }
21  
22 # --- Firebird-specific temp-database seam ---
23  
24 # DSQL cannot prepare a CREATE DATABASE statement (SQLCODE -530), so the temp
25 # database is created out-of-band with DBD::Firebird->create_database -- a class
26 # method that croaks on error and runs no transaction. The temp file is placed
27 # server-side, co-located with the live database (same host[/port] and the same
28 # directory as the live db file), so it lands somewhere predictable and
29 # writable. The full host/port:/abs/path.fdb identifier is returned and is what
30 # _temp_dsn / _temp_connect_info consume, keeping create, connect and drop all
31 # referring to the same database.
32 sub _create_temp_db {
33 my ($self, $dbh) = @_;
34  
35 my ($hostport, $dir) = $self->_live_db_location;
36 my $name = $self->temp_db_prefix . $$ . '_' . time() . '.fdb';
37 my $id = ($hostport eq '' ? '' : "$hostport:") . "$dir/$name";
38  
39 my (undef, $user, $pass) = $self->_temp_connect_info($id);
40 DBD::Firebird->create_database({
41 db_path => $id,
42 user => $user,
43 password => $pass,
44 dialect => 3,
45 page_size => 4096,
46 character_set => 'UTF8',
47 });
48  
49 return $id;
50 }
51  
52 sub _drop_temp_db {
53 my ($self, $dbh, $name) = @_;
54  
55 # DROP DATABASE in Firebird drops whatever database the handle is connected
56 # to -- it takes no name. Running it on the production $dbh would drop the
57 # live database. Connect a throw-away handle to the temp database and drop
58 # that instead. DSQL cannot prepare DROP DATABASE (SQLCODE -104), so the drop
59 # goes through $temp_dbh->func("ib_drop_database"); the handle is invalid
60 # afterwards, so it is not disconnected.
61 my ($dsn, $user, $pass) = $self->_temp_connect_info($name);
62 my $temp_dbh = DBI->connect($dsn, $user, $pass, {
63 RaiseError => 1, AutoCommit => 1,
64 }) or die "Cannot connect to temp database for drop: $DBI::errstr";
65  
66 $temp_dbh->func("ib_drop_database");
67 }
68  
69 # Resolve the live database's server-side location from the storage connect_info:
70 # the host[/port] and the directory of the live db file, so the temp database is
71 # co-located with it. Returns ($hostport, $dir); $hostport is '' for a host-less
72 # (purely local) DSN.
73 sub _live_db_location {
74 my ($self) = @_;
75 my @info = @{ $self->schema->storage->connect_info };
76 my $dsn = ref $info[0] eq 'HASH' ? $info[0]{dsn} : $info[0];
77  
78 die ref($self) . ' does not support coderef DSN for temp database connections'
79 if ref $dsn eq 'CODE';
80  
81 my ($db) = $dsn =~ /(?:database|dbname)=([^;]+)/i
82 or die ref($self) . ": cannot find dbname=/database= in DSN '$dsn'";
83  
84 my ($hostport, $abs);
85 if ($db =~ m{^([^/].*?):(/.*)$}) { ($hostport, $abs) = ($1, $2) }
86 else { ($hostport, $abs) = ('', $db) }
87  
88 return ($hostport, File::Basename::dirname($abs));
89 }
90  
91 # $_[2] is the temp-db identifier passed by _temp_connect_info, which is already
92 # the full host/port:/abs/path.fdb spelling (built by _create_temp_db); connect
93 # to it with the same dbname= DSN form the live DSN uses. Only the DSN form
94 # differs from the base -- the connect-info shape handling, user/password
95 # extraction and coderef-DSN guard are inherited from _temp_connect_info.
96 sub _temp_dsn { "dbi:Firebird:dbname=$_[2]" }
97  
98  
99 1;
100  
101 __END__
102  
103 =pod
104  
105 =encoding UTF-8
106  
107 =head1 NAME
108  
109 DBIO::Firebird::Deploy - Deploy and upgrade Firebird schemas via test-deploy-and-compare
110  
111 =head1 VERSION
112  
113 version 0.900001
114  
115 =head1 DESCRIPTION
116  
117 C<DBIO::Firebird::Deploy> orchestrates the deployment and upgrade of Firebird
118 schemas using a test-deploy-and-compare strategy. The orchestration
119 (C<install>, C<diff>, C<apply>, C<upgrade>, and the create-temp /
120 deploy-introspect / drop-temp flow) is inherited from
121 L<DBIO::Deploy::Base::TempDatabase>; this class supplies only the
122 Firebird-specific seams:
123  
124 =over 4
125  
126 =item * the three class-name hooks (L<DBIO::Deploy::Base/_ddl_class>,
127 C<_introspect_class>, C<_diff_class>);
128  
129 =item * L</_create_temp_db> / L</_drop_temp_db> -- Firebird creates the temp
130 database out-of-band via C<< DBD::Firebird->create_database >> and drops it via
131 C<< $dbh->func("ib_drop_database") >>, because DSQL cannot prepare
132 database-level DDL (C<do("CREATE DATABASE")> / C<do("DROP DATABASE")> fail);
133  
134 =item * L</_temp_dsn> -- Firebird's C<dbi:Firebird:dbname=$id> DSN form, where
135 C<$id> is the full C<host/port:/abs/path.fdb> identifier returned by
136 L</_create_temp_db>.
137  
138 =back
139  
140 my $deploy = DBIO::Firebird::Deploy->new(
141 schema => MyApp::DB->connect($dsn),
142 );
143  
144 $deploy->install; # fresh install
145 $deploy->upgrade; # test-deploy + compare + apply
146  
147 my $diff = $deploy->diff; # or in steps
148 print $diff->summary;
149 $deploy->apply($diff) if $diff->has_changes;
150  
151 =head1 ATTRIBUTES
152  
153 =head2 schema
154  
155 A connected L<DBIO::Schema> instance using the L<DBIO::Firebird> component.
156 Required. (Inherited from L<DBIO::Deploy::Base>.)
157  
158 =seealso
159  
160 =over 4
161  
162 =item * L<DBIO::Deploy::Base::TempDatabase> - shared temp-database orchestration
163  
164 =item * L<DBIO::Firebird> - schema component
165  
166 =item * L<DBIO::Firebird::DDL> - generates the DDL used by C<install> and C<diff>
167  
168 =item * L<DBIO::Firebird::Introspect> - reads the live and temp database state
169  
170 =item * L<DBIO::Firebird::Diff> - compares the two introspected models
171  
172 =back
173  
174 =head1 AUTHOR
175  
176 DBIO & DBIx::Class Authors
177  
178 =head1 COPYRIGHT AND LICENSE
179  
180 Copyright (C) 2026 DBIO Authors
181 Portions Copyright (C) 2005-2025 DBIx::Class Authors
182 Based on DBIx::Class, heavily modified.
183  
184 This is free software; you can redistribute it and/or modify it under
185 the same terms as the Perl 5 programming language system itself.
186  
187 =cut
« Firebird/Introspect.pm ↑ Summary Shortcut/fb.pm »