From f767d8ba04b1b365267cccea06fb21a9af86f51f Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Tue, 17 Oct 2017 11:00:37 -0400 Subject: [PATCH] Allow dossier.yml to override database name As it stands the DATABASE_URL will override anything in dossier.yml. Some configurations need those values especially in the database name. For example where the DATABASE_URL might look like this: postgres://postgres@postgres:5432/ With a dossier.yml: development: database: my_database_dev Would have caused Dossier to break complaining that **Database "/" does not exist** Further still with a DATABASE_URL that has the trailing slash removed dossier will assume the database name is "" (empty string) This is especially problematic when the database name changes based on the RAILS_ENV. --- lib/dossier/connection_url.rb | 10 +++++++++- spec/dossier/connection_url_spec.rb | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/dossier/connection_url.rb b/lib/dossier/connection_url.rb index aa4e430..4c29673 100644 --- a/lib/dossier/connection_url.rb +++ b/lib/dossier/connection_url.rb @@ -17,7 +17,7 @@ def to_hash password: uri.password, host: uri.host, port: uri.port, - database: File.basename(uri.path) + database: database_name }.merge(params).reject { |k,v| v.nil? } end @@ -32,5 +32,13 @@ def params Rack::Utils.parse_nested_query(uri.query).symbolize_keys end + def database_name + path = File.basename(uri.path) + if path.blank? || path == "/" + nil + else + path + end + end end end diff --git a/spec/dossier/connection_url_spec.rb b/spec/dossier/connection_url_spec.rb index cb3df7f..4ea2a43 100644 --- a/spec/dossier/connection_url_spec.rb +++ b/spec/dossier/connection_url_spec.rb @@ -42,4 +42,15 @@ expect(connection_options.slice(:username, :password, :port)).to be_empty end + it "drops blank database name" do + database_url = "postgresql://localhost" + connection_options = described_class.new(database_url).to_hash + expect(connection_options.slice(:database)).to be_empty + end + + it "drops trailing slash database name" do + database_url = "postgresql://localhost/" + connection_options = described_class.new(database_url).to_hash + expect(connection_options.slice(:database)).to be_empty + end end