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
Class Method Summary collapse
-
.connect ⇒ Object
create database connection.
-
.connection_string ⇒ Object
return the connection string for a postgresql database.
- .db_name ⇒ Object
- .db_password ⇒ Object
- .db_port ⇒ Object
-
.db_url ⇒ Object
database connection getters.
- .db_user ⇒ Object
-
.guid ⇒ Object
return a postgresql uuid.
-
.now ⇒ Object
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.
- .seconds_ago(n) ⇒ Object
- .set_db_params(h) ⇒ Object
-
.test(l = nil) ⇒ Object
test the connection to the database.
Class Method Details
.connect ⇒ Object
create database connection
81 82 83 84 85 |
# File 'lib/blackstack-db/postgresql.rb', line 81 def self.connect BlackStack::set_db_type(BlackStack::TYPE_POSTGRESQL) s = BlackStack::PostgreSQL.connection_string Sequel.connect(s) end |
.connection_string ⇒ Object
return the connection string for a postgresql database
11 12 13 |
# File 'lib/blackstack-db/postgresql.rb', line 11 def self.connection_string "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_name}" end |
.db_name ⇒ Object
22 23 24 |
# File 'lib/blackstack-db/postgresql.rb', line 22 def self.db_name @@db_name end |
.db_password ⇒ Object
28 29 30 |
# File 'lib/blackstack-db/postgresql.rb', line 28 def self.db_password @@db_password end |
.db_port ⇒ Object
19 20 21 |
# File 'lib/blackstack-db/postgresql.rb', line 19 def self.db_port @@db_port end |
.db_url ⇒ Object
database connection getters
16 17 18 |
# File 'lib/blackstack-db/postgresql.rb', line 16 def self.db_url @@db_url end |
.db_user ⇒ Object
25 26 27 |
# File 'lib/blackstack-db/postgresql.rb', line 25 def self.db_user @@db_user end |
.guid ⇒ Object
return a postgresql uuid
88 89 90 |
# File 'lib/blackstack-db/postgresql.rb', line 88 def self.guid() DB['SELECT uuid_generate_v4() AS id'].first[:id] end |
.now ⇒ Object
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.
95 96 97 98 |
# File 'lib/blackstack-db/postgresql.rb', line 95 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
100 101 102 103 |
# File 'lib/blackstack-db/postgresql.rb', line 100 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
32 33 34 35 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 |
# File 'lib/blackstack-db/postgresql.rb', line 32 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: 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] end |
.test(l = nil) ⇒ Object
test the connection to the database. raise an exception if the connection fails, or if any incongruence is found.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/blackstack-db/postgresql.rb', line 107 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. 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. end end |