diff --git a/README.md b/README.md index 4d4e017..d4942cc 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,9 @@ Override the locale during creation of the database. Defaults to the default def ####`grant` Grant permissions during creation. Defaults to `ALL`. +####`istemplate` +Define database as a template. Defaults to `false`. + ###Resource: postgresql::database This defined type can be used to create a database with no users and no permissions, which is a rare use case. @@ -343,6 +346,9 @@ Override the character set during creation of the database. Defaults to the defa ####`locale` Override the locale during creation of the database. Defaults to the default defined during installation. +####`istemplate` +Define database as a template. Defaults to `false`. + ###Resource: postgresql::database\_grant This defined type manages grant based access privileges for users. Consult the PostgreSQL documentation for `grant` for more information. diff --git a/manifests/database.pp b/manifests/database.pp index f3c90f7..fd272f4 100644 --- a/manifests/database.pp +++ b/manifests/database.pp @@ -24,7 +24,8 @@ define postgresql::database( $owner = $postgresql::params::user, $tablespace = undef, $charset = $postgresql::params::charset, - $locale = $postgresql::params::locale + $locale = $postgresql::params::locale, + $istemplate = false ) { include postgresql::params @@ -76,4 +77,9 @@ define postgresql::database( refreshonly => true, } + Exec [ $createdb_command ] -> + + postgresql_psql {"UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}'": + unless => "SELECT datname FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}", + } } diff --git a/manifests/db.pp b/manifests/db.pp index f6531c5..0ea3c6d 100644 --- a/manifests/db.pp +++ b/manifests/db.pp @@ -36,21 +36,23 @@ define postgresql::db ( $user, $password, - $charset = $postgresql::params::charset, - $locale = $postgresql::params::locale, - $grant = 'ALL', - $tablespace = undef + $charset = $postgresql::params::charset, + $locale = $postgresql::params::locale, + $grant = 'ALL', + $tablespace = undef, + $istemplate = false ) { include postgresql::params postgresql::database { $name: # TODO: ensure is not yet supported - #ensure => present, - charset => $charset, - tablespace => $tablespace, - #provider => 'postgresql', - require => Class['postgresql::server'], - locale => $locale, + #ensure => present, + charset => $charset, + tablespace => $tablespace, + #provider => 'postgresql', + require => Class['postgresql::server'], + locale => $locale, + istemplate => $istemplate, } if ! defined(Postgresql::Database_user[$user]) { diff --git a/spec/system/install_spec.rb b/spec/system/install_spec.rb index 9c37037..974ff3b 100644 --- a/spec/system/install_spec.rb +++ b/spec/system/install_spec.rb @@ -95,6 +95,74 @@ describe 'install:' do psql('--command="drop database test1" postgres') end end + + it 'should take an istemplate parameter' do + begin + pp = <<-EOS + $db = 'template2' + include postgresql::server + + postgresql::db { $db: + user => $db, + password => postgresql_password($db, $db), + istemplate => true, + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should == 0 + end + + psql('--command="select datname from pg_database" template2') do |r| + r.stdout.should =~ /template2/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + ensure + psql('--command="drop database template2" postgres') do |r| + r.stdout.should be_empty + r.stderr.should =~ /cannot drop a template database/ + r.exit_code.should_not == 0 + end + end + end + + it 'should update istemplate parameter' do + begin + pp = <<-EOS + $db = 'template2' + include postgresql::server + + postgresql::db { $db: + user => $db, + password => postgresql_password($db, $db), + istemplate => false, + } + EOS + + puppet_apply(pp) do |r| + r.exit_code.should_not == 1 + end + + puppet_apply(pp) do |r| + r.exit_code.should == 0 + end + + psql('--command="select datname from pg_database" template2') do |r| + r.stdout.should =~ /template2/ + r.stderr.should be_empty + r.exit_code.should == 0 + end + ensure + psql('--command="drop database template2" postgres') do |r| + r.exit_code.should == 0 + end + end + end end describe 'postgresql::psql' do