sequel_transaction 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 174154719f975c73fece96b0f0e8ff3fc0f41ac4
4
+ data.tar.gz: 9622bebee29e723db29015b71edf7705491d58d6
5
+ SHA512:
6
+ metadata.gz: c49db55af0f1c8ac5126e238e15c47fa4b9b4692bdc89699c3b6f7ee999d9ca0e508bea9a84c3a74660388bb74dbd56d4cce95a8c2845fe9ae382ebe85213a5e
7
+ data.tar.gz: a0217bf3fb26f1c5d88c3805773190460a700436453bd4a5673639e4e787f67a26a2583440af592de07b177ad78a7b443195b116c30b385a0fba0001742a5269
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
19
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rack-test'
7
+ gem 'rake'
8
+ gem 'sequel'
9
+ gem 'sqlite3'
10
+ end
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^spec/.+_spec\.rb|)
3
+ watch(%r|^lib/(.+)\.rb$|) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ Copyright (c) 2012, Peer60
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ - Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ - Neither the name of Katalus nor the names of its contributors may be used
16
+ to endorse or promote products derived from this software without specific
17
+ prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ gem 'sequel_transaction'
6
+
7
+ And then execute:
8
+
9
+ $ bundle
10
+
11
+ Or install it yourself as:
12
+
13
+ $ gem install sequel_transaction
14
+
15
+ ## Sidekiq Wireup
16
+
17
+ ```ruby
18
+ Sidekiq.configure_server do |c|
19
+ c.server_middleware do |chain|
20
+ chain.add Sidekiq::Middleware::SequelTransaction,
21
+ connection: Sequel.connect('sqlite:///')
22
+ end
23
+ end
24
+ ```
25
+
26
+ ## Rack Wireup
27
+
28
+ ```ruby
29
+ use Rack::SequelTransaction,
30
+ connection: Sequel.connect('sqlite:///')
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ puts ENV['TERM']
6
+ t.libs = ['spec', 'lib']
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ end
@@ -0,0 +1,4 @@
1
+ module SequelTransaction
2
+ end
3
+
4
+ Dir[File.expand_path("../sequel_transaction/*.rb", __FILE__)].each { |f| require f }
@@ -0,0 +1 @@
1
+ Dir[File.expand_path("../rack/*.rb", __FILE__)].each { |f| require f }
@@ -0,0 +1,21 @@
1
+ module Rack
2
+ class SequelTransaction
3
+ def initialize(inner, settings)
4
+ @inner = inner
5
+ @connection = settings.fetch(:connection)
6
+ end
7
+
8
+ def call(env)
9
+ @connection.transaction do
10
+ result = @inner.call env
11
+ response = Response.new([], result[0])
12
+ err = env['sinatra.error']
13
+
14
+ if err || response.client_error? || response.server_error?
15
+ raise Sequel::Rollback
16
+ end
17
+ result
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ Dir[File.expand_path("../sidekiq/*.rb", __FILE__)].each { |f| require f }
@@ -0,0 +1,15 @@
1
+ module Sidekiq
2
+ module Middleware
3
+ class SequelTransaction
4
+ def initialize(settings)
5
+ @connection = settings.fetch(:connection)
6
+ end
7
+
8
+ def call(worker_class, message, queue)
9
+ @connection.transaction do
10
+ yield
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module SequelTransaction
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sequel_transaction/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.author = 'vyrak.bunleang@gmail.com'
6
+ gem.description = 'Middlewares for Sequel transactions'
7
+ gem.summary = 'Middlewares for Sequel transactions'
8
+
9
+ gem.files = `git ls-files`.split("\n")
10
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
11
+ gem.name = "sequel_transaction"
12
+ gem.require_paths = ["lib"]
13
+ gem.version = SequelTransaction::VERSION
14
+
15
+ gem.add_dependency 'sequel', '>= 4.0.0'
16
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::SequelTransaction do
4
+ let(:inner) { mock }
5
+ let(:env) { { 'field' => 'variable' } }
6
+ let(:table_name) { :rack }
7
+ let(:dataset) { connection[table_name] }
8
+ let(:error) { RuntimeError.new }
9
+
10
+ subject { Rack::SequelTransaction.new inner, connection: connection }
11
+
12
+ before do
13
+ connection.create_table table_name do
14
+ column :name, String, null: false
15
+ end
16
+ end
17
+
18
+ after do
19
+ connection.drop_table table_name
20
+ inner.verify
21
+ end
22
+
23
+ def expect_call(status)
24
+ inner.expect :call, [status, {}, []] do |(environment), *args|
25
+ dataset.insert(name: 'insert') if args.empty? && environment == env
26
+ end
27
+ end
28
+
29
+ it 'returns result' do
30
+ expect_call 200
31
+ result = subject.call(env)
32
+ result.must_equal [200, {}, []]
33
+ end
34
+
35
+ it 'wont rollback when ok' do
36
+ expect_call 200
37
+ subject.call env
38
+ dataset.wont_be :empty?
39
+ end
40
+
41
+ it 'wont roll back on redirect' do
42
+ expect_call 301
43
+ subject.call env
44
+ dataset.wont_be :empty?
45
+ end
46
+
47
+ it 'rolls back if sinatra exception' do
48
+ expect_call 200
49
+ env['sinatra.error'] = StandardError.new 'snap'
50
+ subject.call env
51
+ dataset.must_be :empty?
52
+ end
53
+
54
+ it 'rolls back on server error' do
55
+ expect_call 500
56
+ subject.call env
57
+ dataset.must_be :empty?
58
+ end
59
+
60
+ it 'rolls back on client error' do
61
+ expect_call 400
62
+ subject.call env
63
+ dataset.must_be :empty?
64
+ end
65
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::SequelTransaction do
4
+ let(:worker_class) { mock }
5
+ let(:msg) { mock }
6
+ let(:queue) { mock }
7
+ let(:table_name) { :sidekiq }
8
+ let(:dataset) { connection[table_name] }
9
+ let(:error) { RuntimeError.new }
10
+
11
+ subject { Sidekiq::Middleware::SequelTransaction.new connection: connection }
12
+
13
+ before do
14
+ connection.create_table table_name do
15
+ column :name, String, null: false
16
+ end
17
+ end
18
+
19
+ after { connection.drop_table table_name }
20
+
21
+ it 'commits transaction' do
22
+ subject.call worker_class, msg, queue do
23
+ dataset.insert name: 'first'
24
+ dataset.insert name: 'second'
25
+ end
26
+ dataset.count.must_equal 2
27
+ end
28
+
29
+ it 'rolls back transaction' do
30
+ actual_error = nil
31
+ begin
32
+ subject.call worker_class, msg, queue do
33
+ dataset.insert name: 'first'
34
+ raise error
35
+ end
36
+ rescue => e
37
+ actual_error = e
38
+ end
39
+ dataset.count.must_equal 0
40
+ actual_error.must_equal error
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ require 'bundler/setup'
2
+
3
+ $:.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'sequel_transaction'
5
+ require 'minitest/pride'
6
+ require 'minitest/autorun'
7
+
8
+ Bundler.require :development
9
+
10
+ class Minitest::Spec
11
+ def mock
12
+ Minitest::Mock.new
13
+ end
14
+
15
+ def connection
16
+ @connection ||= Sequel.connect 'sqlite:///'
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_transaction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - vyrak.bunleang@gmail.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ description: Middlewares for Sequel transactions
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - Guardfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - lib/sequel_transaction.rb
40
+ - lib/sequel_transaction/rack.rb
41
+ - lib/sequel_transaction/rack/middleware.rb
42
+ - lib/sequel_transaction/sidekiq.rb
43
+ - lib/sequel_transaction/sidekiq/middleware.rb
44
+ - lib/sequel_transaction/version.rb
45
+ - sequel_transaction.gemspec
46
+ - spec/sequel_transaction/rack/middleware_spec.rb
47
+ - spec/sequel_transaction/sidekiq/middleware_spec.rb
48
+ - spec/spec_helper.rb
49
+ homepage:
50
+ licenses: []
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.3
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Middlewares for Sequel transactions
72
+ test_files:
73
+ - spec/sequel_transaction/rack/middleware_spec.rb
74
+ - spec/sequel_transaction/sidekiq/middleware_spec.rb
75
+ - spec/spec_helper.rb