monogamy 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.travis.yml +5 -2
- data/README.md +9 -2
- data/ci/Gemfile.rails-3.2.x +4 -0
- data/ci/Gemfile.rails-4.0.x +4 -0
- data/lib/monogamy/version.rb +1 -1
- data/lib/monogamy/with_table_lock.rb +2 -5
- data/monogamy.gemspec +4 -5
- data/test/database.yml +1 -5
- data/test/minitest_helper.rb +2 -5
- data/test/monogamy_test.rb +4 -3
- metadata +10 -47
- data/lib/monogamy/sqlite.rb +0 -33
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZGZiODBiNGQyMWYzYzE4NTM2M2Q2MmM3NjM0MmNiOTNmODM0MWQyOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MDdlNzkxOGU5MTc5MDg0MzBkNjRhNGM0OGE5NmRmN2E4ODM0OTQ5Yw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
N2NjN2JmY2JlNTgzMTk5N2YxZDBiYThmYTlmNDM0NDdkODg3MTcyZTMyNzA5
|
10
|
+
Yzc5MWRkMDViZDc0NTEwYmZhZmUwYzcyMTJhYzZhNTk3NzQyMWQxOWU1ZjI4
|
11
|
+
ZmFjYTZjNzRmMTYzYjJhMjU4Y2FlMjJlNWZiZWNmZjYzNmMyOGI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjdlOTIyOWYzM2UxOGJlZGM3YWI1NzU0MzUzODhiZjdlNDM3ZjMyZmVkOTVh
|
14
|
+
ZWQwNzc1MWIzYTI1ZWIxODZlMTlkMTEwYzcyYWRiY2FhY2EwNzg1NmUxZDY5
|
15
|
+
MjBjZGI3YWY5ZTljZDYzNGJlNTBkYWZlZDg1N2MyNTdlZTk5ZGE=
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
# Monogamy
|
1
|
+
# Monogamy
|
2
2
|
|
3
|
-
Adds table-level locking to ActiveRecord 3.
|
3
|
+
Adds table-level locking to ActiveRecord 3.2 and 4.0. MySQL and PostgreSQL are supported.
|
4
|
+
|
5
|
+
[![Build Status](https://api.travis-ci.org/mceachen/monogamy.png?branch=master)](https://travis-ci.org/mceachen/monogamy)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/monogamy.png)](http://rubygems.org/gems/monogamy)
|
7
|
+
[![Dependency Status](https://gemnasium.com/mceachen/monogamy.png)](https://gemnasium.com/mceachen/monogamy)
|
4
8
|
|
5
9
|
## Usage
|
6
10
|
|
@@ -13,6 +17,9 @@ end
|
|
13
17
|
While your code is inside the block, it will have exclusive read and write access to the model's
|
14
18
|
table.
|
15
19
|
|
20
|
+
A transaction will be opened and closed for you automatically during your block's execution.
|
21
|
+
There's no need to wrap your call to ```with_table_lock``` with a ```transaction do```.
|
22
|
+
|
16
23
|
If your block touches other tables, and you use table-level locking on those tables as well,
|
17
24
|
read up about [deadlocks](http://en.wikipedia.org/wiki/Deadlock). **You have been warned.**
|
18
25
|
|
data/lib/monogamy/version.rb
CHANGED
@@ -5,18 +5,15 @@
|
|
5
5
|
|
6
6
|
require 'monogamy/mysql'
|
7
7
|
require 'monogamy/postgresql'
|
8
|
-
require 'monogamy/sqlite'
|
9
8
|
|
10
9
|
module Monogamy
|
11
10
|
module WithTableLock
|
12
11
|
def with_table_lock(&block)
|
13
12
|
adapter = case (connection.adapter_name.downcase)
|
14
|
-
when
|
13
|
+
when 'postgresql', 'empostgresql', 'postgis'
|
15
14
|
Monogamy::PostgreSQL
|
16
|
-
when
|
15
|
+
when 'mysql', 'mysql2'
|
17
16
|
Monogamy::MySQL
|
18
|
-
when "sqlite"
|
19
|
-
Monogamy::SQLite
|
20
17
|
else
|
21
18
|
raise NotImplementedError, "Support for #{connection.adapter_name} has not been written"
|
22
19
|
end
|
data/monogamy.gemspec
CHANGED
@@ -6,24 +6,23 @@ require 'monogamy/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "monogamy"
|
8
8
|
gem.version = Monogamy::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
9
|
+
gem.authors = ['Matthew McEachen']
|
10
|
+
gem.email = ['matthew+github@mceachen.org']
|
11
11
|
gem.description = %q{Add table-level database locking to ActiveRecord}
|
12
12
|
gem.summary = gem.description
|
13
|
-
gem.homepage =
|
13
|
+
gem.homepage = 'https://github.com/mceachen/monogamy'
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^test/})
|
18
18
|
gem.require_paths = %w(lib)
|
19
19
|
|
20
|
-
gem.add_runtime_dependency 'activerecord', '>= 3.
|
20
|
+
gem.add_runtime_dependency 'activerecord', '>= 3.2'
|
21
21
|
|
22
22
|
gem.add_development_dependency 'rake'
|
23
23
|
gem.add_development_dependency 'yard'
|
24
24
|
gem.add_development_dependency 'minitest'
|
25
25
|
gem.add_development_dependency 'mysql2'
|
26
26
|
gem.add_development_dependency 'pg'
|
27
|
-
gem.add_development_dependency 'sqlite3'
|
28
27
|
gem.add_development_dependency 'database_cleaner'
|
29
28
|
end
|
data/test/database.yml
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
sqlite:
|
2
|
-
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
|
3
|
-
database: monogamy.sqlite3.db
|
4
|
-
pool: 50
|
5
1
|
pg:
|
6
2
|
adapter: postgresql
|
7
3
|
username: postgres
|
@@ -13,4 +9,4 @@ mysql:
|
|
13
9
|
host: localhost
|
14
10
|
username: root
|
15
11
|
database: monogamy_test
|
16
|
-
pool: 50
|
12
|
+
pool: 50
|
data/test/minitest_helper.rb
CHANGED
@@ -9,17 +9,14 @@ ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite")
|
|
9
9
|
ActiveRecord::Migration.verbose = false
|
10
10
|
|
11
11
|
require 'test_models'
|
12
|
-
|
13
|
-
Tag.new # < make sure class has loaded
|
14
|
-
|
15
12
|
require 'minitest/autorun'
|
16
13
|
|
17
14
|
DatabaseCleaner.strategy = :deletion
|
18
15
|
class MiniTest::Spec
|
19
|
-
before
|
16
|
+
before do
|
20
17
|
DatabaseCleaner.start
|
21
18
|
end
|
22
|
-
after
|
19
|
+
after do
|
23
20
|
DatabaseCleaner.clean
|
24
21
|
end
|
25
22
|
end
|
data/test/monogamy_test.rb
CHANGED
@@ -16,15 +16,15 @@ describe "Monogamy" do
|
|
16
16
|
Tag.connection.reconnect!
|
17
17
|
if with_table_lock
|
18
18
|
Tag.with_table_lock do
|
19
|
-
Tag.
|
19
|
+
Tag.where(name: run_at.to_s).first_or_create!
|
20
20
|
end
|
21
21
|
else
|
22
|
-
Tag.
|
22
|
+
Tag.where(name: run_at.to_s).first_or_create!
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def run_workers(with_table_lock)
|
27
|
-
start_time = Time.now.to_i +
|
27
|
+
start_time = Time.now.to_i + 3
|
28
28
|
threads = @workers.times.collect do
|
29
29
|
Thread.new do
|
30
30
|
begin
|
@@ -40,6 +40,7 @@ describe "Monogamy" do
|
|
40
40
|
end
|
41
41
|
|
42
42
|
before :each do
|
43
|
+
Tag.connection.reconnect!
|
43
44
|
@iterations = 5
|
44
45
|
@workers = 7
|
45
46
|
end
|
metadata
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monogamy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Matthew McEachen
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-12-15 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: 3.
|
19
|
+
version: '3.2'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: 3.
|
26
|
+
version: '3.2'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: yard
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: minitest
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ! '>='
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ! '>='
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -78,7 +69,6 @@ dependencies:
|
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: mysql2
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
73
|
- - ! '>='
|
84
74
|
- !ruby/object:Gem::Version
|
@@ -86,7 +76,6 @@ dependencies:
|
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
80
|
- - ! '>='
|
92
81
|
- !ruby/object:Gem::Version
|
@@ -94,7 +83,6 @@ dependencies:
|
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: pg
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
87
|
- - ! '>='
|
100
88
|
- !ruby/object:Gem::Version
|
@@ -102,23 +90,6 @@ dependencies:
|
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
|
-
requirements:
|
107
|
-
- - ! '>='
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: '0'
|
110
|
-
- !ruby/object:Gem::Dependency
|
111
|
-
name: sqlite3
|
112
|
-
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
|
-
requirements:
|
115
|
-
- - ! '>='
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
93
|
requirements:
|
123
94
|
- - ! '>='
|
124
95
|
- !ruby/object:Gem::Version
|
@@ -126,7 +97,6 @@ dependencies:
|
|
126
97
|
- !ruby/object:Gem::Dependency
|
127
98
|
name: database_cleaner
|
128
99
|
requirement: !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
100
|
requirements:
|
131
101
|
- - ! '>='
|
132
102
|
- !ruby/object:Gem::Version
|
@@ -134,7 +104,6 @@ dependencies:
|
|
134
104
|
type: :development
|
135
105
|
prerelease: false
|
136
106
|
version_requirements: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
107
|
requirements:
|
139
108
|
- - ! '>='
|
140
109
|
- !ruby/object:Gem::Version
|
@@ -152,10 +121,11 @@ files:
|
|
152
121
|
- LICENSE.txt
|
153
122
|
- README.md
|
154
123
|
- Rakefile
|
124
|
+
- ci/Gemfile.rails-3.2.x
|
125
|
+
- ci/Gemfile.rails-4.0.x
|
155
126
|
- lib/monogamy.rb
|
156
127
|
- lib/monogamy/mysql.rb
|
157
128
|
- lib/monogamy/postgresql.rb
|
158
|
-
- lib/monogamy/sqlite.rb
|
159
129
|
- lib/monogamy/version.rb
|
160
130
|
- lib/monogamy/with_table_lock.rb
|
161
131
|
- monogamy.gemspec
|
@@ -163,35 +133,28 @@ files:
|
|
163
133
|
- test/minitest_helper.rb
|
164
134
|
- test/monogamy_test.rb
|
165
135
|
- test/test_models.rb
|
166
|
-
homepage:
|
136
|
+
homepage: https://github.com/mceachen/monogamy
|
167
137
|
licenses: []
|
138
|
+
metadata: {}
|
168
139
|
post_install_message:
|
169
140
|
rdoc_options: []
|
170
141
|
require_paths:
|
171
142
|
- lib
|
172
143
|
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
-
none: false
|
174
144
|
requirements:
|
175
145
|
- - ! '>='
|
176
146
|
- !ruby/object:Gem::Version
|
177
147
|
version: '0'
|
178
|
-
segments:
|
179
|
-
- 0
|
180
|
-
hash: -3951791607027416484
|
181
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
-
none: false
|
183
149
|
requirements:
|
184
150
|
- - ! '>='
|
185
151
|
- !ruby/object:Gem::Version
|
186
152
|
version: '0'
|
187
|
-
segments:
|
188
|
-
- 0
|
189
|
-
hash: -3951791607027416484
|
190
153
|
requirements: []
|
191
154
|
rubyforge_project:
|
192
|
-
rubygems_version: 1.
|
155
|
+
rubygems_version: 2.1.10
|
193
156
|
signing_key:
|
194
|
-
specification_version:
|
157
|
+
specification_version: 4
|
195
158
|
summary: Add table-level database locking to ActiveRecord
|
196
159
|
test_files:
|
197
160
|
- test/database.yml
|
data/lib/monogamy/sqlite.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module Monogamy
|
2
|
-
module SQLite
|
3
|
-
# See http://sqlite.org/lang_transaction.html
|
4
|
-
def self.with_table_lock(connection, quoted_table_name, &block)
|
5
|
-
if connection.open_transactions > 0
|
6
|
-
raise NotImplementedError, "Support for nested transactions within sqlite has not been written"
|
7
|
-
end
|
8
|
-
|
9
|
-
begin
|
10
|
-
connection.execute("BEGIN EXCLUSIVE TRANSACTION")
|
11
|
-
rescue Exception => e
|
12
|
-
if e.message.include? "SQLite3::BusyException" # < Rails wraps BusyException in the message (!!!)
|
13
|
-
sleep 0.2
|
14
|
-
retry
|
15
|
-
else
|
16
|
-
raise e
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
begin
|
21
|
-
connection.increment_open_transactions
|
22
|
-
yield
|
23
|
-
connection.execute("COMMIT TRANSACTION")
|
24
|
-
rescue Exception => e
|
25
|
-
puts e
|
26
|
-
puts e.backtrace.join("\n")
|
27
|
-
connection.execute("ROLLBACK TRANSACTION")
|
28
|
-
ensure
|
29
|
-
connection.decrement_open_transactions
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|