From 4b291070398b305cd4acda2fd625075b9b450c12 Mon Sep 17 00:00:00 2001 From: Emre Hasegeli Date: Wed, 10 Jan 2018 15:55:46 +0100 Subject: [PATCH 1/4] Simplify extension unless SQL --- manifests/server/extension.pp | 6 +++--- spec/unit/defines/server/extension_spec.rb | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/manifests/server/extension.pp b/manifests/server/extension.pp index 86f2f9df11..634b5c29a0 100644 --- a/manifests/server/extension.pp +++ b/manifests/server/extension.pp @@ -15,14 +15,14 @@ case $ensure { 'present': { $command = "CREATE EXTENSION \"${extension}\"" - $unless_comp = '=' + $unless_mod = '' $package_require = [] $package_before = Postgresql_psql["Add ${extension} extension to ${database}"] } 'absent': { $command = "DROP EXTENSION \"${extension}\"" - $unless_comp = '!=' + $unless_mod = 'NOT ' $package_require = Postgresql_psql["Add ${extension} extension to ${database}"] $package_before = [] } @@ -48,7 +48,7 @@ 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", + unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')", } if $package_name { diff --git a/spec/unit/defines/server/extension_spec.rb b/spec/unit/defines/server/extension_spec.rb index 6ce5de665a..6652496f55 100644 --- a/spec/unit/defines/server/extension_spec.rb +++ b/spec/unit/defines/server/extension_spec.rb @@ -30,7 +30,6 @@ is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ :db => 'template_postgis', :command => 'CREATE EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count = 1", }).that_requires('Postgresql::Server::Database[template_postgis]') } end @@ -58,7 +57,6 @@ is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ :db => 'template_postgis', :command => 'DROP EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count != 1", }).that_requires('Postgresql::Server::Database[template_postgis]') } @@ -78,7 +76,6 @@ is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ :db => 'template_postgis', :command => 'DROP EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count != 1", }).that_requires('Postgresql::Server::Database[template_postgis]') } @@ -153,7 +150,6 @@ is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis2').with({ :db => 'template_postgis2', :command => 'CREATE EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count = 1", }).that_requires('Postgresql::Server::Database[template_postgis2]') } end From 01d021178e2e9262d1d11f6f5859fa202a43eb36 Mon Sep 17 00:00:00 2001 From: Emre Hasegeli Date: Wed, 10 Jan 2018 16:02:03 +0100 Subject: [PATCH 2/4] Standardize extension command title --- manifests/server/extension.pp | 6 +++--- spec/unit/defines/server/extension_spec.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifests/server/extension.pp b/manifests/server/extension.pp index 634b5c29a0..773530ce27 100644 --- a/manifests/server/extension.pp +++ b/manifests/server/extension.pp @@ -17,13 +17,13 @@ $command = "CREATE EXTENSION \"${extension}\"" $unless_mod = '' $package_require = [] - $package_before = Postgresql_psql["Add ${extension} extension to ${database}"] + $package_before = Postgresql_psql["${database}: ${command}"] } 'absent': { $command = "DROP EXTENSION \"${extension}\"" $unless_mod = 'NOT ' - $package_require = Postgresql_psql["Add ${extension} extension to ${database}"] + $package_require = Postgresql_psql["${database}: ${command}"] $package_before = [] } @@ -39,7 +39,7 @@ } } - postgresql_psql {"Add ${extension} extension to ${database}": + postgresql_psql { "${database}: ${command}": psql_user => $user, psql_group => $group, diff --git a/spec/unit/defines/server/extension_spec.rb b/spec/unit/defines/server/extension_spec.rb index 6652496f55..548005768b 100644 --- a/spec/unit/defines/server/extension_spec.rb +++ b/spec/unit/defines/server/extension_spec.rb @@ -27,7 +27,7 @@ context "with mandatory arguments only" do it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ + is_expected.to contain_postgresql_psql('template_postgis: CREATE EXTENSION "postgis"').with({ :db => 'template_postgis', :command => 'CREATE EXTENSION "postgis"', }).that_requires('Postgresql::Server::Database[template_postgis]') @@ -43,7 +43,7 @@ is_expected.to contain_package('postgis').with({ :ensure => 'present', :name => 'postgis', - }).that_comes_before('Postgresql_psql[Add postgis extension to template_postgis]') + }).that_comes_before('Postgresql_psql[template_postgis: CREATE EXTENSION "postgis"]') } end @@ -54,7 +54,7 @@ }) } it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ + is_expected.to contain_postgresql_psql('template_postgis: DROP EXTENSION "postgis"').with({ :db => 'template_postgis', :command => 'DROP EXTENSION "postgis"', }).that_requires('Postgresql::Server::Database[template_postgis]') @@ -73,7 +73,7 @@ }) } it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ + is_expected.to contain_postgresql_psql('template_postgis: DROP EXTENSION "postgis"').with({ :db => 'template_postgis', :command => 'DROP EXTENSION "postgis"', }).that_requires('Postgresql::Server::Database[template_postgis]') @@ -83,7 +83,7 @@ is_expected.to contain_package('postgis').with({ :ensure => 'present', :name => 'postgis', - }).that_requires('Postgresql_psql[Add postgis extension to template_postgis]') + }).that_requires('Postgresql_psql[template_postgis: DROP EXTENSION "postgis"]') } end end @@ -147,7 +147,7 @@ context "with mandatory arguments only" do it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis2').with({ + is_expected.to contain_postgresql_psql('template_postgis2: CREATE EXTENSION "postgis"').with({ :db => 'template_postgis2', :command => 'CREATE EXTENSION "postgis"', }).that_requires('Postgresql::Server::Database[template_postgis2]') From e51d0709004101583f9648b024d183bcd479b7c8 Mon Sep 17 00:00:00 2001 From: Emre Hasegeli Date: Wed, 10 Jan 2018 16:10:41 +0100 Subject: [PATCH 3/4] Trim trailing whitespaces on README --- README.md | 66 +++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 47345c619d..6d38ac483d 100644 --- a/README.md +++ b/README.md @@ -666,7 +666,7 @@ Installs the packages containing the development libraries for PostgreSQL and sy ##### `link_pg_config` -If the bin directory used by the PostgreSQL page is not `/usr/bin` or `/usr/local/bin`, symlinks `pg_config` from the package's bin dir into `usr/bin` (not applicable to Debian systems). Set to `false` to disable this behavior. +If the bin directory used by the PostgreSQL page is not `/usr/bin` or `/usr/local/bin`, symlinks `pg_config` from the package's bin dir into `usr/bin` (not applicable to Debian systems). Set to `false` to disable this behavior. Valid values: `true`, `false`. @@ -912,7 +912,7 @@ Default value: OS dependent. ##### `port` -Specifies the port for the PostgreSQL server to listen on. **Note:** The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change. +Specifies the port for the PostgreSQL server to listen on. **Note:** The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change. Default value: 5432. Meaning the Postgres server listens on TCP port 5432. @@ -924,55 +924,55 @@ Default value: `undef`. ##### `postgresql_conf_path` -Specifies the path to your `postgresql.conf` file. +Specifies the path to your `postgresql.conf` file. Default value: '${confdir}/postgresql.conf'. ##### `psql_path` -Specifies the path to the `psql` command. +Specifies the path to the `psql` command. Default value: OS dependent. ##### `service_manage` -Defines whether or not Puppet should manage the service. +Defines whether or not Puppet should manage the service. Default value: `true`. ##### `service_name` -Overrides the default PostgreSQL service name. +Overrides the default PostgreSQL service name. Default value: OS dependent. ##### `service_provider` -Overrides the default PostgreSQL service provider. +Overrides the default PostgreSQL service provider. Default value: `undef`. ##### `service_reload` -Overrides the default reload command for your PostgreSQL service. +Overrides the default reload command for your PostgreSQL service. Default value: OS dependent. ##### `service_restart_on_change` -Overrides the default behavior to restart your PostgreSQL service when a config entry has been changed that requires a service restart to become active. +Overrides the default behavior to restart your PostgreSQL service when a config entry has been changed that requires a service restart to become active. Default value: `true`. ##### `service_status` -Overrides the default status check command for your PostgreSQL service. +Overrides the default status check command for your PostgreSQL service. Default value: OS dependent. ##### `user` -Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system. +Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system. Default value: 'postgres'. @@ -1020,7 +1020,7 @@ postgresql::server::config_entry { 'check_function_bodies': ##### `ensure` -Removes an entry if set to 'absent'. +Removes an entry if set to 'absent'. Valid values: 'present', 'absent'. @@ -1040,43 +1040,43 @@ Defines a comment to be stored about the database using the PostgreSQL COMMENT c ##### `connect_settings` -Specifies a hash of environment variables used when connecting to a remote server. +Specifies a hash of environment variables used when connecting to a remote server. Default value: Connects to the local Postgres instance. ##### `dbname` -Sets the name of the database to be created. +Sets the name of the database to be created. Default value: the namevar. ##### `encoding` -Overrides the character set during creation of the database. +Overrides the character set during creation of the database. Default value: The default defined during installation. ##### `grant` -Specifies the permissions to grant during creation. +Specifies the permissions to grant during creation. Default value: 'ALL'. ##### `istemplate` -Specifies that the database is a template, if set to `true`. +Specifies that the database is a template, if set to `true`. Default value: `false`. ##### `locale` -Overrides the locale during creation of the database. +Overrides the locale during creation of the database. Default value: The default defined during installation. ##### `owner` -Sets a user as the owner of the database. +Sets a user as the owner of the database. Default value: '$user' variable set in `postgresql::server` or `postgresql::globals`. @@ -1086,13 +1086,13 @@ Default value: '$user' variable set in `postgresql::server` or `postgresql::glob ##### `tablespace` -Defines the name of the tablespace to allocate the created database to. +Defines the name of the tablespace to allocate the created database to. Default value: PostgreSQL default. ##### `template` -Specifies the name of the template database from which to build this database. +Specifies the name of the template database from which to build this database. Defaults value: `template0`. @@ -1106,13 +1106,13 @@ Creates a database with no users and no permissions. ##### `dbname` -Sets the name of the database. +Sets the name of the database. Defaults value: The namevar. ##### `encoding` -Overrides the character set during creation of the database. +Overrides the character set during creation of the database. Default value: The default defined during installation. @@ -1136,13 +1136,13 @@ Default value: The '$user' variable set in `postgresql::server` or `postgresql:: ##### `tablespace` -Sets tablespace for where to create this database. +Sets tablespace for where to create this database. Default value: The default defined during installation. ##### `template` -Specifies the name of the template database from which to build this database. +Specifies the name of the template database from which to build this database. Default value: 'template0'. @@ -1152,7 +1152,7 @@ Manages grant-based access privileges for users, wrapping the `postgresql::serve #### `connect_settings` -Specifies a hash of environment variables used when connecting to a remote server. +Specifies a hash of environment variables used when connecting to a remote server. Default value: Connects to the local Postgres instance. @@ -1162,13 +1162,13 @@ Specifies the database to which you are granting access. ##### `privilege` -Specifies comma-separated list of privileges to grant. +Specifies comma-separated list of privileges to grant. Valid options: 'ALL', 'CREATE', 'CONNECT', 'TEMPORARY', 'TEMP'. ##### `psql_db` -Defines the database to execute the grant against. +Defines the database to execute the grant against. **This should not ordinarily be changed from the default** @@ -1176,7 +1176,7 @@ Default value: 'postgres'. ##### `psql_user` -Specifies the OS user for running `psql`. +Specifies the OS user for running `psql`. Default value: The default user for the module, usually 'postgres'. @@ -1483,7 +1483,7 @@ The parameters are grouped into these three sections: ##### `target` Provides the target for the rule, and is generally an internal only property. - + **Use with caution.** #### postgresql::server::role @@ -1745,7 +1745,7 @@ This is the namevar. Required. -Specifies the action to create or destroy named slot. +Specifies the action to create or destroy named slot. Valid values: 'present', 'absent'. @@ -1799,7 +1799,7 @@ Default value: '', which generally uses the designated local Unix socket. Defines the port to use when connecting. -Default value: '' +Default value: '' ##### `run_as` @@ -1833,7 +1833,7 @@ This internal function converts a list of `pg_hba.conf` based ACLs (passed in as ### Tasks -The Postgresql module has an example task that allows a user to execute arbitary SQL against a database. Please refer to to the [PE documentation](https://puppet.com/docs/pe/2017.3/orchestrator/running_tasks.html) or [Bolt documentation](https://puppet.com/docs/bolt/latest/bolt.html) on how to execute a task. +The Postgresql module has an example task that allows a user to execute arbitary SQL against a database. Please refer to to the [PE documentation](https://puppet.com/docs/pe/2017.3/orchestrator/running_tasks.html) or [Bolt documentation](https://puppet.com/docs/bolt/latest/bolt.html) on how to execute a task. ## Limitations From 04c850fdec578d07e2266a5b0cd3cc53f7a10d8c Mon Sep 17 00:00:00 2001 From: Emre Hasegeli Date: Wed, 10 Jan 2018 16:14:31 +0100 Subject: [PATCH 4/4] Support extension schemas --- README.md | 4 ++++ manifests/server/extension.pp | 28 ++++++++++++++++++++++ spec/unit/defines/server/extension_spec.rb | 10 ++++++++ 3 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 6d38ac483d..52b4fd5e4b 100644 --- a/README.md +++ b/README.md @@ -1192,6 +1192,10 @@ Manages a PostgreSQL extension. Specifies the database on which to activate the extension. +##### `schema` + +Specifies the schema on which to activate the extension. + ##### `ensure` Specifies whether to activate or deactivate the extension. diff --git a/manifests/server/extension.pp b/manifests/server/extension.pp index 773530ce27..9fb100c246 100644 --- a/manifests/server/extension.pp +++ b/manifests/server/extension.pp @@ -2,6 +2,7 @@ define postgresql::server::extension ( $database, $extension = $name, + Optional[String[1]] $schema = undef, Optional[String[1]] $version = undef, String[1] $ensure = 'present', $package_name = undef, @@ -51,6 +52,33 @@ unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')", } + if $ensure == 'present' and $schema { + $set_schema_command = "ALTER EXTENSION \"${extension}\" SET SCHEMA \"${schema}\"" + + postgresql_psql { "${database}: ${set_schema_command}": + command => $set_schema_command, + unless => @("END") + SELECT 1 + WHERE EXISTS ( + SELECT 1 + FROM pg_extension e + JOIN pg_namespace n ON e.extnamespace = n.oid + WHERE e.extname = '${extension}' AND + n.nspname = '${schema}' + ) + |-END + , + psql_user => $user, + psql_group => $group, + psql_path => $psql_path, + connect_settings => $connect_settings, + db => $database, + require => Postgresql_psql["${database}: ${command}"], + } + + Postgresql::Server::Schema <| db == $database and schema == $schema |> -> Postgresql_psql["${database}: ${set_schema_command}"] + } + if $package_name { $_package_ensure = $package_ensure ? { undef => $ensure, diff --git a/spec/unit/defines/server/extension_spec.rb b/spec/unit/defines/server/extension_spec.rb index 548005768b..efb4b8b59b 100644 --- a/spec/unit/defines/server/extension_spec.rb +++ b/spec/unit/defines/server/extension_spec.rb @@ -34,6 +34,16 @@ } end + context "when schema is specified" do + let (:params) { super().merge({ + :schema => 'pg_catalog', + }) } + + it { + is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" SET SCHEMA "pg_catalog"') + } + end + context "when setting package name" do let (:params) { super().merge({ :package_name => 'postgis',