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, #collation, #create, #database, #drop, #dump, #host, #initialize, #load, #owner, #password, #port, #search_path, #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
16
17
18
19
# File 'lib/sequel_rails/storage/postgres.rb', line 4

def _create
  with_pgpassword do
    commands = ['createdb']
    add_connection_settings commands
    add_option commands, '--maintenance-db', maintenance_db
    add_option commands, '--encoding', encoding
    add_option commands, '--locale', locale
    add_option commands, '--lc-collate', collation
    add_option commands, '--lc-ctype', ctype
    add_option commands, '--template', template
    add_option commands, '--tablespace', tablespace
    add_option commands, '--owner', owner
    commands << database
    safe_exec commands
  end
end

#_dropObject



21
22
23
24
25
26
27
28
29
# File 'lib/sequel_rails/storage/postgres.rb', line 21

def _drop
  with_pgpassword do
    commands = ['dropdb']
    add_connection_settings commands
    add_flag commands, '--if-exists'
    commands << database
    safe_exec commands
  end
end

#_dump(filename) ⇒ Object



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

def _dump(filename)
  with_pgpassword do
    commands = ['pg_dump']
    add_connection_settings commands
    add_flag commands, '-s'
    add_flag commands, '-x'
    add_flag commands, '-O'
    add_option commands, '--file', filename
    commands << database
    safe_exec commands
  end
end

#_load(filename) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/sequel_rails/storage/postgres.rb', line 44

def _load(filename)
  with_pgpassword do
    commands = ['psql']
    add_connection_settings commands
    add_option commands, '--file', filename
    commands << database
    safe_exec commands
  end
end

#close_connectionsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sequel_rails/storage/postgres.rb', line 54

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

#ctypeObject



85
86
87
# File 'lib/sequel_rails/storage/postgres.rb', line 85

def ctype
  @ctype ||= config['ctype'] || ''
end

#encodingObject



73
74
75
# File 'lib/sequel_rails/storage/postgres.rb', line 73

def encoding
  @encoding ||= config['encoding'] || charset
end

#localeObject



77
78
79
# File 'lib/sequel_rails/storage/postgres.rb', line 77

def locale
  @locale ||= config['locale'] || ''
end

#maintenance_dbObject



93
94
95
# File 'lib/sequel_rails/storage/postgres.rb', line 93

def maintenance_db
  @maintenance_db ||= config['maintenance_db'] || ''
end

#schema_information_dump(migrator, sql_dump) ⇒ Object



97
98
99
# File 'lib/sequel_rails/storage/postgres.rb', line 97

def schema_information_dump(migrator, sql_dump)
  schema_information_dump_with_search_path(migrator, sql_dump)
end

#tablespaceObject



89
90
91
# File 'lib/sequel_rails/storage/postgres.rb', line 89

def tablespace
  @tablespace ||= config['tablespace'] || ''
end

#templateObject



81
82
83
# File 'lib/sequel_rails/storage/postgres.rb', line 81

def template
  @template ||= config['template'] || ''
end