Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #54 Add support of ENV['DATABASE_URL'] #70

Merged
merged 3 commits into from
Sep 20, 2016

Conversation

numbata
Copy link
Collaborator

@numbata numbata commented Sep 9, 2015

Using Rails.application.config.database_configuration to get database configuration instead of database.yml file. Works with database configuration from ENV['DATABASE_URL'] also.

@numbata
Copy link
Collaborator Author

numbata commented Sep 9, 2015

@fabn @donbobka @ktaragorn @markgandolfo could you check this PR?

@cap.with rails_env: @cap.fetch(:rails_env) do
@config = @cap.capture(:rails, 'runner "puts Rails.application.config.database_configuration[Rails.env].to_yaml"', '2>/dev/null')
@config.gsub!(/.*\-\-\-/m, '---') # Remove all bundler and rails initialization errors
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this line? Which kind of errors are you trying to prevent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors, warnings and notices like:

Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.

or

Your Gemfile lists the gem X (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.

which captured with database configuration and breaks the config yaml.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am missing something.. But is there a reason to use rails runner here? Rather than just depending on the :environment task or something? Is this because it is happening on the remote?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach in #54 seems marginally cleaner to me, using a rake task

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktaragorn via saving config to temporary file through rake task?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah.. Actually it does pretty much the same thing https://github.com/donbobka/capistrano-db-tasks/blob/77cbfcc5f5ff3a2be6f552cb54f8ae37294b6b72/lib/tasks/capistrano-db-tasks.rake.. Ignore what I said.. Was just wondering if adding this extra layer somehow took care of the errors without you having to manually..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktaragorn i don't wanna using rake task, because we can require 'capistrano-*' gems only in development group. On the production (and staging) server they can be unavailable and our rake task too.

Alternative: #54 (comment) (author @fabn). But save file to tmp/config.yml isn't correct. Because tempdir can be one of [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'] and can be unequal tmp/ (proof).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

@fabn
Copy link

fabn commented Sep 9, 2015

I'll try this tomorrow and report the result here.

@fabn
Copy link

fabn commented Sep 11, 2015

I tried it and I have the following issues

  • when using cap staging db:pull RAILS_ENV is not set to staging but to production
  • In production I'm getting NoMethodError: undefined method[]' for nil:NilClass` after yaml is loaded

For the latest point I found out that Rails.application.config.database_configuration has no production key.

If I start a production console (in a rails 4.1.12 application configured with DATABASE_URL) I have the following

Rails.application.config.database_configuration[Rails.env] # => nil
ActiveRecord::Base.connection.instance_variable_get(:@config) # => {:pool=>"25", :encoding=>"utf8", ...}

@numbata
Copy link
Collaborator Author

numbata commented Sep 17, 2015

@fabn thanks for report. I tried it with different versions of Rails and got similar result.

# Rails 4.2.3
ActiveRecord::Base.connection.instance_variable_get(:@config) # => {:adapter=>"postgresql", :username=>"app_user", :password=>"app_password", :port=>5432, :database=>"app_database", :host=>"127.0.0.1"}
Rails.application.config.database_configuration[Rails.env] # => nil

# Rails 3.2.21
ActiveRecord::Base.connection.instance_variable_get(:@config) # => {:adapter=>"postgresql", :username=>"app_user", :password=>"app_password", :port=>5432, :database=>"app_database", :host=>"127.0.0.1"}
Rails.application.config.database_configuration[Rails.env] # => Errno::ENOENT: No such file or directory - /apps/cctv/config/database.yml

@numbata
Copy link
Collaborator Author

numbata commented Sep 18, 2015

@fabn @ktaragorn review please :)

@numbata
Copy link
Collaborator Author

numbata commented Aug 18, 2016

@fabn @donbobka @ktaragorn @markgandolfo @leifcr so sorry for necro-PR 💀 . But could you check this pull request if it still actual for you?

@leifcr
Copy link
Contributor

leifcr commented Aug 18, 2016

@numbata still valid for my current setup, as with 12 factor setup, the db config is not stored as part of the app, but as part of the infrastructure.

@leifcr
Copy link
Contributor

leifcr commented Sep 15, 2016

For testing purposes i tested separate ENV params as well, and it works with this PR:

staging:
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>

@artempartos
Copy link
Contributor

Need this PR! Please, merge it

@numbata numbata force-pushed the feature/add_support_database_url branch from e79f8fd to fc7856f Compare September 20, 2016 12:02
@numbata
Copy link
Collaborator Author

numbata commented Sep 20, 2016

Happy birthday, PR! 🎉 🎈

Now, let's merge it.

@numbata numbata merged commit ae11141 into sgruhier:master Sep 20, 2016
@leifcr
Copy link
Contributor

leifcr commented Sep 20, 2016

thanks @numbata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants