PostgreSQL/DDL.pm

filter: Statementstmt 75.5% 151 / 200 Branchbran 57.1% 64 / 112 Conditioncond 44.0% 22 / 50 MC/DCmcdc 18.7% 6 / 32 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 62.5% 252 / 403 CC 94 SCAR 60.0
CC
94
Cov
65%
install_ddl
473.5
_pg_column_type
10.8
BEGIN
1.0
CRAP 404.2
↑ Summary PostgreSQL/Diff.pm »
1 package DBIO::PostgreSQL::DDL;
2 # ABSTRACT: Generate PostgreSQL DDL from DBIO schema classes
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use DBIO::Schema::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use DBIO::PostgreSQL::Adapter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 my $ADAPTER = DBIO::PostgreSQL::Adapter->new;
13  
14  
15  
16 4 ▶sub install_ddl {
Subroutine install_ddl: called CC=86 SCAR=61.6
Pod documented
17 4 my ($class, $schema) = @_;
18 4 my @stmts;
19 my @view_stmts;
20 4 my %seen_table;
21 4 my %seen_view;
22  
23 # 1. Extensions
24 4 for my $ext ($schema->pg_extensions) {
25 0 push @stmts, "CREATE EXTENSION IF NOT EXISTS \"$ext\";";
26 }
27  
28 # 2. Schemas (namespaces)
29 4 for my $ns ($schema->pg_schemas) {
30 2 ▶ next if $ns eq 'public'; # public always exists
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "public" 0 2 2
31 2 push @stmts, sprintf 'CREATE SCHEMA IF NOT EXISTS %s;', _quote_ident($ns);
32 }
33  
34 # 3. Types from PgSchema classes
35 4 for my $ns ($schema->pg_schemas) {
36 2 my $pg_schema_class = $schema->pg_schema_class($ns);
37 2 ▶ next unless $pg_schema_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg_schema_class 2 0 2
38  
39 # Enums
40 0 for my $def (@{ $pg_schema_class->_pg_enum_defs }) {
41 0 my ($name, $values) = @$def;
42 0 my $vals = join ', ', map { "'$_'" } @$values;
43 0 push @stmts, sprintf "CREATE TYPE %s.%s AS ENUM (%s);",
44 _quote_ident($ns), _quote_ident($name), $vals;
45 }
46  
47 # Composite types
48 0 for my $def (@{ $pg_schema_class->_pg_type_defs }) {
49 0 my ($name, $fields) = @$def;
50 0 my @attrs;
51 0 for my $fname (sort keys %$fields) {
52 0 push @attrs, sprintf ' %s %s', _quote_ident($fname), $fields->{$fname};
53 }
54 0 push @stmts, sprintf "CREATE TYPE %s.%s AS (\n%s\n);",
55 _quote_ident($ns), _quote_ident($name), join(",\n", @attrs);
56 }
57  
58 # Functions
59 0 for my $def (@{ $pg_schema_class->_pg_function_defs }) {
60 0 my ($name, $sql) = @$def;
61 0 $sql =~ s/^\s+|\s+$//g;
62 0 ▶ $sql .= ';' unless $sql =~ /;\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /;\s*$/ 0 0 0
63 0 push @stmts, $sql;
64 }
65 }
66  
67 # 3b. Auto-generate CREATE TYPE for Cake-style enum columns
68 # (data_type => 'enum' with extra->{list} but no pg_enum_type)
69 {
70 4 my @sources = $schema->sources;
71 4 for my $source_name (sort @sources) {
72 4 my $source = $schema->source($source_name);
73 4 my $result_class = $source->result_class;
74 4 my $table_name = $source->name;
75  
76 # Only real tables carry enum columns worth a CREATE TYPE; views and
77 # subquery sources (scalar-ref name) have no backing table.
78 4 ▶ next if $source->isa('DBIO::ResultSource::View') || ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIO::ResultSource::View") or ref $table_name 0 4 4
Condition33%1/3
$source->isa("DBIO::ResultSource::View") or ref $table_name
l!l&&r!l&&!r
004
MC/DC0%0/2
$source->isa("DBIO::ResultSource::View") or ref $table_name
$source->isa("DBIO::ResultSource::View") ref $table_name
Truth table33%1/3
A or B
A= $source->isa("DBIO::ResultSource::View")B= ref $table_name
ABresult
1X1
011
000
79  
80 4 ▶ my $pg_schema_name = $result_class->can('pg_schema')
Branch: 1 branch
BranchTrueFalseTotal
$result_class->can('pg_schema') ? : 3 1 4
81 ? $result_class->pg_schema : undef;
82  
83 4 for my $col_name ($source->columns) {
84 9 my $info = $source->column_info($col_name);
85 9 ▶ next unless ($info->{data_type} // '') eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"data_type"} // "") eq "enum" 7 2 9
Condition50%1/2
$info->{"data_type"} // ""
l!l
90
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
86 2 ▶ next if $info->{pg_enum_type}; # already declared via PgSchema
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"pg_enum_type"} 0 2 2
87 2 ▶ next unless $info->{extra} && $info->{extra}{list}; # need values
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"extra"} and $info->{"extra"}{"list"} 0 2 2
Condition33%1/3
$info->{"extra"} and $info->{"extra"}{"list"}
!ll&&!rl&&r
002
MC/DC0%0/2
$info->{"extra"} and $info->{"extra"}{"list"}
$info->{"extra"} $info->{"extra"}{"list"}
Truth table33%1/3
A and B
A= $info->{"extra"}B= $info->{"extra"}{"list"}
ABresult
0X0
100
111
88  
89 # Generate a type name: {table}_{column}_enum
90 2 my $type_name = "${table_name}_${col_name}_enum";
91 2 ▶ my $qualified = $pg_schema_name
Branch: 1 branch
BranchTrueFalseTotal
$pg_schema_name ? : 1 1 2
92 ? sprintf('%s.%s', _quote_ident($pg_schema_name), _quote_ident($type_name))
93 : _quote_ident($type_name);
94  
95 2 my $vals = join ', ', map { "'$_'" } @{ $info->{extra}{list} };
96 2 push @stmts, sprintf "CREATE TYPE %s AS ENUM (%s);", $qualified, $vals;
97  
98 # Set pg_enum_type so _pg_column_type picks it up
99 2 ▶ $info->{pg_enum_type} = $pg_schema_name
Branch: 1 branch
BranchTrueFalseTotal
$pg_schema_name ? : 1 1 2
100 ? "$pg_schema_name.$type_name"
101 : $type_name;
102 }
103 }
104 }
105  
106 # 4. Tables from Result classes
107 4 my @sources = $schema->sources;
108 4 for my $source_name (sort @sources) {
109 4 my $source = $schema->source($source_name);
110 4 my $result_class = $source->result_class;
111  
112 4 my $table_name = $source->name;
113 4 my $pg_schema_name;
114 4 ▶ if ($result_class->can('pg_schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_schema")) 3 1 4
115 3 $pg_schema_name = $result_class->pg_schema;
116 }
117  
118 # Views: emit CREATE VIEW after all tables; skip virtual views.
119 4 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 0 4 4
120 0 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 0 0 0
Condition0%0/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
000
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table0%0/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
121 0 ▶ my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name eq 'SCALAR' ? : 0 0 0
122 0 ▶ next if ref $vname;
Branch: 1 branch
BranchTrueFalseTotal
if ref $vname 0 0 0
123 0 ▶ my $vqual = $pg_schema_name ? "$pg_schema_name.$vname" : $vname;
Branch: 1 branch
BranchTrueFalseTotal
$pg_schema_name ? : 0 0 0
124 0 ▶ next if $seen_view{$vqual}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_view{$vqual}++ 0 0 0
125 0 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 0 0 0
126 0 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 0 0
Condition0%0/3
defined $def and length $def
!ll&&!rl&&r
000
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table0%0/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
127 0 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', $vqual, $def;
128 0 next;
129 }
130  
131 # A scalar-ref name is either a literal table name (\'cd') or an inline
132 # subquery source; deref the former, skip the latter.
133 4 ▶ if (ref $table_name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table_name eq "SCALAR") 0 4 4
134 0 $table_name = $$table_name;
135 0 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 0 0
136 }
137 4 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 4 4
138  
139 4 ▶ my $qualified = $pg_schema_name
Branch: 1 branch
BranchTrueFalseTotal
$pg_schema_name ? : 2 2 4
140 ? "$pg_schema_name.$table_name"
141 : $table_name;
142  
143 # Multiple result sources can map to one physical table; emit it once.
144 4 ▶ next if $seen_table{$qualified}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$qualified}++ 0 4 4
145  
146 # Column definitions
147 4 my @col_defs;
148 4 for my $col_name ($source->columns) {
149 9 my $info = $source->column_info($col_name);
150 9 my $type = _pg_column_type($info);
151 9 my $def = sprintf ' %s %s', _quote_ident($col_name), $type;
152 9 ▶ $def .= ' NOT NULL' if defined $info->{is_nullable} && !$info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"is_nullable"} and not $info->{"is_nullable"} 8 1 9
Condition66%2/3
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
!ll&&!rl&&r
108
MC/DC50%1/2
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
defined $info->{"is_nullable"} not $info->{"is_nullable"}
Truth table66%2/3
A and not(B)
A= defined $info->{"is_nullable"}B= not $info->{"is_nullable"}
ABresult
0X0
100
111
153 9 ▶ if ($info->{is_auto_increment} && $type !~ /^(?:big)?serial$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'is_auto_increment'} and not $type =~ /^(?:big)?serial$/) { } 1 8 9
elsif (defined $info->{'default_value'}) { } 0 8 8
Condition100%3/3
$info->{"is_auto_increment"} and not $type =~ /^(?:big)?serial$/
!ll&&!rl&&r
621
MC/DC100%2/2
$info->{"is_auto_increment"} and not $type =~ /^(?:big)?serial$/
$info->{"is_auto_increment"} not $type =~ /^(?:big)?serial$/
Truth table100%3/3
A and not(B)
A= $info->{"is_auto_increment"}B= not $type =~ /^(?:big)?serial$/
ABresult
0X0
100
111
154 # DBIO convention: auto-increment columns are GENERATED ALWAYS AS
155 # IDENTITY unless the user explicitly opts into BY DEFAULT via
156 # extra->{identity} = 'd'. ALWAYS prevents accidental client-side
157 # inserts, matches the DBIx::Class::ResultSource default, and is
158 # what DBIO::PostgreSQL::Diff::target_from_compiled synthesizes
159 # (so the round-trip diff against the introspected source is
160 # empty for the default schema).
161 my $kind = DBIO::PostgreSQL::Introspect->identity_kind(
162 $info->{extra} && $info->{extra}{identity}
163 1 ▶ ) // 'ALWAYS';
Condition40%2/5
"DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"}) // "ALWAYS"
l!l
01
$info->{'extra'} && $info->{'extra'}{'identity'}
!ll&&!rl&&r
100
MC/DC0%0/1
"DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"}) // "ALWAYS"
"DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"})
MC/DC0%0/2
$info->{'extra'} && $info->{'extra'}{'identity'}
$info->{'extra'} $info->{'extra'}{'identity'}
Truth table50%1/2
A
A= "DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"})
Aresult
11
00
Truth table33%1/3
A && B
A= $info->{'extra'}B= $info->{'extra'}{'identity'}
ABresult
0X0
100
111
164 1 $def .= " GENERATED $kind AS IDENTITY";
165 }
166 elsif (defined $info->{default_value}) {
167 0 my $dv = $info->{default_value};
168 0 ▶ if (ref $dv eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dv eq 'SCALAR') { } 0 0 0
169 0 $def .= " DEFAULT $$dv";
170 } else {
171 0 $def .= " DEFAULT '$dv'";
172 }
173 }
174 9 push @col_defs, $def;
175 }
176  
177 # Primary key
178 4 my @pk = $source->primary_columns;
179 4 ▶ if (@pk) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk) 3 1 4
180 push @col_defs, sprintf ' PRIMARY KEY (%s)',
181 3 join(', ', map { _quote_ident($_) } @pk);
182 }
183  
184 # Unique constraints declared via add_unique_constraint / Cake's `unique`.
185 # The 'primary' constraint is auto-added by set_primary_key and already
186 # emitted above as PRIMARY KEY, so it must be skipped here.
187 4 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 3 1 4
188 3 my %uniques = $source->unique_constraints;
189 3 for my $uname (sort keys %uniques) {
190 5 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 3 2 5
191 2 my $cols = $uniques{$uname};
192 push @col_defs, sprintf ' CONSTRAINT %s UNIQUE (%s)',
193 _quote_ident($uname),
194 2 join(', ', map { _quote_ident($_) } @$cols);
195 }
196 }
197  
198 4 push @stmts, sprintf "CREATE TABLE %s (\n%s\n);",
199 $qualified, join(",\n", @col_defs);
200  
201 # PostgreSQL-specific indexes
202 4 ▶ if ($result_class->can('pg_indexes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_indexes")) 3 1 4
203 3 my $indexes = $result_class->pg_indexes;
204 3 for my $idx_name (sort keys %$indexes) {
205 1 my $idx = $indexes->{$idx_name};
206 1 ▶ my $using = $idx->{using} ? " USING $idx->{using}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'using'} ? : 0 1 1
207 1 my $columns;
208 1 ▶ if ($idx->{expression}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->{'expression'}) { } 1 0 1
209 1 $columns = $idx->{expression};
210 } else {
211 0 ▶ $columns = join(', ', @{ $idx->{columns} // [] });
Condition0%0/2
$idx->{"columns"} // []
l!l
00
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table0%0/2
A
A= $idx->{"columns"}
Aresult
11
00
212 }
213 1 ▶ my $unique = $idx->{unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'unique'} ? : 1 0 1
214 1 my $sql = sprintf 'CREATE %sINDEX %s ON %s%s (%s)',
215 $unique, _quote_ident($idx_name), $qualified, $using, $columns;
216 1 ▶ if ($idx->{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->{"with"}) 0 1 1
217 0 my @with_parts;
218 0 for my $k (sort keys %{ $idx->{with} }) {
219 0 push @with_parts, "$k = $idx->{with}{$k}";
220 }
221 0 $sql .= ' WITH (' . join(', ', @with_parts) . ')';
222 }
223 1 ▶ $sql .= " WHERE $idx->{where}" if $idx->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $idx->{"where"} 0 1 1
224 1 push @stmts, "$sql;";
225 }
226 }
227  
228 # Triggers
229 4 ▶ if ($result_class->can('pg_triggers')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_triggers")) 3 1 4
230 3 my $triggers = $result_class->pg_triggers;
231 3 for my $trg_name (sort keys %$triggers) {
232 1 my $trg = $triggers->{$trg_name};
233 1 ▶ my $for_each = $trg->{for_each} || 'ROW';
Condition50%1/2
$trg->{'for_each'} || 'ROW'
l!l
10
MC/DC0%0/1
$trg->{'for_each'} || 'ROW'
$trg->{'for_each'}
Truth table50%1/2
A
A= $trg->{'for_each'}
Aresult
11
00
234 push @stmts, sprintf 'CREATE TRIGGER %s %s %s ON %s FOR EACH %s EXECUTE FUNCTION %s;',
235 _quote_ident($trg_name), $trg->{when}, $trg->{event},
236 1 $qualified, $for_each, $trg->{execute};
237 }
238 }
239  
240 # RLS
241 4 ▶ if ($result_class->can('pg_rls') && $result_class->pg_rls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_rls") and $result_class->pg_rls) 1 3 4
Condition100%3/3
$result_class->can("pg_rls") and $result_class->pg_rls
!ll&&!rl&&r
121
MC/DC100%2/2
$result_class->can("pg_rls") and $result_class->pg_rls
$result_class->can("pg_rls") $result_class->pg_rls
Truth table100%3/3
A and B
A= $result_class->can("pg_rls")B= $result_class->pg_rls
ABresult
0X0
100
111
242 1 my $rls = $result_class->pg_rls;
243 1 ▶ if ($rls->{enable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rls->{"enable"}) 1 0 1
244 1 push @stmts, sprintf 'ALTER TABLE %s ENABLE ROW LEVEL SECURITY;', $qualified;
245 }
246 1 ▶ if ($rls->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rls->{"force"}) 1 0 1
247 1 push @stmts, sprintf 'ALTER TABLE %s FORCE ROW LEVEL SECURITY;', $qualified;
248 }
249 1 ▶ if ($rls->{policies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rls->{"policies"}) 1 0 1
250 1 for my $pol_name (sort keys %{ $rls->{policies} }) {
251 1 my $pol = $rls->{policies}{$pol_name};
252 1 my $sql = sprintf 'CREATE POLICY %s ON %s',
253 _quote_ident($pol_name), $qualified;
254 1 ▶ $sql .= sprintf ' FOR %s', $pol->{for} if $pol->{for} && $pol->{for} ne 'ALL';
Branch: 1 branch
BranchTrueFalseTotal
if $pol->{"for"} and $pol->{"for"} ne "ALL" 0 1 1
Condition33%1/3
$pol->{"for"} and $pol->{"for"} ne "ALL"
!ll&&!rl&&r
010
MC/DC0%0/2
$pol->{"for"} and $pol->{"for"} ne "ALL"
$pol->{"for"} $pol->{"for"} ne "ALL"
Truth table33%1/3
A and B
A= $pol->{"for"}B= $pol->{"for"} ne "ALL"
ABresult
0X0
100
111
255 1 ▶ if ($pol->{roles} && @{ $pol->{roles} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pol->{"roles"} and @{$pol->{"roles"};}) 1 0 1
Condition33%1/3
$pol->{"roles"} and @{$pol->{"roles"};}
!ll&&!rl&&r
001
MC/DC0%0/2
$pol->{"roles"} and @{$pol->{"roles"};}
$pol->{"roles"} @{$pol->{"roles"};}
Truth table33%1/3
A and B
A= $pol->{"roles"}B= @{$pol->{"roles"};}
ABresult
0X0
100
111
256 1 $sql .= sprintf ' TO %s', join(', ', @{ $pol->{roles} });
257 }
258 1 ▶ $sql .= sprintf ' USING (%s)', $pol->{using} if $pol->{using};
Branch: 1 branch
BranchTrueFalseTotal
if $pol->{"using"} 1 0 1
259 1 ▶ $sql .= sprintf ' WITH CHECK (%s)', $pol->{with_check} if $pol->{with_check};
Branch: 1 branch
BranchTrueFalseTotal
if $pol->{"with_check"} 1 0 1
260 1 push @stmts, "$sql;";
261 }
262 }
263 }
264  
265 # CHECK constraints
266 4 ▶ if ($result_class->can('pg_check_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_check_constraints")) 3 1 4
267 3 my $checks = $result_class->pg_check_constraints;
268 3 for my $name (sort keys %$checks) {
269 0 my $entry = $checks->{$name};
270 0 my $expr = $entry->{definition};
271 0 ▶ $expr = "CHECK ($expr)" unless $expr =~ /^\s*CHECK\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr =~ /^\s*CHECK\b/i 0 0 0
272 0 push @stmts, sprintf 'ALTER TABLE %s ADD CONSTRAINT %s %s;',
273 $qualified, _quote_ident($name), $expr;
274 }
275 }
276  
277 # Raw trailing DDL (e.g. cross-table FKs that DBIO::PostgreSQL::DDL does
278 # not auto-generate from belongs_to)
279 4 ▶ if ($result_class->can('pg_extra_ddl')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_extra_ddl")) 3 1 4
280 3 my $extra = $result_class->pg_extra_ddl;
281 3 for my $sql (@$extra) {
282 2 my $stmt = $sql;
283 2 $stmt =~ s/\s*;\s*\z//;
284 2 push @stmts, "$stmt;";
285 }
286 }
287 }
288  
289 # 5. Settings
290 4 my $settings = $schema->pg_settings;
291 4 ▶ if ($settings && %$settings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings and %$settings) 0 4 4
Condition66%2/3
$settings and %$settings
!ll&&!rl&&r
130
MC/DC0%0/2
$settings and %$settings
$settings %$settings
Truth table66%2/3
A and B
A= $settingsB= %$settings
ABresult
0X0
100
111
292 0 for my $key (sort keys %$settings) {
293 push @stmts, sprintf "ALTER DATABASE CURRENT SET %s = '%s';",
294 0 $key, $settings->{$key};
295 }
296 }
297  
298 # 6. Search path
299 4 my @search_path = $schema->pg_search_path;
300 4 ▶ if (@search_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@search_path) 3 1 4
301 3 push @stmts, sprintf "SET search_path TO %s;",
302 join(', ', @search_path);
303 }
304  
305 4 return join("\n\n", @stmts, @view_stmts) . "\n";
306 }
307  
308  
309 9 ▶sub _pg_column_type {
Subroutine _pg_column_type: called CC=9 SCAR=23.8
310 9 my ($info) = @_;
311 9 my $type = $info->{data_type};
312  
313 # Handle enum types
314 9 ▶ if ($type eq 'enum' && $info->{pg_enum_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum" and $info->{"pg_enum_type"}) 2 7 9
Condition66%2/3
$type eq "enum" and $info->{"pg_enum_type"}
!ll&&!rl&&r
702
MC/DC50%1/2
$type eq "enum" and $info->{"pg_enum_type"}
$type eq "enum" $info->{"pg_enum_type"}
Truth table66%2/3
A and B
A= $type eq "enum"B= $info->{"pg_enum_type"}
ABresult
0X0
100
111
315 2 return $info->{pg_enum_type};
316 }
317  
318 # Handle composite types
319 7 ▶ if ($type eq 'composite' && $info->{pg_type_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "composite" and $info->{"pg_type_name"}) 0 7 7
Condition33%1/3
$type eq "composite" and $info->{"pg_type_name"}
!ll&&!rl&&r
700
MC/DC0%0/2
$type eq "composite" and $info->{"pg_type_name"}
$type eq "composite" $info->{"pg_type_name"}
Truth table33%1/3
A and B
A= $type eq "composite"B= $info->{"pg_type_name"}
ABresult
0X0
100
111
320 0 return $info->{pg_type_name};
321 }
322  
323 # Handle arrays
324 7 ▶ return $type if $type =~ /\[\]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\[\]$/ 0 7 7
325  
326 # Handle parameterized types (varchar(N), numeric(P,S), vector(N))
327 7 ▶ return $type if $type =~ /\(.+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\(.+\)$/ 0 7 7
328  
329 # Portable base types: delegate to the adapter (single source of truth)
330 7 ▶ if (DBIO::Schema::Type::is_base_type(lc $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DBIO::Schema::Type::is_base_type(lc $type)) 3 4 7
331 3 my $canon = DBIO::Schema::Type::canonical_column('_', $info);
332 3 return $ADAPTER->to_native($canon);
333 }
334  
335 # Legacy PG-dialect aliases: pass through as their native PG names
336 4 my %type_map = (
337 bigint => 'bigint',
338 smallint => 'smallint',
339 serial => 'serial',
340 bigserial => 'bigserial',
341 varchar => 'character varying',
342 float => 'double precision',
343 real => 'real',
344 date => 'date',
345 'timestamp with time zone' => 'timestamp with time zone',
346 timestamptz => 'timestamp with time zone',
347 uuid => 'uuid',
348 json => 'json',
349 jsonb => 'jsonb',
350 bytea => 'bytea',
351 inet => 'inet',
352 cidr => 'cidr',
353 macaddr => 'macaddr',
354 tsvector => 'tsvector',
355 tsquery => 'tsquery',
356 xml => 'xml',
357 money => 'money',
358 interval => 'interval',
359 point => 'point',
360 line => 'line',
361 lseg => 'lseg',
362 box => 'box',
363 path => 'path',
364 polygon => 'polygon',
365 circle => 'circle',
366 );
367  
368 4 ▶ return $type_map{$type} // $type;
Condition33%1/3
$type_map{$type} // $type
l!l&&r!l&&!r
400
MC/DC0%0/2
$type_map{$type} // $type
$type_map{$type} $type
Truth table33%1/3
A // B
A= $type_map{$type}B= $type
ABresult
1X1
011
000
369 }
370  
371 6 1;
372  
373 __END__
374  
375 =pod
376  
377 =encoding UTF-8
378  
379 =head1 NAME
380  
381 DBIO::PostgreSQL::DDL - Generate PostgreSQL DDL from DBIO schema classes
382  
383 =head1 VERSION
384  
385 version 0.900001
386  
387 =head1 DESCRIPTION
388  
389 C<DBIO::PostgreSQL::DDL> generates PostgreSQL DDL statements directly from
390 DBIO schema and result classes, without going through SQL::Translator. This
391 produces correct, PostgreSQL-native SQL that preserves all PostgreSQL-specific
392 features.
393  
394 The generated DDL is used by L<DBIO::PostgreSQL::Deploy/install> for fresh
395 installs and by the test-deploy side of L<DBIO::PostgreSQL::Deploy/diff> for
396 upgrade diffing. Reach for this class directly when you want the raw SQL text;
397 use L<DBIO::PostgreSQL::Deploy> when you want database orchestration.
398  
399 =head1 METHODS
400  
401 =head2 install_ddl
402  
403 my $sql = DBIO::PostgreSQL::DDL->install_ddl($schema);
404  
405 Generates a complete PostgreSQL DDL script for the connected schema object.
406 The script is ordered to satisfy dependencies:
407  
408 =over 4
409  
410 =item 1. C<CREATE EXTENSION IF NOT EXISTS> statements
411  
412 =item 2. C<CREATE SCHEMA IF NOT EXISTS> statements (skipping C<public>)
413  
414 =item 3. Enum types, composite types, and functions from L<DBIO::PostgreSQL::PgSchema> subclasses
415  
416 =item 4. C<CREATE TABLE> statements with columns, primary keys, indexes, triggers, and RLS from L<DBIO::PostgreSQL::Result> classes
417  
418 =item 5. C<ALTER DATABASE CURRENT SET> for C<pg_settings>
419  
420 =item 6. C<SET search_path TO> from C<pg_search_path>
421  
422 =back
423  
424 Returns the DDL as a single string with statements separated by blank lines.
425  
426 =seealso
427  
428 =over 4
429  
430 =item * L<DBIO::PostgreSQL> - schema component that calls C<pg_install_ddl>
431  
432 =item * L<DBIO::PostgreSQL::Deploy> - uses C<install_ddl> for fresh installs and diff
433  
434 =item * L<DBIO::PostgreSQL::PgSchema> - source of enum, type, and function definitions
435  
436 =item * L<DBIO::PostgreSQL::Result> - source of table, index, trigger, and RLS definitions
437  
438 =back
439  
440 =head1 AUTHOR
441  
442 DBIO & DBIx::Class Authors
443  
444 =head1 COPYRIGHT AND LICENSE
445  
446 Copyright (C) 2026 DBIO Authors
447 Portions Copyright (C) 2005-2025 DBIx::Class Authors
448 Based on DBIx::Class, heavily modified.
449  
450 This is free software; you can redistribute it and/or modify it under
451 the same terms as the Perl 5 programming language system itself.
452  
453 =cut
↑ Summary PostgreSQL/Diff.pm »