From 435d63297abad00910adb3acaa8660d15a01e563 Mon Sep 17 00:00:00 2001 From: juniorsysadmin Date: Fri, 9 Jan 2015 14:08:23 +1100 Subject: [PATCH] (MODULES-1153) Add database comment parameter --- README.md | 3 +++ manifests/server/database.pp | 8 ++++++++ manifests/server/db.pp | 2 ++ spec/acceptance/db_spec.rb | 5 +++++ spec/unit/defines/server/database_spec.rb | 6 ++++++ 5 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 257adf0..d9edfaa 100644 --- a/README.md +++ b/README.md @@ -584,6 +584,9 @@ For example, to create a database called `test1` with a corresponding user of th ####`namevar` The namevar for the resource designates the name of the database. +####`comment` +A comment to be stored about the database using the PostgreSQL COMMENT command. + ####`dbname` The name of the database to be created. Defaults to `namevar`. diff --git a/manifests/server/database.pp b/manifests/server/database.pp index 946c716..586cb38 100644 --- a/manifests/server/database.pp +++ b/manifests/server/database.pp @@ -1,5 +1,6 @@ # Define for creating a database. See README.md for more details. define postgresql::server::database( + $comment = undef, $dbname = $title, $owner = $postgresql::server::user, $tablespace = undef, @@ -76,6 +77,13 @@ define postgresql::server::database( db => $default_db, } + if $comment { + Exec[ $createdb_command ]-> + postgresql_psql {"COMMENT ON DATABASE ${dbname} IS '${comment}'": + 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}'", + } + } + # Build up dependencies on tablespace if($tablespace != undef and defined(Postgresql::Server::Tablespace[$tablespace])) { Postgresql::Server::Tablespace[$tablespace]->Exec[$createdb_command] diff --git a/manifests/server/db.pp b/manifests/server/db.pp index d9bb841..9c59931 100644 --- a/manifests/server/db.pp +++ b/manifests/server/db.pp @@ -3,6 +3,7 @@ define postgresql::server::db ( $user, $password, + $comment = undef, $dbname = $title, $encoding = $postgresql::server::encoding, $locale = $postgresql::server::locale, @@ -15,6 +16,7 @@ define postgresql::server::db ( if ! defined(Postgresql::Server::Database[$dbname]) { postgresql::server::database { $dbname: + comment => $comment, encoding => $encoding, tablespace => $tablespace, template => $template, diff --git a/spec/acceptance/db_spec.rb b/spec/acceptance/db_spec.rb index 41a89e2..92827ae 100644 --- a/spec/acceptance/db_spec.rb +++ b/spec/acceptance/db_spec.rb @@ -10,6 +10,7 @@ describe 'postgresql::server::db', :unless => UNSUPPORTED_PLATFORMS.include?(fac location => '#{tmpdir}', } -> postgresql::server::db { 'postgresql_test_db': + comment => 'testcomment', user => 'test', password => 'test1', tablespace => 'postgresql_test_db', @@ -27,6 +28,10 @@ describe 'postgresql::server::db', :unless => UNSUPPORTED_PLATFORMS.include?(fac psql('--command="SELECT 1 FROM pg_roles WHERE rolname=\'test\'"') do |r| expect(r.stdout).to match(/\(1 row\)/) end + + 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| + expect(r.stdout).to match(/\(1 row\)/) + end ensure psql('--command="drop database postgresql_test_db" postgres') psql('--command="DROP USER test"') diff --git a/spec/unit/defines/server/database_spec.rb b/spec/unit/defines/server/database_spec.rb index ae7bd25..edb9cb6 100644 --- a/spec/unit/defines/server/database_spec.rb +++ b/spec/unit/defines/server/database_spec.rb @@ -22,4 +22,10 @@ describe 'postgresql::server::database', :type => :define do it { is_expected.to contain_postgresql__server__database('test') } it { is_expected.to contain_postgresql_psql("Check for existence of db 'test'") } + + context "with comment set to 'test comment'" do + let (:params) {{ :comment => 'test comment' }} + + it { is_expected.to contain_postgresql_psql("COMMENT ON DATABASE test IS 'test comment'") } + end end