Module: BlackStack::CRDB
- Defined in:
- lib/blackstack-db/crdb.rb
Constant Summary collapse
- @@db_url =
database connection parameters
nil
- @@db_port =
nil
- @@db_cluster =
nil
- @@db_name =
nil
- @@db_user =
nil
- @@db_password =
nil
- @@db_sslmode =
nil
Class Method Summary collapse
-
.connect ⇒ Object
create database connection.
-
.connection_string ⇒ Object
return the connection string for a postgresql database.
-
.connection_string_2 ⇒ Object
return the connection string for a postgresql database.
- .db_cluster ⇒ Object
- .db_name ⇒ Object
- .db_password ⇒ Object
- .db_port ⇒ Object
- .db_sslmode ⇒ 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.
- .set_db_params(h) ⇒ Object
-
.test(l = nil) ⇒ Object
test the connection to the database.
Class Method Details
.connect ⇒ Object
create database connection
32 33 34 35 |
# File 'lib/blackstack-db/crdb.rb', line 32 def self.connect BlackStack::set_db_type(BlackStack::TYPE_CRDB) Sequel.connect(BlackStack::CRDB.connection_string) end |
.connection_string ⇒ Object
return the connection string for a postgresql database
13 14 15 16 17 18 19 20 21 |
# File 'lib/blackstack-db/crdb.rb', line 13 def self.connection_string ret = nil if @@db_cluster ret = "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_cluster}.#{@@db_name}?sslmode=#{@@db_sslmode}" else ret = "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_name}?sslmode=#{@@db_sslmode}" end ret end |
.connection_string_2 ⇒ Object
return the connection string for a postgresql database
DEPRECATED!
27 28 29 |
# File 'lib/blackstack-db/crdb.rb', line 27 def self.connection_string_2 "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}?sslmode=#{@@db_sslmode}&options=--cluster%3D#{@@db_cluster}" end |
.db_cluster ⇒ Object
44 45 46 |
# File 'lib/blackstack-db/crdb.rb', line 44 def self.db_cluster @@db_cluster end |
.db_name ⇒ Object
47 48 49 |
# File 'lib/blackstack-db/crdb.rb', line 47 def self.db_name @@db_name end |
.db_password ⇒ Object
53 54 55 |
# File 'lib/blackstack-db/crdb.rb', line 53 def self.db_password @@db_password end |
.db_port ⇒ Object
41 42 43 |
# File 'lib/blackstack-db/crdb.rb', line 41 def self.db_port @@db_port end |
.db_sslmode ⇒ Object
56 57 58 |
# File 'lib/blackstack-db/crdb.rb', line 56 def self.db_sslmode @@db_sslmode end |
.db_url ⇒ Object
database connection getters
38 39 40 |
# File 'lib/blackstack-db/crdb.rb', line 38 def self.db_url @@db_url end |
.db_user ⇒ Object
50 51 52 |
# File 'lib/blackstack-db/crdb.rb', line 50 def self.db_user @@db_user end |
.guid ⇒ Object
return a postgresql uuid
110 111 112 |
# File 'lib/blackstack-db/crdb.rb', line 110 def self.guid() DB['SELECT gen_random_uuid() 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.
117 118 119 120 |
# File 'lib/blackstack-db/crdb.rb', line 117 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 |
.set_db_params(h) ⇒ Object
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/blackstack-db/crdb.rb', line 59 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_CRDB) # map values @@db_url = h[:db_url] @@db_port = h[:db_port].to_i @@db_cluster = h[:db_cluster] # default is nil @@db_name = h[:db_name] @@db_user = h[:db_user] @@db_password = h[:db_password] @@db_sslmode = h[:db_sslmode] || 'verify-full' # default is verify-full end |
.test(l = nil) ⇒ Object
test the connection to the database. raise an exception if the connection fails, or if any incongruence is found.
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 151 |
# File 'lib/blackstack-db/crdb.rb', line 124 def self.test(l=nil) l = BlackStack::DummyLogger.new if l.nil? @db = nil #l.log "Connection String:" #l.log BlackStack::TYPE_CRDB::connection_string l.logs "Testing connection... " begin @db = BlackStack::CRDB::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["SHOW DATABASES"].all.map { |x| x[:database_name] }.join("\n") raise 'Wrong database name'.red if s !~ /#{Regexp.escape(BlackStack::CRDB::db_name)}/i l.logf "success".green rescue => e l.logf "failed".red l.log e. end end |