Module: Whodunit::Chronicles::Connection
- Included in:
- Processor
- Defined in:
- lib/whodunit/chronicles/connection.rb
Overview
Handles database connections for chronicles processing
Provides adapter-agnostic connection management for both PostgreSQL and MySQL
Instance Method Summary collapse
- #connection_active? ⇒ Boolean private
- #create_connection ⇒ Object private
- #detect_database_type(url) ⇒ Object private
- #ensure_connection ⇒ Object private
- #parse_mysql_url(url) ⇒ Object private
- #setup_connection_specifics ⇒ Object private
Instance Method Details
#connection_active? ⇒ Boolean (private)
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/whodunit/chronicles/connection.rb', line 59 def connection_active? case detect_database_type(@audit_database_url || Chronicles.config.database_url) when :postgresql @connection && !@connection.finished? when :mysql @connection&.ping else false end end |
#create_connection ⇒ Object (private)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/whodunit/chronicles/connection.rb', line 13 def create_connection audit_url = @audit_database_url || Chronicles.config.database_url case detect_database_type(audit_url) when :postgresql require 'pg' PG.connect(audit_url) when :mysql require 'trilogy' parsed = parse_mysql_url(audit_url) Trilogy.new( host: parsed[:host], port: parsed[:port] || 3306, username: parsed[:username], password: parsed[:password], database: parsed[:database], ssl: parsed[:ssl], ) else raise ConfigurationError, 'Unsupported database type for connection' end end |
#detect_database_type(url) ⇒ Object (private)
36 37 38 39 40 41 42 43 |
# File 'lib/whodunit/chronicles/connection.rb', line 36 def detect_database_type(url) return Chronicles.config.adapter unless url return :postgresql if url.start_with?('postgres://', 'postgresql://') return :mysql if url.start_with?('mysql://', 'mysql2://') # Fallback to configured adapter Chronicles.config.adapter end |
#ensure_connection ⇒ Object (private)
79 80 81 82 83 84 85 |
# File 'lib/whodunit/chronicles/connection.rb', line 79 def ensure_connection return if @connection && connection_active? @connection = create_connection setup_connection_specifics ensure_table_exists end |
#parse_mysql_url(url) ⇒ Object (private)
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/whodunit/chronicles/connection.rb', line 45 def parse_mysql_url(url) return {} if url.nil? || url.empty? uri = URI.parse(url) { host: uri.host, port: uri.port, username: uri.user, password: uri.password, database: uri.path&.sub('/', ''), ssl: uri.query&.include?('ssl=true'), } end |
#setup_connection_specifics ⇒ Object (private)
70 71 72 73 74 75 76 77 |
# File 'lib/whodunit/chronicles/connection.rb', line 70 def setup_connection_specifics case detect_database_type(@audit_database_url || Chronicles.config.database_url) when :postgresql @connection.type_map_for_results = PG::BasicTypeMapForResults.new(@connection) when :mysql # MySQL/Trilogy doesn't need special setup end end |