| 1 | package DBIO::PostgreSQL::PostGIS::Extension; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The postgis extension for deploy, introspection and diff (DBIO extension interface) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use base 'DBIO::Extension'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use DBIO::PostgreSQL::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | sub extension_interface { 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extension_interface: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | sub name { 'postgis' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub driver { 'DBIO::PostgreSQL' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
driver: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | sub storage_layer { 'DBIO::PostgreSQL::PostGIS::Storage' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
storage_layer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | sub inflation_component { 'DBIO::PostgreSQL::PostGIS' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inflation_component: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 108 | ▶ | sub annotate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
annotate: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 108 | my ($class, $model, $ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # The schema the types live in, per side of the diff: the model's own word | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # first (a temp database may differ from the live one), the entry's second. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | my $postgis_schema = ($model->{extensions} // {})->{postgis} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ? $model->{extensions}{postgis}{schema_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 108 | ▶ | : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$model->{"extensions"} // {}
MC/DC100%1/1
$model->{"extensions"} // {}
$model->{"extensions"}
Truth table100%2/2
A
A= $model->{"extensions"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 108 | ▶ | $postgis_schema //= ($ctx->{self} // {})->{schema}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
$postgis_schema //= +($ctx->{"self"} // {})->{"schema"}
$ctx->{"self"} // {}
MC/DC100%2/2
$postgis_schema //= +($ctx->{"self"} // {})->{"schema"}
$postgis_schema
$ctx->{"self"}
Truth table100%3/3
A //= B
A= $postgis_schemaB= $ctx->{"self"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 108 | my @triples; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 108 | ▶ | my $columns_by_table = $model->{columns} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$model->{"columns"} // {}
MC/DC0%0/1
$model->{"columns"} // {}
$model->{"columns"}
Truth table50%1/2
A
A= $model->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 108 | for my $table_key (sort keys %$columns_by_table) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 127 | ▶ | for my $col (@{ $columns_by_table->{$table_key} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$columns_by_table->{$table_key} // []
MC/DC0%0/1
$columns_by_table->{$table_key} // []
$columns_by_table->{$table_key}
Truth table50%1/2
A
A= $columns_by_table->{$table_key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 211 | ▶ | my $dt = $col->{data_type} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$col->{"data_type"} // ""
MC/DC100%1/1
$col->{"data_type"} // ""
$col->{"data_type"}
Truth table100%2/2
A
A= $col->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 211 | ▶ | next unless $dt =~ /\A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | (?: (?: "((?:[^"]|"")+)" | ([A-Za-z_][\w\$]*) ) \. )? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | (geometry|geography) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | (?: \( (\w+) (?: ,(\d+) )? \) )? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | \z/xi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 123 | my ($quoted, $plain, $base, $gtype, $srid) = ($1, $2, $3, $4, $5); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 123 | ▶ | if (defined $postgis_schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Where the type lives: the catalog's word first, the prefix of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # string otherwise. Only when neither says, the type is taken as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # PostGIS's -- an unqualified name is resolved through the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # search_path, and the search_path found the PostGIS type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 118 | my $schema = $col->{type_schema}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 118 | ▶ | unless (defined $schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | ▶ | if (defined $quoted) { ($schema = $quoted) =~ s/""/"/g } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | else { $schema = $plain } # may be undef | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 118 | ▶ | next if defined $schema && $schema ne $postgis_schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $schema and $schema ne $postgis_schema
MC/DC100%2/2
defined $schema and $schema ne $postgis_schema
defined $schema
$schema ne $postgis_schema
Truth table100%3/3
A and B
A= defined $schemaB= $schema ne $postgis_schema
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 118 | my $address = [ column => $table_key, $col->{column_name} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 118 | push @triples, [ $address, base_type => lc $base ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 118 | ▶ | push @triples, [ $address, geometry_type => lc $gtype ] if $gtype; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 118 | ▶ | push @triples, [ $address, srid => $srid + 0 ] if defined $srid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 108 | return \@triples; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 91 | ▶ | sub verdict { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verdict: called CC=16 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 91 | my ($class, $change, $ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 91 | ▶ | return undef unless ($change->{kind} // '') eq 'column_type'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$change->{"kind"} // ""
MC/DC0%0/1
$change->{"kind"} // ""
$change->{"kind"}
Truth table50%1/2
A
A= $change->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 90 | my ($old, $new) = @{$change}{qw(old_info new_info)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | return undef unless $old && $new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 90 | ▶ | && defined $old->{base_type} && defined $new->{base_type}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$old and $new and defined $old->{"base_type"} and defined $new->{"base_type"}
$old and $new and defined $old->{"base_type"}
$old and $new
MC/DC50%2/4
$old and $new and defined $old->{"base_type"} and defined $new->{"base_type"}
$old
$new
defined $old->{"base_type"}
defined $new->{"base_type"}
Truth table60%3/5
A and B and C and D
A= $oldB= $newC= defined $old->{"base_type"}D= defined $new->{"base_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 88 | my $column = $change->{column_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # the one place that refuses a change of type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 88 | ▶ | if ($old->{base_type} ne $new->{base_type} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$old->{"base_type"} ne $new->{"base_type"} or defined $old->{"geometry_type"} and defined $new->{"geometry_type"} and $old->{"geometry_type"} ne $new->{"geometry_type"}
defined $old->{"geometry_type"} and defined $new->{"geometry_type"} and $old->{"geometry_type"} ne $new->{"geometry_type"}
defined $old->{"geometry_type"} and defined $new->{"geometry_type"}
MC/DC100%4/4
$old->{"base_type"} ne $new->{"base_type"} or defined $old->{"geometry_type"} and defined $new->{"geometry_type"} and $old->{"geometry_type"} ne $new->{"geometry_type"}
$old->{"base_type"} ne $new->{"base_type"}
defined $old->{"geometry_type"}
defined $new->{"geometry_type"}
$old->{"geometry_type"} ne $new->{"geometry_type"}
Truth table100%5/5
A or B and C and D
A= $old->{"base_type"} ne $new->{"base_type"}B= defined $old->{"geometry_type"}C= defined $new->{"geometry_type"}D= $old->{"geometry_type"} ne $new->{"geometry_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | || (defined $old->{geometry_type} && defined $new->{geometry_type} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | && $old->{geometry_type} ne $new->{geometry_type})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | ▶ | my $what = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | my ($i) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 8 | join ' ', grep { defined } $i->{base_type}, $i->{geometry_type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | return { refuse => sprintf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | "column %s.%s changes from %s to %s. A change of the geometry type, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | . "or between geometry and geography, has no conversion the diff can " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | . "write; the server refuses it on a table that holds rows. No " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | . "statement was generated. Add a column of the new type, fill it, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | . "and drop the old one.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | $change->{table} // '', $column, $what->($old), $what->($new) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$change->{"table"} // ""
MC/DC0%0/1
$change->{"table"} // ""
$change->{"table"}
Truth table50%1/2
A
A= $change->{"table"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 84 | my ($from, $to) = ($old->{srid}, $new->{srid}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 84 | ▶ | return undef unless $from && $to && $from != $to; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$from and $to and $from != $to
$from and $to
MC/DC100%3/3
$from and $to and $from != $to
$from
$to
$from != $to
Truth table100%4/4
A and B and C
A= $fromB= $toC= $from != $to
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 81 | my $expr = DBIO::PostgreSQL::Util::quote_ident($column); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 81 | ▶ | return { using => $new->{base_type} eq 'geography' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ? sprintf('ST_Transform(%s::geometry, %d)::geography', $expr, $to) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | : sprintf('ST_Transform(%s, %d)', $expr, $to) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||