简体   繁体   中英

When using ActiveRecord::Database::Tasks outside of Rails, what do I need to configure to avoid a development database not configured error?

I'm calling ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, file) in a Ruby file script (essentially it's a Thor task) in a Ruby off Rails app and I get the following error:

/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'development' database is not configured. Available: [] (ActiveRecord::AdapterNotSpecified)

Does ActiveRecord::Tasks::DatabaseTasks give me the functions I need to set up a default database configuration outside of rails (I'm assuming I need to provide info similar to what's in database.yml in Rails)? If so, what are the functions I would need to call? I'm looking at http://api.rubyonrails.org/classes/ActiveRecord/Tasks/DatabaseTasks.html but it's a bit unclear to me.

在加载架构之前,您需要加载数据库配置(例如database.yml):

DatabaseTasks.database_configuration = YAML.load_file('my_database_config.yml')

I was able to get this to work in a non-Rails project with the following...

db_config = YAML.load_file('path/to/database.yml')
ActiveRecord::Tasks::DatabaseTasks.load_schema_for(
  db_config, :ruby, 'path/to/schema.rb')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM