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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/capistrano-db-tasks/database.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Database
class Base
DBCONFIG_BEGIN_FLAG = "__CAPISTRANODB_CONFIG_BEGIN_FLAG__".freeze
DBCONFIG_END_FLAG = "__CAPISTRANODB_CONFIG_END_FLAG__".freeze

attr_accessor :config, :output_file

Expand Down Expand Up @@ -106,8 +108,15 @@ def dump_cmd_ignore_data_tables_opts
class Remote < Base
def initialize(cap_instance)
super(cap_instance)
@config = @cap.capture("cat #{@cap.current_path}/config/database.yml")
@config = YAML.load(ERB.new(@config).result)[@cap.fetch(:rails_env).to_s]
@cap.info "Loading remote database config"
@cap.within @cap.current_path do
@cap.with rails_env: @cap.fetch(:rails_env) do
dirty_config_content = @cap.capture(:rails, "runner \"puts '#{DBCONFIG_BEGIN_FLAG}' + ActiveRecord::Base.connection.instance_variable_get(:@config).to_yaml + '#{DBCONFIG_END_FLAG}'\"", '2>/dev/null')
# Remove all warnings, errors and artefacts produced by bunlder, rails and other useful tools
config_content = dirty_config_content.match(/#{DBCONFIG_BEGIN_FLAG}(.*?)#{DBCONFIG_END_FLAG}/)[1]
@config = YAML.load(config_content).inject({}) { |h, (k, v)| h[k.to_s] = v; h }
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!

end
end

def dump
Expand Down Expand Up @@ -145,8 +154,13 @@ def dump_file_path
class Local < Base
def initialize(cap_instance)
super(cap_instance)
@config = YAML.load(ERB.new(File.read(File.join('config', 'database.yml'))).result)[fetch(:local_rails_env).to_s]
puts "local #{@config}"
@cap.info "Loading local database config"
dirty_config_content = @cap.run_locally do
capture(:rails, "runner \"puts '#{DBCONFIG_BEGIN_FLAG}' + ActiveRecord::Base.connection.instance_variable_get(:@config).to_yaml + '#{DBCONFIG_END_FLAG}'\"")
end
# Remove all warnings, errors and artefacts produced by bunlder, rails and other useful tools
config_content = dirty_config_content.match(/#{DBCONFIG_BEGIN_FLAG}(.*?)#{DBCONFIG_END_FLAG}/)[1]
@config = YAML.load(config_content).inject({}) { |h, (k, v)| h[k.to_s] = v; h }
end

# cleanup = true removes the mysqldump file after loading, false leaves it in db/
Expand Down Expand Up @@ -208,5 +222,4 @@ def local_to_remote(instance)
File.unlink(local_db.output_file) if instance.fetch(:db_local_clean)
end
end

end