NAME

DBIO::Firebird::Diff::Constraint - Diff operations for Firebird primary keys, foreign keys and unique constraints

VERSION

version 0.900004

DESCRIPTION

Primary keys, foreign keys and unique constraints in a diff. The target model is the temp database DBIO::Firebird::Deploy deploys "install_ddl" in DBIO::Firebird::DDL into, so it holds every constraint the schema declares. Every difference becomes statements:

  • A table that is new. Its constraints are created by

    ALTER TABLE "t" ADD CONSTRAINT "t_a_fk" FOREIGN KEY ("a") REFERENCES "o" ("id");
    ALTER TABLE "t" ADD CONSTRAINT "t_a_b_uq" UNIQUE ("a", "b");

    after all the CREATE TABLE statements of the diff, and the unique constraints before the foreign keys (a key can point at a unique constraint), so neither the order of the tables nor a cycle between them matters. The CREATE TABLE of DBIO::Firebird::Diff::Table carries no foreign key. The statements are written by the helpers install_ddl uses.

  • A table that exists on both sides, with other constraints. Each constraint the database has and the schema does not is dropped (ALTER TABLE "t" DROP CONSTRAINT "name"), each the schema has and the database lacks is added; a constraint that changed -- other columns, other actions -- is a drop plus an add. The order of a whole diff, set by DBIO::Firebird::Diff: foreign keys to drop, tables, primary keys and unique constraints to drop, columns, indexes, primary keys and unique constraints to add, foreign keys to add. Check constraints are not part of the model and are never touched.

The primary key is compared too although the model has no section for it: it is read from is_pk and pk_position of the columns, by its columns in key order. Its name is the primary_key_name of the table in the tables section (the server's INTEG_<n> unless the table was created with a named one); a key to add is written without a name, ALTER TABLE "t" ADD PRIMARY KEY (...), as install_ddl writes it. A model without that name cannot drop a primary key: as_sql dies, naming the table. Firebird has no DROP PRIMARY KEY and refuses a second key, so the old one is dropped first. A view has no constraints and is skipped.

A key another table's foreign key points at cannot be dropped, so a foreign key that stays the same but points at a primary key or unique constraint dropped here is dropped before it and added again after.

A last case: tables dropped together that point at each other in a cycle ("diff" in DBIO::Firebird::Diff::Table). The keys among them are dropped by action drop operations, ALTER TABLE ... DROP CONSTRAINT, before the tables.

Constraints are compared by what they say, not by name: a foreign key by its columns, target table, target columns and both actions; a unique constraint by its columns in order. An absent action and the server's RESTRICT are the same rule, and a foreign key's MATCH is not compared (Firebird has one, FULL).

ATTRIBUTES

replaced

True on a drop whose constraint comes back in the same diff: the primary key of the table when another one is added, a unique constraint or foreign key of the same name when it is added again (a changed one, or a foreign key dropped before the key it points at and added after it). "classify" in DBIO::Diff::Base counts such a drop as changing; the drop of a constraint the schema no longer has is destructive and runs with allow_destructive (core ADR 0080).

METHODS

diff

my @ops = DBIO::Firebird::Diff::Constraint->diff(
  $source_uniques, $target_uniques, $source_fks, $target_fks,
  $source_tables, $target_tables, $source_columns, $target_columns);

The unique constraints of the new tables first, then their foreign keys, then for the tables that exist on both sides the foreign keys to drop, the primary keys and unique constraints to drop, those to add, the foreign keys to add. Each is one operation with action create or drop and kind pk, unique or fk; DBIO::Firebird::Diff orders them among the other operations. The columns carry the primary key.

as_sql

ALTER TABLE ... ADD CONSTRAINT / ADD PRIMARY KEY, or ALTER TABLE ... DROP CONSTRAINT.

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.