| 1 | package DBIO::PostgreSQL::Diff::Table; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL tables | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 9 | __PACKAGE__->mk_diff_accessors(qw(schema_name table_name table_info columns)); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 9 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 9 | my ($class, $source, $target, $source_cols, $target_cols) = @_; | |||||||||||||||||||||||||||
| 21 | 9 | my @ops; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | for my $key (sort keys %$target) { | |||||||||||||||||||||||||||
| 24 | 9 | ▶ | next if exists $source->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 7 | my $t = $target->{$key}; | |||||||||||||||||||||||||||
| 26 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 27 | action => 'create', | ||||||||||||||||||||||||||||
| 28 | schema_name => $t->{schema_name}, | ||||||||||||||||||||||||||||
| 29 | table_name => $t->{table_name}, | ||||||||||||||||||||||||||||
| 30 | table_info => $t, | ||||||||||||||||||||||||||||
| 31 | 7 | ▶ | columns => $target_cols->{$key} // [], | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_cols->{$key} // []
MC/DC100%1/1
$target_cols->{$key} // []
$target_cols->{$key}
Truth table100%2/2
A
A= $target_cols->{$key}
| |||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9 | for my $key (sort keys %$source) { | |||||||||||||||||||||||||||
| 36 | 5 | ▶ | next if exists $target->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 3 | my $t = $source->{$key}; | |||||||||||||||||||||||||||
| 38 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 39 | action => 'drop', | ||||||||||||||||||||||||||||
| 40 | schema_name => $t->{schema_name}, | ||||||||||||||||||||||||||||
| 41 | table_name => $t->{table_name}, | ||||||||||||||||||||||||||||
| 42 | 3 | table_info => $t, | |||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 9 | return @ops; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 10 | ▶ | sub qualified_name { | ||||||||||||||||||||||||||
|
Subroutine
qualified_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 52 | 10 | return $self->schema_name . '.' . $self->table_name; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub _identity_kind { | ||||||||||||||||||||||||||
|
Subroutine
_identity_kind: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 0 | return DBIO::PostgreSQL::Introspect->identity_kind(@_); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | sub _column_def { | ||||||||||||||||||||||||||
|
Subroutine
_column_def: called CC=8 SCAR=23.1
| |||||||||||||||||||||||||||||
| 61 | 2 | my ($col) = @_; | |||||||||||||||||||||||||||
| 62 | 2 | ▶ | my $def = $col->{column_name} . ' ' . ($col->{data_type} // 'text'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$col->{"data_type"} // "text"
MC/DC0%0/1
$col->{"data_type"} // "text"
$col->{"data_type"}
Truth table50%1/2
A
A= $col->{"data_type"}
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # NOT NULL | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | $def .= ' NOT NULL' if $col->{not_null}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Default value | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if (defined $col->{default_value} && $col->{default_value} ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $col->{"default_value"} and $col->{"default_value"} ne ""
MC/DC0%0/2
defined $col->{"default_value"} and $col->{"default_value"} ne ""
defined $col->{"default_value"}
$col->{"default_value"} ne ""
Truth table33%1/3
A and B
A= defined $col->{"default_value"}B= $col->{"default_value"} ne ""
| |||||||||||||||||||||||||||||
| 69 | 0 | $def .= sprintf ' DEFAULT %s', $col->{default_value}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Identity | ||||||||||||||||||||||||||||
| 73 | 2 | my $kind = DBIO::PostgreSQL::Introspect->identity_kind($col->{identity}); | |||||||||||||||||||||||||||
| 74 | 2 | ▶ | $def .= " GENERATED $kind AS IDENTITY" if $kind; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Generated columns (attgenerated = 's') | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | $def .= ' GENERATED ALWAYS AS STORED' if $col->{generated} && $col->{generated} eq 's'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$col->{"generated"} and $col->{"generated"} eq "s"
MC/DC0%0/2
$col->{"generated"} and $col->{"generated"} eq "s"
$col->{"generated"}
$col->{"generated"} eq "s"
Truth table33%1/3
A and B
A= $col->{"generated"}B= $col->{"generated"} eq "s"
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | return $def; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | 4 | ▶ | if ($self->action eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | my @col_defs = map { _column_def($_) } @{ $self->columns // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->columns // []
MC/DC0%0/1
$self->columns // []
$self->columns
Truth table50%1/2
A
A= $self->columns
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if (@col_defs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1 | return sprintf 'CREATE TABLE %s (%s);', | |||||||||||||||||||||||||||
| 88 | $self->qualified_name, join ', ', @col_defs; | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 1 | return sprintf 'CREATE TABLE %s ();', $self->qualified_name; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ($self->action eq 'drop') { | ||||||||||||||||||||||||||||
| 93 | 2 | return sprintf 'DROP TABLE %s CASCADE;', $self->qualified_name; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | 2 | ▶ | return sprintf ' %stable: %s', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | ($self->action eq 'create' ? '+' : '-'), $self->qualified_name; | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 9 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =pod | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 NAME | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | DBIO::PostgreSQL::Diff::Table - Diff operations for PostgreSQL tables | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 VERSION | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | version 0.900001 | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Represents a table-level diff operation: C<CREATE TABLE ...> (with full | ||||||||||||||||||||||||||||
| 123 | column definitions for new tables) or C<DROP TABLE CASCADE>. | ||||||||||||||||||||||||||||
| 124 | Instances are produced by L</diff> and consumed by L<DBIO::PostgreSQL::Diff>. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Diff::Column handles column ADDs for existing tables only; new tables get | ||||||||||||||||||||||||||||
| 127 | their full column list inlined in the CREATE TABLE statement produced here. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 schema_name | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | PostgreSQL schema containing the table. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 table_name | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | The table name. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 table_info | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Hashref of table metadata from introspection (C<kind>, C<rls_enabled>, etc.). | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 columns | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | ArrayRef of column hashrefs for new tables (populated when C<action> is | ||||||||||||||||||||||||||||
| 146 | C<create>), so that C<as_sql> can emit a complete CREATE TABLE with all | ||||||||||||||||||||||||||||
| 147 | column definitions rather than an empty shell. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 METHODS | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 diff | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | my @ops = DBIO::PostgreSQL::Diff::Table->diff($source, $target, $source_cols, $target_cols); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Compares two table hashrefs (keyed by C<schema.table>) and returns operations | ||||||||||||||||||||||||||||
| 156 | for tables present only in target (create) or only in source (drop). | ||||||||||||||||||||||||||||
| 157 | For new tables, passes column metadata so C<as_sql> can emit a complete | ||||||||||||||||||||||||||||
| 158 | CREATE TABLE with full column definitions (types, nullability, defaults, | ||||||||||||||||||||||||||||
| 159 | identity). Diff::Column only handles ALTER for existing tables. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 qualified_name | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | my $fqn = $op->qualified_name; # 'auth.users' | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Returns the schema-qualified table name. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 as_sql | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns the SQL for this operation. For C<create>, emits a complete | ||||||||||||||||||||||||||||
| 170 | CREATE TABLE with all column definitions inline (type, nullability, | ||||||||||||||||||||||||||||
| 171 | default, identity). Diff::Column handles column ADDs for existing tables. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 summary | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Returns a one-line description such as C<+table: auth.users>. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 184 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 185 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 188 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||