我正在编写一个在 Rails/ActiveRecord 之外执行一些数据库工作的 rake 任务。
有没有办法获取当前环境中定义的数据库连接信息(主机、用户名、密码、数据库名称)database.yml
?
我想得到它,这样我就可以用它来像这样连接......
con = Mysql.real_connect("host", "user", "pw", "current_db")
我正在编写一个在 Rails/ActiveRecord 之外执行一些数据库工作的 rake 任务。
有没有办法获取当前环境中定义的数据库连接信息(主机、用户名、密码、数据库名称)database.yml
?
我想得到它,这样我就可以用它来像这样连接......
con = Mysql.real_connect("host", "user", "pw", "current_db")
在 rails 中,您可以创建一个配置对象并从中获取必要的信息:
config = Rails.configuration.database_configuration
host = config[Rails.env]["host"]
database = config[Rails.env]["database"]
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]
有关详细信息,请参阅 Rails::Configuration 的文档。
这只是使用YAML::load从数据库配置文件 ( database.yml
) 加载配置,您可以使用它自己从 rails 环境外部获取信息:
require 'YAML'
info = YAML::load(IO.read("database.yml"))
print info["production"]["host"]
print info["production"]["database"]
...
布莱恩在上述评论中的回答值得更多曝光:
>> Rails.configuration.database_configuration[Rails.env]
=> {"encoding"=>"unicode", "username"=>"postgres", "adapter"=>"postgresql", "port"=>5432, "host"=>"localhost", "password"=>"postgres", "database"=>"mydb", "pool"=>5}
ActiveRecord::Base.connection_config
以散列形式返回连接配置:
=> {:adapter=>ADAPTER_NAME, :host=>HOST, :port=>PORT,
:database=>DB, :pool=>POOL, :username=>USERNAME,
:password=>PASSWORD}
正如tpett
他们在评论中所说:这个解决方案考虑了database.yml
从环境变量合并配置DATABASE_URL
。
我认为这是最简单的解决方案。经过一些测试(至少在 Rails 5.2 中)这将正确解析 DATABASE_URL。
ActiveRecord::Base.configurations[Rails.env]
从 Rails 6.1 开始,ActiveRecord::Base.connection_config
已弃用。较新的访问器是ActiveRecord::Base.connection_db_config
[1] pry(main)> ActiveRecord::Base.connection_db_config
=> #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fe04ae72e58
@configuration_hash=
{:adapter=>"postgresql",
:encoding=>"utf8",
:min_messages=>"WARNING",
:host=>"localhost",
:username=>"postgres",
:password=>"P@ssw0rd",
:port=>5432,
:database=>"myapp_development"},
@env_name="development",
@name="primary">
老问题,但这是我查找如何做到这一点的第一站,所以我认为这可能对其他人有所帮助。我通常在主目录中有 .my.cnf 文件。因此,在我的 database.yml 配置文件中使用 'parseconfig' gem 和一些 ERB 语法意味着我有动态文件,我可以很好地检查源代码控制并简化部署(在我的情况下)。另请注意常用套接字列表,这使我的应用程序更容易移动到可能具有不同 Unix 套接字路径的不同操作系统。
<%
require 'parseconfig'
c=ParseConfig.new('../../.my.cnf') %>
mysqlevn: &mysql
adapter: mysql
username: <%= c.params['client']['user'] %>
password: <%= c.params['client']['password'] %>
host: localhost
socket: <%= [
'/var/run/mysqld/mysqld.sock',
'/var/lib/mysql/mysql.sock',
'/tmp/mysqld.sock',
'/tmp/mysql.sock'].detect { |socket| File.exist?(socket) } %>
production:
database: app_production
<<: *mysql
development:
database: app_development
<<: *mysql
# Do not set this db to the same as development or production.
test:
database: app_test
<<: *mysql
参考:http ://effectif.com/articles/database-yml-should-be-checked-in
在 Rails 6.1+ 中,您可以使用:
ActiveRecord::Base.connection_db_config.configuration_hash
这将返回一个包含当前环境连接信息的哈希:
{
:adapter=>"postgresql",
:encoding=>"utf8",
:min_messages=>"WARNING",
:host=>"localhost",
:username=>"postgres",
:password=>"P@ssw0rd",
:port=>5432,
:database=>"my_app_development"
}