Skip to content

Commit 04c850f

Browse files
committed
Support extension schemas
1 parent e51d070 commit 04c850f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,10 @@ Manages a PostgreSQL extension.
11921192

11931193
Specifies the database on which to activate the extension.
11941194

1195+
##### `schema`
1196+
1197+
Specifies the schema on which to activate the extension.
1198+
11951199
##### `ensure`
11961200

11971201
Specifies whether to activate or deactivate the extension.

manifests/server/extension.pp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
define postgresql::server::extension (
33
$database,
44
$extension = $name,
5+
Optional[String[1]] $schema = undef,
56
Optional[String[1]] $version = undef,
67
String[1] $ensure = 'present',
78
$package_name = undef,
@@ -51,6 +52,33 @@
5152
unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')",
5253
}
5354

55+
if $ensure == 'present' and $schema {
56+
$set_schema_command = "ALTER EXTENSION \"${extension}\" SET SCHEMA \"${schema}\""
57+
58+
postgresql_psql { "${database}: ${set_schema_command}":
59+
command => $set_schema_command,
60+
unless => @("END")
61+
SELECT 1
62+
WHERE EXISTS (
63+
SELECT 1
64+
FROM pg_extension e
65+
JOIN pg_namespace n ON e.extnamespace = n.oid
66+
WHERE e.extname = '${extension}' AND
67+
n.nspname = '${schema}'
68+
)
69+
|-END
70+
,
71+
psql_user => $user,
72+
psql_group => $group,
73+
psql_path => $psql_path,
74+
connect_settings => $connect_settings,
75+
db => $database,
76+
require => Postgresql_psql["${database}: ${command}"],
77+
}
78+
79+
Postgresql::Server::Schema <| db == $database and schema == $schema |> -> Postgresql_psql["${database}: ${set_schema_command}"]
80+
}
81+
5482
if $package_name {
5583
$_package_ensure = $package_ensure ? {
5684
undef => $ensure,

spec/unit/defines/server/extension_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
}
3535
end
3636

37+
context "when schema is specified" do
38+
let (:params) { super().merge({
39+
:schema => 'pg_catalog',
40+
}) }
41+
42+
it {
43+
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" SET SCHEMA "pg_catalog"')
44+
}
45+
end
46+
3747
context "when setting package name" do
3848
let (:params) { super().merge({
3949
:package_name => 'postgis',

0 commit comments

Comments
 (0)