Class: Rails::Sequel::Database::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/rails3_sequel/adapters/postgres.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Postgres

Returns a new instance of Postgres.



6
7
8
9
# File 'lib/rails3_sequel/adapters/postgres.rb', line 6

def initialize (env)
  @env = env
  @config = parse_special_options(Database.configurations[@env])
end

Instance Method Details

#connect(options = {}) ⇒ Object



11
12
13
14
# File 'lib/rails3_sequel/adapters/postgres.rb', line 11

def connect (options = {})
  options = parse_special_options(options)
  ::Sequel.connect(@config.merge(options))
end

#create_database(options = {}) ⇒ Object

from ActiveRecord



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails3_sequel/adapters/postgres.rb', line 17

def create_database (options = {})
  db = management_connect
  name = @config['database']

  if name.nil? or name.strip == '' then
    raise "No valid database specified for #{@env} environment"
  end

  options = options.reverse_merge(:encoding => "utf8")

  option_string = options.sum do |key, value|
    case key
    when 'owner'
      " OWNER = \"#{value}\""
    when 'template'
      " TEMPLATE = \"#{value}\""
    when 'encoding'
      " ENCODING = '#{value}'"
    when 'tablespace'
      " TABLESPACE = \"#{value}\""
    when :connection_limit
      " CONNECTION LIMIT = #{value}"
    else 
      ""   
    end  
  end  

  # TODO: quote table name
  db.execute "CREATE DATABASE #{name} #{option_string}"

  # create schema too, if the previous command succeeds, this should be fine
  # first connect without schema specified
  db = connect('schema_search_path' => nil)
  for schema in @config['schema_search_path'].split(',') do
    next if schema.strip == 'public'
    db.execute "CREATE SCHEMA #{schema}"
  end
end

#drop_databaseObject



56
57
58
59
60
61
# File 'lib/rails3_sequel/adapters/postgres.rb', line 56

def drop_database
  db = management_connect
  name = @config['database']

  db.execute "DROP DATABASE #{name}"
end