提交 3a0b6c8e 编写于 作者: R Rafael Mendonça França

Merge pull request #9120 from hone/db_migrate_with_database_url

fix rake db:migrate when used with ENV['DATABASE_URL']
...@@ -38,7 +38,7 @@ def spec ...@@ -38,7 +38,7 @@ def spec
private private
def resolve_string_connection(spec) # :nodoc: def resolve_string_connection(spec) # :nodoc:
hash = configurations.fetch(spec) do |k| hash = configurations.fetch(spec) do |k|
connection_url_to_hash(k) self.class.connection_url_to_hash(k)
end end
raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash
...@@ -69,7 +69,7 @@ def resolve_hash_connection(spec) # :nodoc: ...@@ -69,7 +69,7 @@ def resolve_hash_connection(spec) # :nodoc:
SIMPLE_INT = /\A\d+\z/ SIMPLE_INT = /\A\d+\z/
SIMPLE_FLOAT = /\A\d+\.\d+\z/ SIMPLE_FLOAT = /\A\d+\.\d+\z/
def connection_url_to_hash(url) # :nodoc: def self.connection_url_to_hash(url) # :nodoc:
config = URI.parse url config = URI.parse url
adapter = config.scheme adapter = config.scheme
adapter = "postgresql" if adapter == "postgres" adapter = "postgresql" if adapter == "postgres"
......
...@@ -141,9 +141,7 @@ class Railtie < Rails::Railtie # :nodoc: ...@@ -141,9 +141,7 @@ class Railtie < Rails::Railtie # :nodoc:
# and then establishes the connection. # and then establishes the connection.
initializer "active_record.initialize_database" do |app| initializer "active_record.initialize_database" do |app|
ActiveSupport.on_load(:active_record) do ActiveSupport.on_load(:active_record) do
unless ENV['DATABASE_URL'] self.configurations = app.config.database_configuration
self.configurations = app.config.database_configuration
end
establish_connection establish_connection
end end
end end
......
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* fix rake db:* tasks to work with DATABASE_URL and without config/database.yml
*Terence Lee*
* Add notice message for destroy action in scaffold generator. * Add notice message for destroy action in scaffold generator.
......
...@@ -101,8 +101,12 @@ def threadsafe! ...@@ -101,8 +101,12 @@ def threadsafe!
# contents of the file are processed via ERB before being sent through # contents of the file are processed via ERB before being sent through
# YAML::load. # YAML::load.
def database_configuration def database_configuration
require 'erb' if ENV['DATABASE_URL']
YAML.load ERB.new(IO.read(paths["config/database"].first)).result {Rails.env => ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.connection_url_to_hash(ENV['DATABASE_URL']).stringify_keys}
else
require 'erb'
YAML.load ERB.new(IO.read(paths["config/database"].first)).result
end
rescue Psych::SyntaxError => e rescue Psych::SyntaxError => e
raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " \ raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " \
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
......
...@@ -21,6 +21,8 @@ def database_url_db_name ...@@ -21,6 +21,8 @@ def database_url_db_name
def set_database_url def set_database_url
ENV['DATABASE_URL'] = "sqlite3://:@localhost/#{database_url_db_name}" ENV['DATABASE_URL'] = "sqlite3://:@localhost/#{database_url_db_name}"
# ensure it's using the DATABASE_URL
FileUtils.rm_rf("#{app_path}/config/database.yml")
end end
def expected def expected
...@@ -166,9 +168,16 @@ def db_test_load_structure ...@@ -166,9 +168,16 @@ def db_test_load_structure
end end
test 'db:test:load_structure with database_url' do test 'db:test:load_structure with database_url' do
require "#{app_path}/config/environment" old_rails_env = ENV["RAILS_ENV"]
set_database_url ENV["RAILS_ENV"] = 'test'
db_test_load_structure
begin
require "#{app_path}/config/environment"
set_database_url
db_test_load_structure
ensure
ENV["RAILS_ENV"] = old_rails_env
end
end end
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册