xid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +7 -0
- data/LICENSE +22 -0
- data/README.md +40 -0
- data/Rakefile +2 -0
- data/lib/xid/connection_adapters/abstract_adapter.rb +19 -0
- data/lib/xid/connection_adapters/postgresql_adapter.rb +26 -0
- data/lib/xid/railtie.rb +9 -0
- data/lib/xid/version.rb +3 -0
- data/lib/xid.rb +12 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/xid/connection_adapters/abstract_adapter_spec.rb +19 -0
- data/spec/xid/connection_adapters/postgresql_adapter_spec.rb +33 -0
- data/xid.gemspec +17 -0
- metadata +84 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Adam Meehan
|
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,40 @@
|
|
1
|
+
# XID
|
2
|
+
|
3
|
+
Adds ability to ActiveRecord to retrieve the current transaction ID. This can be used to as metadata
|
4
|
+
for the purposes of grouping operations performed on many records across many tables. In particular I
|
5
|
+
had its usage with auditing libraries in mind.
|
6
|
+
|
7
|
+
Currently supports:
|
8
|
+
|
9
|
+
- PostgreSQL
|
10
|
+
|
11
|
+
Please fork and add functionality for other databases.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'xid'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install xid
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Simply call this to get the current ID:
|
30
|
+
|
31
|
+
ActiveRecord::Base.connection.transaction_id
|
32
|
+
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module XID
|
2
|
+
module ConnectionAdapters
|
3
|
+
module AbstractAdapter
|
4
|
+
|
5
|
+
def supports_transaction_id?
|
6
|
+
false
|
7
|
+
end
|
8
|
+
|
9
|
+
def transaction_id
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
|
18
|
+
include XID::ConnectionAdapters::AbstractAdapter
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module XID
|
2
|
+
module ConnectionAdapters
|
3
|
+
module PostgreSQLAdapter
|
4
|
+
|
5
|
+
def supports_transaction_id?
|
6
|
+
true
|
7
|
+
end
|
8
|
+
|
9
|
+
# From the PostgreSQL manual: http://www.postgresql.org/docs/9.1/static/functions-info.html#FUNCTIONS-TXID-SNAPSHOT
|
10
|
+
#
|
11
|
+
# The internal transaction ID type (xid) is 32 bits wide and wraps around every
|
12
|
+
# 4 billion transactions. However, these functions export a 64-bit format that
|
13
|
+
# is extended with an "epoch" counter so it will not wrap around during the
|
14
|
+
# life of an installation.
|
15
|
+
#
|
16
|
+
def transaction_id
|
17
|
+
select_value('SELECT txid_current()').to_i
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
|
25
|
+
include XID::ConnectionAdapters::PostgreSQLAdapter
|
26
|
+
end
|
data/lib/xid/railtie.rb
ADDED
data/lib/xid/version.rb
ADDED
data/lib/xid.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "xid/version"
|
2
|
+
|
3
|
+
module XID
|
4
|
+
def self.load
|
5
|
+
adapter_name = ActiveRecord::Base.connection_pool.spec.config[:adapter]
|
6
|
+
require "xid/connection_adapters/#{adapter_name}_adapter"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load(:active_record) do
|
11
|
+
require "xid/connection_adapters/abstract_adapter"
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'xid'
|
5
|
+
|
6
|
+
module SpecHelpers
|
7
|
+
def connect_with_adapter(adapter_name)
|
8
|
+
before(:all) do
|
9
|
+
ActiveRecord::Base.establish_connection({:adapter => adapter_name, :database => 'activerecord_unittest'})
|
10
|
+
require "xid/connection_adapters/#{adapter_name}_adapter"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
17
|
+
config.run_all_when_everything_filtered = true
|
18
|
+
config.filter_run :focus
|
19
|
+
|
20
|
+
# Run specs in random order to surface order dependencies. If you find an
|
21
|
+
# order dependency and want to debug it, you can fix the order by providing
|
22
|
+
# the seed, which is printed after each run.
|
23
|
+
# --seed 1234
|
24
|
+
config.order = 'random'
|
25
|
+
|
26
|
+
config.extend SpecHelpers
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require "xid/connection_adapters/abstract_adapter"
|
4
|
+
|
5
|
+
describe 'XID::ConnectionAdapters::AbstractAdapter' do
|
6
|
+
let(:adapter) { ActiveRecord::ConnectionAdapters::AbstractAdapter.new(stub.as_null_object) }
|
7
|
+
|
8
|
+
describe "#supports_transaction_id?" do
|
9
|
+
it "should respond false to supports_transaction_id?" do
|
10
|
+
adapter.supports_transaction_id?.should be_false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#transaction_id" do
|
15
|
+
it "should return nil" do
|
16
|
+
adapter.transaction_id.should be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'XID::ConnectionAdapters::PostgreSQLAdapter' do
|
4
|
+
connect_with_adapter 'postgresql'
|
5
|
+
|
6
|
+
let(:adapter) { ActiveRecord::Base.connection }
|
7
|
+
|
8
|
+
describe "#supports_transaction_id?" do
|
9
|
+
it "should respond false to supports_transaction_id?" do
|
10
|
+
adapter.supports_transaction_id?.should be_true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#transaction_id" do
|
15
|
+
it "should return number" do
|
16
|
+
adapter.transaction_id.should be_kind_of(Fixnum)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return a different id within an explicit transaction" do
|
20
|
+
tids = []
|
21
|
+
tids << adapter.transaction_id
|
22
|
+
|
23
|
+
ActiveRecord::Base.transaction do
|
24
|
+
tids << adapter.transaction_id
|
25
|
+
end
|
26
|
+
|
27
|
+
tids << adapter.transaction_id
|
28
|
+
|
29
|
+
tids.uniq.size.should eq 3
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/xid.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/xid/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Adam Meehan"]
|
6
|
+
gem.email = ["adam.meehan@gmail.com"]
|
7
|
+
gem.description = %q{Adds ability to ActiveRecord to retrieve the current transaction ID}
|
8
|
+
gem.summary = %q{Adds ability to ActiveRecord to retrieve the current transaction ID}
|
9
|
+
gem.homepage = "https://github.com/adzap/xid"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "xid"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = XID::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Adam Meehan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-05 00:00:00 +10:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Adds ability to ActiveRecord to retrieve the current transaction ID
|
23
|
+
email:
|
24
|
+
- adam.meehan@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rspec
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/xid.rb
|
39
|
+
- lib/xid/connection_adapters/abstract_adapter.rb
|
40
|
+
- lib/xid/connection_adapters/postgresql_adapter.rb
|
41
|
+
- lib/xid/railtie.rb
|
42
|
+
- lib/xid/version.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
- spec/xid/connection_adapters/abstract_adapter_spec.rb
|
45
|
+
- spec/xid/connection_adapters/postgresql_adapter_spec.rb
|
46
|
+
- xid.gemspec
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: https://github.com/adzap/xid
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.5.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Adds ability to ActiveRecord to retrieve the current transaction ID
|
81
|
+
test_files:
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
- spec/xid/connection_adapters/abstract_adapter_spec.rb
|
84
|
+
- spec/xid/connection_adapters/postgresql_adapter_spec.rb
|