简体   繁体   English

在rake任务中使用database.yml而不是提供用户/密码

[英]Using database.yml in rake tasks instead of providing user/pass

How do I convert (this works): 如何转换(有效):

desc "Import Bar"
task :import_bar => :environment do
  OCI8.new('user', 'pass', 'database').exec('select foo from bar') do |r| 
    puts r
  end
end

To something like this (not working): 对于这样的事情(不起作用):

desc "Import Bar"
task :import_bar => :environment do
  ActiveRecord::Base.connection.execute('select foo from bar') do |r|
    puts r
  end
end

database.yml database.yml的

development:
  adapter: oracle_enhanced
  database: database
  username: user
  password: pass
  pool: 5
  timeout: 5000

rake import_bar

no output 没有输出

You forgot to iterate through the objects using fetch, try: 您忘记使用fetch遍历对象,请尝试:

desc "Import Bar"
task :import_bar => :environment do
  ActiveRecord::Base.connection.execute('select foo from bar').fetch() do |r|
    r.join(',')
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 瑞克任务似乎忽略了database.yml配置 - Rake tasks seem to ignore database.yml configuration 瑞克任务将database.example.yml复制到database.yml - Rake task to copy database.example.yml to database.yml 需要从database.yml文件中获取用户名和密码,并在rake任务中传入方法 - Need to get usernames and passwords from database.yml file and pass into method in rake task 关于数据库适配器与database.yml不一致的问题 - rake about database adapter inconsistent with database.yml 通过rake任务访问database.yml信息 - Accessing database.yml info through a rake task 部署时.gitignore中的/config/database.yml找不到/config/database.yml,应该改用/shared/database.yml,怎么办? - /config/database.yml in .gitignore on deploy /config/database.yml not found should use /shared/database.yml instead, how? 在database.yml中使用erb会破坏设计 - Using erb in database.yml breaks Devise Postgres连接失败,使用系统用户代替database.yml中提供的用户 - Postgres connection fails, uses system user instead the one provided in database.yml Mysql2 ::使用database.yml中的env变量“拒绝用户访问” - Mysql2::Error “Access denied for user” using env variable in database.yml database.yml文件配置和postgres - rake db:drop db:create db:migrate - database.yml file configuration and postgres - rake db:drop db:create db:migrate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM