| 1 | package DBIO::PostgreSQL::Diff::Schema; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL schemas | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 103 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 103 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 103 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 103 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 103 | ▶ | use DBIO::PostgreSQL::Diff::CaseOnly (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 103 | ▶ | use DBIO::PostgreSQL::Diff::Dependents (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 103 | ▶ | use DBIO::PostgreSQL::Diff::NoStatement (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 103 | ▶ | use DBIO::PostgreSQL::Util qw(quote_ident create_schema_if_missing); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 103 | __PACKAGE__->mk_diff_accessors(qw(schema_name if_missing)); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # The extensions the diff keeps, by the schema the source has them in. | ||||||||||||||||||||||||||||
| 23 | 75 | ▶ | sub _kept_extensions_by_schema { | ||||||||||||||||||||||||||
|
Subroutine
_kept_extensions_by_schema: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 24 | 75 | my ($source_extensions, $target_extensions) = @_; | |||||||||||||||||||||||||||
| 25 | 75 | my %kept; | |||||||||||||||||||||||||||
| 26 | 75 | ▶ | for my $name (sort keys %{ $source_extensions // {} }) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$source_extensions // {}
MC/DC100%1/1
$source_extensions // {}
$source_extensions
Truth table100%2/2
A
A= $source_extensions
| |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | next unless exists $target_extensions->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 6 | my $schema = $source_extensions->{$name}{schema_name}; | |||||||||||||||||||||||||||
| 29 | 6 | ▶ | push @{ $kept{$schema} }, $name if defined $schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 75 | return \%kept; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub _refuse_drop_under_extension { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_drop_under_extension: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | my ($schema, @extensions) = @_; | |||||||||||||||||||||||||||
| 36 | die join "\n", | ||||||||||||||||||||||||||||
| 37 | sprintf( | ||||||||||||||||||||||||||||
| 38 | 'refusing to diff: schema %s is not in the target and would be dropped, and it holds %s, which the target keeps', | ||||||||||||||||||||||||||||
| 39 | 1 | $schema, join(', ', map { "extension $_" } @extensions), | |||||||||||||||||||||||||||
| 40 | ), | ||||||||||||||||||||||||||||
| 41 | '', | ||||||||||||||||||||||||||||
| 42 | 'An extension depends on the schema it is installed in: the server refuses', | ||||||||||||||||||||||||||||
| 43 | 'DROP SCHEMA ' . quote_ident($schema) . ' while the extension is in it, and CASCADE would drop the', | ||||||||||||||||||||||||||||
| 44 | 'extension too, and with the extension every column, index and', | ||||||||||||||||||||||||||||
| 45 | 'function that uses what it brings -- in tables this diff does not name. No', | ||||||||||||||||||||||||||||
| 46 | 'statement was generated.', | ||||||||||||||||||||||||||||
| 47 | '', | ||||||||||||||||||||||||||||
| 48 | 'Either keep the schema (declare it with pg_schemas), or take the extension out', | ||||||||||||||||||||||||||||
| 49 | 'of it before the diff -- a relocatable extension moves, any other has to be', | ||||||||||||||||||||||||||||
| 50 | 'dropped and created again:', | ||||||||||||||||||||||||||||
| 51 | 1 | ( map { ' ALTER EXTENSION ' . quote_ident($_) . ' SET SCHEMA ...;' } @extensions ), | |||||||||||||||||||||||||||
| 52 | ''; | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 76 | ▶ | sub _refuse_case_only { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_case_only: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 56 | 76 | my ($source, $target) = @_; | |||||||||||||||||||||||||||
| 57 | DBIO::PostgreSQL::Diff::CaseOnly->refuse( | ||||||||||||||||||||||||||||
| 58 | 'DROP SCHEMA comes after the drop of everything that is in the schema, tables and their rows included.', | ||||||||||||||||||||||||||||
| 59 | map { | ||||||||||||||||||||||||||||
| 60 | 1 | my ($old, $new) = map { $_->{name} } @$_; | |||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | 1 | kind => 'schema', | |||||||||||||||||||||||||||
| 63 | database => $old, | ||||||||||||||||||||||||||||
| 64 | target => $new, | ||||||||||||||||||||||||||||
| 65 | rename => sprintf('ALTER SCHEMA %s RENAME TO %s;', quote_ident($old), quote_ident($new)), | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } DBIO::PostgreSQL::Diff::CaseOnly->pairs( | ||||||||||||||||||||||||||||
| 68 | 76 | ▶ | map { [ map { { in => [], name => $_ } } keys %{ $_ // {} } ] } $source, $target, | ||||||||||||||||||||||||||
|
Condition50%1/2
$_ // {}
MC/DC0%0/1
$_ // {}
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||
| 69 | ), | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 76 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 76 | my ($class, $source, $target, $source_extensions, $target_extensions, $source_dependents) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 76 | _refuse_case_only($source, $target); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 75 | my $kept = _kept_extensions_by_schema($source_extensions, $target_extensions); | |||||||||||||||||||||||||||
| 79 | 75 | ▶ | for my $name (sort keys %{ $source // {} }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$source // {}
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||
| 80 | 108 | ▶ | next if exists $target->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 12 | ▶ | _refuse_drop_under_extension($name, @{ $kept->{$name} }) if $kept->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # What stands in a schema and is not in the model is named, not left to the server. | ||||||||||||||||||||||||||||
| 85 | DBIO::PostgreSQL::Diff::Dependents->refuse(map { { | ||||||||||||||||||||||||||||
| 86 | object => "schema $_", | ||||||||||||||||||||||||||||
| 87 | 11 | ▶ | dependents => ($source_dependents // {})->{schemas}{$_}, | ||||||||||||||||||||||||||
|
Condition100%2/2
$source_dependents // {}
MC/DC100%1/1
$source_dependents // {}
$source_dependents
Truth table100%2/2
A
A= $source_dependents
| |||||||||||||||||||||||||||||
| 88 | 74 | ▶ | } } grep { !exists $target->{$_} } sort keys %{ $source // {} }); | ||||||||||||||||||||||||||
|
Condition50%1/2
$source // {}
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | return $class->diff_toplevel( | ||||||||||||||||||||||||||||
| 91 | $source, $target, | ||||||||||||||||||||||||||||
| 92 | 10 | ▶ | create => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 93 | 10 | my ($name) = @_; | |||||||||||||||||||||||||||
| 94 | $class->new(action => 'create', schema_name => $name, | ||||||||||||||||||||||||||||
| 95 | 10 | ▶ | if_missing => ($target->{$name} && $target->{$name}{create_if_missing} ? 1 : 0)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$target->{$name} and $target->{$name}{"create_if_missing"}
MC/DC50%1/2
$target->{$name} and $target->{$name}{"create_if_missing"}
$target->{$name}
$target->{$name}{"create_if_missing"}
Truth table66%2/3
A and B
A= $target->{$name}B= $target->{$name}{"create_if_missing"}
| |||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||
| 97 | 10 | ▶ | drop => sub { my ($name) = @_; $class->new(action => 'drop', schema_name => $name) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 73 | ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 63 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 63 | my ($self) = @_; | |||||||||||||||||||||||||||
| 104 | 63 | ▶ | if ($self->action eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 105 | 32 | ▶ | return create_schema_if_missing($self->schema_name) . ';' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | if $self->if_missing; | ||||||||||||||||||||||||||||
| 107 | 31 | return sprintf 'CREATE SCHEMA %s;', | |||||||||||||||||||||||||||
| 108 | quote_ident($self->schema_name); | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | elsif ($self->action eq 'drop') { | ||||||||||||||||||||||||||||
| 111 | 30 | return sprintf 'DROP SCHEMA %s;', | |||||||||||||||||||||||||||
| 112 | quote_ident($self->schema_name); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 1 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw($self, 'schema ' . ($self->schema_name // '')); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->schema_name // ""
MC/DC0%0/1
$self->schema_name // ""
$self->schema_name
Truth table50%1/2
A
A= $self->schema_name
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 120 | 3 | ▶ | return sprintf ' %sschema: %s', ($self->action eq 'create' ? '+' : '-'), $self->schema_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 103 | 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||