safer_rails_console 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cdf260deb3867b5bed0d92646aa88de6f64afd03
4
- data.tar.gz: 3cbaac5740f226b7a0364168f4ab137d080fe1ec
3
+ metadata.gz: fdba797ed52f07225b6f16b76e99a9dac4731b4b
4
+ data.tar.gz: 7ecdb4551bb8c70d3d01d7aa964198cb667ed852
5
5
  SHA512:
6
- metadata.gz: 470e4da18f0fe1ad4e10df31ae6189f66c7ee58f66389bc8bc3c6d0224f6f8c816dec8d7b94001b22911778bc595a6da7e0de0e3a360c348e29b56de64f7638f
7
- data.tar.gz: a3afb5cc08b8a001879271c7f86b4c78dc5b6cb13d97f2a7602f4ab3ebe96437cc9fb0d22c33ef872fbf9d9daa85e5bd7cc44c43b2e679d2baab112be7c8ef5d
6
+ metadata.gz: c792c0b4684d712180eb6a963144faf05a68a28bc9565725b71987f17c8bff788362f839219faf2945952fcb025ac4ee9d76287cd0eff660882617b8c798020d
7
+ data.tar.gz: 5cda5a4537478ab21d1cbce6629e081e33864d650a9eb56c932c50c7e673efce188c114f6447c71c67041901d14159f4534f46005938f3b483b8551cbecb7787
@@ -1,5 +1,19 @@
1
1
  # Change Log
2
2
 
3
+ ## [v0.2.0](https://github.com/salsify/safer_rails_console/tree/v0.2.0) (2017-09-07)
4
+ [Full Changelog](https://github.com/salsify/safer_rails_console/compare/v0.1.4...v0.2.0)
5
+
6
+ **Implemented enhancements:**
7
+
8
+ - Sandbox mode should make it clear that edits don't stick [\#19](https://github.com/salsify/safer_rails_console/issues/19)
9
+ - Writes made in sandbox mode take out DB locks [\#18](https://github.com/salsify/safer_rails_console/issues/18)
10
+ - Confusion over unsandboxed/sandboxed terminology in command prompt [\#17](https://github.com/salsify/safer_rails_console/issues/17)
11
+
12
+ **Merged pull requests:**
13
+
14
+ - Set DB transactions to read-only and provide messaging for non-read operations [\#21](https://github.com/salsify/safer_rails_console/pull/21) ([timothysu](https://github.com/timothysu))
15
+ - Change 'sandboxed' and 'unsandboxed' to 'read-only' and 'writable' and add respective flags [\#20](https://github.com/salsify/safer_rails_console/pull/20) ([timothysu](https://github.com/timothysu))
16
+
3
17
  ## [v0.1.4](https://github.com/salsify/safer_rails_console/tree/v0.1.4) (2017-08-15)
4
18
  [Full Changelog](https://github.com/salsify/safer_rails_console/compare/v0.1.3...v0.1.4)
5
19
 
data/README.md CHANGED
@@ -27,11 +27,14 @@ require 'safer_rails_console/patches/boot'
27
27
 
28
28
  The quickest way to demo this gem is to run `bundle exec rails console --sandbox`.
29
29
 
30
- A way to explicitly enable or disable the sandbox is added to Rails console as a flag with the last install step.
30
+ Several ways to explicitly enable or disable the sandbox are added to Rails console as flags with the last install step. The order of precedence is `-s`, `-r`, then `-w` if multiple sandbox related flags are specified.
31
31
  ```ruby
32
32
  bundle exec rails console --help
33
33
 
34
+ Usage: rails console [environment] [options]
34
35
  -s, --[no-]sandbox Explicitly enable/disable sandbox mode.
36
+ -w, --writable Alias for --no-sandbox.
37
+ -r, --read-only Alias for --sandbox.
35
38
  -e, --environment=name Specifies the environment to run this console under (test/development/production).
36
39
  Default: development
37
40
  --debugger Enable the debugger.
@@ -2,7 +2,7 @@ include SaferRailsConsole::Colors
2
2
 
3
3
  app_name = ::Rails.application.class.parent.to_s.underscore.dasherize
4
4
  env_name = SaferRailsConsole.environment_name
5
- status = ::Rails.application.sandbox ? 'sandboxed' : 'unsandboxed'
5
+ status = ::Rails.application.sandbox ? 'read-only' : 'writable'
6
6
  color = SaferRailsConsole.prompt_color
7
7
 
8
8
  prompt = "#{app_name}(#{env_name})(#{status}):%03n:%i"
@@ -4,6 +4,16 @@ module SaferRailsConsole
4
4
  module Patches
5
5
  module Boot
6
6
  module SandboxFlag
7
+ def self.console_options(opt, options = {})
8
+ opt.banner = 'Usage: rails console [environment] [options]'
9
+ opt.on('-s', '--[no-]sandbox', 'Explicitly enable/disable sandbox mode.') { |v| options[:sandbox] = v }
10
+ opt.on('-w', '--writable', 'Alias for --no-sandbox.') { |v| options[:writable] = v }
11
+ opt.on('-r', '--read-only', 'Alias for --sandbox.') { |v| options[:'read-only'] = v }
12
+ opt.on('-e', '--environment=name', String,
13
+ 'Specifies the environment to run this console under (test/development/production).',
14
+ 'Default: development') { |v| options[:environment] = v.strip }
15
+ end
16
+
7
17
  module Rails
8
18
  module CommandsTasks4
9
19
  def console
@@ -18,11 +28,7 @@ module SaferRailsConsole
18
28
  options = {}
19
29
 
20
30
  OptionParser.new do |opt|
21
- opt.banner = 'Usage: rails console [environment] [options]'
22
- opt.on('-s', '--[no-]sandbox', 'Explicitly enable/disable sandbox mode.') { |v| options[:sandbox] = v }
23
- opt.on('-e', '--environment=name', String,
24
- 'Specifies the environment to run this console under (test/development/production).',
25
- 'Default: development') { |v| options[:environment] = v.strip }
31
+ ::SaferRailsConsole::Patches::Boot::SandboxFlag.console_options(opt, options)
26
32
  opt.on('--debugger', 'Enable the debugger.') { |v| options[:debugger] = v }
27
33
  opt.parse!(arguments)
28
34
  end
@@ -53,11 +59,7 @@ module SaferRailsConsole
53
59
  options = {}
54
60
 
55
61
  OptionParser.new do |opt|
56
- opt.banner = 'Usage: rails console [environment] [options]'
57
- opt.on('-s', '--[no-]sandbox', 'Explicitly enable/disable sandbox mode.') { |v| options[:sandbox] = v }
58
- opt.on('-e', '--environment=name', String,
59
- 'Specifies the environment to run this console under (test/development/production).',
60
- 'Default: development') { |v| options[:environment] = v.strip }
62
+ ::SaferRailsConsole::Patches::Boot::SandboxFlag.console_options(opt, options)
61
63
  opt.parse!(arguments)
62
64
  end
63
65
 
@@ -83,6 +85,8 @@ elsif SaferRailsConsole::RailsVersion.five_one?
83
85
  ::Rails::Command::ConsoleCommand.class_eval do
84
86
  remove_class_option :sandbox
85
87
  class_option :sandbox, aliases: '-s', type: :boolean, desc: 'Explicitly enable/disable sandbox mode.'
88
+ class_option :writable, aliases: '-w', type: :boolean, desc: 'Alias for --no-sandbox.'
89
+ class_option :'read-only', aliases: '-r', type: :boolean, desc: 'Alias for --sandbox.'
86
90
  end
87
91
  else
88
92
  unless SaferRailsConsole::RailsVersion.supported?
@@ -6,8 +6,15 @@ module SaferRailsConsole
6
6
  def start(*args)
7
7
  options = args.last
8
8
 
9
- options[:sandbox] = SaferRailsConsole.sandbox_environment? if options[:sandbox].nil?
10
- options[:sandbox] = SaferRailsConsole::Console.sandbox_user_prompt if SaferRailsConsole.sandbox_environment? && SaferRailsConsole.config.sandbox_prompt
9
+ if options[:sandbox].nil?
10
+ options[:sandbox] = if options[:'read-only']
11
+ true
12
+ elsif options[:writable]
13
+ false
14
+ else
15
+ SaferRailsConsole.sandbox_environment? && SaferRailsConsole.config.sandbox_prompt ? SaferRailsConsole::Console.sandbox_user_prompt : SaferRailsConsole.sandbox_environment?
16
+ end
17
+ end
11
18
 
12
19
  super *args
13
20
  end
@@ -2,27 +2,37 @@ module SaferRailsConsole
2
2
  module Patches
3
3
  module Sandbox
4
4
  module AutoRollback
5
+ extend SaferRailsConsole::Colors
6
+
5
7
  def self.rollback_and_begin_new_transaction
6
8
  connection = ::ActiveRecord::Base.connection
7
9
  connection.rollback_db_transaction
8
10
  connection.begin_db_transaction
9
11
  end
10
12
 
13
+ def self.handle_and_reraise_exception(e)
14
+ if e.message.include?('PG::ReadOnlySqlTransaction')
15
+ puts color_text('An operation could not be completed due to read-only mode.', RED) # rubocop:disable Rails/Output
16
+ else
17
+ rollback_and_begin_new_transaction
18
+ end
19
+
20
+ raise e
21
+ end
22
+
11
23
  module ActiveRecord
12
24
  module ConnectionAdapters
13
25
  module PostgreSQLAdapter41
14
26
  def exec_no_cache(sql, name, binds)
15
27
  super
16
28
  rescue => e
17
- SaferRailsConsole::Patches::Sandbox::AutoRollback.rollback_and_begin_new_transaction
18
- raise e
29
+ SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(e)
19
30
  end
20
31
 
21
32
  def exec_cache(sql, name, binds)
22
33
  super
23
34
  rescue => e
24
- SaferRailsConsole::Patches::Sandbox::AutoRollback.rollback_and_begin_new_transaction
25
- raise e
35
+ SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(e)
26
36
  end
27
37
  end
28
38
 
@@ -30,8 +40,7 @@ module SaferRailsConsole
30
40
  def execute_and_clear(sql, name, binds)
31
41
  super
32
42
  rescue => e
33
- SaferRailsConsole::Patches::Sandbox::AutoRollback.rollback_and_begin_new_transaction
34
- raise e
43
+ SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(e)
35
44
  end
36
45
  end
37
46
 
@@ -39,8 +48,7 @@ module SaferRailsConsole
39
48
  def execute_and_clear(sql, name, binds, prepare: false)
40
49
  super
41
50
  rescue => e
42
- SaferRailsConsole::Patches::Sandbox::AutoRollback.rollback_and_begin_new_transaction
43
- raise e
51
+ SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(e)
44
52
  end
45
53
  end
46
54
  end
@@ -0,0 +1,26 @@
1
+ module SaferRailsConsole
2
+ module Patches
3
+ module Sandbox
4
+ module TransactionReadOnly
5
+ module ActiveRecord
6
+ module ConnectionAdapters
7
+ module PostgreSQLAdapter
8
+ def begin_db_transaction
9
+ super
10
+ execute 'SET TRANSACTION READ ONLY'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
21
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(SaferRailsConsole::Patches::Sandbox::TransactionReadOnly::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
22
+
23
+ # Ensure transaction is read-only if it was began before this patch was loaded
24
+ connection = ::ActiveRecord::Base.connection
25
+ connection.execute 'SET TRANSACTION READ ONLY' if connection.open_transactions > 0
26
+ end
@@ -1,3 +1,3 @@
1
1
  module SaferRailsConsole
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safer_rails_console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify, Inc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-15 00:00:00.000000000 Z
11
+ date: 2017-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -206,6 +206,7 @@ files:
206
206
  - lib/safer_rails_console/patches/railtie/sandbox.rb
207
207
  - lib/safer_rails_console/patches/sandbox.rb
208
208
  - lib/safer_rails_console/patches/sandbox/auto_rollback.rb
209
+ - lib/safer_rails_console/patches/sandbox/transaction_read_only.rb
209
210
  - lib/safer_rails_console/rails_version.rb
210
211
  - lib/safer_rails_console/railtie.rb
211
212
  - lib/safer_rails_console/version.rb