ruby - Rake task create not getting Rails.env in db:create for ActiveRecord>=4.05 -
ruby - Rake task create not getting Rails.env in db:create for ActiveRecord>=4.05 -
for activerecord 3.2.18, in /lib/active_record/railties/databases.rake:
task :load_config activerecord::base.configurations = rails.application.config.database_configuration activerecord::migrator.migrations_paths = rails.application.paths['db/migrate'].to_a if defined?(engine_path) && engine = rails::engine.find(engine_path) if engine.paths['db/migrate'].existent activerecord::migrator.migrations_paths += engine.paths['db/migrate'].to_a end end end desc 'create database database_url or config/database.yml current rails.env (use db:create:all create dbs in config)' task :create => [:load_config, :rails_env] if env['database_url'] create_database(database_url_config) else configs_for_environment.each { |config| create_database(config) } activerecord::base.establish_connection(configs_for_environment.first) end end
for activerecord 4.0.5+, in /lib/active_record/railties/databases.rake:
task :load_config activerecord::base.configurations = activerecord::tasks::databasetasks.database_configuration || {} activerecord::migrator.migrations_paths = activerecord::tasks::databasetasks.migrations_paths end desc 'create database database_url or config/database.yml current rails.env (use db:create:all create dbs in config)' task :create => [:load_config] if env['database_url'] activerecord::tasks::databasetasks.create_database_url else activerecord::tasks::databasetasks.create_current end end
when phone call bundle exec rake db:create rakefile (for gem i'm testing), activerecord 3.2.18, activerecord::base.configurations gets info needs test/config/database.yml file via rails.application.config.database_configuration. when calling db:create using activerecord 4.0.5+, activerecord::adapternotspecified: database configuration not specify adapter error. doesn't matter whether phone call rails_env=some_environment. how give activerecord 4.0.5+ database configuration needs without monkey patching it? ideal solution somehow rakefile.
i had same problem, , "fixed" monkey patch:
in rakefile, after imported active_record, did following:
# hack create works sqlite3 module rails def self.root file.dirname(__file__) end def self.env "development" end end
for sure can whatever want within "self.env" method.
ruby-on-rails ruby activerecord
Comments
Post a Comment