Skip to content

Support extension schemas #948

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 4 commits into from
Jan 30, 2018
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
70 changes: 37 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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.

Expand All @@ -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'.

Expand Down Expand Up @@ -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'.

Expand All @@ -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`.

Expand All @@ -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`.

Expand All @@ -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.

Expand All @@ -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'.

Expand All @@ -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.

Expand All @@ -1162,21 +1162,21 @@ 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**

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'.

Expand All @@ -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.
Expand Down Expand Up @@ -1483,7 +1487,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
Expand Down Expand Up @@ -1745,7 +1749,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'.

Expand Down Expand Up @@ -1799,7 +1803,7 @@ Default value: '', which generally uses the designated local Unix socket.

Defines the port to use when connecting.

Default value: ''
Default value: ''

##### `run_as`

Expand Down Expand Up @@ -1833,7 +1837,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

Expand Down
40 changes: 34 additions & 6 deletions manifests/server/extension.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,15 +16,15 @@
case $ensure {
'present': {
$command = "CREATE EXTENSION \"${extension}\""
$unless_comp = '='
$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_comp = '!='
$package_require = Postgresql_psql["Add ${extension} extension to ${database}"]
$unless_mod = 'NOT '
$package_require = Postgresql_psql["${database}: ${command}"]
$package_before = []
}

Expand All @@ -39,7 +40,7 @@
}
}

postgresql_psql {"Add ${extension} extension to ${database}":
postgresql_psql { "${database}: ${command}":

psql_user => $user,
psql_group => $group,
Expand All @@ -48,7 +49,34 @@

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 $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 {
Expand Down
Loading