sequel-nonsequential_id 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 556975d177a23d88c9c79a0dbbfd5b10dfa0da907848ae8a18e048c5983cb855
4
- data.tar.gz: 1e4ffcbf3f5e29577d17a23d9cdd06a17534fef107218cd42edf4f57cba66684
3
+ metadata.gz: 840ea9c131d342706670449161549bdb60d487d1f840d55b9d2ea62a0a739638
4
+ data.tar.gz: 546f9996f736dbf205dc5a371267f4be1a2df900066ac2c069242176bf440c27
5
5
  SHA512:
6
- metadata.gz: d036cefe901807033faf13f000406edaf5bf28f73b179a781185859538168434d9213bf0f886b73b03d8a774a1d519a2aa42a2d67501901c89380eed185545a8
7
- data.tar.gz: 4ded9c4e5449800b4ff0433ae1549acf8611ae15685ee00295b7b17c8b996ed917a1000cca2ed2785d2be673d52235d94b4b38b48e3fa1d573344a57fc112765
6
+ metadata.gz: 755931214c9f7dd82076f0d5c66f2fb9b69f418a5beeb25b26eb2cc021c91f472ab4701266e478f102012929dbb27a3ca69666e841ee828503b348a4933a5e10
7
+ data.tar.gz: 1367bc011bb4014671b7dce4e2326d1f9fe723698351dc4f29bd455f832f04d0559d28cb01a3d24bef4bc89803e1999f9aae1314abe4b54fc93bae5cd5303491
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ sequel-uniqueid
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
@@ -15,21 +15,24 @@ module Sequel
15
15
  end
16
16
 
17
17
  module InstanceMethods
18
- def before_create
19
- set_nonsequential_id
20
- super
18
+ def around_create
19
+ begin
20
+ set_nonsequential_id
21
+ super
22
+ rescue Sequel::UniqueConstraintViolation => e
23
+ if e.message =~ /\b#{model.table_name}\.#{model.id_field}\b/
24
+ retry
25
+ else
26
+ raise
27
+ end
28
+ end
21
29
  end
22
30
 
31
+
23
32
  private def set_nonsequential_id
24
33
  method = :"#{model.id_field}="
25
-
26
- loop do
27
- id = SecureRandom.hex.hex.to_s(36)
28
- unless model.first("#{model.id_field}": id)
29
- set_column_value(method, id)
30
- return id
31
- end
32
- end
34
+ id = SecureRandom.hex.hex.to_s(36)
35
+ set_column_value(method, id)
33
36
  end
34
37
  end
35
38
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'sequel-nonsequential_id'
5
- spec.version = '1.0.0'
5
+ spec.version = '1.0.1'
6
6
  spec.authors = ['Jesse Bowes']
7
7
  spec.email = ['jbowes@dashingrocket.com']
8
8
  spec.summary = 'Sequel Non-Sequential Unique ID Plugin'
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'bundler', '~> 1.16'
22
22
  spec.add_development_dependency 'rake', '~> 12.0'
23
23
  spec.add_development_dependency 'sqlite3', '~> 1.3'
24
+ spec.add_development_dependency 'mocha', '~> 1.7'
24
25
  end
@@ -1,5 +1,8 @@
1
1
  require 'test/unit'
2
+ require 'mocha/test_unit'
3
+
2
4
  require 'sequel'
5
+ require 'timeout'
3
6
 
4
7
  require_relative '../lib/sequel/plugins/nonsequential_id'
5
8
 
@@ -7,7 +10,7 @@ DB = Sequel.sqlite
7
10
 
8
11
  DB.create_table :test_models do
9
12
  primary_key :id, type: String, auto_increment: false
10
- String :name
13
+ String :name, unique: true
11
14
  end
12
15
 
13
16
  DB.create_table :test_model2s do
@@ -28,6 +31,11 @@ class NonsequentialIdTest < Test::Unit::TestCase
28
31
 
29
32
  end
30
33
 
34
+ def teardown
35
+ DB[:test_models].delete
36
+ DB[:test_model2s].delete
37
+ end
38
+
31
39
  def test_nonsequential_id
32
40
  obj1 = TestModel.create(name: 'obj1')
33
41
  obj2 = TestModel.create(name: 'obj2')
@@ -44,7 +52,7 @@ class NonsequentialIdTest < Test::Unit::TestCase
44
52
  assert_equal(obj2.id, TestModel.first(id: obj2.id).id)
45
53
  end
46
54
 
47
- def test_test_nonsequential_id_custom_field
55
+ def test_nonsequential_id_custom_field
48
56
  obj1 = TestModel2.create(name: 'obj1')
49
57
  obj2 = TestModel2.create(name: 'obj2')
50
58
 
@@ -59,4 +67,22 @@ class NonsequentialIdTest < Test::Unit::TestCase
59
67
  assert_equal(obj1.custom_id, TestModel2.first(custom_id: obj1.custom_id).custom_id)
60
68
  assert_equal(obj2.custom_id, TestModel2.first(custom_id: obj2.custom_id).custom_id)
61
69
  end
70
+
71
+ def test_duplicate_id
72
+ SecureRandom.stubs(:hex).returns('a', 'a', 'b')
73
+ obj1 = TestModel.create(name: 'obj1')
74
+ obj2 = nil
75
+
76
+ Timeout::timeout(1) do
77
+ obj2 = TestModel.create(name: 'obj2')
78
+ end
79
+
80
+ assert_equal 'a', obj1.id
81
+ assert_equal 'b', obj2.id
82
+ end
83
+
84
+ def test_duplicate_other
85
+ TestModel.create(name: 'obj1')
86
+ assert_raise(Sequel::UniqueConstraintViolation){TestModel.create(name: 'obj1')}
87
+ end
62
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-nonsequential_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Bowes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-02 00:00:00.000000000 Z
11
+ date: 2018-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.7'
83
97
  description: Automatically generate non-sequential (pseudo-random) IDs for Sequel
84
98
  Models
85
99
  email:
@@ -89,6 +103,8 @@ extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
105
  - ".gitignore"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
92
108
  - Gemfile
93
109
  - LICENSE
94
110
  - README.md