spectre-mysql 2.0.2 → 2.1.0
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 +4 -4
- data/lib/spectre/mysql.rb +43 -55
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1a957d159ca7f939e6710886b86c1cc613c91f820bb80a0125370490baa63cec
|
|
4
|
+
data.tar.gz: cf8797af1f3ce1029d6d557c03f92298ab06623e6fc7db808d1497f5f3264a47
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 60b752ef0adb33e8dccd977da8fce8be334b775b7bc09fac5b4271880dddfc594f0bdcb99b36e268b08a6c0d05bc692b63b95e5cfaa3114c7b3d353761a4e0be
|
|
7
|
+
data.tar.gz: 5cb6a0dad716fccbd96355e3e97eb72b7d393d5b47d0531f61b04d024a01ecbf2ba041e767979928eaebba3ab62f2e20c272ce213f2f4fa1d1b2308081149a5e
|
data/lib/spectre/mysql.rb
CHANGED
|
@@ -7,29 +7,33 @@ module Spectre
|
|
|
7
7
|
class MySqlQuery
|
|
8
8
|
include Spectre::Delegate if defined? Spectre::Delegate
|
|
9
9
|
|
|
10
|
-
def initialize
|
|
11
|
-
@
|
|
10
|
+
def initialize config
|
|
11
|
+
@__config = config
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def host hostname
|
|
15
|
-
@
|
|
15
|
+
@__config['host'] = hostname
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def username user
|
|
19
|
-
@
|
|
19
|
+
@__config['username'] = user
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def password pass
|
|
23
|
-
@
|
|
23
|
+
@__config['password'] = pass
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def database name
|
|
27
|
-
@
|
|
27
|
+
@__config['database'] = name
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def ssl mode
|
|
31
|
+
@__config['ssl'] = mode
|
|
28
32
|
end
|
|
29
33
|
|
|
30
34
|
def query statement
|
|
31
|
-
@
|
|
32
|
-
@
|
|
35
|
+
@__config['query'] = [] unless @__config.key? 'query'
|
|
36
|
+
@__config['query'].append(statement)
|
|
33
37
|
end
|
|
34
38
|
end
|
|
35
39
|
|
|
@@ -45,71 +49,55 @@ module Spectre
|
|
|
45
49
|
end
|
|
46
50
|
|
|
47
51
|
def mysql(name = nil, &)
|
|
48
|
-
|
|
52
|
+
config = {}
|
|
49
53
|
|
|
50
54
|
if !name.nil? and @config.key? name
|
|
51
|
-
|
|
55
|
+
config.merge! @config[name]
|
|
52
56
|
|
|
53
|
-
unless
|
|
57
|
+
unless config['host']
|
|
54
58
|
raise "No `host' set for MySQL client '#{name}'. Check your MySQL config in your environment."
|
|
55
59
|
end
|
|
56
60
|
|
|
57
61
|
elsif !name.nil?
|
|
58
|
-
|
|
59
|
-
elsif @last_conn
|
|
62
|
+
config['host'] = name
|
|
63
|
+
elsif @last_conn
|
|
64
|
+
config['host'] = @last_conn[:host]
|
|
65
|
+
config['username'] = @last_conn[:username]
|
|
66
|
+
config['password'] = @last_conn[:password]
|
|
67
|
+
config['database'] = @last_conn[:database]
|
|
68
|
+
config['ssl'] = @last_conn[:ssl_mode]
|
|
69
|
+
else
|
|
60
70
|
raise 'No name given and there was no previous MySQL connection to use'
|
|
61
71
|
end
|
|
62
72
|
|
|
63
|
-
MySqlQuery.new(
|
|
73
|
+
MySqlQuery.new(config).instance_eval(&) if block_given?
|
|
64
74
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
end
|
|
75
|
+
@last_conn = {
|
|
76
|
+
host: config['host'],
|
|
77
|
+
username: config['username'],
|
|
78
|
+
password: config['password'],
|
|
79
|
+
database: config['database'],
|
|
80
|
+
ssl_mode: (config['ssl'] || :required).to_sym,
|
|
81
|
+
}
|
|
73
82
|
|
|
74
|
-
@logger.info "Connecting to database #{
|
|
83
|
+
@logger.info "Connecting to database #{@last_conn[:username]}@#{@last_conn[:host]}:#{@last_conn[:database]}"
|
|
75
84
|
|
|
76
85
|
client = ::Mysql2::Client.new(**@last_conn)
|
|
77
86
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
query['query']&.each do |statement|
|
|
81
|
-
@logger.info("Executing statement '#{statement}'")
|
|
87
|
+
begin
|
|
88
|
+
res = nil
|
|
82
89
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
# FIXED: Safely convert rows to OpenStruct with error handling
|
|
89
|
-
# If casting is still problematic, we catch the error and retry with manual conversion
|
|
90
|
-
if res
|
|
91
|
-
begin
|
|
92
|
-
@result = res.map { |row| OpenStruct.new row }
|
|
93
|
-
rescue ArgumentError => e
|
|
94
|
-
raise unless e.message.include?('BigDecimal')
|
|
95
|
-
|
|
96
|
-
@logger.warn "BigDecimal casting error detected, falling back to safe conversion: #{e.message}"
|
|
97
|
-
# Manually convert each row, skipping problematic values
|
|
98
|
-
@result = res.map do |row|
|
|
99
|
-
safe_row = {}
|
|
100
|
-
row.each do |key, value|
|
|
101
|
-
safe_row[key] = begin
|
|
102
|
-
value.to_s
|
|
103
|
-
rescue StandardError
|
|
104
|
-
value
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
OpenStruct.new(safe_row)
|
|
108
|
-
end
|
|
90
|
+
config['query']&.each do |statement|
|
|
91
|
+
@logger.info("Executing statement '#{statement}'")
|
|
92
|
+
# cast: false returns all columns as strings — Spectre tests treat values as opaque,
|
|
93
|
+
# and this avoids mysql2's BigDecimal/Date casting failures on driver-level mismatches.
|
|
94
|
+
res = client.query(statement, cast: false, cast_booleans: false)
|
|
109
95
|
end
|
|
110
|
-
end
|
|
111
96
|
|
|
112
|
-
|
|
97
|
+
@result = res.map { |row| OpenStruct.new(row) } if res
|
|
98
|
+
ensure
|
|
99
|
+
client.close
|
|
100
|
+
end
|
|
113
101
|
end
|
|
114
102
|
|
|
115
103
|
def result
|