NAME

DBIO::DB2::Diff::Column - Diff operations for DB2 columns

VERSION

version 0.900007

DESCRIPTION

Column-level diff operations for DB2. DB2 has a reasonably complete ALTER TABLE repertoire:

  • ALTER TABLE ... ADD COLUMN

  • ALTER TABLE ... DROP COLUMN

  • ALTER TABLE ... ALTER COLUMN ... SET DATA TYPE

  • ALTER TABLE ... ALTER COLUMN ... SET NOT NULL / DROP NOT NULL

  • ALTER TABLE ... ALTER COLUMN ... SET DEFAULT / DROP DEFAULT

DROP COLUMN, SET NOT NULL, DROP NOT NULL and most SET DATA TYPE leave the table in reorg-pending state; "reorg_recommended" says which, and DBIO::DB2::Diff follows the column pass with the REORG (DBIO::DB2::Diff::Reorg).

Brand-new tables get their columns inline via DBIO::DB2::Diff::Table -- this module only sees columns of tables that exist in both models.

Both models are read out of the catalog by "fetch" in DBIO::DB2::Introspect::Columns, which gives each column the normalized fields for the generator and, for this diff, the internal catalog fields; the catalog fields are what is compared and rendered here (ADR 0013). catalog_type is TYPENAME. catalog_size is the catalog's length, which DBIO::DB2::Type leaves out for the types Db2 declares without one. catalog_default is the SQL text of the default -- 'new' with its quotes -- and goes into DEFAULT and SET DEFAULT as it is; quoting it as a literal would make the quoted text the default, and the next diff would find it different again. The normalized data_type, size and default_value are not read.

Identity columns

is_auto_increment is compared here (DBIO::Diff::Compare does not), and an identity column can be added to an existing table (ADR 0012). What Db2 12.1.5 takes, as measured:

  • An added identity column is three statements: ADD COLUMN ... NOT NULL DEFAULT 0, ALTER COLUMN ... DROP DEFAULT, ALTER COLUMN ... SET GENERATED ALWAYS AS IDENTITY. The clause in ADD COLUMN is refused (SQL0104N), and so is NOT NULL without a default (SQL0193N). Rows that exist stay at 0.

  • A changed is_auto_increment between two columns is SET GENERATED ALWAYS AS IDENTITY (after the NOT NULL and the DROP DEFAULT it needs) or DROP IDENTITY (before everything else of the column). The operations that let go of an identity come first within a table: it has one identity column (SQL0372N).

None of them leaves the table reorg-pending ("reorg_recommended"). The model carries no START WITH or INCREMENT BY; the identity written is the default, 1 and 1, and the counter starts at 1 whatever the column holds.

What the server refuses, the diff refuses with a croak naming table and column, when the operations are built: an identity over a type other than SMALLINT, INTEGER, BIGINT or a DECIMAL without scale (SQL0846N), over a column that is not NOT NULL (SQL0270N), and a change of type of a column that is an identity before and after (SQL0270N).

new, action and summary_prefix come from DBIO::Diff::Op.

METHODS

diff

lossy

my $bool = $op->lossy;

True for the alter of a column whose type change can lose or alter stored values (optional operation method, ADR 0080): "apply" in DBIO::Deploy::Base runs such a diff with allow_destructive => 1 only. The answer is "column_change_lossy" in DBIO::Diff::Compare over the catalog fields the diff compares, catalog_type as the type and catalog_size as the size (ADR 0013). A change keeps every value when the type stays and the size does not get smaller (scale equal), or when it is one of the widenings Db2 casts without loss: SMALLINT to INTEGER, SMALLINT to BIGINT, INTEGER to BIGINT. Every other type change is lossy. A change of nullability, default or identity alone is not (SET NOT NULL over a NULL is refused by "_check_before_apply" in DBIO::DB2::Deploy), and neither is an added column.

adds_identity

my $bool = $op->adds_identity;

True for the add of a column that is an identity: the operation whose ADD COLUMN ... NOT NULL DEFAULT 0 gives every row of the table that exists the value 0 (see "Identity columns"). The diff knows no rows; "apply" in DBIO::DB2::Deploy asks the server for them before it runs this operation (ADR 0012).

adds_not_null_without_default

my $bool = $op->adds_not_null_without_default;

True for the add of a column that is NOT NULL and has no default (a DEFAULT NULL is none) and is no identity: Db2 refuses ALTER TABLE ... ADD COLUMN for it whatever rows the table holds (SQL0193N), and has nothing to fill the rows with, so no value is made up. "apply" in DBIO::DB2::Deploy refuses it before the first statement runs, naming table and column (the conformance case diff-column-added-not-null-populated). The identity column is the one NOT NULL add the diff can write ("Identity columns").

sets_not_null

my $bool = $op->sets_not_null;

True for the alter of a nullable column into a NOT NULL one. A row that holds NULL there makes the server refuse SET NOT NULL (SQL0407N) in the middle of the upgrade; the diff knows no rows, so "apply" in DBIO::DB2::Deploy asks the server before the first statement and refuses, naming table and column (the conformance case diff-not-null-over-nulls).

as_sql

my $bool = $op->reorg_recommended;

True when a statement of this operation leaves the table in reorg-pending state, where Db2 refuses INSERT, UPDATE and CREATE INDEX with SQL0668N rc 7 until a REORG has run. DBIO::DB2::Diff asks every column operation and has DBIO::DB2::Diff::Reorg write one REORG per table.

The answers are what Db2 12.1.5 does (measured; the table is in ADR 0008):

  • drop -- yes.

  • alter with SET NOT NULL or DROP NOT NULL -- yes.

  • alter with SET DATA TYPE -- yes, with one exception: a change of length alone within VARCHAR, VARGRAPHIC or VARBINARY, longer or shorter. Everything else that was tried asks for one, a longer CHAR and a wider DECIMAL included, and so does every combination that was not tried: a REORG too many costs time, one too few breaks the upgrade.

  • add, and an alter that only sets or drops the default -- no. Neither do SET GENERATED ALWAYS AS IDENTITY and DROP IDENTITY, nor an identity column added in its three statements (ADR 0012); the NOT NULL or type change they come with is asked about as before.

The conditions are the ones "as_sql" writes its statements on, so the answer is about the SQL this operation renders and not about the model fields that differ.

summary

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.