Skip to content

Commit 7a5a298

Browse files
committed
Merge pull request puppetlabs#551 from juniorsysadmin/database-comment
(MODULES-1153) Add database comment parameter
2 parents 4656745 + 435d632 commit 7a5a298

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,9 @@ For example, to create a database called `test1` with a corresponding user of th
584584
####`namevar`
585585
The namevar for the resource designates the name of the database.
586586

587+
####`comment`
588+
A comment to be stored about the database using the PostgreSQL COMMENT command.
589+
587590
####`dbname`
588591
The name of the database to be created. Defaults to `namevar`.
589592

manifests/server/database.pp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Define for creating a database. See README.md for more details.
22
define postgresql::server::database(
3+
$comment = undef,
34
$dbname = $title,
45
$owner = $postgresql::server::user,
56
$tablespace = undef,
@@ -76,6 +77,13 @@
7677
db => $default_db,
7778
}
7879

80+
if $comment {
81+
Exec[ $createdb_command ]->
82+
postgresql_psql {"COMMENT ON DATABASE ${dbname} IS '${comment}'":
83+
unless => "SELECT pg_catalog.shobj_description(d.oid, 'pg_database') as \"Description\" FROM pg_catalog.pg_database d WHERE datname = '${dbname}' AND pg_catalog.shobj_description(d.oid, 'pg_database') = '${comment}'",
84+
}
85+
}
86+
7987
# Build up dependencies on tablespace
8088
if($tablespace != undef and defined(Postgresql::Server::Tablespace[$tablespace])) {
8189
Postgresql::Server::Tablespace[$tablespace]->Exec[$createdb_command]

manifests/server/db.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
define postgresql::server::db (
44
$user,
55
$password,
6+
$comment = undef,
67
$dbname = $title,
78
$encoding = $postgresql::server::encoding,
89
$locale = $postgresql::server::locale,
@@ -15,6 +16,7 @@
1516

1617
if ! defined(Postgresql::Server::Database[$dbname]) {
1718
postgresql::server::database { $dbname:
19+
comment => $comment,
1820
encoding => $encoding,
1921
tablespace => $tablespace,
2022
template => $template,

spec/acceptance/db_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class { 'postgresql::server': }
1010
location => '#{tmpdir}',
1111
} ->
1212
postgresql::server::db { 'postgresql_test_db':
13+
comment => 'testcomment',
1314
user => 'test',
1415
password => 'test1',
1516
tablespace => 'postgresql_test_db',
@@ -27,6 +28,10 @@ class { 'postgresql::server': }
2728
psql('--command="SELECT 1 FROM pg_roles WHERE rolname=\'test\'"') do |r|
2829
expect(r.stdout).to match(/\(1 row\)/)
2930
end
31+
32+
psql('--command="SELECT pg_catalog.shobj_description(d.oid, \'pg_database\') FROM pg_catalog.pg_database d WHERE datname = \'postgresql_test_db\' AND pg_catalog.shobj_description(d.oid, \'pg_database\') = \'testcomment\'"') do |r|
33+
expect(r.stdout).to match(/\(1 row\)/)
34+
end
3035
ensure
3136
psql('--command="drop database postgresql_test_db" postgres')
3237
psql('--command="DROP USER test"')

spec/unit/defines/server/database_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@
2222

2323
it { is_expected.to contain_postgresql__server__database('test') }
2424
it { is_expected.to contain_postgresql_psql("Check for existence of db 'test'") }
25+
26+
context "with comment set to 'test comment'" do
27+
let (:params) {{ :comment => 'test comment' }}
28+
29+
it { is_expected.to contain_postgresql_psql("COMMENT ON DATABASE test IS 'test comment'") }
30+
end
2531
end

0 commit comments

Comments
 (0)