Skip to content

Commit

Permalink
Add warning when accessing console in production
Browse files Browse the repository at this point in the history
If you have multiple Rails consoles open, it's often difficult to know
whether you're connected to a local console or to production, which
risks mistakes being made.

This change makes Pry the default Rails console in all environments, and
additionally, when starting a production console without the `--sandbox`
switch, show an obvious warning banner to the user.

Additionally, the prompt shows "writable" or "sandbox" on every line.

You can customise the environment name shown in the warning message and
prompts by overriding the `PRODUCTION_ENV` environment variable (e.g.
'staging').

Co-authored-by: Robert Lee-Cann <[email protected]>
  • Loading branch information
tahb and leeky committed Jun 22, 2021
1 parent c5145df commit 08020ae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
30 changes: 30 additions & 0 deletions .pryrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Pry.config.color = true

if defined?(Rails)
production_env = ENV.fetch("PRODUCTION_ENV", "production")

if Rails.env.production? && Rails.application.sandbox == false
production_warning = "Warning: You are using the #{production_env} console in non-sandboxed mode"

puts
puts Pry::Helpers::Text.red("*" * production_warning.size)
puts Pry::Helpers::Text.red(production_warning)
puts Pry::Helpers::Text.red("*" * production_warning.size)
puts
end

environment_prompt = if Rails.env.production?
"(" + Pry::Helpers::Text.red(production_env) + ")"
else
"(" + Pry::Helpers::Text.green(Rails.env) + ")"
end

sandbox_prompt = Rails.application.sandbox ? "(sandbox)" : "(writable)"

Pry.config.prompt_name = [
environment_prompt,
" ",
sandbox_prompt,
" "
].join
end
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem "high_voltage"
gem "jbuilder", "~> 2.11"
gem "jquery-rails"
gem "pg"
gem "pry-rails"
gem "mini_racer"
gem "puma", "~> 5.3"
gem "rollbar"
Expand Down Expand Up @@ -46,7 +47,6 @@ group :development, :test do
gem "dotenv-rails"
gem "factory_bot_rails"
gem "faker"
gem "pry"
gem "rspec-rails"
gem "standard"
end
Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ GEM
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-rails (0.3.9)
pry (>= 0.10.4)
public_suffix (4.0.6)
puma (5.3.2)
nio4r (~> 2.0)
Expand Down Expand Up @@ -321,7 +323,7 @@ DEPENDENCIES
listen (>= 3.0.5, < 3.6)
mini_racer
pg
pry
pry-rails
puma (~> 5.3)
rails (~> 6.1.3)
rails_layout
Expand Down

0 comments on commit 08020ae

Please sign in to comment.