diff --git a/manifests/server/database.pp b/manifests/server/database.pp index 13b43cd..d6cce69 100644 --- a/manifests/server/database.pp +++ b/manifests/server/database.pp @@ -1,110 +1,121 @@ # Define for creating a database. See README.md for more details. define postgresql::server::database( $comment = undef, $dbname = $title, - $owner = $postgresql::server::user, + $owner = undef, $tablespace = undef, $template = 'template0', $encoding = $postgresql::server::encoding, $locale = $postgresql::server::locale, $istemplate = false, $connect_settings = $postgresql::server::default_connect_settings, ) { $createdb_path = $postgresql::server::createdb_path $user = $postgresql::server::user $group = $postgresql::server::group $psql_path = $postgresql::server::psql_path $default_db = $postgresql::server::default_database # If possible use the version of the remote database, otherwise # fallback to our local DB version if $connect_settings != undef and has_key( $connect_settings, 'DBVERSION') { $version = $connect_settings['DBVERSION'] } else { $version = $postgresql::server::_version } # If the connection settings do not contain a port, then use the local server port if $connect_settings != undef and has_key( $connect_settings, 'PGPORT') { $port = undef } else { $port = $postgresql::server::port } # Set the defaults for the postgresql_psql resource Postgresql_psql { db => $default_db, psql_user => $user, psql_group => $group, psql_path => $psql_path, port => $port, connect_settings => $connect_settings, } # Optionally set the locale switch. Older versions of createdb may not accept # --locale, so if the parameter is undefined its safer not to pass it. if ($version != '8.1') { $locale_option = $locale ? { undef => '', default => "LC_COLLATE = '${locale}' LC_CTYPE = '${locale}'", } $public_revoke_privilege = 'CONNECT' } else { $locale_option = '' $public_revoke_privilege = 'ALL' } $template_option = $template ? { undef => '', default => "TEMPLATE = \"${template}\"", } $encoding_option = $encoding ? { undef => '', default => "ENCODING = '${encoding}'", } $tablespace_option = $tablespace ? { undef => '', default => "TABLESPACE = \"${tablespace}\"", } if $createdb_path != undef { warning('Passing "createdb_path" to postgresql::database is deprecated, it can be removed safely for the same behaviour') } postgresql_psql { "CREATE DATABASE \"${dbname}\"": - command => "CREATE DATABASE \"${dbname}\" WITH OWNER = \"${owner}\" ${template_option} ${encoding_option} ${locale_option} ${tablespace_option}", + command => "CREATE DATABASE \"${dbname}\" WITH ${template_option} ${encoding_option} ${locale_option} ${tablespace_option}", unless => "SELECT 1 FROM pg_database WHERE datname = '${dbname}'", require => Class['postgresql::server::service'] }~> # This will prevent users from connecting to the database unless they've been # granted privileges. postgresql_psql { "REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public": refreshonly => true, } Postgresql_psql["CREATE DATABASE \"${dbname}\""]-> postgresql_psql { "UPDATE pg_database SET datistemplate = ${istemplate} WHERE datname = '${dbname}'": unless => "SELECT 1 FROM pg_database WHERE datname = '${dbname}' AND datistemplate = ${istemplate}", } if $comment { # The shobj_description function was only introduced with 8.2 $comment_information_function = $version ? { '8.1' => 'obj_description', default => 'shobj_description', } Postgresql_psql["CREATE DATABASE \"${dbname}\""]-> postgresql_psql { "COMMENT ON DATABASE \"${dbname}\" IS '${comment}'": unless => "SELECT 1 FROM pg_catalog.pg_database d WHERE datname = '${dbname}' AND pg_catalog.${comment_information_function}(d.oid, 'pg_database') = '${comment}'", db => $dbname, } } + if $owner { + postgresql_psql { "ALTER DATABASE \"${dbname}\" OWNER TO \"${owner}\"": + unless => "SELECT 1 FROM pg_database JOIN pg_roles rol ON datdba = rol.oid WHERE datname = '${dbname}' AND rolname = '${owner}'", + require => Postgresql_psql["CREATE DATABASE \"${dbname}\""], + } + + if defined(Postgresql::Server::Role[$owner]) { + Postgresql::Server::Role[$owner]->Postgresql_psql["ALTER DATABASE \"${dbname}\" OWNER TO \"${owner}\""] + } + } + # Build up dependencies on tablespace if $tablespace != undef and defined(Postgresql::Server::Tablespace[$tablespace]) { Postgresql::Server::Tablespace[$tablespace]->Postgresql_psql["CREATE DATABASE \"${dbname}\""] } } diff --git a/spec/unit/defines/server/database_spec.rb b/spec/unit/defines/server/database_spec.rb index ef0ba21..c9993f6 100644 --- a/spec/unit/defines/server/database_spec.rb +++ b/spec/unit/defines/server/database_spec.rb @@ -1,72 +1,78 @@ require 'spec_helper' describe 'postgresql::server::database', :type => :define do let :facts do { :osfamily => 'Debian', :operatingsystem => 'Debian', :operatingsystemrelease => '6.0', :kernel => 'Linux', :concat_basedir => tmpfilename('contrib'), :id => 'root', :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', } end let :title do 'test' end let :pre_condition do "class {'postgresql::server':}" end it { is_expected.to contain_postgresql__server__database('test') } it { is_expected.to contain_postgresql_psql('CREATE DATABASE "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'").with_connect_settings( {} ) } end context "with specific db connection settings - default port" do let :pre_condition do "class {'postgresql::server':}" end let (:params) {{ :connect_settings => { 'PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', }}} it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings( { 'PGHOST' => 'postgres-db-server','DBVERSION' => '9.1' } ).with_port( 5432 ) } end context "with specific db connection settings - including port" do let :pre_condition do "class {'postgresql::globals':} class {'postgresql::server':}" end let (:params) {{ :connect_settings => { 'PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234' }}} it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings( { 'PGHOST' => 'postgres-db-server','DBVERSION' => '9.1','PGPORT' => '1234' } ).with_port( nil ) } end context "with global db connection settings - including port" do let :pre_condition do "class {'postgresql::globals': default_connect_settings => { 'PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.2', 'PGPORT' => '1234' } } class {'postgresql::server':}" end it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings( { 'PGHOST' => 'postgres-db-server','DBVERSION' => '9.2','PGPORT' => '1234' } ).with_port( nil ) } end + + context "with different owner" do + let (:params) {{ :owner => 'test_owner' }} + + it { is_expected.to contain_postgresql_psql('ALTER DATABASE "test" OWNER TO "test_owner"') } + end end