| 1 | package DBIO::PostgreSQL; | ||||||||||
| 2 | # ABSTRACT: PostgreSQL-specific schema management for DBIO | ||||||||||
| 3 | 5 | our $VERSION = '0.900001'; | |||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 5 | ▶ | use base 'DBIO::Base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 5 | __PACKAGE__->mk_classdata('_pg_schema_classes' => {}); | |||||||||
| 11 | 5 | __PACKAGE__->mk_classdata('_pg_extensions' => []); | |||||||||
| 12 | 5 | __PACKAGE__->mk_classdata('_pg_search_path' => ['public']); | |||||||||
| 13 | 5 | __PACKAGE__->mk_classdata('_pg_settings' => {}); | |||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | 8 | ▶ | sub pg_schemas { | ||||||||
|
Subroutine
pg_schemas: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 17 | 8 | my $class = shift; | |||||||||
| 18 | 8 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 2 | my @schemas = @_; | |||||||||
| 20 | $class->_pg_schema_classes({ | ||||||||||
| 21 | 2 | map { $_ => undef } @schemas | |||||||||
| 22 | }); | ||||||||||
| 23 | } | ||||||||||
| 24 | 8 | return keys %{ $class->_pg_schema_classes }; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 2 | ▶ | sub pg_schema_class { | ||||||||
|
Subroutine
pg_schema_class: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 29 | 2 | my ($class, $name, $pg_schema_class) = @_; | |||||||||
| 30 | 2 | my $classes = { %{ $class->_pg_schema_classes } }; | |||||||||
| 31 | 2 | ▶ | if ($pg_schema_class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | $classes->{$name} = $pg_schema_class; | |||||||||
| 33 | 0 | $class->_pg_schema_classes($classes); | |||||||||
| 34 | } | ||||||||||
| 35 | 2 | return $classes->{$name}; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 3 | ▶ | sub pg_extensions { | ||||||||
|
Subroutine
pg_extensions: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 40 | 3 | my $class = shift; | |||||||||
| 41 | 3 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | $class->_pg_extensions([@_]); | |||||||||
| 43 | } | ||||||||||
| 44 | 3 | return @{ $class->_pg_extensions }; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | sub pg_search_path { | ||||||||
|
Subroutine
pg_search_path: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 49 | 3 | my $class = shift; | |||||||||
| 50 | 3 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | $class->_pg_search_path([@_]); | |||||||||
| 52 | } | ||||||||||
| 53 | 3 | return @{ $class->_pg_search_path }; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | 3 | ▶ | sub pg_settings { | ||||||||
|
Subroutine
pg_settings: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 58 | 3 | my $class = shift; | |||||||||
| 59 | 3 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | $class->_pg_settings($_[0]); | |||||||||
| 61 | } | ||||||||||
| 62 | 3 | return $class->_pg_settings; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | sub connection { | ||||||||
|
Subroutine
connection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 1 | my ($self, @info) = @_; | |||||||||
| 68 | 1 | $self->storage_type('+DBIO::PostgreSQL::Storage'); | |||||||||
| 69 | 1 | return $self->next::method(@info); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub pg_deploy { | ||||||||
|
Subroutine
pg_deploy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | my ($self, %args) = @_; | |||||||||
| 75 | 0 | require DBIO::PostgreSQL::Deploy; | |||||||||
| 76 | 0 | return DBIO::PostgreSQL::Deploy->new( | |||||||||
| 77 | schema => $self, | ||||||||||
| 78 | %args, | ||||||||||
| 79 | ); | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub pg_install_ddl { | ||||||||
|
Subroutine
pg_install_ddl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 84 | 0 | my ($self) = @_; | |||||||||
| 85 | 0 | require DBIO::PostgreSQL::DDL; | |||||||||
| 86 | 0 | return DBIO::PostgreSQL::DDL->install_ddl($self); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 5 | 1; | |||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||
| 92 | |||||||||||
| 93 | =pod | ||||||||||
| 94 | |||||||||||
| 95 | =encoding UTF-8 | ||||||||||
| 96 | |||||||||||
| 97 | =head1 NAME | ||||||||||
| 98 | |||||||||||
| 99 | DBIO::PostgreSQL - PostgreSQL-specific schema management for DBIO | ||||||||||
| 100 | |||||||||||
| 101 | =head1 VERSION | ||||||||||
| 102 | |||||||||||
| 103 | version 0.900001 | ||||||||||
| 104 | |||||||||||
| 105 | =head1 SYNOPSIS | ||||||||||
| 106 | |||||||||||
| 107 | =head2 Schema class | ||||||||||
| 108 | |||||||||||
| 109 | package MyApp::Schema; | ||||||||||
| 110 | use DBIO 'Schema'; | ||||||||||
| 111 | __PACKAGE__->load_components('PostgreSQL'); | ||||||||||
| 112 | |||||||||||
| 113 | __PACKAGE__->pg_schemas(qw( public auth )); | ||||||||||
| 114 | |||||||||||
| 115 | Or use the C<-pg> shortcut, which pins | ||||||||||
| 116 | C<+DBIO::PostgreSQL::Storage> as the storage type directly: | ||||||||||
| 117 | |||||||||||
| 118 | package MyApp::Schema; | ||||||||||
| 119 | use DBIO Schema => -pg; | ||||||||||
| 120 | |||||||||||
| 121 | =head2 Result classes | ||||||||||
| 122 | |||||||||||
| 123 | package MyApp::Schema::Result::Artist; | ||||||||||
| 124 | use DBIO; | ||||||||||
| 125 | |||||||||||
| 126 | __PACKAGE__->table('artist'); | ||||||||||
| 127 | __PACKAGE__->add_columns( | ||||||||||
| 128 | id => { data_type => 'integer', is_auto_increment => 1 }, | ||||||||||
| 129 | name => { data_type => 'varchar', size => 100 }, | ||||||||||
| 130 | ); | ||||||||||
| 131 | __PACKAGE__->set_primary_key('id'); | ||||||||||
| 132 | |||||||||||
| 133 | For result classes with PostgreSQL-native features, use C<-pg> to load | ||||||||||
| 134 | L<DBIO::PostgreSQL::Result>: | ||||||||||
| 135 | |||||||||||
| 136 | package MyApp::Schema::Result::User; | ||||||||||
| 137 | use DBIO -pg; | ||||||||||
| 138 | |||||||||||
| 139 | __PACKAGE__->pg_schema('auth'); | ||||||||||
| 140 | __PACKAGE__->table('users'); | ||||||||||
| 141 | __PACKAGE__->add_columns( | ||||||||||
| 142 | id => { data_type => 'uuid', default_value => \'gen_random_uuid()' }, | ||||||||||
| 143 | name => { data_type => 'varchar', size => 100 }, | ||||||||||
| 144 | tags => { data_type => 'text[]' }, | ||||||||||
| 145 | ); | ||||||||||
| 146 | __PACKAGE__->set_primary_key('id'); | ||||||||||
| 147 | __PACKAGE__->pg_index('idx_users_name' => { columns => ['name'] }); | ||||||||||
| 148 | __PACKAGE__->pg_index('idx_users_tags' => { using => 'gin', columns => ['tags'] }); | ||||||||||
| 149 | |||||||||||
| 150 | =head2 Connecting | ||||||||||
| 151 | |||||||||||
| 152 | my $schema = MyApp::Schema->connect($dsn, $user, $pass); | ||||||||||
| 153 | |||||||||||
| 154 | The classic C<use base> form is still supported: | ||||||||||
| 155 | |||||||||||
| 156 | package MyApp::Schema; | ||||||||||
| 157 | use base 'DBIO::Schema'; | ||||||||||
| 158 | __PACKAGE__->load_components('PostgreSQL'); | ||||||||||
| 159 | |||||||||||
| 160 | =head1 DESCRIPTION | ||||||||||
| 161 | |||||||||||
| 162 | L<DBIO::PostgreSQL> is the PostgreSQL driver component for DBIO. | ||||||||||
| 163 | |||||||||||
| 164 | When this component is loaded into a schema class, C<connection()> sets | ||||||||||
| 165 | L<DBIO::Schema/storage_type> to C<+DBIO::PostgreSQL::Storage>, which enables | ||||||||||
| 166 | PostgreSQL-specific storage behavior automatically. | ||||||||||
| 167 | |||||||||||
| 168 | This distribution also provides PostgreSQL-native DDL/deploy helpers and | ||||||||||
| 169 | introspection/diff tooling. | ||||||||||
| 170 | |||||||||||
| 171 | =head1 METHODS | ||||||||||
| 172 | |||||||||||
| 173 | =head2 pg_schemas | ||||||||||
| 174 | |||||||||||
| 175 | Get or set the list of PostgreSQL schema names tracked by this schema class. | ||||||||||
| 176 | |||||||||||
| 177 | =head2 pg_schema_class | ||||||||||
| 178 | |||||||||||
| 179 | Get or set the class mapped to a specific PostgreSQL schema name. | ||||||||||
| 180 | |||||||||||
| 181 | =head2 pg_extensions | ||||||||||
| 182 | |||||||||||
| 183 | Get or set PostgreSQL extensions to include during deploy/DDL operations. | ||||||||||
| 184 | |||||||||||
| 185 | =head2 pg_search_path | ||||||||||
| 186 | |||||||||||
| 187 | Get or set the default PostgreSQL C<search_path> list. | ||||||||||
| 188 | |||||||||||
| 189 | =head2 pg_settings | ||||||||||
| 190 | |||||||||||
| 191 | Get or set additional PostgreSQL settings stored on the schema class. | ||||||||||
| 192 | |||||||||||
| 193 | =head2 connection | ||||||||||
| 194 | |||||||||||
| 195 | Overrides L<DBIO/connection> to force | ||||||||||
| 196 | C<+DBIO::PostgreSQL::Storage> as C<storage_type>. | ||||||||||
| 197 | |||||||||||
| 198 | =head2 pg_deploy | ||||||||||
| 199 | |||||||||||
| 200 | Returns a L<DBIO::PostgreSQL::Deploy> instance for the schema. | ||||||||||
| 201 | |||||||||||
| 202 | =head2 pg_install_ddl | ||||||||||
| 203 | |||||||||||
| 204 | Generates PostgreSQL-native DDL statements for the schema. | ||||||||||
| 205 | |||||||||||
| 206 | =head1 MIGRATION NOTES | ||||||||||
| 207 | |||||||||||
| 208 | The PostgreSQL storage class was split out of the historical DBIx::Class | ||||||||||
| 209 | monolithic distribution: | ||||||||||
| 210 | |||||||||||
| 211 | =over 4 | ||||||||||
| 212 | |||||||||||
| 213 | =item * | ||||||||||
| 214 | |||||||||||
| 215 | Old: C<DBIx::Class::Storage::DBI::Pg> | ||||||||||
| 216 | |||||||||||
| 217 | =item * | ||||||||||
| 218 | |||||||||||
| 219 | New: C<DBIO::PostgreSQL::Storage> | ||||||||||
| 220 | |||||||||||
| 221 | =back | ||||||||||
| 222 | |||||||||||
| 223 | If C<DBIO-PostgreSQL> is installed, core L<DBIO::Storage::DBI> can autodetect | ||||||||||
| 224 | PostgreSQL DSNs and load the new storage class via the driver registry. | ||||||||||
| 225 | |||||||||||
| 226 | =head1 TESTING | ||||||||||
| 227 | |||||||||||
| 228 | Integration tests in this distribution use: | ||||||||||
| 229 | |||||||||||
| 230 | DBIO_TEST_PG_DSN | ||||||||||
| 231 | DBIO_TEST_PG_USER | ||||||||||
| 232 | DBIO_TEST_PG_PASS | ||||||||||
| 233 | |||||||||||
| 234 | SQLMaker-focused tests can run offline via L<DBIO::Test> with: | ||||||||||
| 235 | |||||||||||
| 236 | storage_type => 'DBIO::PostgreSQL::Storage' | ||||||||||
| 237 | |||||||||||
| 238 | Replicated-path tests can reuse the same harness with: | ||||||||||
| 239 | |||||||||||
| 240 | replicated => 1, | ||||||||||
| 241 | storage_type => 'DBIO::PostgreSQL::Storage' | ||||||||||
| 242 | |||||||||||
| 243 | =head1 AUTHOR | ||||||||||
| 244 | |||||||||||
| 245 | DBIO & DBIx::Class Authors | ||||||||||
| 246 | |||||||||||
| 247 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 248 | |||||||||||
| 249 | Copyright (C) 2026 DBIO Authors | ||||||||||
| 250 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||
| 251 | Based on DBIx::Class, heavily modified. | ||||||||||
| 252 | |||||||||||
| 253 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 254 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 255 | |||||||||||
| 256 | =cut | ||||||||||