Skip to content

Commit 85493de

Browse files
authored
Merge pull request #948 from hasegeli/extension_schema
Support extension schemas
2 parents 1705a35 + 04c850f commit 85493de

File tree

3 files changed

+87
-49
lines changed

3 files changed

+87
-49
lines changed

README.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ Installs the packages containing the development libraries for PostgreSQL and sy
666666

667667
##### `link_pg_config`
668668

669-
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.
669+
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.
670670

671671
Valid values: `true`, `false`.
672672

@@ -912,7 +912,7 @@ Default value: OS dependent.
912912

913913
##### `port`
914914

915-
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.
915+
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.
916916

917917
Default value: 5432. Meaning the Postgres server listens on TCP port 5432.
918918

@@ -924,55 +924,55 @@ Default value: `undef`.
924924

925925
##### `postgresql_conf_path`
926926

927-
Specifies the path to your `postgresql.conf` file.
927+
Specifies the path to your `postgresql.conf` file.
928928

929929
Default value: '${confdir}/postgresql.conf'.
930930

931931
##### `psql_path`
932932

933-
Specifies the path to the `psql` command.
933+
Specifies the path to the `psql` command.
934934

935935
Default value: OS dependent.
936936

937937
##### `service_manage`
938938

939-
Defines whether or not Puppet should manage the service.
939+
Defines whether or not Puppet should manage the service.
940940

941941
Default value: `true`.
942942

943943
##### `service_name`
944944

945-
Overrides the default PostgreSQL service name.
945+
Overrides the default PostgreSQL service name.
946946

947947
Default value: OS dependent.
948948

949949
##### `service_provider`
950950

951-
Overrides the default PostgreSQL service provider.
951+
Overrides the default PostgreSQL service provider.
952952

953953
Default value: `undef`.
954954

955955
##### `service_reload`
956956

957-
Overrides the default reload command for your PostgreSQL service.
957+
Overrides the default reload command for your PostgreSQL service.
958958

959959
Default value: OS dependent.
960960

961961
##### `service_restart_on_change`
962962

963-
Overrides the default behavior to restart your PostgreSQL service when a config entry has been changed that requires a service restart to become active.
963+
Overrides the default behavior to restart your PostgreSQL service when a config entry has been changed that requires a service restart to become active.
964964

965965
Default value: `true`.
966966

967967
##### `service_status`
968968

969-
Overrides the default status check command for your PostgreSQL service.
969+
Overrides the default status check command for your PostgreSQL service.
970970

971971
Default value: OS dependent.
972972

973973
##### `user`
974974

975-
Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
975+
Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
976976

977977
Default value: 'postgres'.
978978

@@ -1020,7 +1020,7 @@ postgresql::server::config_entry { 'check_function_bodies':
10201020

10211021
##### `ensure`
10221022

1023-
Removes an entry if set to 'absent'.
1023+
Removes an entry if set to 'absent'.
10241024

10251025
Valid values: 'present', 'absent'.
10261026

@@ -1040,43 +1040,43 @@ Defines a comment to be stored about the database using the PostgreSQL COMMENT c
10401040

10411041
##### `connect_settings`
10421042

1043-
Specifies a hash of environment variables used when connecting to a remote server.
1043+
Specifies a hash of environment variables used when connecting to a remote server.
10441044

10451045
Default value: Connects to the local Postgres instance.
10461046

10471047
##### `dbname`
10481048

1049-
Sets the name of the database to be created.
1049+
Sets the name of the database to be created.
10501050

10511051
Default value: the namevar.
10521052

10531053
##### `encoding`
10541054

1055-
Overrides the character set during creation of the database.
1055+
Overrides the character set during creation of the database.
10561056

10571057
Default value: The default defined during installation.
10581058

10591059
##### `grant`
10601060

1061-
Specifies the permissions to grant during creation.
1061+
Specifies the permissions to grant during creation.
10621062

10631063
Default value: 'ALL'.
10641064

10651065
##### `istemplate`
10661066

1067-
Specifies that the database is a template, if set to `true`.
1067+
Specifies that the database is a template, if set to `true`.
10681068

10691069
Default value: `false`.
10701070

10711071
##### `locale`
10721072

1073-
Overrides the locale during creation of the database.
1073+
Overrides the locale during creation of the database.
10741074

10751075
Default value: The default defined during installation.
10761076

10771077
##### `owner`
10781078

1079-
Sets a user as the owner of the database.
1079+
Sets a user as the owner of the database.
10801080

10811081
Default value: '$user' variable set in `postgresql::server` or `postgresql::globals`.
10821082

@@ -1086,13 +1086,13 @@ Default value: '$user' variable set in `postgresql::server` or `postgresql::glob
10861086

10871087
##### `tablespace`
10881088

1089-
Defines the name of the tablespace to allocate the created database to.
1089+
Defines the name of the tablespace to allocate the created database to.
10901090

10911091
Default value: PostgreSQL default.
10921092

10931093
##### `template`
10941094

1095-
Specifies the name of the template database from which to build this database.
1095+
Specifies the name of the template database from which to build this database.
10961096

10971097
Defaults value: `template0`.
10981098

@@ -1106,13 +1106,13 @@ Creates a database with no users and no permissions.
11061106

11071107
##### `dbname`
11081108

1109-
Sets the name of the database.
1109+
Sets the name of the database.
11101110

11111111
Defaults value: The namevar.
11121112

11131113
##### `encoding`
11141114

1115-
Overrides the character set during creation of the database.
1115+
Overrides the character set during creation of the database.
11161116

11171117
Default value: The default defined during installation.
11181118

@@ -1136,13 +1136,13 @@ Default value: The '$user' variable set in `postgresql::server` or `postgresql::
11361136

11371137
##### `tablespace`
11381138

1139-
Sets tablespace for where to create this database.
1139+
Sets tablespace for where to create this database.
11401140

11411141
Default value: The default defined during installation.
11421142

11431143
##### `template`
11441144

1145-
Specifies the name of the template database from which to build this database.
1145+
Specifies the name of the template database from which to build this database.
11461146

11471147
Default value: 'template0'.
11481148

@@ -1152,7 +1152,7 @@ Manages grant-based access privileges for users, wrapping the `postgresql::serve
11521152

11531153
#### `connect_settings`
11541154

1155-
Specifies a hash of environment variables used when connecting to a remote server.
1155+
Specifies a hash of environment variables used when connecting to a remote server.
11561156

11571157
Default value: Connects to the local Postgres instance.
11581158

@@ -1162,21 +1162,21 @@ Specifies the database to which you are granting access.
11621162

11631163
##### `privilege`
11641164

1165-
Specifies comma-separated list of privileges to grant.
1165+
Specifies comma-separated list of privileges to grant.
11661166

11671167
Valid options: 'ALL', 'CREATE', 'CONNECT', 'TEMPORARY', 'TEMP'.
11681168

11691169
##### `psql_db`
11701170

1171-
Defines the database to execute the grant against.
1171+
Defines the database to execute the grant against.
11721172

11731173
**This should not ordinarily be changed from the default**
11741174

11751175
Default value: 'postgres'.
11761176

11771177
##### `psql_user`
11781178

1179-
Specifies the OS user for running `psql`.
1179+
Specifies the OS user for running `psql`.
11801180

11811181
Default value: The default user for the module, usually 'postgres'.
11821182

@@ -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.
@@ -1483,7 +1487,7 @@ The parameters are grouped into these three sections:
14831487

14841488
##### `target`
14851489
Provides the target for the rule, and is generally an internal only property.
1486-
1490+
14871491
**Use with caution.**
14881492

14891493
#### postgresql::server::role
@@ -1754,7 +1758,7 @@ This is the namevar.
17541758

17551759
Required.
17561760

1757-
Specifies the action to create or destroy named slot.
1761+
Specifies the action to create or destroy named slot.
17581762

17591763
Valid values: 'present', 'absent'.
17601764

@@ -1808,7 +1812,7 @@ Default value: '', which generally uses the designated local Unix socket.
18081812

18091813
Defines the port to use when connecting.
18101814

1811-
Default value: ''
1815+
Default value: ''
18121816

18131817
##### `run_as`
18141818

@@ -1842,7 +1846,7 @@ This internal function converts a list of `pg_hba.conf` based ACLs (passed in as
18421846

18431847
### Tasks
18441848

1845-
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.
1849+
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.
18461850

18471851
## Limitations
18481852

manifests/server/extension.pp

Lines changed: 34 additions & 6 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,
@@ -15,15 +16,15 @@
1516
case $ensure {
1617
'present': {
1718
$command = "CREATE EXTENSION \"${extension}\""
18-
$unless_comp = '='
19+
$unless_mod = ''
1920
$package_require = []
20-
$package_before = Postgresql_psql["Add ${extension} extension to ${database}"]
21+
$package_before = Postgresql_psql["${database}: ${command}"]
2122
}
2223

2324
'absent': {
2425
$command = "DROP EXTENSION \"${extension}\""
25-
$unless_comp = '!='
26-
$package_require = Postgresql_psql["Add ${extension} extension to ${database}"]
26+
$unless_mod = 'NOT '
27+
$package_require = Postgresql_psql["${database}: ${command}"]
2728
$package_before = []
2829
}
2930

@@ -39,7 +40,7 @@
3940
}
4041
}
4142

42-
postgresql_psql {"Add ${extension} extension to ${database}":
43+
postgresql_psql { "${database}: ${command}":
4344

4445
psql_user => $user,
4546
psql_group => $group,
@@ -48,7 +49,34 @@
4849

4950
db => $database,
5051
command => $command,
51-
unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = '${extension}') as t WHERE t.count ${unless_comp} 1",
52+
unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')",
53+
}
54+
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}"]
5280
}
5381

5482
if $package_name {

0 commit comments

Comments
 (0)