Skip to content

Commit 475b82d

Browse files
committed
Support extension schemas
1 parent a5c71fd commit 475b82d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@
4040
end
4141
end
4242

43+
context "when schema is specified" do
44+
let (:params) { super().merge({
45+
:schema => 'pg_catalog',
46+
}) }
47+
48+
it {
49+
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" SET SCHEMA "pg_catalog"')
50+
}
51+
52+
context "when schema is specified again" do
53+
let (:pre_condition) { super() + "
54+
postgresql::server::extension { 'postgis':
55+
database => 'template_postgis',
56+
schema => 'pg_catalog',
57+
}" }
58+
59+
it {
60+
is_expected.not_to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" SET SCHEMA "pg_catalog"')
61+
}
62+
end
63+
end
64+
4365
context "when setting package name" do
4466
let (:params) { super().merge({
4567
:package_name => 'postgis',

0 commit comments

Comments
 (0)