Class: SequelRails::Storage::Postgres

Inherits:
Abstract
  • Object
show all
Defined in:
lib/sequel_rails/storage/postgres.rb

Instance Attribute Summary

Attributes inherited from Abstract

#config

Instance Method Summary collapse

Methods inherited from Abstract

#charset, #create, #database, #drop, #host, #initialize, #owner, #password, #port, #username

Constructor Details

This class inherits a constructor from SequelRails::Storage::Abstract

Instance Method Details

#_createObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/sequel_rails/storage/postgres.rb', line 4

def _create
  ENV["PGPASSWORD"] = password unless password.blank?
  commands = ["createdb", "--encoding", charset]
  commands << "--username" << username unless username.blank?
  commands << "--owner" << owner unless owner.blank?
  commands << "--port" << port.to_s unless port.blank?
  commands << "--host" << host unless host.blank?
  commands << database
  res = system(*commands)
  ENV["PGPASSWORD"] = nil unless password.blank?
  res
end

#_dropObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sequel_rails/storage/postgres.rb', line 17

def _drop
  ENV["PGPASSWORD"] = password unless password.blank?
  commands = ["dropdb"]
  commands << "-U" << username unless username.blank?
  commands << "--port" << port.to_s unless port.blank?
  commands << "--host" << host unless host.blank?
  commands << database
  res = system(*commands)
  ENV["PGPASSWORD"] = nil unless password.blank?
  res
end

#close_connectionsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sequel_rails/storage/postgres.rb', line 29

def close_connections
  begin
    db = ::Sequel.connect(config)
    # Will only work on Postgres > 8.4
    pid_column = db.server_version < 90200 ? "procpid" : "pid"
    db.execute <<-SQL.gsub(/^\s{12}/,'')
      SELECT COUNT(pg_terminate_backend(#{pid_column}))
      FROM pg_stat_activity
      WHERE datname = '#{database}';
    SQL
  rescue => _
    # Will raise an error as it kills existing process running this 
    # command. Seems to be only way to ensure *all* test connections 
    # are closed
  end
end