4

I want to require capistrano/postgresql only if stage is not production. However, this Capfile always require capistrano/postgresql because fetch(:stage) is empty. ( puts fetch(:stage) || "no stage" in Capfile prints "no stage")

require 'capistrano/bundler'
require 'capistrano/npm'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/puma'
require 'capistrano/puma/nginx'
require 'capistrano/postgresql' unless fetch(:stage) == "production"
require 'capistrano/secrets_yml'

Should I put require 'capistrano/postgresql' in config/deploy/staging.rb etc.. (I don't know whether it works or not)?

Or is there any other concise way?

EDIT

If I put require 'capistrano/postgresql' in config/deploy/staging.rb, the following error appears.

WARNING: load:defaults has already been invoked and can no longer be modified.
Check that you haven't loaded a Capistrano plugin in deploy.rb by mistake.
Plugins must be loaded in the Capfile to initialize properly.
(Backtrace restricted to imported tasks)
cap aborted!
can't modify frozen #<Class:#<Rake::Task:0x007fd8bcd22868>>
yskkin
  • 854
  • 7
  • 24
  • I might have time for a more comprehensive answer later, but I'd suggest trying to unregister the hooks if it is not production later in the process. Capistrano isn't fully booted in the Capfile. – will_in_wi Jan 27 '17 at 15:13

1 Answers1

3

In Capefile add

task :use_postgresql do
  require 'capistrano/postgresql'
end

task 'staging' => [:use_postgresql]

In that way capistrano will use posgresql only on staging env

Vladimir Dimitrov
  • 1,008
  • 7
  • 21