Module: BlackStack::PostgreSQL

Defined in:
lib/blackstack-db/postgresql.rb

Constant Summary collapse

@@db_url =

database connection parameters

nil
@@db_port =
nil
@@db_name =
nil
@@db_user =
nil
@@db_password =
nil
@@db_max_connections =
nil

Class Method Summary collapse

Class Method Details

.connectObject

create database connection



95
96
97
98
99
100
101
# File 'lib/blackstack-db/postgresql.rb', line 95

def self.connect
    BlackStack::set_db_type(BlackStack::TYPE_POSTGRESQL)
    s = BlackStack::PostgreSQL.connection_string
    opts = {}
    opts[:max_connections] = @@db_max_connections if @@db_max_connections
    Sequel.connect(s, **opts)
end

.connection_stringObject

return the connection string for a postgresql database



12
13
14
# File 'lib/blackstack-db/postgresql.rb', line 12

def self.connection_string
    "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_name}"
end

.db_max_connectionsObject



32
33
34
# File 'lib/blackstack-db/postgresql.rb', line 32

def self.db_max_connections
    @@db_max_connections
end

.db_nameObject



23
24
25
# File 'lib/blackstack-db/postgresql.rb', line 23

def self.db_name
    @@db_name
end

.db_passwordObject



29
30
31
# File 'lib/blackstack-db/postgresql.rb', line 29

def self.db_password
    @@db_password
end

.db_portObject



20
21
22
# File 'lib/blackstack-db/postgresql.rb', line 20

def self.db_port
    @@db_port
end

.db_urlObject

database connection getters



17
18
19
# File 'lib/blackstack-db/postgresql.rb', line 17

def self.db_url
    @@db_url
end

.db_userObject



26
27
28
# File 'lib/blackstack-db/postgresql.rb', line 26

def self.db_user
    @@db_user
end

.guidObject

return a postgresql uuid



104
105
106
# File 'lib/blackstack-db/postgresql.rb', line 104

def self.guid()
    DB['SELECT uuid_generate_v4() AS id'].first[:id]
end

.nowObject

return current datetime with format ‘%Y-%m-%d %H:%M:%S %Z`, using the timezone of the database (`select current_setting(’TIMEZONE’)‘) TODO: I am hardcoding the value of tz because for any reason `SELECT current_setting(’TIMEZONE’)‘ returns UTC instead of America/Argentina/Buenos_Aires when I run it from Ruby. Be sure your database is ALWAYS configured with the correct timezone.



111
112
113
114
# File 'lib/blackstack-db/postgresql.rb', line 111

def self.now()
    tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz]
    DB["SELECT current_timestamp at TIME ZONE '#{tz}' AS now"].first[:now]
end

.seconds_ago(n) ⇒ Object



116
117
118
119
# File 'lib/blackstack-db/postgresql.rb', line 116

def self.seconds_ago(n)
    tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz]
    DB["SELECT (current_timestamp - interval '#{n} seconds') at TIME ZONE '#{tz}' AS now"].first[:now]
end

.set_db_params(h) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/blackstack-db/postgresql.rb', line 36

def self.set_db_params(h)
    # validate: the parameter h is requred
    raise "The parameter h is required." if h.nil?

    # validate: the parameter h must be a hash
    raise "The parameter h must be a hash" unless h.is_a?(Hash)

    # validate: the :db_url key is required
    raise 'The key :db_url is required' unless h.has_key?(:db_url)

    # validate: the :db_port key is required
    raise 'The key :db_port is required' unless h.has_key?(:db_port)

    # validate: the db_name key is required
    raise 'The key :db_name is required' unless h.has_key?(:db_name)

    # validate: the db_user key is required
    raise 'The key :db_user is required' unless h.has_key?(:db_user)

    # validate: the db_password key is required
    raise 'The key :db_password is required' unless h.has_key?(:db_password)

                # validate: optional :max_connections key
                if h.has_key?(:max_connections)
                    value = h[:max_connections]
                    unless value.is_a?(Integer) || (value.is_a?(String) && value.to_i.to_s == value)
                        raise 'The key :max_connections must be an integer'
                    end
                    raise 'The key :max_connections must be greater than zero' unless value.to_i > 0
                end

    # validate: the :db_url key must be a string
    raise 'The key :db_url must be a string' unless h[:db_url].is_a?(String)

    # validate: the :db_port key must be an integer, or a string that can be converted to an integer
    raise 'The key :db_port must be an integer' unless h[:db_port].is_a?(Integer) || (h[:db_port].is_a?(String) && h[:db_port].to_i.to_s == h[:db_port])

    # validate: the :db_name key must be a string
    raise 'The key :db_name must be a string' unless h[:db_name].is_a?(String)

    # validate: the :db_user key must be a string
    raise 'The key :db_user must be a string' unless h[:db_user].is_a?(String)

    # validate: the :db_password key must be a string
    raise 'The key :db_password must be a string' unless h[:db_password].is_a?(String)

    # set type
    BlackStack::set_db_type(BlackStack::TYPE_POSTGRESQL)

    # map values
    @@db_url = h[:db_url]
    @@db_port = h[:db_port].to_i
    @@db_name = h[:db_name]
    @@db_user = h[:db_user]
    @@db_password = h[:db_password]
    @@db_max_connections = h[:max_connections] ? h[:max_connections].to_i : nil
end

.test(l = nil) ⇒ Object

test the connection to the database. raise an exception if the connection fails, or if any incongruence is found.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/blackstack-db/postgresql.rb', line 123

def self.test(l=nil)
    l = BlackStack::DummyLogger.new if l.nil?
    @db = nil

    #l.log "Connection String:"
    #l.log BlackStack::PostgreSQL::connection_string
    
    l.logs "Testing connection... "
    begin
        @db = BlackStack::PostgreSQL::connect
        l.logf "success".green
    rescue => e
        l.logf "failed".red
        l.log e.message
    end
    
    # if the name of databsase in `config.rb` is wrong, the connection is made to the defaultdb.
    # This validation checks the connection to the correct database.
    begin
        l.logs "Verify database name... "
        s = @db["SELECT datname FROM pg_database WHERE datname='#{BlackStack::PostgreSQL::db_name}'"].first
        raise 'Wrong database name' if s.nil?
        l.logf "success".green
    rescue => e
        l.logf "failed".red
        l.log e.message
    end
end