| 1 | package DBIO::PostgreSQL::Diff::Type; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL types (enums, composites, ranges) | ||||||||||||||||||||||||||||
| 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 | __PACKAGE__->mk_diff_accessors(qw(type_key type_info added_values)); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 3 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||
| 18 | 3 | my @ops; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | for my $key (sort keys %$target) { | |||||||||||||||||||||||||||
| 21 | 3 | ▶ | if (!exists $source->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 23 | action => 'create', | ||||||||||||||||||||||||||||
| 24 | type_key => $key, | ||||||||||||||||||||||||||||
| 25 | 2 | type_info => $target->{$key}, | |||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | 2 | next; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Existing type — check for enum value additions | ||||||||||||||||||||||||||||
| 31 | 1 | my $src = $source->{$key}; | |||||||||||||||||||||||||||
| 32 | 1 | my $tgt = $target->{$key}; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | if ($tgt->{type_kind} eq 'enum' && $src->{type_kind} eq 'enum') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tgt->{"type_kind"} eq "enum" and $src->{"type_kind"} eq "enum"
MC/DC0%0/2
$tgt->{"type_kind"} eq "enum" and $src->{"type_kind"} eq "enum"
$tgt->{"type_kind"} eq "enum"
$src->{"type_kind"} eq "enum"
Truth table33%1/3
A and B
A= $tgt->{"type_kind"} eq "enum"B= $src->{"type_kind"} eq "enum"
| |||||||||||||||||||||||||||||
| 35 | 1 | my %src_vals = map { $_ => 1 } @{ $src->{values} }; | |||||||||||||||||||||||||||
| 36 | 1 | my @new_vals = grep { !$src_vals{$_} } @{ $tgt->{values} }; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | if (@new_vals) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | push @ops, $class->new( | |||||||||||||||||||||||||||
| 39 | action => 'add_value', | ||||||||||||||||||||||||||||
| 40 | type_key => $key, | ||||||||||||||||||||||||||||
| 41 | type_info => $tgt, | ||||||||||||||||||||||||||||
| 42 | added_values => \@new_vals, | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | for my $key (sort keys %$source) { | |||||||||||||||||||||||||||
| 49 | 1 | ▶ | next if exists $target->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 51 | action => 'drop', | ||||||||||||||||||||||||||||
| 52 | type_key => $key, | ||||||||||||||||||||||||||||
| 53 | 0 | type_info => $source->{$key}, | |||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | return @ops; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=7 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | 4 | my $info = $self->type_info; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | if ($self->action eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if ($info->{type_kind} eq 'enum') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 67 | 2 | my $values = join ', ', map { "'$_'" } @{ $info->{values} }; | |||||||||||||||||||||||||||
| 68 | 2 | return sprintf "CREATE TYPE %s AS ENUM (%s);", $self->type_key, $values; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif ($info->{type_kind} eq 'composite') { | ||||||||||||||||||||||||||||
| 71 | my $attrs = join ",\n ", map { | ||||||||||||||||||||||||||||
| 72 | 2 | "$_->{name} $_->{type}" | |||||||||||||||||||||||||||
| 73 | 1 | } @{ $info->{attributes} }; | |||||||||||||||||||||||||||
| 74 | 1 | return sprintf "CREATE TYPE %s AS (\n %s\n);", $self->type_key, $attrs; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | elsif ($info->{type_kind} eq 'range') { | ||||||||||||||||||||||||||||
| 77 | return sprintf "CREATE TYPE %s AS RANGE (SUBTYPE = %s);", | ||||||||||||||||||||||||||||
| 78 | 0 | $self->type_key, $info->{subtype}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | elsif ($self->action eq 'drop') { | ||||||||||||||||||||||||||||
| 82 | 0 | return sprintf "DROP TYPE %s CASCADE;", $self->type_key; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | elsif ($self->action eq 'add_value') { | ||||||||||||||||||||||||||||
| 85 | return join "\n", map { | ||||||||||||||||||||||||||||
| 86 | 1 | sprintf "ALTER TYPE %s ADD VALUE '%s';", $self->type_key, $_ | |||||||||||||||||||||||||||
| 87 | 1 | } @{ $self->added_values }; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($self->action eq 'add_value') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $count = scalar @{ $self->added_values }; | |||||||||||||||||||||||||||
| 96 | 0 | my $vals = join ', ', @{ $self->added_values }; | |||||||||||||||||||||||||||
| 97 | 0 | return sprintf ' ~type: %s +%d value(s) (%s)', $self->type_key, $count, $vals; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | return sprintf ' %stype: %s (%s)', $prefix, $self->type_key, $self->type_info->{type_kind}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 9 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =pod | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 NAME | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | DBIO::PostgreSQL::Diff::Type - Diff operations for PostgreSQL types (enums, composites, ranges) | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 VERSION | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | version 0.900001 | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Represents a type-level diff operation: C<CREATE TYPE>, C<DROP TYPE CASCADE>, | ||||||||||||||||||||||||||||
| 122 | or C<ALTER TYPE ... ADD VALUE> (for enum value additions). Handles enum, | ||||||||||||||||||||||||||||
| 123 | composite, and range types. Note that enum value removal is not supported by | ||||||||||||||||||||||||||||
| 124 | PostgreSQL -- only addition is possible without recreating the type. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 type_key | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | The C<schema.type_name> key. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 type_key | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | The C<schema.type_name> key. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 type_info | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Type metadata hashref from introspection (C<type_kind>, C<values>, | ||||||||||||||||||||||||||||
| 139 | C<attributes>, or C<subtype> depending on kind). | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 METHODS | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 diff | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | my @ops = DBIO::PostgreSQL::Diff::Type->diff($source, $target); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Compares two type hashrefs. Produces C<create> operations for new types, | ||||||||||||||||||||||||||||
| 148 | C<drop> for removed types, and C<add_value> for enum types that have gained | ||||||||||||||||||||||||||||
| 149 | new values. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 as_sql | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Returns the SQL for this operation. For C<add_value>, returns one | ||||||||||||||||||||||||||||
| 154 | C<ALTER TYPE ... ADD VALUE> statement per new enum value. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 summary | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Returns a one-line description such as C<+type: auth.role_type (enum)> or | ||||||||||||||||||||||||||||
| 159 | C<~type auth.role_type: +1 value(s) (superadmin)>. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 168 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 169 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 172 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||