Skip to content

Commit 847f54c

Browse files
committed
Merge pull request puppetlabs#204 from mcanevet/set_istemplate
Add support for istemplate parameter where creating db
2 parents 95452ce + 988f45c commit 847f54c

File tree

4 files changed

+93
-11
lines changed

4 files changed

+93
-11
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ Override the locale during creation of the database. Defaults to the default def
325325
####`grant`
326326
Grant permissions during creation. Defaults to `ALL`.
327327

328+
####`istemplate`
329+
Define database as a template. Defaults to `false`.
330+
328331
###Resource: postgresql::database
329332
This defined type can be used to create a database with no users and no permissions, which is a rare use case.
330333

@@ -343,6 +346,9 @@ Override the character set during creation of the database. Defaults to the defa
343346
####`locale`
344347
Override the locale during creation of the database. Defaults to the default defined during installation.
345348

349+
####`istemplate`
350+
Define database as a template. Defaults to `false`.
351+
346352
###Resource: postgresql::database\_grant
347353
This defined type manages grant based access privileges for users. Consult the PostgreSQL documentation for `grant` for more information.
348354

manifests/database.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
$owner = $postgresql::params::user,
2525
$tablespace = undef,
2626
$charset = $postgresql::params::charset,
27-
$locale = $postgresql::params::locale
27+
$locale = $postgresql::params::locale,
28+
$istemplate = false
2829
) {
2930
include postgresql::params
3031

@@ -76,4 +77,9 @@
7677
refreshonly => true,
7778
}
7879

80+
Exec [ $createdb_command ] ->
81+
82+
postgresql_psql {"UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}'":
83+
unless => "SELECT datname FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}",
84+
}
7985
}

manifests/db.pp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,23 @@
3636
define postgresql::db (
3737
$user,
3838
$password,
39-
$charset = $postgresql::params::charset,
40-
$locale = $postgresql::params::locale,
41-
$grant = 'ALL',
42-
$tablespace = undef
39+
$charset = $postgresql::params::charset,
40+
$locale = $postgresql::params::locale,
41+
$grant = 'ALL',
42+
$tablespace = undef,
43+
$istemplate = false
4344
) {
4445
include postgresql::params
4546

4647
postgresql::database { $name:
4748
# TODO: ensure is not yet supported
48-
#ensure => present,
49-
charset => $charset,
50-
tablespace => $tablespace,
51-
#provider => 'postgresql',
52-
require => Class['postgresql::server'],
53-
locale => $locale,
49+
#ensure => present,
50+
charset => $charset,
51+
tablespace => $tablespace,
52+
#provider => 'postgresql',
53+
require => Class['postgresql::server'],
54+
locale => $locale,
55+
istemplate => $istemplate,
5456
}
5557

5658
if ! defined(Postgresql::Database_user[$user]) {

spec/system/install_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,74 @@ class { 'postgresql::server': }
9595
psql('--command="drop database test1" postgres')
9696
end
9797
end
98+
99+
it 'should take an istemplate parameter' do
100+
begin
101+
pp = <<-EOS
102+
$db = 'template2'
103+
include postgresql::server
104+
105+
postgresql::db { $db:
106+
user => $db,
107+
password => postgresql_password($db, $db),
108+
istemplate => true,
109+
}
110+
EOS
111+
112+
puppet_apply(pp) do |r|
113+
r.exit_code.should_not == 1
114+
end
115+
116+
puppet_apply(pp) do |r|
117+
r.exit_code.should == 0
118+
end
119+
120+
psql('--command="select datname from pg_database" template2') do |r|
121+
r.stdout.should =~ /template2/
122+
r.stderr.should be_empty
123+
r.exit_code.should == 0
124+
end
125+
ensure
126+
psql('--command="drop database template2" postgres') do |r|
127+
r.stdout.should be_empty
128+
r.stderr.should =~ /cannot drop a template database/
129+
r.exit_code.should_not == 0
130+
end
131+
end
132+
end
133+
134+
it 'should update istemplate parameter' do
135+
begin
136+
pp = <<-EOS
137+
$db = 'template2'
138+
include postgresql::server
139+
140+
postgresql::db { $db:
141+
user => $db,
142+
password => postgresql_password($db, $db),
143+
istemplate => false,
144+
}
145+
EOS
146+
147+
puppet_apply(pp) do |r|
148+
r.exit_code.should_not == 1
149+
end
150+
151+
puppet_apply(pp) do |r|
152+
r.exit_code.should == 0
153+
end
154+
155+
psql('--command="select datname from pg_database" template2') do |r|
156+
r.stdout.should =~ /template2/
157+
r.stderr.should be_empty
158+
r.exit_code.should == 0
159+
end
160+
ensure
161+
psql('--command="drop database template2" postgres') do |r|
162+
r.exit_code.should == 0
163+
end
164+
end
165+
end
98166
end
99167

100168
describe 'postgresql::psql' do

0 commit comments

Comments
 (0)