activerecord-wrapped_transaction 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8516f90c26bfd324c7e7cb99cb66f3e1293d09f6
4
+ data.tar.gz: 848e4b660600b027ca8f64a3a4aa669a2deebe0d
5
+ SHA512:
6
+ metadata.gz: bb0df845987d747f62e3079ca3ae7df3230f3932544ccaad7b048baa79b7a6cc77b95598e356f97673150e781d9e2b3713bb1ba1c6ff2ceed8890a3014c70c6f
7
+ data.tar.gz: c53d50ac11583b99d463288c9f3342b1328df51f6970e1e432715ce69214b301a16a05b7367609b76ee14f18ea2c9eead4c545a7e799f6bce6db1e04c70ae9f7
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /db/*.sqlite3
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ env:
3
+ - DB=mysql
4
+ - DB=postgres
5
+ - DB=sqlite
6
+ rvm:
7
+ - 2.1.8
8
+ - 2.2.4
9
+ - 2.3.0
10
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at devel@mouse.vc. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-wrapped_transaction.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexa Grey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # Activerecord::WrappedTransaction
2
+
3
+ Wrap transactions in a way that lets you easily detect if the block succeeded or rolled back
4
+ for complex, procedural usage with an object interface.
5
+
6
+ It supports MySQL, PostgreSQL, and SQLite.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'activerecord-wrapped_transaction'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install activerecord-wrapped_transaction
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ ActiveRecord::Base.include ActiveRecord::WrappedTransaction
28
+
29
+ wrapped_result = ActiveRecord::Base.wrapped_transaction do
30
+ # Do something
31
+ end
32
+
33
+ wrapped_result.result #
34
+ wrapped_result.success?
35
+ wrapped_result.rolled_back?
36
+ ```
37
+
38
+ You can pass the same options you would to `ActiveRecord::Base.transaction`: `requires_new`, `isolation`, `joinable`
39
+
40
+ ## Todo
41
+
42
+ * Test coverage for multiple connections (should be supported, but not guaranteed)
43
+ * `Maybe` monad support for being able to execute complex logic more fluently.
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/scryptmouse/activerecord-wrapped_transaction. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |c|
5
+ c.verbose = false
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/wrapped_transaction/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-wrapped_transaction"
8
+ spec.version = ActiveRecord::WrappedTransaction::VERSION
9
+ spec.authors = ["Alexa Grey"]
10
+ spec.email = ["devel@mouse.vc"]
11
+
12
+ spec.summary = %q{Wrap transactions in a manner that detects if the transaction completed}
13
+ spec.description = %q{Wrap transactions in a manner that detects if the transaction completed}
14
+ spec.homepage = "https://github.com/scryptmouse/activerecord-wrapped_transaction"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activerecord", ">= 3.2", "< 6"
23
+
24
+ spec.add_development_dependency "simplecov", "~> 0.12.0"
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.5"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pg", "~> 0.18.0"
30
+ spec.add_development_dependency "mysql2", "~> 0.4.4"
31
+ spec.add_development_dependency "sqlite3"
32
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/wrapped_transaction"
5
+ require_relative "../db/connection"
6
+
7
+ require "pry"
8
+ Pry.start
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env bash
2
+ PROJECT_ROOT=$(realpath "$(dirname $0)/../")
3
+
4
+ if [ -z "$PG_USER" ]; then
5
+ if [[ "$(uname)" =~ "Darwin" ]]; then
6
+ PG_USER=$(whoami)
7
+ else
8
+ PG_USER=postgres
9
+ fi
10
+ fi
11
+
12
+ set -euo pipefail
13
+ IFS=$'\n\t'
14
+ #set -vx
15
+
16
+ bundle install >/dev/null
17
+
18
+ sqlite3 "$PROJECT_ROOT/db/wrapped_transaction_test.sqlite3" '.databases' >/dev/null
19
+ mysql -e 'CREATE DATABASE IF NOT EXISTS wrapped_transaction_test;' >/dev/null
20
+ psql -U $PG_USER -tc "SELECT 1 FROM pg_database WHERE datname = 'wrapped_transaction_test'" | grep -q 1 || psql -U $PG_USER -c "CREATE DATABASE wrapped_transaction_test"
@@ -0,0 +1,32 @@
1
+ require 'active_record'
2
+
3
+ class DBConnector
4
+ attr_reader :options
5
+
6
+ def initialize
7
+ @options = {}
8
+
9
+ case ENV['DB']
10
+ when /mysql/
11
+ options[:adapter] = 'mysql2'
12
+ options[:username] = 'root'
13
+ options[:database] = 'wrapped_transaction_test'
14
+ options[:encoding] = 'utf8'
15
+ when /postgres/
16
+ options[:adapter] = 'postgresql'
17
+ options[:database] = 'wrapped_transaction_test'
18
+ else
19
+ options[:adapter] = 'sqlite3'
20
+ options[:database] = File.join(__dir__, 'wrapped_transaction_test.sqlite3')
21
+ end
22
+ end
23
+
24
+ def ci?
25
+ ENV['CI'].present?
26
+ end
27
+ end
28
+
29
+ ActiveRecord::Base.establish_connection DBConnector.new.options
30
+
31
+ require_relative './schema'
32
+ require_relative './models'
@@ -0,0 +1,9 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+
4
+ include ActiveRecord::WrappedTransaction
5
+ end
6
+
7
+ class Widget < ApplicationRecord
8
+ validates_presence_of :name
9
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :widgets, force: true do |t|
5
+ t.string :name
6
+
7
+ t.timestamps null: false
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'activerecord/wrapped_transaction'
@@ -0,0 +1,26 @@
1
+ require "active_record"
2
+ require "activerecord/wrapped_transaction/version"
3
+ require "activerecord/wrapped_transaction/result"
4
+
5
+ module ActiveRecord
6
+ module WrappedTransaction
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ delegate :wrapped_transaction, to: :class
11
+ end
12
+
13
+ class_methods do
14
+ # @return [ActiveRecord::WrappedTransaction::Result]
15
+ def wrapped_transaction(options = {}, &block)
16
+ ActiveRecord::WrappedTransaction::Result.new(transactor: connection, transaction_options: options, &block)
17
+ end
18
+ end
19
+
20
+ class << self
21
+ def call(transactor: ActiveRecord::Base, **options)
22
+ ActiveRecord::WrappedTransaction::Result.new(transactor: transactor, transaction_options: options) { yield }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,93 @@
1
+ module ActiveRecord
2
+ module WrappedTransaction
3
+ class Result
4
+ TXN_COMPLETE = Object.new.freeze
5
+ NOT_CANCELLED = Object.new.freeze
6
+
7
+ private_constant :TXN_COMPLETE
8
+ private_constant :NOT_CANCELLED
9
+
10
+ # @!attribute [r] cancellation_reason
11
+ # @return [Object, nil]
12
+ attr_reader :cancellation_reason
13
+
14
+ # @!attribute [r] error
15
+ # @return [Exception, nil]
16
+ attr_reader :error
17
+
18
+ # @!attribute [r] result
19
+ # @return [Object, nil]
20
+ attr_reader :result
21
+
22
+ # @!attribute [r] transactor
23
+ # @return [#transaction, ActiveRecord::ConnectionAdapters::AbstractAdapter]
24
+ attr_reader :transactor
25
+
26
+ # @param [#transaction, ActiveRecord::ConnectionAdapters::AbstractAdapter] transactor
27
+ # @yield A block that goes in the transaction.
28
+ # @yieldreturn [Object]
29
+ def initialize(transactor: ActiveRecord::Base, transaction_options: {})
30
+ unless block_given?
31
+ raise ArgumentError, "Must call with a block to run in the transaction"
32
+ end
33
+
34
+ unless transactor.respond_to?(:transaction)
35
+ raise ArgumentError, "transactor `#{transactor.inspect} must respond to :transaction"
36
+ else
37
+ @transactor = transactor
38
+ end
39
+
40
+ wrap_transaction transaction_options do
41
+ execute_transaction do
42
+ watch_for_cancellation do
43
+ @result = yield
44
+ end
45
+ end
46
+ end
47
+
48
+ freeze
49
+ end
50
+
51
+ def cancelled?
52
+ @cancelled
53
+ end
54
+
55
+ def success?
56
+ @success
57
+ end
58
+
59
+ def rolled_back?
60
+ !success?
61
+ end
62
+
63
+ private
64
+ def wrap_transaction(transaction_options = {})
65
+ caught = transactor.transaction(transaction_options) do
66
+ yield
67
+ end
68
+ ensure
69
+ @success = caught.eql?(TXN_COMPLETE)
70
+ end
71
+
72
+ def execute_transaction
73
+ yield
74
+ rescue Exception => e
75
+ @error = e
76
+
77
+ raise ActiveRecord::Rollback
78
+ else
79
+ TXN_COMPLETE
80
+ end
81
+
82
+ def watch_for_cancellation
83
+ cancellation_reason = catch(:cancel_transaction) { yield; NOT_CANCELLED }
84
+
85
+ unless cancellation_reason.eql?(NOT_CANCELLED)
86
+ @cancelled = true
87
+ @cancellation_reason = cancellation_reason
88
+ raise ActiveRecord::Rollback, "Cancelled transaction"
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module WrappedTransaction
3
+ VERSION = "0.5.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-wrapped_transaction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexa Grey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: simplecov
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.12.0
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.12.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.11'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.5'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.5'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pg
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.18.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.18.0
117
+ - !ruby/object:Gem::Dependency
118
+ name: mysql2
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 0.4.4
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 0.4.4
131
+ - !ruby/object:Gem::Dependency
132
+ name: sqlite3
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ description: Wrap transactions in a manner that detects if the transaction completed
146
+ email:
147
+ - devel@mouse.vc
148
+ executables: []
149
+ extensions: []
150
+ extra_rdoc_files: []
151
+ files:
152
+ - ".gitignore"
153
+ - ".rspec"
154
+ - ".travis.yml"
155
+ - CODE_OF_CONDUCT.md
156
+ - Gemfile
157
+ - LICENSE.txt
158
+ - README.md
159
+ - Rakefile
160
+ - activerecord-wrapped_transaction.gemspec
161
+ - bin/console
162
+ - bin/setup
163
+ - db/connection.rb
164
+ - db/models.rb
165
+ - db/schema.rb
166
+ - lib/activerecord-wrapped_transaction.rb
167
+ - lib/activerecord/wrapped_transaction.rb
168
+ - lib/activerecord/wrapped_transaction/result.rb
169
+ - lib/activerecord/wrapped_transaction/version.rb
170
+ homepage: https://github.com/scryptmouse/activerecord-wrapped_transaction
171
+ licenses:
172
+ - MIT
173
+ metadata: {}
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubyforge_project:
190
+ rubygems_version: 2.4.5
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: Wrap transactions in a manner that detects if the transaction completed
194
+ test_files: []