Skip to content

Commit 1a304a3

Browse files
committed
Resolve dependencies to the databases automatically
1 parent 887a0a9 commit 1a304a3

File tree

10 files changed

+15
-34
lines changed

10 files changed

+15
-34
lines changed

manifests/server/database.pp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
require => Class['postgresql::server::service']
8080
}
8181

82+
# Automatically require the database for the statements that requires it
83+
#
84+
# XXX: We should probably do everything in this class before. Postgresql_psql resource could
85+
# specify this better, but currently it is impossible to use "autorequire" against custom
86+
# resources. We can improve this when a better way is available.
87+
Postgresql_psql["CREATE DATABASE \"${dbname}\""] -> Postgresql_psql <| db == $dbname |>
88+
8289
# This will prevent users from connecting to the database unless they've been
8390
# granted privileges.
8491
postgresql_psql { "REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":

manifests/server/extension.pp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@
3333
}
3434
}
3535

36-
if( $database != 'postgres' ) {
37-
# The database postgres cannot managed by this module, so it is exempt from this dependency
38-
Postgresql_psql {
39-
require => Postgresql::Server::Database[$database],
40-
}
41-
}
42-
4336
postgresql_psql { "${database}: ${command}":
4437

4538
psql_user => $user,

manifests/server/grant.pp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,4 @@
419419
if($role != undef and defined(Postgresql::Server::Role[$role])) {
420420
Postgresql::Server::Role[$role]->Postgresql_psql["grant:${name}"]
421421
}
422-
423-
if($db != undef and defined(Postgresql::Server::Database[$db])) {
424-
Postgresql::Server::Database[$db]->Postgresql_psql["grant:${name}"]
425-
}
426422
}

manifests/server/reassign_owned_by.pp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,4 @@
5454
if($new_role != undef and defined(Postgresql::Server::Role[$new_role])) {
5555
Postgresql::Server::Role[$new_role]->Postgresql_psql["reassign_owned_by:${db}:${sql_command}"]
5656
}
57-
58-
if($db != undef and defined(Postgresql::Server::Database[$db])) {
59-
Postgresql::Server::Database[$db]->Postgresql_psql["reassign_owned_by:${db}:${sql_command}"]
60-
}
6157
}

spec/acceptance/server/grant_role_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class { 'postgresql::server': }
5353
# Create a role to grant to the user
5454
postgresql::server::role { $group:
5555
login => false,
56-
require => Postgresql::Server::Database[$db],
5756
}
5857
5958
# Grant the role to the user
@@ -106,7 +105,6 @@ class { 'postgresql::server': }
106105
# Create a role to grant to the user
107106
postgresql::server::role { $group:
108107
login => false,
109-
require => Postgresql::Server::Database[$db],
110108
}
111109
112110
# Grant the role to the user
@@ -159,7 +157,6 @@ class { 'postgresql::server': }
159157
# Create a role to grant to the user
160158
postgresql::server::role { $group:
161159
login => false,
162-
require => Postgresql::Server::Database[$db],
163160
}
164161
165162
# Grant the role to the user
@@ -195,7 +192,6 @@ class { 'postgresql::server': }
195192
# Create a role to grant to the nonexistent user
196193
postgresql::server::role { $group:
197194
login => false,
198-
require => Postgresql::Server::Database[$db],
199195
}
200196
201197
# Grant the role to the nonexistent user

spec/acceptance/server/grant_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class { 'postgresql::server': }
5858
db => $db,
5959
psql_user => '#{superuser}',
6060
unless => "SELECT 1 from pg_language where lanname = 'plpgsql'",
61-
require => Postgresql::Server::Database[$db],
6261
}
6362
6463
postgresql::server::grant { 'grant usage on plpgsql':
@@ -138,7 +137,6 @@ class { 'postgresql::server': }
138137
db => $db,
139138
psql_user => $owner,
140139
unless => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq'",
141-
require => Postgresql::Server::Database[$db],
142140
}
143141
144142
postgresql::server::grant { 'grant usage on test_seq':
@@ -159,7 +157,6 @@ class { 'postgresql::server': }
159157
db => $db,
160158
psql_user => $owner,
161159
unless => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq'",
162-
require => Postgresql::Server::Database[$db],
163160
}
164161
165162
postgresql::server::grant { 'grant update on test_seq':
@@ -224,7 +221,6 @@ class { 'postgresql::server': }
224221
db => $db,
225222
psql_user => $owner,
226223
unless => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq2'",
227-
require => Postgresql::Server::Database[$db],
228224
}
229225
230226
postgresql::server::grant { 'grant usage on all sequences':
@@ -246,7 +242,6 @@ class { 'postgresql::server': }
246242
db => $db,
247243
psql_user => $owner,
248244
unless => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq2'",
249-
require => Postgresql::Server::Database[$db],
250245
}
251246
252247
postgresql::server::grant { 'grant usage on all sequences':

spec/acceptance/server/reassign_owned_by_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ class { 'postgresql::server': }
7878
db => '#{db}',
7979
psql_user => '#{old_owner}',
8080
unless => "SELECT tablename FROM pg_catalog.pg_tables WHERE tablename = 'test_tbl'",
81-
require => Postgresql::Server::Database['#{db}'],
8281
}
8382
postgresql_psql { 'create test sequence':
8483
command => 'CREATE SEQUENCE test_seq',
8584
db => '#{db}',
8685
psql_user => '#{old_owner}',
8786
unless => "SELECT relname FROM pg_catalog.pg_class WHERE relkind='S' AND relname = 'test_seq'",
88-
require => [ Postgresql_psql['create test table'], Postgresql::Server::Database['#{db}'] ],
87+
require => Postgresql_psql['create test table'],
8988
}
9089
MANIFEST
9190
end

spec/acceptance/server/schema_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class { 'postgresql::server': }
4848
postgresql::server::schema { $user:
4949
db => $db,
5050
owner => $user,
51-
require => Postgresql::Server::Database[$db],
5251
}
5352
MANIFEST
5453
end

spec/unit/defines/server/db_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
it { is_expected.to contain_postgresql__server__db('test') }
3434
it { is_expected.to contain_postgresql__server__database('test').with_owner('tester') }
35-
it { is_expected.to contain_postgresql__server__role('test').that_comes_before('Postgresql::Server::Database[test]') }
35+
it { is_expected.to contain_postgresql__server__role('test') }
3636
it { is_expected.to contain_postgresql__server__database_grant('GRANT test - ALL - test') }
3737
end
3838

spec/unit/defines/server/extension_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
context 'with mandatory arguments only' do
3131
it {
3232
is_expected.to contain_postgresql_psql('template_postgis: CREATE EXTENSION "postgis"')
33-
.with(db: 'template_postgis', command: 'CREATE EXTENSION "postgis"').that_requires('Postgresql::Server::Database[template_postgis]')
33+
.with(db: 'template_postgis', command: 'CREATE EXTENSION "postgis"')
3434
}
3535
end
3636

@@ -63,7 +63,7 @@
6363

6464
it {
6565
is_expected.to contain_postgresql_psql('template_postgis: DROP EXTENSION "postgis"')
66-
.with(db: 'template_postgis', command: 'DROP EXTENSION "postgis"').that_requires('Postgresql::Server::Database[template_postgis]')
66+
.with(db: 'template_postgis', command: 'DROP EXTENSION "postgis"')
6767
}
6868

6969
it {
@@ -78,7 +78,7 @@
7878

7979
it {
8080
is_expected.to contain_postgresql_psql('template_postgis: DROP EXTENSION "postgis"')
81-
.with(db: 'template_postgis', command: 'DROP EXTENSION "postgis"').that_requires('Postgresql::Server::Database[template_postgis]')
81+
.with(db: 'template_postgis', command: 'DROP EXTENSION "postgis"')
8282
}
8383

8484
it {
@@ -97,7 +97,7 @@
9797

9898
it {
9999
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" UPDATE TO \'99.99.99\'')
100-
.with(db: 'template_postgis', unless: "SELECT 1 FROM pg_extension WHERE extname='postgis' AND extversion='99.99.99'").that_requires('Postgresql::Server::Database[template_postgis]')
100+
.with(db: 'template_postgis', unless: "SELECT 1 FROM pg_extension WHERE extname='postgis' AND extversion='99.99.99'")
101101
}
102102
end
103103

@@ -111,7 +111,7 @@
111111
it {
112112
is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" UPDATE')
113113
.with(db: 'template_postgis',
114-
unless: "SELECT 1 FROM pg_available_extensions WHERE name = 'postgis' AND default_version = installed_version").that_requires('Postgresql::Server::Database[template_postgis]')
114+
unless: "SELECT 1 FROM pg_available_extensions WHERE name = 'postgis' AND default_version = installed_version")
115115
}
116116
end
117117
end
@@ -147,7 +147,7 @@
147147
context 'with mandatory arguments only' do
148148
it {
149149
is_expected.to contain_postgresql_psql('template_postgis2: CREATE EXTENSION "postgis"')
150-
.with(db: 'template_postgis2', command: 'CREATE EXTENSION "postgis"').that_requires('Postgresql::Server::Database[template_postgis2]')
150+
.with(db: 'template_postgis2', command: 'CREATE EXTENSION "postgis"')
151151
}
152152
end
153153
end

0 commit comments

Comments
 (0)