DB2/Deploy.pm

filter: Statementstmt 21.6% 16 / 74 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 38.4% 5 / 13 Podpod n/a totaltotal 18.9% 21 / 111 CC 15 SCAR 31.2
CC
15
Cov
17%
_drop_test_schema
56.0
_build_target_model
30.0
_split_qualify_ddl
12.0
CRAP 22.7
« DB2/Introspect/ForeignKeys.pm ↑ Summary DB2/Introspect/Tables.pm »
1 package DBIO::DB2::Deploy;
2 # ABSTRACT: Deploy and upgrade DB2 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';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use DBIO::DB2::DDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DBIO::SQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # --- class-name hooks for DBIO::Deploy::Base -------------------------------
14  
15 0 ▶sub _ddl_class { 'DBIO::DB2::DDL' }
Subroutine _ddl_class: not called CC=1 SCAR=6.9
16 0 ▶sub _introspect_class { 'DBIO::DB2::Introspect' }
Subroutine _introspect_class: not called CC=1 SCAR=6.9
17 0 ▶sub _diff_class { 'DBIO::DB2::Diff' }
Subroutine _diff_class: not called CC=1 SCAR=6.9
18  
19  
20 0 ▶sub _new_introspect {
Subroutine _new_introspect: not called CC=2 SCAR=17.9
21 0 my ($self, $dbh, $schema) = @_;
22 0 ▶ return $self->_introspect_class->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 0 0 0
23 dbh => $dbh,
24 (defined $schema ? (schema => $schema) : ()),
25 );
26 }
27  
28  
29 0 ▶sub _introspect_current {
Subroutine _introspect_current: not called CC=2 SCAR=17.9
30 0 my ($self) = @_;
31 0 my $dbh = $self->_dbh;
32 # CURRENT SCHEMA is CHAR(128), space-padded; right-trim before use.
33 0 my ($current_schema) = $dbh->selectrow_array('VALUES CURRENT SCHEMA');
34 0 ▶ $current_schema =~ s/\s+$// if defined $current_schema;
Branch: 1 branch
BranchTrueFalseTotal
if defined $current_schema 0 0 0
35 0 return $self->_new_introspect($dbh, $current_schema)->model;
36 }
37  
38  
39 0 ▶sub _build_target_model {
Subroutine _build_target_model: not called CC=5 SCAR=34.0
40 0 my ($self) = @_;
41 0 my $dbh = $self->_dbh;
42 # Must start with a letter and be uppercase: DB2 rejects a leading-underscore
43 # unquoted identifier (CREATE SCHEMA _dbio_test_<pid> => SQL20521N), and SYSCAT
44 # stores schema names uppercased, so a lowercase name would never match the
45 # introspect filter (WHERE tabschema = ?). $$ is digits, so DBIO_TEST_<pid> is a
46 # valid bare uppercase identifier that round-trips through SYSCAT unchanged.
47 0 my $test_schema = 'DBIO_TEST_' . $$;
48  
49 # Defensively clear a same-named schema leaked by a crashed prior run (or an
50 # earlier same-pid call whose teardown was skipped) so CREATE SCHEMA cannot
51 # collide (SQL0601N). _drop_test_schema is best-effort and copes with a
52 # populated schema.
53 0 my ($exists) = $dbh->selectrow_array(
54 'SELECT 1 FROM syscat.schemata WHERE schemaname = ?', undef, $test_schema,
55 );
56 0 ▶ $self->_drop_test_schema($dbh, $test_schema) if $exists;
Branch: 1 branch
BranchTrueFalseTotal
if $exists 0 0 0
57  
58 0 $dbh->do("CREATE SCHEMA $test_schema");
59  
60 0 my $model = eval {
61 0 my $ddl = $self->_ddl_class->install_ddl($self->schema);
62 0 for my $stmt ($self->_split_qualify_ddl($ddl, $test_schema)) {
63 0 $dbh->do($stmt);
64 }
65 0 return $self->_new_introspect($dbh, $test_schema)->model;
66 };
67  
68 0 $self->_drop_test_schema($dbh, $test_schema, $model);
69 0 ▶ die $@ if $@ and not $model;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $model 0 0 0
Condition0%0/3
$@ and not $model
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $model
$@ not $model
Truth table0%0/3
A and not(B)
A= $@B= not $model
ABresult
0X0
100
111
70  
71 0 return $model;
72 }
73  
74 # Tear down the throwaway compare schema. DB2's DROP SCHEMA ... RESTRICT only
75 # succeeds on an EMPTY schema, but by this point the schema holds the deployed
76 # tables, so the schema must be emptied first. SYSPROC.ADMIN_DROP_SCHEMA can drop
77 # a populated schema, but it requires the SYSTOOLSPACE tablespace, which does not
78 # exist on a stock DB2 instance (SQL0443N "... SYSTOOLSPACE tablespace does not
79 # exist"); when it is missing the call fails, the schema is NOT dropped, and the
80 # next CREATE SCHEMA in the same process collides (SQL0601N). So we tear the
81 # schema down with plain DDL + catalog lookups, robust on any DB2: drop the FK
82 # constraints first (so inter-table FK dependencies don't block table drops),
83 # then the tables (indexes drop with their table) and any views, then the now
84 # empty schema. Best-effort throughout -- each statement is its own eval so one
85 # failure does not abort teardown; the caller re-raises only the model-build
86 # error (when no model was produced). Catalog names match $test_schema verbatim:
87 # it is the uppercase DBIO_TEST_<pid>, which is how SYSCAT stores it.
88 0 ▶sub _drop_test_schema {
Subroutine _drop_test_schema: not called CC=7 SCAR=40.3
89 0 my ($self, $dbh, $test_schema, $model) = @_;
90 0 local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys
91  
92 # 1. Drop FK constraints owned by tables in the schema (type 'F'), so the
93 # referenced tables can then be dropped in any order.
94 0 my $fks = $dbh->selectall_arrayref(
95 q{SELECT tabname, constname FROM syscat.tabconst WHERE tabschema = ? AND type = 'F'},
96 { Slice => {} }, $test_schema,
97 );
98 0 ▶ for my $fk (@{ $fks || [] }) {
Condition0%0/2
$fks || []
l!l
00
MC/DC0%0/1
$fks || []
$fks
Truth table0%0/2
A
A= $fks
Aresult
11
00
99 0 eval {
100 0 local $dbh->{PrintError} = 0;
101 0 $dbh->do("ALTER TABLE $test_schema.$fk->{tabname} DROP FOREIGN KEY $fk->{constname}");
102 };
103 }
104  
105 # 2. Drop tables (indexes drop with them) and any views.
106 0 my $tables = $dbh->selectall_arrayref(
107 q{SELECT tabname FROM syscat.tables WHERE tabschema = ? AND type = 'T'},
108 { Slice => {} }, $test_schema,
109 );
110 0 ▶ for my $t (@{ $tables || [] }) {
Condition0%0/2
$tables || []
l!l
00
MC/DC0%0/1
$tables || []
$tables
Truth table0%0/2
A
A= $tables
Aresult
11
00
111 0 eval { local $dbh->{PrintError} = 0; $dbh->do("DROP TABLE $test_schema.$t->{tabname}") };
112 }
113 0 my $views = $dbh->selectall_arrayref(
114 q{SELECT tabname FROM syscat.tables WHERE tabschema = ? AND type = 'V'},
115 { Slice => {} }, $test_schema,
116 );
117 0 ▶ for my $v (@{ $views || [] }) {
Condition0%0/2
$views || []
l!l
00
MC/DC0%0/1
$views || []
$views
Truth table0%0/2
A
A= $views
Aresult
11
00
118 0 eval { local $dbh->{PrintError} = 0; $dbh->do("DROP VIEW $test_schema.$v->{tabname}") };
119 }
120  
121 # 3. Drop the now-empty schema.
122 0 eval { local $dbh->{PrintError} = 0; $dbh->do("DROP SCHEMA $test_schema RESTRICT") };
123 0 return;
124 }
125  
126 # Internal: rewrite CREATE/DROP TABLE/INDEX statements emitted by
127 # DBIO::DB2::DDL so they target $schema instead of the live one. The
128 # regex pass is conservative -- DBIO::DB2::DDL emits a known shape --
129 # so we trade a small fragility for not duplicating DDL rendering.
130 #
131 # The inline FK clause from _fk_constraint_clause is
132 # CONSTRAINT <name> FOREIGN KEY (<from>) REFERENCES <reftable>(<cols>)
133 # where <reftable> is UNQUALIFIED. Without rewriting it too, a child table
134 # deployed into the test schema would resolve its REFERENCES against CURRENT
135 # SCHEMA (the live schema) and cross-reference the live parent. So we also
136 # qualify the REFERENCES target to the test schema. The CONSTRAINT name itself
137 # is schema-scoped in DB2 (it lives in the test table's schema), so it cannot
138 # collide with the live constraint of the same name and is left untouched.
139 0 ▶sub _split_qualify_ddl {
Subroutine _split_qualify_ddl: not called CC=3 SCAR=24.8
140 0 my ($self, $ddl, $test_schema) = @_;
141 0 my @stmts = DBIO::SQL::Util::_split_statements($ddl);
142 0 for my $stmt (@stmts) {
143 0 ▶ next if $stmt =~ /^\s*--/;
Branch: 1 branch
BranchTrueFalseTotal
if $stmt =~ /^\s*--/ 0 0 0
144 0 $stmt =~ s/\bCREATE TABLE (\w+)/CREATE TABLE $test_schema.$1/g;
145 0 $stmt =~ s/\bDROP TABLE (\w+)/DROP TABLE $test_schema.$1/g;
146 0 $stmt =~ s/\bCREATE INDEX (\w+) ON (\w+)/CREATE INDEX $test_schema.$1 ON $test_schema.$2/g;
147 0 $stmt =~ s/\bDROP INDEX (\w+)/DROP INDEX $test_schema.$1/g;
148 0 $stmt =~ s/\bREFERENCES (\w+)\(/REFERENCES $test_schema.$1(/g;
149 }
150 0 return @stmts;
151 }
152  
153  
154 2 1;
155  
156 __END__
157  
158 =pod
159  
160 =encoding UTF-8
161  
162 =head1 NAME
163  
164 DBIO::DB2::Deploy - Deploy and upgrade DB2 schemas via test-deploy-and-compare
165  
166 =head1 VERSION
167  
168 version 0.900001
169  
170 =head1 DESCRIPTION
171  
172 C<DBIO::DB2::Deploy> orchestrates schema deployment and upgrades for
173 DB2 using the test-deploy-and-compare strategy, parallel to
174 L<DBIO::SQLite::Deploy> and L<DBIO::PostgreSQL::Deploy>.
175  
176 For upgrades it:
177  
178 =over 4
179  
180 =item 1. Introspects the live database via C<SYSCAT>
181  
182 =item 2. Deploys the desired schema (from DBIO classes) into a temporary
183 schema in the same DB (DB2 requires an existing database; only
184 a schema can be throwaway)
185  
186 =item 3. Introspects the database after deployment
187  
188 =item 4. Computes the diff between the two models using L<DBIO::DB2::Diff>
189  
190 =back
191  
192 C<install>/C<diff>/C<apply>/C<upgrade> and the dbh/schema accessors come
193 from L<DBIO::Deploy::Base>; this class supplies the three class-name hooks
194 (L</_ddl_class>, L</_introspect_class>, L</_diff_class>), the
195 L</_new_introspect> factory that threads a target schema into the
196 introspector, and the genuinely DB2-specific L</_build_target_model> that
197 splices the desired schema into a throwaway C<CREATE SCHEMA> block.
198  
199 my $deploy = DBIO::DB2::Deploy->new(
200 schema => MyApp::DB->connect("dbi:DB2:database=mydb"),
201 );
202 $deploy->install; # fresh
203 my $diff = $deploy->diff; # or step-by-step
204 $deploy->apply($diff) if $diff->has_changes;
205 $deploy->upgrade; # convenience
206  
207 =head1 METHODS
208  
209 =head2 _new_introspect
210  
211 my $intro = $self->_new_introspect($dbh);
212 my $intro = $self->_new_introspect($dbh, $schema);
213  
214 Factory for the introspector. The optional C<$schema> selects the DB2
215 schema to introspect (defaults to the introspector's own default, used
216 when introspecting the live C<schema()>).
217  
218 =head2 _introspect_current
219  
220 my $source_model = $self->_introspect_current;
221  
222 The introspected model of the live (source) database. Overrides the
223 L<DBIO::Deploy::Base> default, which introspects with no schema arg and so
224 falls back to L<DBIO::DB2::Introspect>'s C<USER> default -- a schema DB2
225 connections do not actually use (e.g. C<db2inst1> has CURRENT SCHEMA
226 C<DB2INST1>), making the source model come back empty and diff/upgrade
227 phantom-create the whole schema.
228  
229 Resolves the connection's CURRENT SCHEMA (the schema where C<install_ddl>'s
230 unqualified objects land) and threads it into the source introspector, so the
231 high-level diff/upgrade path compares against the live objects rather than an
232 empty model.
233  
234 =head2 _build_target_model
235  
236 my $target_model = $self->_build_target_model;
237  
238 DB2-specific target model construction:
239  
240 =over 4
241  
242 =item 1. C<CREATE SCHEMA DBIO_TEST_<pid>> in the same database
243  
244 =item 2. Re-emit the install DDL with every C<CREATE/DROP TABLE/INDEX>
245 statement schema-qualified to the test schema (DBIO::DB2::DDL
246 does not auto-qualify)
247  
248 =item 3. Introspect the test schema
249  
250 =item 4. Drop the (now populated) test schema on the way out, even on failure
251 (via L</_drop_test_schema>, which copes with a non-empty schema)
252  
253 =back
254  
255 Returns the introspected model hashref for the test schema, suitable as
256 the C<target> of L<DBIO::DB2::Diff>.
257  
258 =seealso
259  
260 =over 4
261  
262 =item * L<DBIO::DB2> - schema component
263  
264 =item * L<DBIO::DB2::DDL> - generates DDL
265  
266 =item * L<DBIO::DB2::Introspect> - reads live database state
267  
268 =item * L<DBIO::DB2::Diff> - compares two introspected models
269  
270 =item * L<DBIO::Deploy::Base> - shared install/apply/upgrade orchestration
271  
272 =back
273  
274 =head1 AUTHOR
275  
276 DBIO & DBIx::Class Authors
277  
278 =head1 COPYRIGHT AND LICENSE
279  
280 Copyright (C) 2026 DBIO Authors
281 Portions Copyright (C) 2005-2025 DBIx::Class Authors
282 Based on DBIx::Class, heavily modified.
283  
284 This is free software; you can redistribute it and/or modify it under
285 the same terms as the Perl 5 programming language system itself.
286  
287 =cut
« DB2/Introspect/ForeignKeys.pm ↑ Summary DB2/Introspect/Tables.pm »