blackstack-db 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/blackstack-db/crdb.rb +153 -0
- data/lib/blackstack-db/postgresql.rb +135 -0
- data/lib/blackstack-db.rb +39 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0071d134f56134425dfa5667077f9ca8ee1d57fc4c23783be3ad6fb05afca2a9
|
4
|
+
data.tar.gz: 694fe13b831760553ad8736a120d56615650f437f4fa3987711bb5534d1783b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: adc65af017b86399f60d1015f474fdf18565fffa4ee25c420bc1ac51f9257cbca87407dca6e7b436faf706e4f2828b1445a90ef9e76c32ba4b2454f439107c31
|
7
|
+
data.tar.gz: 9c16d76967f2eb07a74e32c12fc984e925e844e934ca3079ef8d52a6f5f8b8adc38a331466e51ec098500683b174879dd3c8a476024f8c3245141d59117ab82f
|
@@ -0,0 +1,153 @@
|
|
1
|
+
module BlackStack
|
2
|
+
module CRDB
|
3
|
+
# database connection parameters
|
4
|
+
@@db_url = nil
|
5
|
+
@@db_port = nil
|
6
|
+
@@db_cluster = nil
|
7
|
+
@@db_name = nil
|
8
|
+
@@db_user = nil
|
9
|
+
@@db_password = nil
|
10
|
+
@@db_sslmode = nil
|
11
|
+
|
12
|
+
# return the connection string for a postgresql database
|
13
|
+
def self.connection_string
|
14
|
+
ret = nil
|
15
|
+
if @@db_cluster
|
16
|
+
ret = "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_cluster}.#{@@db_name}?sslmode=#{@@db_sslmode}"
|
17
|
+
else
|
18
|
+
ret = "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_name}?sslmode=#{@@db_sslmode}"
|
19
|
+
end
|
20
|
+
ret
|
21
|
+
end # connection_string
|
22
|
+
|
23
|
+
# return the connection string for a postgresql database
|
24
|
+
#
|
25
|
+
# DEPRECATED!
|
26
|
+
#
|
27
|
+
def self.connection_string_2
|
28
|
+
"postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}?sslmode=#{@@db_sslmode}&options=--cluster%3D#{@@db_cluster}"
|
29
|
+
end # connection_string
|
30
|
+
|
31
|
+
# create database connection
|
32
|
+
def self.connect
|
33
|
+
BlackStack::set_db_type(BlackStack::TYPE_CRDB)
|
34
|
+
Sequel.connect(BlackStack::CRDB.connection_string)
|
35
|
+
end
|
36
|
+
|
37
|
+
# database connection getters
|
38
|
+
def self.db_url
|
39
|
+
@@db_url
|
40
|
+
end
|
41
|
+
def self.db_port
|
42
|
+
@@db_port
|
43
|
+
end
|
44
|
+
def self.db_cluster
|
45
|
+
@@db_cluster
|
46
|
+
end
|
47
|
+
def self.db_name
|
48
|
+
@@db_name
|
49
|
+
end
|
50
|
+
def self.db_user
|
51
|
+
@@db_user
|
52
|
+
end
|
53
|
+
def self.db_password
|
54
|
+
@@db_password
|
55
|
+
end
|
56
|
+
def self.db_sslmode
|
57
|
+
@@db_sslmode
|
58
|
+
end
|
59
|
+
def self.set_db_params(h)
|
60
|
+
# validate: the parameter h is requred
|
61
|
+
raise "The parameter h is required." if h.nil?
|
62
|
+
|
63
|
+
# validate: the parameter h must be a hash
|
64
|
+
raise "The parameter h must be a hash" unless h.is_a?(Hash)
|
65
|
+
|
66
|
+
# validate: the :db_url key is required
|
67
|
+
raise 'The key :db_url is required' unless h.has_key?(:db_url)
|
68
|
+
|
69
|
+
# validate: the :db_port key is required
|
70
|
+
raise 'The key :db_port is required' unless h.has_key?(:db_port)
|
71
|
+
|
72
|
+
# validate: the db_name key is required
|
73
|
+
raise 'The key :db_name is required' unless h.has_key?(:db_name)
|
74
|
+
|
75
|
+
# validate: the db_user key is required
|
76
|
+
raise 'The key :db_user is required' unless h.has_key?(:db_user)
|
77
|
+
|
78
|
+
# validate: the db_password key is required
|
79
|
+
raise 'The key :db_password is required' unless h.has_key?(:db_password)
|
80
|
+
|
81
|
+
# validate: the :db_url key must be a string
|
82
|
+
raise 'The key :db_url must be a string' unless h[:db_url].is_a?(String)
|
83
|
+
|
84
|
+
# validate: the :db_port key must be an integer, or a string that can be converted to an integer
|
85
|
+
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])
|
86
|
+
|
87
|
+
# validate: the :db_name key must be a string
|
88
|
+
raise 'The key :db_name must be a string' unless h[:db_name].is_a?(String)
|
89
|
+
|
90
|
+
# validate: the :db_user key must be a string
|
91
|
+
raise 'The key :db_user must be a string' unless h[:db_user].is_a?(String)
|
92
|
+
|
93
|
+
# validate: the :db_password key must be a string
|
94
|
+
raise 'The key :db_password must be a string' unless h[:db_password].is_a?(String)
|
95
|
+
|
96
|
+
# map values
|
97
|
+
@@db_url = h[:db_url]
|
98
|
+
@@db_port = h[:db_port].to_i
|
99
|
+
@@db_cluster = h[:db_cluster] # default is nil
|
100
|
+
@@db_name = h[:db_name]
|
101
|
+
@@db_user = h[:db_user]
|
102
|
+
@@db_password = h[:db_password]
|
103
|
+
@@db_sslmode = h[:db_sslmode] || 'verify-full' # default is verify-full
|
104
|
+
end # set_db_params
|
105
|
+
|
106
|
+
# return a postgresql uuid
|
107
|
+
def self.guid()
|
108
|
+
DB['SELECT gen_random_uuid() AS id'].first[:id]
|
109
|
+
end
|
110
|
+
|
111
|
+
# return current datetime with format `%Y-%m-%d %H:%M:%S %Z`, using the timezone of the database (`select current_setting('TIMEZONE')`)
|
112
|
+
# TODO: I am hardcoding the value of `tz` because for any reason `SELECT current_setting('TIMEZONE')` returns `UTC` instead of
|
113
|
+
# `America/Argentina/Buenos_Aires` when I run it from Ruby. Be sure your database is ALWAYS configured with the correct timezone.
|
114
|
+
def self.now()
|
115
|
+
tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz]
|
116
|
+
DB["SELECT current_timestamp() at TIME ZONE '#{tz}' AS now"].first[:now]
|
117
|
+
end
|
118
|
+
=begin
|
119
|
+
# test the connection to the database.
|
120
|
+
# raise an exception if the connection fails, or if any incongruence is found.
|
121
|
+
def self.test(l=nil)
|
122
|
+
l = BlackStack::DummyLogger.new if l.nil?
|
123
|
+
@db = nil
|
124
|
+
|
125
|
+
#l.log "Connection String:"
|
126
|
+
#l.log BlackStack::TYPE_CRDB::connection_string
|
127
|
+
|
128
|
+
l.logs "Testing connection... "
|
129
|
+
begin
|
130
|
+
@db = BlackStack::Deployer::DB::connect(
|
131
|
+
BlackStack::TYPE_CRDB::connection_string # use the connection parameters setting in ./config.rb
|
132
|
+
)
|
133
|
+
l.logf "success"
|
134
|
+
rescue => e
|
135
|
+
l.logf "failed"
|
136
|
+
l.log e.message
|
137
|
+
end
|
138
|
+
|
139
|
+
# if the name of databsase in `config.rb` is wrong, the connection is made to the defaultdb.
|
140
|
+
# This validation checks the connection to the correct database.
|
141
|
+
begin
|
142
|
+
l.logs "Verify database name... "
|
143
|
+
s = @db["SHOW DATABASES"].first.to_s
|
144
|
+
raise 'Wrong database name' if s !~ /:database_name=>\"#{Regexp.escape(BlackStack::TYPE_CRDB::db_name)}\"/i
|
145
|
+
l.logf "success"
|
146
|
+
rescue => e
|
147
|
+
l.logf "failed"
|
148
|
+
l.log e.message
|
149
|
+
end
|
150
|
+
end
|
151
|
+
=end
|
152
|
+
end # module CRDB
|
153
|
+
end # module BlackStack
|
@@ -0,0 +1,135 @@
|
|
1
|
+
module BlackStack
|
2
|
+
module PostgreSQL
|
3
|
+
# database connection parameters
|
4
|
+
@@db_url = nil
|
5
|
+
@@db_port = nil
|
6
|
+
@@db_name = nil
|
7
|
+
@@db_user = nil
|
8
|
+
@@db_password = nil
|
9
|
+
|
10
|
+
# return the connection string for a postgresql database
|
11
|
+
def self.connection_string
|
12
|
+
"postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_name}"
|
13
|
+
end # connection_string
|
14
|
+
|
15
|
+
# database connection getters
|
16
|
+
def self.db_url
|
17
|
+
@@db_url
|
18
|
+
end
|
19
|
+
def self.db_port
|
20
|
+
@@db_port
|
21
|
+
end
|
22
|
+
def self.db_name
|
23
|
+
@@db_name
|
24
|
+
end
|
25
|
+
def self.db_user
|
26
|
+
@@db_user
|
27
|
+
end
|
28
|
+
def self.db_password
|
29
|
+
@@db_password
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.set_db_params(h)
|
33
|
+
# validate: the parameter h is requred
|
34
|
+
raise "The parameter h is required." if h.nil?
|
35
|
+
|
36
|
+
# validate: the parameter h must be a hash
|
37
|
+
raise "The parameter h must be a hash" unless h.is_a?(Hash)
|
38
|
+
|
39
|
+
# validate: the :db_url key is required
|
40
|
+
raise 'The key :db_url is required' unless h.has_key?(:db_url)
|
41
|
+
|
42
|
+
# validate: the :db_port key is required
|
43
|
+
raise 'The key :db_port is required' unless h.has_key?(:db_port)
|
44
|
+
|
45
|
+
# validate: the db_name key is required
|
46
|
+
raise 'The key :db_name is required' unless h.has_key?(:db_name)
|
47
|
+
|
48
|
+
# validate: the db_user key is required
|
49
|
+
raise 'The key :db_user is required' unless h.has_key?(:db_user)
|
50
|
+
|
51
|
+
# validate: the db_password key is required
|
52
|
+
raise 'The key :db_password is required' unless h.has_key?(:db_password)
|
53
|
+
|
54
|
+
# validate: the :db_url key must be a string
|
55
|
+
raise 'The key :db_url must be a string' unless h[:db_url].is_a?(String)
|
56
|
+
|
57
|
+
# validate: the :db_port key must be an integer, or a string that can be converted to an integer
|
58
|
+
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])
|
59
|
+
|
60
|
+
# validate: the :db_name key must be a string
|
61
|
+
raise 'The key :db_name must be a string' unless h[:db_name].is_a?(String)
|
62
|
+
|
63
|
+
# validate: the :db_user key must be a string
|
64
|
+
raise 'The key :db_user must be a string' unless h[:db_user].is_a?(String)
|
65
|
+
|
66
|
+
# validate: the :db_password key must be a string
|
67
|
+
raise 'The key :db_password must be a string' unless h[:db_password].is_a?(String)
|
68
|
+
|
69
|
+
# map values
|
70
|
+
@@db_url = h[:db_url]
|
71
|
+
@@db_port = h[:db_port].to_i
|
72
|
+
@@db_name = h[:db_name]
|
73
|
+
@@db_user = h[:db_user]
|
74
|
+
@@db_password = h[:db_password]
|
75
|
+
end # set_db_params
|
76
|
+
|
77
|
+
# create database connection
|
78
|
+
def self.connect
|
79
|
+
BlackStack::set_db_type(BlackStack::TYPE_POSTGRESQL)
|
80
|
+
Sequel.connect(BlackStack::PostgreSQL.connection_string)
|
81
|
+
end
|
82
|
+
|
83
|
+
# return a postgresql uuid
|
84
|
+
def self.guid()
|
85
|
+
DB['SELECT uuid_generate_v4() AS id'].first[:id]
|
86
|
+
end
|
87
|
+
|
88
|
+
# return current datetime with format `%Y-%m-%d %H:%M:%S %Z`, using the timezone of the database (`select current_setting('TIMEZONE')`)
|
89
|
+
# TODO: I am hardcoding the value of `tz` because for any reason `SELECT current_setting('TIMEZONE')` returns `UTC` instead of
|
90
|
+
# `America/Argentina/Buenos_Aires` when I run it from Ruby. Be sure your database is ALWAYS configured with the correct timezone.
|
91
|
+
def self.now()
|
92
|
+
tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz]
|
93
|
+
DB["SELECT current_timestamp at TIME ZONE '#{tz}' AS now"].first[:now]
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.seconds_ago(n)
|
97
|
+
tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz]
|
98
|
+
DB["SELECT (current_timestamp - interval '#{n} seconds') at TIME ZONE '#{tz}' AS now"].first[:now]
|
99
|
+
end
|
100
|
+
=begin
|
101
|
+
# test the connection to the database.
|
102
|
+
# raise an exception if the connection fails, or if any incongruence is found.
|
103
|
+
def self.test(l=nil)
|
104
|
+
l = BlackStack::DummyLogger.new if l.nil?
|
105
|
+
@db = nil
|
106
|
+
|
107
|
+
#l.log "Connection String:"
|
108
|
+
#l.log BlackStack::PostgreSQL::connection_string
|
109
|
+
|
110
|
+
l.logs "Testing connection... "
|
111
|
+
begin
|
112
|
+
@db = BlackStack::Deployer::DB::connect(
|
113
|
+
BlackStack::PostgreSQL::connection_string # use the connection parameters setting in ./config.rb
|
114
|
+
)
|
115
|
+
l.logf "success"
|
116
|
+
rescue => e
|
117
|
+
l.logf "failed"
|
118
|
+
l.log e.message
|
119
|
+
end
|
120
|
+
|
121
|
+
# if the name of databsase in `config.rb` is wrong, the connection is made to the defaultdb.
|
122
|
+
# This validation checks the connection to the correct database.
|
123
|
+
begin
|
124
|
+
l.logs "Verify database name... "
|
125
|
+
s = @db["SHOW DATABASES"].first.to_s
|
126
|
+
raise 'Wrong database name' if s !~ /:database_name=>\"#{Regexp.escape(BlackStack::PostgreSQL::db_name)}\"/i
|
127
|
+
l.logf "success"
|
128
|
+
rescue => e
|
129
|
+
l.logf "failed"
|
130
|
+
l.log e.message
|
131
|
+
end
|
132
|
+
end
|
133
|
+
=end
|
134
|
+
end # module PostgreSQL
|
135
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require_relative './blackstack-db/postgresql.rb'
|
3
|
+
require_relative './blackstack-db/crdb.rb'
|
4
|
+
|
5
|
+
module BlackStack
|
6
|
+
TYPE_POSTGRESQL = 0
|
7
|
+
TYPE_CRDB = 1
|
8
|
+
@@db_type = nil
|
9
|
+
|
10
|
+
def self.set_db_type(n)
|
11
|
+
@@db_type = n
|
12
|
+
end # set_db_type
|
13
|
+
|
14
|
+
def self.db_type
|
15
|
+
@@db_type
|
16
|
+
end # db_type
|
17
|
+
end # BlackStack
|
18
|
+
|
19
|
+
# return a postgresql uuid
|
20
|
+
def guid()
|
21
|
+
if BlackStack.db_type == BlackStack::TYPE_POSTGRESQL
|
22
|
+
return BlackStack::PostgreSQL.guid
|
23
|
+
elsif BlackStack.db_type == BlackStack::TYPE_CRDB
|
24
|
+
return BlackStack::CRDB.guid
|
25
|
+
else
|
26
|
+
raise "Unknown database type"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# return current datetime with format `YYYY-MM-DD HH:MM:SS`
|
31
|
+
def now()
|
32
|
+
if BlackStack.db_type == BlackStack::TYPE_POSTGRESQL
|
33
|
+
return BlackStack::PostgreSQL.now
|
34
|
+
elsif BlackStack.db_type == BlackStack::TYPE_CRDB
|
35
|
+
return BlackStack::CRDB.now
|
36
|
+
else
|
37
|
+
raise "Unknown database type"
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blackstack-db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leandro Daniel Sardi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.56.0
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 5.56.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 5.56.0
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 5.56.0
|
33
|
+
description: 'Modules for simple setup of database connections: https://github.com/leandrosardi/blackstack-db.'
|
34
|
+
email: leandro@connectionsphere.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- lib/blackstack-db.rb
|
40
|
+
- lib/blackstack-db/crdb.rb
|
41
|
+
- lib/blackstack-db/postgresql.rb
|
42
|
+
homepage: https://rubygems.org/gems/blackstack-db
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.3.7
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Modules for simple setup of database connections.
|
65
|
+
test_files: []
|