Class: Chef::Provider::Database::Postgresql

Inherits:
Chef::Provider show all
Includes:
Mixin::ShellOut
Defined in:
lib/cookbooks/database/libraries/provider_database_postgresql.rb

Direct Known Subclasses

PostgresqlUser

Instance Method Summary collapse

Instance Method Details

#action_createObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cookbooks/database/libraries/provider_database_postgresql.rb', line 36

def action_create
  unless exists?
    begin
      encoding = @new_resource.encoding
      if encoding != "DEFAULT"
        encoding = "'#{@new_resource.encoding}'"
      end
      Chef::Log.debug("#{@new_resource}: Creating database #{new_resource.database_name}")
      create_sql = "CREATE DATABASE #{new_resource.database_name}"
      create_sql += " TEMPLATE = #{new_resource.template}" if new_resource.template
      create_sql += " ENCODING = #{encoding}" if new_resource.encoding
      create_sql += " TABLESPACE = #{new_resource.tablespace}" if new_resource.tablespace
      create_sql += " LC_CTYPE = '#{new_resource.collation}' LC_COLLATE = '#{new_resource.collation}'" if new_resource.collation
      create_sql += " CONNECTION LIMIT = #{new_resource.connection_limit}" if new_resource.connection_limit
      create_sql += " OWNER = #{new_resource.owner}" if new_resource.owner
      Chef::Log.debug("#{@new_resource}: Performing query [#{create_sql}]")
      db("template1").query(create_sql)
      @new_resource.updated_by_last_action(true)
    ensure
      close
    end
  end
end

#action_dropObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cookbooks/database/libraries/provider_database_postgresql.rb', line 60

def action_drop
  if exists?
    begin
      Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
      db("template1").query("drop database #{new_resource.database_name}")
      @new_resource.updated_by_last_action(true)
    ensure
      close
    end
  end
end

#action_queryObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cookbooks/database/libraries/provider_database_postgresql.rb', line 72

def action_query
  if exists?
    begin
      Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql_query}]")
      db(@new_resource.database_name).query(@new_resource.sql_query)
      Chef::Log.debug("#{@new_resource}: query [#{new_resource.sql_query}] succeeded")
      @new_resource.updated_by_last_action(true)
    ensure
      close
    end
  end
end

#load_current_resourceObject



28
29
30
31
32
33
34
# File 'lib/cookbooks/database/libraries/provider_database_postgresql.rb', line 28

def load_current_resource
  Gem.clear_paths
  require 'pg'
  @current_resource = Chef::Resource::Database.new(@new_resource.name)
  @current_resource.database_name(@new_resource.database_name)
  @current_resource
end