Skip to content

Commit

Permalink
Support rails dbconsole (#8)
Browse files Browse the repository at this point in the history
* Support `rails dbconsole`

* Bump to 0..11.0

* Rubocop
  • Loading branch information
lloydwatkin authored Apr 12, 2024
1 parent b436fbd commit c34e50c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
janus-ar (0.7.0)
janus-ar (0.11.0)

GEM
remote: http://rubygems.org/
Expand Down
16 changes: 16 additions & 0 deletions lib/active_record/connection_adapters/janus_mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def janus_mysql2_connection(config)
end
end

module ActiveRecord
class Base
def self.janus_mysql2_adapter_class
ActiveRecord::ConnectionAdapters::JanusMysql2Adapter
end
end
end

module ActiveRecord
module ConnectionAdapters
class JanusMysql2Adapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter
Expand All @@ -28,6 +36,14 @@ class JanusMysql2Adapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter

attr_reader :config

class << self
def dbconsole(config, options = {})
connection_config = Janus::DbConsoleConfig.new(config)

super(connection_config, options)
end
end

def initialize(*args)
args[0][:janus]['replica']['database'] = args[0][:database]
args[0][:janus]['primary']['database'] = args[0][:database]
Expand Down
1 change: 1 addition & 0 deletions lib/janus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Janus
autoload :Context, 'janus/context'
autoload :VERSION, 'janus/version'
autoload :DbConsoleConfig, 'janus/db_console_config'

module Logging
autoload :Subscriber, 'janus/logging/subscriber'
Expand Down
17 changes: 17 additions & 0 deletions lib/janus/db_console_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Janus
class DbConsoleConfig
def initialize(config)
@_config = config.configuration_hash
end

def configuration_hash
@_config[:janus]['replica'].symbolize_keys
end

def database
@_config[:database]
end
end
end
2 changes: 1 addition & 1 deletion lib/janus/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Janus
unless defined?(::Janus::VERSION)
module VERSION
MAJOR = 0
MINOR = 10
MINOR = 11
PATCH = 0
PRE = nil

Expand Down

0 comments on commit c34e50c

Please sign in to comment.