Skip to content

add parameter "version" to postgresql::server::extension to update the extension version #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,21 @@ Valid options: 'present' or 'absent'.

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

#### `version`

Specifies the version of the extension which the database uses.
When an extension package is updated, this does not automatically change the effective version in each database.

This needs be updated using the PostgreSQL-specific SQL `ALTER EXTENSION...`

`version` may be set to `latest`, in which case the SQL `ALTER EXTENSION "extension" UPDATE` is applied to this database (only).

`version` may be set to a specific version, in which case the extension is updated using `ALTER EXTENSION "extension" UPDATE TO 'version'`

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.

`version` may be omitted, in which case no `ALTER EXTENSION...` SQL is applied, and the version will be left unchanged.

##### `package_name`

Specifies a package to install prior to activating the extension.
Expand Down
37 changes: 31 additions & 6 deletions manifests/server/extension.pp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Activate an extension on a postgresql database
define postgresql::server::extension (
$database,
$extension = $name,
String[1] $ensure = 'present',
$package_name = undef,
$package_ensure = undef,
$connect_settings = $postgresql::server::default_connect_settings,
$extension = $name,
Optional[String[1]] $version = undef,
String[1] $ensure = 'present',
$package_name = undef,
$package_ensure = undef,
$connect_settings = $postgresql::server::default_connect_settings,
) {
$user = $postgresql::server::user
$group = $postgresql::server::group
Expand Down Expand Up @@ -42,7 +43,10 @@
db => $database,
command => $command,
unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = '${extension}') as t WHERE t.count ${unless_comp} 1",
require => Postgresql::Server::Database[$database],
}

if($database != undef and defined(Postgresql::Server::Database[$database])) {
Postgresql::Server::Database[$database]->Postgresql_psql["Add ${extension} extension to ${database}"]
}

if $package_name {
Expand All @@ -58,4 +62,25 @@
before => $package_before,
})
}
if $version {
if $version == 'latest' {
$alter_extension_sql = "ALTER EXTENSION \"${extension}\" UPDATE"
$update_unless = "SELECT 1 FROM pg_available_extensions WHERE name = '${extension}' AND default_version = installed_version"
} else {
$alter_extension_sql = "ALTER EXTENSION \"${extension}\" UPDATE TO '${version}'"
$update_unless = "SELECT 1 FROM pg_extension WHERE extname='${extension}' AND extversion='${version}'"
}
postgresql_psql { "${database}: ${alter_extension_sql}":
db => $database,
psql_user => $user,
psql_group => $group,
psql_path => $psql_path,
connect_settings => $connect_settings,
command => $alter_extension_sql,
unless => $update_unless,
}
if($database != undef and defined(Postgresql::Server::Database[$database])) {
Postgresql::Server::Database[$database]->Postgresql_psql["${database}: ${alter_extension_sql}"]
}
}
}
30 changes: 30 additions & 0 deletions spec/unit/defines/server/extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@
}
end
end

context "when extension version is specified" do
let (:params) { super().merge({
:ensure => 'absent',
:package_name => 'postgis',
:version => '99.99.99',
}) }

it {
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" UPDATE TO \'99.99.99\'').with({
:db => 'template_postgis',
:unless => "SELECT 1 FROM pg_extension WHERE extname='postgis' AND extversion='99.99.99'",
}).that_requires('Postgresql::Server::Database[template_postgis]')
}
end

context "when extension version is latest" do
let (:params) { super().merge({
:ensure => 'absent',
:package_name => 'postgis',
:version => 'latest',
}) }

it {
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" UPDATE').with({
:db => 'template_postgis',
:unless => "SELECT 1 FROM pg_available_extensions WHERE name = 'postgis' AND default_version = installed_version",
}).that_requires('Postgresql::Server::Database[template_postgis]')
}
end
end

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