forked from trilogy-libraries/activerecord-trilogy-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request trilogy-libraries#55 from lorint/add_dbconsole_sup…
…port Fixes trilogy-libraries#53 - Implement dbconsole support
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module TrilogyAdapter | ||
module Rails | ||
module DBConsole | ||
class AdapterAdapter < SimpleDelegator | ||
def adapter | ||
"mysql" | ||
end | ||
end | ||
|
||
def db_config | ||
if super.adapter == "trilogy" | ||
AdapterAdapter.new(super) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Rails | ||
class DBConsole | ||
# require "rails/commands/dbconsole/dbconsole_command" | ||
if ActiveRecord.version < ::Gem::Version.new('6.1.a') | ||
alias _brick_start start | ||
def start | ||
ENV["RAILS_ENV"] ||= @options[:environment] || environment | ||
|
||
if config["adapter"] == "trilogy" | ||
begin | ||
::ActiveRecord::ConnectionAdapters::TrilogyAdapter.dbconsole(config, @options) | ||
rescue NotImplementedError | ||
abort "Unknown command-line client for #{db_config.database}." | ||
end | ||
else | ||
_brick_start | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/testing/method_call_assertions" | ||
|
||
module ActiveRecord | ||
class TrilogyTestCase < TestCase | ||
def self.run(*args) | ||
super | ||
end | ||
end | ||
|
||
module ConnectionAdapters | ||
class TrilogyDbConsoleTest < ActiveRecord::TrilogyTestCase | ||
include ActiveSupport::Testing::MethodCallAssertions | ||
|
||
def test_trilogy | ||
config = make_db_config(adapter: "trilogy", database: "db") | ||
|
||
assert_find_cmd_and_exec_called_with([%w[mysql mysql5], "db"]) do | ||
TrilogyAdapter.dbconsole(config) | ||
end | ||
end | ||
|
||
def test_mysql_full | ||
config = make_db_config( | ||
adapter: "trilogy", | ||
database: "db", | ||
host: "localhost", | ||
port: 1234, | ||
socket: "socket", | ||
username: "user", | ||
password: "qwerty", | ||
encoding: "UTF-8", | ||
sslca: "/path/to/ca-cert.pem", | ||
sslcert: "/path/to/client-cert.pem", | ||
sslcapath: "/path/to/cacerts", | ||
sslcipher: "DHE-RSA-AES256-SHA", | ||
sslkey: "/path/to/client-key.pem", | ||
ssl_mode: "VERIFY_IDENTITY" | ||
) | ||
|
||
args = [ | ||
%w[mysql mysql5], | ||
"--host=localhost", | ||
"--port=1234", | ||
"--socket=socket", | ||
"--user=user", | ||
"--default-character-set=UTF-8", | ||
"--ssl-ca=/path/to/ca-cert.pem", | ||
"--ssl-cert=/path/to/client-cert.pem", | ||
"--ssl-capath=/path/to/cacerts", | ||
"--ssl-cipher=DHE-RSA-AES256-SHA", | ||
"--ssl-key=/path/to/client-key.pem", | ||
"--ssl-mode=VERIFY_IDENTITY", | ||
"-p", "db" | ||
] | ||
|
||
assert_find_cmd_and_exec_called_with(args) do | ||
TrilogyAdapter.dbconsole(config) | ||
end | ||
end | ||
|
||
def test_mysql_include_password | ||
config = make_db_config(adapter: "trilogy", database: "db", username: "user", password: "qwerty") | ||
|
||
assert_find_cmd_and_exec_called_with([%w[mysql mysql5], "--user=user", "--password=qwerty", "db"]) do | ||
TrilogyAdapter.dbconsole(config, include_password: true) | ||
end | ||
end | ||
|
||
private | ||
def make_db_config(config) | ||
ActiveRecord::DatabaseConfigurations::HashConfig.new("test", "primary", config) | ||
end | ||
|
||
def assert_find_cmd_and_exec_called_with(args, &block) | ||
assert_called_with(TrilogyAdapter, :find_cmd_and_exec, args, &block) | ||
end | ||
end | ||
end | ||
end |