module-postgresql/manifests/server/schema.pp
Igor Galić 324c291b3f defined type for creating database schemas
This defined type helps create database schemas, and assign them to an
`owner`. It is closely modeled after Postgresql::Server::Tablespace.
It uses PostgreSQL's builtin IF NOT EXISTS to guarantee idempotency.
(>= 9.3, else it checks pg_namespace).

n.b.: This defined type *requires* that a `db` is passed. This is a
concious design decision, since we find it rather useless to create such
schemas in the default `postgres` database, and if *were* useful, one
can always "over-specify".

This addresses MODULES-1098.
2014-07-25 17:10:40 +02:00

44 lines
1.3 KiB
Puppet

# This defined types creates database schemas. See README.md for more details.
define postgresql::server::schema(
$db,
$owner = undef,
$schema = $title,
) {
$user = $postgresql::server::user
$group = $postgresql::server::group
$port = $postgresql::server::port
$psql_path = $postgresql::server::psql_path
$version = $postgresql::server::version
Postgresql_psql {
db => $db,
psql_user => $user,
psql_group => $group,
psql_path => $psql_path,
port => $port,
}
$schema_title = "Create Schema '${schema}'"
$authorization = $owner? {
undef => '',
default => "AUTHORIZATION \"${owner}\"",
}
if(versioncmp($version, '9.3') >= 0) {
$schema_command = "CREATE SCHEMA IF NOT EXISTS \"${schema}\" ${authorization}"
$unless = undef
} else {
$schema_command = "CREATE SCHEMA \"${schema}\" ${authorization}"
$unless = "SELECT nspname FROM pg_namespace WHERE nspname='${schema}'"
}
postgresql_psql { $schema_title:
command => $schema_command,
unless => $unless,
require => Class['postgresql::server'],
}
if($owner != undef and defined(Postgresql::Server::Role[$owner])) {
Postgresql::Server::Role[$owner]->Postgresql_psql[$schema_title]
}
}