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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/spectre/mysql.rb +43 -55
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06d437bc149a21da027b8388b21bb85462a2f4d04a4e3183c57447e6c2a5c756
4
- data.tar.gz: 79588cac00639fc145c5f08064fc1444f00280ec94efd78f9687e179824ab7d1
3
+ metadata.gz: 1a957d159ca7f939e6710886b86c1cc613c91f820bb80a0125370490baa63cec
4
+ data.tar.gz: cf8797af1f3ce1029d6d557c03f92298ab06623e6fc7db808d1497f5f3264a47
5
5
  SHA512:
6
- metadata.gz: dae040d6f4fb8f9db79cdf62eafd6074694b1730d248bb00289aa9719277bab0f96eda6150219374da8f68b125979a4fea90642253425c1d64c630a58e9ee18c
7
- data.tar.gz: 7c7c148a57c20fc03ce2e6421a524058ccd99d74f6d8975d3c5b55ac36149d62d0d73858ce4306e3c09718b3ec7c633e41409f672c5fcd0786054d80582dbcf2
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 query
11
- @__query = query
10
+ def initialize config
11
+ @__config = config
12
12
  end
13
13
 
14
14
  def host hostname
15
- @__query['host'] = hostname
15
+ @__config['host'] = hostname
16
16
  end
17
17
 
18
18
  def username user
19
- @__query['username'] = user
19
+ @__config['username'] = user
20
20
  end
21
21
 
22
22
  def password pass
23
- @__query['password'] = pass
23
+ @__config['password'] = pass
24
24
  end
25
25
 
26
26
  def database name
27
- @__query['database'] = name
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
- @__query['query'] = [] unless @__query.key? 'query'
32
- @__query['query'].append(statement)
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
- query = {}
52
+ config = {}
49
53
 
50
54
  if !name.nil? and @config.key? name
51
- query.merge! @config[name]
55
+ config.merge! @config[name]
52
56
 
53
- unless query['host']
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
- query['host'] = name
59
- elsif @last_conn.nil?
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(query).instance_eval(&) if block_given?
73
+ MySqlQuery.new(config).instance_eval(&) if block_given?
64
74
 
65
- unless name.nil?
66
- @last_conn = {
67
- host: query['host'],
68
- username: query['username'],
69
- password: query['password'],
70
- database: query['database'],
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 #{query['username']}@#{query['host']}:#{query['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
- res = []
79
-
80
- query['query']&.each do |statement|
81
- @logger.info("Executing statement '#{statement}'")
87
+ begin
88
+ res = nil
82
89
 
83
- # FIXED: Disable automatic casting to prevent BigDecimal errors
84
- # This prevents mysql2 from trying to cast VARCHAR columns to BigDecimal
85
- res = client.query(statement, cast: false, cast_booleans: false)
86
- end
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
- client.close
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spectre-mysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Neubauer