monogamy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.idea
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.3
6
+
7
+ env:
8
+ - DB=sqlite
9
+ - DB=mysql
10
+ - DB=pg
11
+
12
+ script: bundle exec rake
13
+
14
+ before_script:
15
+ - mysql -e 'create database monogamy_test'
16
+ - psql -c 'create database monogamy_test' -U postgres
17
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matthew McEachen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Monogamy [![Build Status](https://api.travis-ci.org/mceachen/monogamy.png?branch=master)](https://travis-ci.org/mceachen/monogamy)
2
+
3
+ Adds table-level locking to ActiveRecord 3.x. MySQL, PostgreSQL, and SQLite are supported.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ Tag.with_table_lock do
9
+ Tag.find_or_create_by_name("example")
10
+ end
11
+ ```
12
+
13
+ While your code is inside the block, it will have exclusive read and write access to the model's
14
+ table.
15
+
16
+ If your block touches other tables, and you use table-level locking on those tables as well,
17
+ read up about [deadlocks](http://en.wikipedia.org/wiki/Deadlock). **You have been warned.**
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ``` ruby
24
+ gem 'monogamy'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ ## Changelog
32
+
33
+ ### 0.0.1
34
+
35
+ * First whack
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'yard'
4
+ YARD::Rake::YardocTask.new do |t|
5
+ t.files = ['lib/**/*.rb', 'README.md']
6
+ end
7
+
8
+ require 'rake/testtask'
9
+
10
+ Rake::TestTask.new do |t|
11
+ t.libs.push "lib"
12
+ t.libs.push "test"
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ task :default => :test
data/lib/monogamy.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'monogamy/with_table_lock'
2
+
3
+ module Monogamy
4
+ ActiveSupport.on_load :active_record do
5
+ ActiveRecord::Base.send :extend, Monogamy::WithTableLock
6
+ ActiveRecord::Base.send :include, Monogamy::WithTableLock
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ module Monogamy
2
+ module MySQL
3
+ # See http://dev.mysql.com/doc/refman/5.0/en/lock-tables-and-transactions.html
4
+ def self.with_table_lock(connection, quoted_table_name, &block)
5
+ begin
6
+ connection.transaction do
7
+ connection.execute("LOCK TABLES #{quoted_table_name} WRITE")
8
+ yield
9
+ end
10
+ ensure
11
+ connection.execute("UNLOCK TABLES")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Monogamy
2
+ module PostgreSQL
3
+ # See http://www.postgresql.org/docs/9.0/static/sql-lock.html
4
+ def self.with_table_lock(connection, quoted_table_name, &block)
5
+ connection.transaction do
6
+ connection.execute("LOCK TABLE #{quoted_table_name} IN ACCESS EXCLUSIVE MODE")
7
+ yield
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
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
@@ -0,0 +1,3 @@
1
+ module Monogamy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # Tried desperately to monkeypatch the polymorphic connection object,
2
+ # but rails lazyloading is too clever by half.
3
+
4
+ # Think of this module as a hipster, using "case" ironically.
5
+
6
+ require 'monogamy/mysql'
7
+ require 'monogamy/postgresql'
8
+ require 'monogamy/sqlite'
9
+
10
+ module Monogamy
11
+ module WithTableLock
12
+ def with_table_lock(&block)
13
+ adapter = case (connection.adapter_name.downcase)
14
+ when "postgresql"
15
+ Monogamy::PostgreSQL
16
+ when "mysql", "mysql2"
17
+ Monogamy::MySQL
18
+ when "sqlite"
19
+ Monogamy::SQLite
20
+ else
21
+ raise NotImplementedError, "Support for #{connection.adapter_name} has not been written"
22
+ end
23
+ adapter.with_table_lock(connection, quoted_table_name, &block)
24
+ end
25
+ end
26
+
27
+ end
data/monogamy.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'monogamy/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "monogamy"
8
+ gem.version = Monogamy::VERSION
9
+ gem.authors = ["Matthew McEachen"]
10
+ gem.email = ["matthew+github@mceachen.org"]
11
+ gem.description = %q{Add table-level database locking to ActiveRecord}
12
+ gem.summary = gem.description
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^test/})
18
+ gem.require_paths = %w(lib)
19
+
20
+ gem.add_runtime_dependency 'activerecord', '>= 3.0.0'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'yard'
24
+ gem.add_development_dependency 'minitest'
25
+ gem.add_development_dependency 'mysql2'
26
+ gem.add_development_dependency 'pg'
27
+ gem.add_development_dependency 'sqlite3'
28
+ gem.add_development_dependency 'database_cleaner'
29
+ end
data/test/database.yml ADDED
@@ -0,0 +1,16 @@
1
+ sqlite:
2
+ adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
3
+ database: monogamy.sqlite3.db
4
+ pool: 50
5
+ pg:
6
+ adapter: postgresql
7
+ username: postgres
8
+ database: monogamy_test
9
+ min_messages: ERROR
10
+ pool: 50
11
+ mysql:
12
+ adapter: mysql2
13
+ host: localhost
14
+ username: root
15
+ database: monogamy_test
16
+ pool: 50
@@ -0,0 +1,26 @@
1
+ require 'erb'
2
+ require 'active_record'
3
+ require 'monogamy'
4
+ require 'database_cleaner'
5
+
6
+ db_config = File.expand_path("database.yml", File.dirname(__FILE__))
7
+ ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(db_config)).result)
8
+ ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite")
9
+ ActiveRecord::Migration.verbose = false
10
+
11
+ require 'test_models'
12
+
13
+ Tag.new # < make sure class has loaded
14
+
15
+ require 'minitest/autorun'
16
+
17
+ DatabaseCleaner.strategy = :deletion
18
+ class MiniTest::Spec
19
+ before :each do
20
+ DatabaseCleaner.start
21
+ end
22
+ after :each do
23
+ DatabaseCleaner.clean
24
+ end
25
+ end
26
+
@@ -0,0 +1,61 @@
1
+ require 'minitest_helper'
2
+
3
+ describe "Monogamy" do
4
+
5
+ it "adds with_table_lock to ActiveRecord classes" do
6
+ assert Tag.respond_to?(:with_table_lock)
7
+ end
8
+
9
+ it "adds with_table_lock to ActiveRecord instances" do
10
+ assert Tag.new.respond_to?(:with_table_lock)
11
+ end
12
+
13
+ def find_or_create_at_even_second(run_at, with_table_lock)
14
+ Tag.connection.close
15
+ sleep(run_at - Time.now.to_f)
16
+ Tag.connection.reconnect!
17
+ if with_table_lock
18
+ Tag.with_table_lock do
19
+ Tag.find_or_create_by_name(run_at.to_s)
20
+ end
21
+ else
22
+ Tag.find_or_create_by_name(run_at.to_s)
23
+ end
24
+ end
25
+
26
+ def run_workers(with_table_lock)
27
+ start_time = Time.now.to_i + 2
28
+ threads = @workers.times.collect do
29
+ Thread.new do
30
+ begin
31
+ @iterations.times do |ea|
32
+ find_or_create_at_even_second(start_time + (ea * 2), with_table_lock)
33
+ end
34
+ ensure
35
+ ActiveRecord::Base.connection.close
36
+ end
37
+ end
38
+ end
39
+ threads.each { |ea| ea.join }
40
+ end
41
+
42
+ before :each do
43
+ @iterations = 5
44
+ @workers = 7
45
+ end
46
+
47
+ it "parallel threads create multiple duplicate rows" do
48
+ run_workers(with_table_lock = false)
49
+ puts "Created #{Tag.all.size} without lock"
50
+ if Tag.connection.adapter_name == "SQLite" && RUBY_VERSION == "1.9.3"
51
+ Tag.all.size.must_equal @iterations # <- sqlite on 1.9.3 doesn't create dupes IKNOWNOTWHY
52
+ else
53
+ Tag.all.size.must_be :>, @iterations # <- any duplicated rows will make me happy.
54
+ end
55
+ end
56
+
57
+ it "parallel threads with_table_lock don't create multiple duplicate rows" do
58
+ run_workers(with_table_lock = true)
59
+ Tag.all.size.must_equal @iterations # <- any duplicated rows will NOT make me happy.
60
+ end
61
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table "tags", :force => true do |t|
3
+ t.string "name"
4
+ end
5
+ end
6
+
7
+ class Tag < ActiveRecord::Base
8
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monogamy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew McEachen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: yard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: mysql2
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: pg
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ 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
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: database_cleaner
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: Add table-level database locking to ActiveRecord
143
+ email:
144
+ - matthew+github@mceachen.org
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - .gitignore
150
+ - .travis.yml
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - lib/monogamy.rb
156
+ - lib/monogamy/mysql.rb
157
+ - lib/monogamy/postgresql.rb
158
+ - lib/monogamy/sqlite.rb
159
+ - lib/monogamy/version.rb
160
+ - lib/monogamy/with_table_lock.rb
161
+ - monogamy.gemspec
162
+ - test/database.yml
163
+ - test/minitest_helper.rb
164
+ - test/monogamy_test.rb
165
+ - test/test_models.rb
166
+ homepage: ''
167
+ licenses: []
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ none: false
174
+ requirements:
175
+ - - ! '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ segments:
179
+ - 0
180
+ hash: -3951791607027416484
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ! '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ segments:
188
+ - 0
189
+ hash: -3951791607027416484
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 1.8.23
193
+ signing_key:
194
+ specification_version: 3
195
+ summary: Add table-level database locking to ActiveRecord
196
+ test_files:
197
+ - test/database.yml
198
+ - test/minitest_helper.rb
199
+ - test/monogamy_test.rb
200
+ - test/test_models.rb
201
+ has_rdoc: