Create postgres databases from template0 and set the collation and ctype
authorTom Hughes <tom@compton.nu>
Tue, 4 Jun 2013 12:55:31 +0000 (13:55 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 4 Jun 2013 12:56:38 +0000 (13:56 +0100)
cookbooks/postgresql/libraries/postgresql.rb
cookbooks/postgresql/providers/database.rb
cookbooks/postgresql/resources/database.rb

index bd10a02..2f67296 100644 (file)
@@ -75,10 +75,12 @@ class Chef
     end
 
     def databases
-      @databases ||= query("SELECT d.datname, u.usename, d.encoding FROM pg_database AS d INNER JOIN pg_user AS u ON d.datdba = u.usesysid").inject({}) do |databases,database|
+      @databases ||= query("SELECT d.datname, u.usename, d.encoding, d.datcollate, d.datctype FROM pg_database AS d INNER JOIN pg_user AS u ON d.datdba = u.usesysid").inject({}) do |databases,database|
         databases[database[:datname]] = {
           :owner => database[:usename],
-          :encoding => database[:encoding]
+          :encoding => database[:encoding],
+          :collate => database[:datcollate],
+          :ctype => database[:datctype]
         }
         databases
       end
index eee7903..9cf9dd4 100644 (file)
@@ -26,13 +26,15 @@ def load_current_resource
   if pg_database = @pg.databases[@current_resource.database]
     @current_resource.owner(pg_database[:owner])
     @current_resource.encoding(pg_database[:encoding])
+    @current_resource.encoding(pg_database[:collate])
+    @current_resource.encoding(pg_database[:ctype])
   end
   @current_resource
 end
 
 action :create do
   unless @pg.databases.include?(new_resource.database)
-    @pg.execute(:command => "CREATE DATABASE #{new_resource.database} OWNER #{new_resource.owner} ENCODING '#{new_resource.encoding}'")
+    @pg.execute(:command => "CREATE DATABASE #{new_resource.database} OWNER #{new_resource.owner} TEMPLATE template0 ENCODING '#{new_resource.encoding}' LC_COLLATE '#{new_resource.collation}' LC_CTYPE '#{new_resource.ctype}'")
     new_resource.updated_by_last_action(true)
   else
     if new_resource.owner != @current_resource.owner
index 039c124..1a63f3a 100644 (file)
@@ -23,6 +23,8 @@ attribute :database, :kind_of => String, :name_attribute => true
 attribute :cluster, :kind_of => String, :required => true
 attribute :owner, :kind_of => String, :required => true
 attribute :encoding, :kind_of => String, :default => "UTF8"
+attribute :collation, :kind_of => String, :default => "en_GB.UTF8"
+attribute :ctype, :kind_of => String, :default => "en_GB.UTF8"
 
 def initialize(*args)
   super