NAME
DBIO::DB2::Deploy - Deploy and upgrade DB2 schemas via test-deploy-and-compare
VERSION
version 0.900007
DESCRIPTION
DBIO::DB2::Deploy orchestrates schema deployment and upgrades for DB2 using the test-deploy-and-compare strategy, parallel to DBIO::SQLite::Deploy and DBIO::PostgreSQL::Deploy.
For upgrades it:
- 1. Introspects the live database via
SYSCAT - 2. Deploys the desired schema (from DBIO classes) into a temporary schema in the same DB (DB2 requires an existing database; only a schema can be throwaway)
- 3. Introspects the database after deployment
- 4. Computes the diff between the two models using DBIO::DB2::Diff
install/diff/apply/upgrade and the dbh/schema accessors come from DBIO::Deploy::Base, which also holds the allow_destructive release (ADR 0080); this class supplies the three class-name hooks ("_ddl_class" in DBIO::Deploy::Base, "_introspect_class" in DBIO::Deploy::Base, "_diff_class" in DBIO::Deploy::Base), the "_new_introspect" factory that threads a target schema into the introspector, and the genuinely DB2-specific "_build_target_model" that splices the desired schema into a throwaway CREATE SCHEMA block, and "_check_before_apply", the Db2 checks that run before the first statement of an apply.
my $deploy = DBIO::DB2::Deploy->new(
schema => MyApp::DB->connect("dbi:DB2:database=mydb"),
);
$deploy->install; # fresh
my $diff = $deploy->diff; # or step-by-step
$deploy->apply($diff) if $diff->has_changes;
$deploy->upgrade; # convenience
Transactional DDL -- the engine has it, this deploy does not use it
Db2 LUW runs DDL inside the unit of work and a ROLLBACK takes it back; the sentences that say so, with their versions, are quoted at the declaration in DBIO::DB2::Storage. This deploy still does not use it. DBIO::DB2::Storage declares _use_transactional_ddl(0), so "_execute_ddl" in DBIO::Deploy::Base runs the statements one at a time with AutoCommit on and core emits its one-time "non-transactional DDL" notice. Among the drivers answering 0 DB2 is, with SQLite, one of the two where the engine is not the reason.
The reason is "_build_target_model". DB2 requires an existing database, so only a schema can be thrown away, and the compare deploy therefore runs on $self->_dbh -- the handle the storage itself owns. The other transactional engines in this family build their target model through DBIO::Deploy::Base::TempDatabase, which opens a connection of its own that core deliberately never brackets. Here there is no second connection to hide behind, and "upgrade" in DBIO::DeploymentHandler wraps its whole body -- diff and apply -- in txn_do once the capability is true. A 1 would therefore not just make the migration atomic: it would pull CREATE SCHEMA DBIO_TEST_<pid>, the entire install DDL re-emitted into that schema, its SYSCAT introspection and the teardown DROPs into the same live unit of work as the migration, holding catalog locks open for throwaway work that gains nothing from being atomic.
The notice core prints is accurate about recovery: an apply that fails half way leaves the earlier statements applied. Re-running "upgrade" in DBIO::Deploy::Base is safe, because the diff is recomputed against live state and the second run picks up only what is left -- which is also what makes the FK pass idempotent, since a constraint that is already gone is simply not in the next diff.
Turning this into a 1 is a live-server change, not a flag flip: the compare deploy has to leave the migration's unit of work first, and the two shapes nobody has run against a real server -- the drop-then-add FK pair on one constraint name emitted by DBIO::DB2::Diff::ForeignKey, and the compare schema being created and dropped inside one unit of work -- have to be measured. See the declaration comment in DBIO::DB2::Storage for both.
Views -- the compare deploy also moves CURRENT SCHEMA
_split_qualify_ddl re-points object positions at the compare schema by rewriting the known statement shapes DBIO::DB2::DDL emits. A CREATE VIEW breaks that model in half. Its name is an ordinary object position and is rewritten like any other; its body is the user's own view_definition -- arbitrary SQL, with arbitrary unqualified table references in it.
Qualifying only the name would be worse than doing nothing. A CREATE VIEW DBIO_TEST_<pid>."v" AS SELECT ... FROM "author" resolves its FROM against CURRENT SCHEMA, i.e. against the live author -- so the compare view would mean something different from the live view of the same name and the diff would be comparing two unlike things. Rewriting the body instead would mean parsing arbitrary SQL in a regex pass whose whole justification is that it only ever sees a shape this distribution emits itself.
So the body is not rewritten at all: the compare deploy runs with CURRENT SCHEMA set to the throwaway schema (_set_current_schema). Db2 qualifies unqualified object references in a dynamically prepared statement with CURRENT SCHEMA, so the view body resolves against the compare tables without a single character of it being touched. A body that qualifies its references explicitly is left pointing where it says, which is what the live view does too -- the two sides still agree.
Two properties this leans on:
The register is saved and restored on every path. The compare deploy runs on the handle the live storage owns (see above), so leaving CURRENT SCHEMA moved would silently re-point every later unqualified statement. It is read before the first side effect, put back before the teardown, and a failed restore is fatal.
Nothing else in the deploy path depends on the register. The
SYSCATreads in DBIO::DB2::Introspect and_drop_test_schemaare explicitly qualified, the introspector scopes ontabschemaas a bind value, and the rewritten DDL names its schema outright. The register only reaches the one statement that has no other way of being told.
The textual CREATE VIEW rewrite is kept alongside it rather than dropped as redundant: it is deterministic and testable without a server, and it is what keeps a view out of the live schema even if the register leg ever fails.
METHODS
_new_introspect
my $intro = $self->_new_introspect($dbh);
my $intro = $self->_new_introspect($dbh, $schema);
Factory for the introspector. The optional $schema selects the DB2 schema to introspect (defaults to the introspector's own default, used when introspecting the live schema()).
_introspect_current
my $source_model = $self->_introspect_current;
The introspected model of the live (source) database. Overrides the DBIO::Deploy::Base default, which introspects with no schema arg and so falls back to DBIO::DB2::Introspect's USER default -- a schema DB2 connections do not actually use (e.g. db2inst1 has CURRENT SCHEMA DB2INST1), making the source model come back empty and diff/upgrade phantom-create the whole schema.
Resolves the connection's CURRENT SCHEMA (the schema where install_ddl's unqualified objects land) and threads it into the source introspector, so the high-level diff/upgrade path compares against the live objects rather than an empty model.
_qualified_schemas
my @schemas = $self->_qualified_schemas;
The schemas, sorted and once each, of the tables named schema.table of the schema ("qualified_tables" in DBIO::DB2::DDL).
_merge_schema_model
$class->_merge_schema_model($model, $other, $schema_name, \%declared);
Merges the tables of $other (the model of one schema, tables under their own names) that are in %declared and are tables, not views, into $model, under the name schema.table: the tables, their columns, indexes, foreign keys and unique constraints. A foreign key to a table of the same schema names it schema.table as well. Nothing else of $other is taken.
_refuse_cross_schema_keys
$self->_refuse_cross_schema_keys($schema);
A foreign key between a table of the current schema and one named schema.table, or between two schemas, cannot be compared: the compare deploy holds the tables of one schema at a time. The diff and the upgrade refuse it with a DBIO::Exception naming both tables, before their first statement. install writes such a key as declared.
_install_ddl
The install DDL, with the connection's CURRENT SCHEMA as the schema the views are created in: a view that reads another through that qualifier is ordered by it, one that reads through any other schema is not ("order_views" in DBIO::DB2::DDL).
_build_target_model
my $target_model = $self->_build_target_model;
DB2-specific target model construction:
- 1.
CREATE SCHEMA DBIO_TEST_<pid> in the same database - 2. Point
CURRENT SCHEMAat it for the duration of the compare deploy, and put the saved value back on every path out (see "Views -- the compare deploy also moves CURRENT SCHEMA") - 3. Re-emit the install DDL with every
CREATE/DROP TABLE/INDEX,CREATE VIEWandREFERENCESobject position schema-qualified to the test schema (DBIO::DB2::DDL does not auto-qualify) - 4. Introspect the test schema
- 5. Drop the (now populated) test schema on the way out, even on failure (via
_drop_test_schema, which copes with a non-empty schema)
Returns the introspected model hashref for the test schema, suitable as the target of DBIO::DB2::Diff.
_check_before_apply
The Db2 checks of a diff against the live tables, run by "apply" in DBIO::Deploy::Base (and so by "upgrade" in DBIO::Deploy::Base) once the diff is released and before its first statement. The order is the reorganisation of the pending tables, the question for rows an identity column meets, the refusal of what Db2 cannot be given a value for. diff, as_sql and summary stay reads and do not look. Nothing has been executed when a check throws.
Reorg-pending tables are reorganised first (ADR 0008 addendum). Reorg-pending is a state of the table and not part of the introspected model, so after an upgrade that stopped between
ALTERandREORG, aREORGthat failed, or anALTER TABLErun by hand the diff holds no column operation and therefore no DBIO::DB2::Diff::Reorg -- and itsCREATE INDEX, like everyINSERTand every read, dies with SQL0668N reason code 7. TheREORGthe diff itself carries covers only the tables its own column operations put in that state. The tables looked at are those the diff can touch: a table (not a view) present in the source model and the target model, in the live schema. A table only in the source is dropped by the diff, a table only in the target is created by it; aREORGis wasted on the first and impossible on the second. The server is asked once, throughSYSIBMADM.ADMINTABINFOfiltered onTABSCHEMA(the connection'sCURRENT SCHEMA, which is the schema the source model is read from), and not at all when no table is in both models. Every pending table among them getsCALL SYSPROC.ADMIN_CMD('REORG TABLE ...'), rendered by "as_sql" in DBIO::DB2::Diff::Reorg and run through "_execute_ddl" in DBIO::Deploy::Base, tables in name order. Awarn(carp) names the tables, because the rewrite is work the caller did not see in$diff->as_sql. A diff without changes is not applied and reorganises nothing. TheREORGchanges neither schema nor rows.An identity column is added to an empty table only (ADR 0012 addendum). The diff adds an identity column in three statements,
ADD COLUMN ... NOT NULL DEFAULT 0,DROP DEFAULT,SET GENERATED ALWAYS AS IDENTITY, so every row the table already holds keeps the value 0. For every table that gets such a column ("adds_identity" in DBIO::DB2::Diff::Column; the table is in both models) the server is askedSELECT 1 FROM "table" FETCH FIRST 1 ROW ONLY. A row, and the apply is refused with an exception naming table and column. An empty table takes the column as built.What Db2 cannot be given a value for is refused. A
NOT NULLcolumn without a default added to a table that exists (SQL0193N, whatever rows it holds), and a column madeNOT NULLin a table that holds a row where it isNULL(SQL0407N): the exception names table and column.
SEE ALSO
DBIO::DB2 - schema component
DBIO::DB2::DDL - generates DDL
DBIO::DB2::Introspect - reads live database state
DBIO::DB2::Diff - compares two introspected models
DBIO::Deploy::Base - shared install/apply/upgrade orchestration
AUTHOR
DBIO & DBIx::Class Authors
COPYRIGHT AND LICENSE
Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.