Skip to content

Commit fe5f278

Browse files
authored
Merge pull request #896 from infoxchange/extension_upgrade
add parameter "version" to postgresql::server::extension to update the extension version
2 parents 193d058 + 50d510c commit fe5f278

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,21 @@ Valid options: 'present' or 'absent'.
11911191

11921192
Specifies the extension to activate. If left blank, uses the name of the resource.
11931193

1194+
#### `version`
1195+
1196+
Specifies the version of the extension which the database uses.
1197+
When an extension package is updated, this does not automatically change the effective version in each database.
1198+
1199+
This needs be updated using the PostgreSQL-specific SQL `ALTER EXTENSION...`
1200+
1201+
`version` may be set to `latest`, in which case the SQL `ALTER EXTENSION "extension" UPDATE` is applied to this database (only).
1202+
1203+
`version` may be set to a specific version, in which case the extension is updated using `ALTER EXTENSION "extension" UPDATE TO 'version'`
1204+
1205+
eg. If extension is set to `postgis` and version is set to `2.3.3`, this will apply the SQL `ALTER EXTENSION "postgis" UPDATE TO '2.3.3'` to this database only.
1206+
1207+
`version` may be omitted, in which case no `ALTER EXTENSION...` SQL is applied, and the version will be left unchanged.
1208+
11941209
##### `package_name`
11951210

11961211
Specifies a package to install prior to activating the extension.

manifests/server/extension.pp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Activate an extension on a postgresql database
22
define postgresql::server::extension (
33
$database,
4-
$extension = $name,
5-
String[1] $ensure = 'present',
6-
$package_name = undef,
7-
$package_ensure = undef,
8-
$connect_settings = $postgresql::server::default_connect_settings,
4+
$extension = $name,
5+
Optional[String[1]] $version = undef,
6+
String[1] $ensure = 'present',
7+
$package_name = undef,
8+
$package_ensure = undef,
9+
$connect_settings = $postgresql::server::default_connect_settings,
910
) {
1011
$user = $postgresql::server::user
1112
$group = $postgresql::server::group
@@ -42,7 +43,10 @@
4243
db => $database,
4344
command => $command,
4445
unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = '${extension}') as t WHERE t.count ${unless_comp} 1",
45-
require => Postgresql::Server::Database[$database],
46+
}
47+
48+
if($database != undef and defined(Postgresql::Server::Database[$database])) {
49+
Postgresql::Server::Database[$database]->Postgresql_psql["Add ${extension} extension to ${database}"]
4650
}
4751

4852
if $package_name {
@@ -58,4 +62,25 @@
5862
before => $package_before,
5963
})
6064
}
65+
if $version {
66+
if $version == 'latest' {
67+
$alter_extension_sql = "ALTER EXTENSION \"${extension}\" UPDATE"
68+
$update_unless = "SELECT 1 FROM pg_available_extensions WHERE name = '${extension}' AND default_version = installed_version"
69+
} else {
70+
$alter_extension_sql = "ALTER EXTENSION \"${extension}\" UPDATE TO '${version}'"
71+
$update_unless = "SELECT 1 FROM pg_extension WHERE extname='${extension}' AND extversion='${version}'"
72+
}
73+
postgresql_psql { "${database}: ${alter_extension_sql}":
74+
db => $database,
75+
psql_user => $user,
76+
psql_group => $group,
77+
psql_path => $psql_path,
78+
connect_settings => $connect_settings,
79+
command => $alter_extension_sql,
80+
unless => $update_unless,
81+
}
82+
if($database != undef and defined(Postgresql::Server::Database[$database])) {
83+
Postgresql::Server::Database[$database]->Postgresql_psql["${database}: ${alter_extension_sql}"]
84+
}
85+
}
6186
}

spec/unit/defines/server/extension_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@
9090
}
9191
end
9292
end
93+
94+
context "when extension version is specified" do
95+
let (:params) { super().merge({
96+
:ensure => 'absent',
97+
:package_name => 'postgis',
98+
:version => '99.99.99',
99+
}) }
100+
101+
it {
102+
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" UPDATE TO \'99.99.99\'').with({
103+
:db => 'template_postgis',
104+
:unless => "SELECT 1 FROM pg_extension WHERE extname='postgis' AND extversion='99.99.99'",
105+
}).that_requires('Postgresql::Server::Database[template_postgis]')
106+
}
107+
end
108+
109+
context "when extension version is latest" do
110+
let (:params) { super().merge({
111+
:ensure => 'absent',
112+
:package_name => 'postgis',
113+
:version => 'latest',
114+
}) }
115+
116+
it {
117+
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" UPDATE').with({
118+
:db => 'template_postgis',
119+
:unless => "SELECT 1 FROM pg_available_extensions WHERE name = 'postgis' AND default_version = installed_version",
120+
}).that_requires('Postgresql::Server::Database[template_postgis]')
121+
}
122+
end
93123
end
94124

95125
describe 'postgresql::server::extension', :type => :define do

0 commit comments

Comments
 (0)