PostgreSQL/Deploy.pm

filter: Statementstmt 54.3% 25 / 46 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 53.3% 8 / 15 Podpod 100% 1 / 1 totaltotal 50.0% 34 / 68 CC 4 SCAR 10.6
CC
4
Cov
48%
_new_introspect
6.0
_create_temp_db
6.0
_drop_temp_db
6.0
CRAP 2.9
« PostgreSQL/SQLMaker.pm ↑ Summary PostgreSQL/Diff/Schema.pm »
1 package DBIO::PostgreSQL::Deploy;
2 # ABSTRACT: Deploy and upgrade PostgreSQL schemas via test-deploy-and-compare
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base 'DBIO::Deploy::Base::TempDatabase';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DBIO::PostgreSQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DBIO::PostgreSQL::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # --- class-name hooks for DBIO::Deploy::Base -------------------------------
17  
18 0 ▶sub _ddl_class { 'DBIO::PostgreSQL::DDL' }
Subroutine _ddl_class: not called CC=1 SCAR=6.9
19 0 ▶sub _introspect_class { 'DBIO::PostgreSQL::Introspect' }
Subroutine _introspect_class: not called CC=1 SCAR=6.9
20 0 ▶sub _diff_class { 'DBIO::PostgreSQL::Diff' }
Subroutine _diff_class: not called CC=1 SCAR=6.9
21  
22  
23 0 ▶sub install_schema {
Subroutine install_schema: not called CC=1 SCAR=6.9
Pod documented
24 0 my ($self, $schema_name) = @_;
25 0 my $dbh = $self->_dbh;
26 0 $dbh->do(sprintf 'CREATE SCHEMA IF NOT EXISTS %s', _quote_ident($schema_name));
27 0 return 1;
28 }
29  
30 # --- PostgreSQL-specific overrides -----------------------------------------
31  
32  
33 0 ▶sub _new_introspect {
Subroutine _new_introspect: not called CC=2 SCAR=17.9
34 0 my ($self, $dbh) = @_;
35 0 my @schemas = $self->schema->pg_schemas;
36 0 ▶ my $filter = @schemas ? \@schemas : undef;
Branch: 1 branch
BranchTrueFalseTotal
@schemas ? : 0 0 0
37 0 return $self->_introspect_class->new(
38 dbh => $dbh,
39 schema_filter => $filter,
40 );
41 }
42  
43  
44 0 ▶sub _create_temp_db {
Subroutine _create_temp_db: not called CC=2 SCAR=17.9
45 0 my ($self, $dbh) = @_;
46 0 my $name = $self->temp_db_prefix . $$ . '_' . time();
47 0 ▶ $dbh->do("COMMIT") if $dbh->{AutoCommit} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"AutoCommit"} == 0 0 0 0
48 0 local $dbh->{AutoCommit} = 1;
49 0 $dbh->do(sprintf 'CREATE DATABASE %s', _quote_ident($name));
50 0 return $name;
51 }
52  
53  
54 0 ▶sub _drop_temp_db {
Subroutine _drop_temp_db: not called CC=2 SCAR=17.9
55 0 my ($self, $dbh, $name) = @_;
56 0 ▶ $dbh->do("COMMIT") if $dbh->{AutoCommit} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"AutoCommit"} == 0 0 0 0
57 0 local $dbh->{AutoCommit} = 1;
58 0 $dbh->do(sprintf 'DROP DATABASE IF EXISTS %s', _quote_ident($name));
59 }
60  
61 3 1;
62  
63 __END__
64  
65 =pod
66  
67 =encoding UTF-8
68  
69 =head1 NAME
70  
71 DBIO::PostgreSQL::Deploy - Deploy and upgrade PostgreSQL schemas via test-deploy-and-compare
72  
73 =head1 VERSION
74  
75 version 0.900001
76  
77 =head1 DESCRIPTION
78  
79 C<DBIO::PostgreSQL::Deploy> orchestrates the deployment and upgrade of
80 PostgreSQL schemas using a test-deploy-and-compare strategy. The shared
81 orchestration (L<install|/install>, L<diff|/diff>, L<apply|/apply>,
82 L<upgrade|/upgrade>) and the temporary-database dance are inherited from
83 L<DBIO::Deploy::Base::TempDatabase>; this class supplies only the genuinely
84 PostgreSQL-specific seams (the C<CREATE DATABASE> / C<DROP DATABASE>
85 dialect and the C<schema_filter> passed to the introspector).
86  
87 For upgrades, the strategy is:
88  
89 =over 4
90  
91 =item 1. Introspect the live database via C<pg_catalog>
92  
93 =item 2. Create a temporary database
94  
95 =item 3. Deploy the desired schema (from DBIO classes) into the temp database
96  
97 =item 4. Introspect the temp database via C<pg_catalog>
98  
99 =item 5. Compute the diff between the two models using L<DBIO::PostgreSQL::Diff>
100  
101 =item 6. Drop the temp database
102  
103 =back
104  
105 This means PostgreSQL is comparing with itself — the diff is always accurate
106 regardless of how complex the schema features are.
107  
108 my $deploy = DBIO::PostgreSQL::Deploy->new(
109 schema => MyApp::DB->connect($dsn),
110 );
111  
112 # Fresh install
113 $deploy->install;
114  
115 # Upgrade (test-deploy + compare + apply)
116 $deploy->upgrade;
117  
118 # Or in steps:
119 my $diff = $deploy->diff;
120 print $diff->summary;
121 $deploy->apply($diff) if $diff->has_changes;
122  
123 =head1 METHODS
124  
125 =head2 install_schema
126  
127 $deploy->install_schema('tenant_42');
128  
129 Creates a single PostgreSQL schema (namespace) using C<CREATE SCHEMA IF NOT
130 EXISTS>. Useful for multi-tenant setups where each tenant gets its own
131 schema.
132  
133 =head2 _new_introspect
134  
135 my $intro = $self->_new_introspect($dbh);
136  
137 Factory for the introspector. Override in a subclass to use a custom
138 L<DBIO::PostgreSQL::Introspect> subclass (e.g. L<DBIO::PostgreSQL::PostGIS::Introspect>).
139  
140 The base class only passes C<dbh>; here we also forward the C<schema_filter>
141 derived from the connected schema's C<pg_schemas> so introspection stays
142 scoped to the schemas this driver manages.
143  
144 =head2 _create_temp_db
145  
146 my $name = $self->_create_temp_db($dbh);
147  
148 PostgreSQL C<CREATE DATABASE> cannot run inside a transaction block, so the
149 dbh is forced into autocommit mode for the duration. Returns the new database
150 name (C<< temp_db_prefix . $$ . '_' . time() >>).
151  
152 =head2 _drop_temp_db
153  
154 $self->_drop_temp_db($dbh, $name);
155  
156 Drops the temporary database. Forcibly commits any open transaction first
157 (symmetric to L</_create_temp_db>).
158  
159 =over 4
160  
161 =item * L<DBIO::PostgreSQL> - schema component with C<pg_deploy> factory method
162  
163 =item * L<DBIO::PostgreSQL::DDL> - generates the DDL used by C<install> and C<diff>
164  
165 =item * L<DBIO::PostgreSQL::Introspect> - reads the live and temp database state
166  
167 =item * L<DBIO::PostgreSQL::Diff> - compares the two introspected models
168  
169 =item * L<DBIO::Deploy::Base::TempDatabase> - inherited orchestration
170  
171 =back
172  
173 =head1 AUTHOR
174  
175 DBIO & DBIx::Class Authors
176  
177 =head1 COPYRIGHT AND LICENSE
178  
179 Copyright (C) 2026 DBIO Authors
180 Portions Copyright (C) 2005-2025 DBIx::Class Authors
181 Based on DBIx::Class, heavily modified.
182  
183 This is free software; you can redistribute it and/or modify it under
184 the same terms as the Perl 5 programming language system itself.
185  
186 =cut
« PostgreSQL/SQLMaker.pm ↑ Summary PostgreSQL/Diff/Schema.pm »