NAME

DBIO::PostgreSQL::Diff::View - Diff operations for PostgreSQL views and materialized views

VERSION

version 0.900007

DESCRIPTION

Represents a diff operation on a view or a materialized view: CREATE VIEW / CREATE MATERIALIZED VIEW from the definition the catalog gives, or DROP VIEW / DROP MATERIALIZED VIEW.

The model files views among the tables (DBIO::PostgreSQL::Introspect::Tables, kind v and m), and this class reads them from there. DBIO::PostgreSQL::Diff::Table leaves them to it, and DBIO::PostgreSQL::Diff::Column has nothing to do for them: a view's columns are what its definition says.

A view is compared by its definition (pg_get_viewdef), like an index by its (ADR 0003). One that differs is dropped and created again -- see "diff".

ATTRIBUTES

schema_name

The schema of the view.

table_name

The name of the view.

table_info

The view's entry in the tables section of the model: kind, view_definition, depends_on.

replaced

True on a drop that belongs to the rebuilding of views ("rebuilt") and therefore stays ahead of the creates that follow it, instead of going to the removals ("ORDER OF OPERATIONS" in DBIO::PostgreSQL::Diff): the drop of a view this diff creates again, and the drop of one that goes for good but stands on a view that is created again -- PostgreSQL does not drop a view while another depends on it.

METHODS

is_view

next if DBIO::PostgreSQL::Diff::View->is_view($tables->{$key});

True for an entry of the tables section that is a view or a materialized view, and so this class's to create and drop.

rebuilt

my $rebuilt = DBIO::PostgreSQL::Diff::View->rebuilt(
    $source_tables, $target_tables, $source_columns, $target_columns);

The views of the source that have to make way for a change, as a hashref of their keys: every view whose definition differs in the target, every view the target has again that reads a column whose type changes in the target ("type_changed_readers" in DBIO::PostgreSQL::Diff::Column; only with the columns sections given), and every view and materialized view that stands on one of those, directly or through another. "diff" drops them all and creates again the ones the target has -- but refuses where a materialized view the target keeps is among them. DBIO::PostgreSQL::Diff::Trigger asks for the same set, because DROP VIEW takes the triggers of a view with it.

A materialized view that reads a column whose type changes is not among them: it is not built anew for that, and "diff" in DBIO::PostgreSQL::Diff::Column refuses the change.

with_dependents

my @keys = DBIO::PostgreSQL::Diff::View->with_dependents($tables, @view_keys);

The views named, and every view and materialized view of $tables that stands on one of them, directly or through another -- in the order they can be dropped, each ahead of the views it stands on. DBIO::PostgreSQL::Diff::Column names them as what has to go before a column they read can change its type.

drop_sql

my $sql = DBIO::PostgreSQL::Diff::View->drop_sql($tables->{$key});

DROP VIEW schema.name; or DROP MATERIALIZED VIEW schema.name; for a view's entry in the tables section: the statement a refusal names as the way out.

diff

my @ops = DBIO::PostgreSQL::Diff::View->diff($source_tables, $target_tables);

Compares the views and materialized views of two tables sections.

One only the target has is created, one only the source has is dropped.

A column that changes its type under a view is the other cause: PostgreSQL does not alter it while a view reads it, so every view of the database that reads the column -- and every view that stands on those -- is dropped ahead of the change and created again after it, from the target's definition. DBIO::PostgreSQL::Diff puts these drops ahead of the changes to tables and columns, and Deploy::apply runs the statements in one transaction. A materialized view is not built anew for it, and a view only the database has is not created again: "diff" in DBIO::PostgreSQL::Diff::Column refuses the change for either -- a materialized view that reads the column or stands on a view that does -- and names it.

A view whose definition differs is dropped and created again: PostgreSQL has no statement that changes a view to any other definition (CREATE OR REPLACE VIEW can only add columns at the end). Every view that stands on it has to be out of the way for that, so those are dropped ahead of it and -- if the target has them -- created again after it, the same definition or not. What is lost with a view that is dropped is what the model does not hold: the privileges granted on it, its comment, its options.

Among themselves the operations follow the dependency the model carries (depends_on), not the names: a view is created after the views it reads and dropped before them.

Three things are refused, with refusing to diff: ... and no statement:

  • a materialized view whose definition differs. Dropping and creating it again builds its data anew and takes its indexes with it.

  • a materialized view the target keeps, on a view that is dropped and created again. It would have to be rebuilt with it, which builds its data anew and takes its indexes with it. (For the type of a column a view reads, "diff" in DBIO::PostgreSQL::Diff::Column refuses it first, naming the column.)

  • a view to create, or to create again, that the target model has no definition for (DBIO::PostgreSQL::Diff::NoStatement).

A relation that is a view on one side and something else on the other is not this class's to decide: "diff" in DBIO::PostgreSQL::Diff::Table refuses it.

Nor is a materialized view to create whose name differs only in upper and lower case from one to drop -- Totals in the target, totals in the database. "diff" in DBIO::PostgreSQL::Diff::Table refuses that as well, together with the tables of that kind, because the drop takes the data and the indexes (DBIO::PostgreSQL::Diff::CaseOnly). A view in two spellings is this class's and is not refused: it is created under the one name and dropped under the other, which loses what every view that is created again loses.

qualified_name

my $fqn = $op->qualified_name;  # 'api.active_users'

The schema-qualified name as a reader sees it, unquoted. It is what "summary" prints and is not SQL.

as_sql

Returns CREATE VIEW schema.name AS ...; or DROP VIEW schema.name;, with MATERIALIZED VIEW for a materialized view.

Schema and view are identifiers by the server's rule ("quote_ident" in DBIO::PostgreSQL::Util). The definition is the server's own text (pg_get_viewdef) and is written as given, without the blank ahead of it and the semicolon after it.

A DROP has no CASCADE: what stands on the view and is in the model has been dropped ahead of it, and for anything else PostgreSQL refuses the statement.

An operation whose table_info is not a view's or a materialized view's has no statement, nor has a create without a definition or an action this class does not know: as_sql dies ("throw" in DBIO::PostgreSQL::Diff::NoStatement). "diff" builds no such operation; one built by hand can be.

summary

Returns a one-line description such as +view: api.active_users or -materialized view: public.totals.

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.