Skip to content

Commit 4b2174d

Browse files
tsvallenderrafaelfranca
authored andcommitted
Allow use of alternative database interfaces
The CLI tool used as a database interface is now specified and customisable via ActiveRecord.database_cli. This specifies the current defaults but allows them to be overridden by users. It continues to accept array values to allow fallback options.
1 parent d4df3d5 commit 4b2174d

File tree

9 files changed

+62
-3
lines changed

9 files changed

+62
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Support use of alternative database interfaces via the `database_cli` ActiveRecord configuration option.
2+
3+
```ruby
4+
Rails.application.configure do
5+
config.active_record.database_cli = { postgresql: "pgcli" }
6+
end
7+
```
8+
9+
*T S Vallender*
10+
111
* Add support for dumping table inheritance and native partitioning table definitions for PostgeSQL adapter
212

313
*Justin Talbott*

activerecord/lib/active_record.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def self.schema_cache_ignored_table?(table_name)
207207
end
208208
end
209209

210+
singleton_class.attr_accessor :database_cli
211+
self.database_cli = { postgresql: "psql", mysql: %w[mysql mysql5], sqlite: "sqlite3" }
212+
210213
singleton_class.attr_reader :default_timezone
211214

212215
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def dbconsole(config, options = {})
7979

8080
args << config.database
8181

82-
find_cmd_and_exec(["mysql", "mysql5"], *args)
82+
find_cmd_and_exec(ActiveRecord.database_cli[:mysql], *args)
8383
end
8484
end
8585

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def dbconsole(config, options = {})
8686
"-c #{name}=#{value.to_s.gsub(/[ \\]/, '\\\\\0')}" unless value == ":default" || value == :default
8787
end.join(" ")
8888
end
89-
find_cmd_and_exec("psql", config.database)
89+
find_cmd_and_exec(ActiveRecord.database_cli[:postgresql], config.database)
9090
end
9191
end
9292

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def dbconsole(config, options = {})
4545
args << "-header" if options[:header]
4646
args << File.expand_path(config.database, Rails.respond_to?(:root) ? Rails.root : nil)
4747

48-
find_cmd_and_exec("sqlite3", *args)
48+
find_cmd_and_exec(ActiveRecord.database_cli[:sqlite], *args)
4949
end
5050
end
5151

activerecord/test/cases/adapters/mysql2/dbconsole_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ def test_mysql_include_password
6363
end
6464
end
6565

66+
def test_mysql_can_use_alternative_cli
67+
ActiveRecord.database_cli[:mysql] = "mycli"
68+
config = make_db_config(adapter: "mysql2", database: "db", database_cli: "mycli")
69+
70+
assert_find_cmd_and_exec_called_with(["mycli", "db"]) do
71+
Mysql2Adapter.dbconsole(config)
72+
end
73+
ensure
74+
ActiveRecord.database_cli[:mysql] = %w[mysql mysql5]
75+
end
76+
6677
private
6778
def make_db_config(config)
6879
ActiveRecord::DatabaseConfigurations::HashConfig.new("test", "primary", config)

activerecord/test/cases/adapters/postgresql/dbconsole_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ def test_postgresql_include_variables
7878
assert_equal "-c search_path=my_schema,\\ default,\\ \\\\my_schema -c statement_timeout=5000", ENV["PGOPTIONS"]
7979
end
8080

81+
def test_postgresql_can_use_alternative_cli
82+
ActiveRecord.database_cli[:postgresql] = "pgcli"
83+
config = make_db_config(adapter: "postgresql", database: "db")
84+
85+
assert_find_cmd_and_exec_called_with(["pgcli", "db"]) do
86+
PostgreSQLAdapter.dbconsole(config)
87+
end
88+
ensure
89+
ActiveRecord.database_cli[:postgresql] = "psql"
90+
end
91+
8192
private
8293
def preserve_pg_env
8394
old_values = ENV_VARS.map { |var| ENV[var] }

activerecord/test/cases/adapters/sqlite3/dbconsole_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ def test_sqlite3_db_with_defined_rails_root
5252
Rails.singleton_class.remove_method(:root)
5353
end
5454

55+
def test_sqlite3_can_use_alternative_cli
56+
ActiveRecord.database_cli[:sqlite] = "sqlitecli"
57+
config = make_db_config(adapter: "sqlite3", database: "config/db.sqlite3", database_cli: "sqlitecli")
58+
59+
assert_find_cmd_and_exec_called_with(["sqlitecli", root.join("config/db.sqlite3").to_s]) do
60+
SQLite3Adapter.dbconsole(config)
61+
end
62+
ensure
63+
ActiveRecord.database_cli[:sqlite] = "sqlite3"
64+
end
65+
5566
private
5667
def root
5768
Pathname(__dir__).join("../../../..")

guides/source/configuring.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,19 @@ warning, or neither.
16331633
| --------------------- | -------------------- |
16341634
| (original) | `true` |
16351635

1636+
#### config.active_record.database_cli
1637+
1638+
Controls which CLI tool will be used for accessing the database when running `rails dbconsole`. By default
1639+
the standard tool for the database will be used (e.g. `psql` for PostgreSQL and `mysql` for MySQL). The option
1640+
takes a hash which specifies the tool per-database system, and an array can be used where fallback options are
1641+
required:
1642+
1643+
```ruby
1644+
# config/application.rb
1645+
1646+
config.active_record.database_cli = { postgresql: "pgcli", mysql: %w[ mycli mysql ] }
1647+
```
1648+
16361649
#### `ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans` and `ActiveRecord::ConnectionAdapters::TrilogyAdapter.emulate_booleans`
16371650

16381651
Controls whether the Active Record MySQL adapter will consider all `tinyint(1)` columns as booleans. Defaults to `true`.

0 commit comments

Comments
 (0)