active_record-sequence 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +15 -0
- data/Appraisals +28 -0
- data/Gemfile +4 -0
- data/README.md +136 -0
- data/Rakefile +6 -0
- data/active_record-sequence.gemspec +28 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/gemfiles/rails_4.2.7.gemfile +7 -0
- data/gemfiles/rails_4.2.7.gemfile.lock +88 -0
- data/gemfiles/rails_5.0.0.gemfile +7 -0
- data/gemfiles/rails_5.0.0.gemfile.lock +86 -0
- data/lib/active_record/sequence/error.rb +11 -0
- data/lib/active_record/sequence/sequence_sql_builder.rb +48 -0
- data/lib/active_record/sequence/version.rb +5 -0
- data/lib/active_record/sequence.rb +117 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2043cdec6848e80f36c99347db78eb9d68f879a2
|
4
|
+
data.tar.gz: cd9abcd82560fb81592fd8542c30760e89f450fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78c26a534695256415675748ba9fa6d2c6b9b2ca617146f226e1d5e447f5940bfb3c45614ba9bebfc0d9ee4e21104a16e9376498529e96fe21483364315d3b84
|
7
|
+
data.tar.gz: 7057f3e410367eefded9028303b65993887cc78b1df61d154e0adbd14f920df3e4a3d16c30e70e72882ee6da022a295cf052642c98646c073c819408720a2439
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.2.5
|
5
|
+
- 2.3.1
|
6
|
+
services:
|
7
|
+
- postgresql
|
8
|
+
before_script:
|
9
|
+
- "./bin/setup"
|
10
|
+
script:
|
11
|
+
- bundle exec rake spec
|
12
|
+
- bundle exec rubocop --fail-level C
|
13
|
+
gemfile:
|
14
|
+
- gemfiles/rails_4.2.7.gemfile
|
15
|
+
- gemfiles/rails_5.0.0.gemfile
|
data/Appraisals
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
ruby_versions = %w(2.2.5 2.3.1)
|
4
|
+
rails_versions = %w(4.2.7 5.0.0)
|
5
|
+
|
6
|
+
rails_versions.each do |rails_version|
|
7
|
+
appraise "rails_#{rails_version}" do
|
8
|
+
gem 'activerecord', rails_version
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
travis = ::YAML.dump(
|
13
|
+
'language' => 'ruby',
|
14
|
+
'rvm' => ruby_versions,
|
15
|
+
'services' => ['postgresql'],
|
16
|
+
'before_script' => [
|
17
|
+
'./bin/setup',
|
18
|
+
],
|
19
|
+
'script' => [
|
20
|
+
'bundle exec rake spec',
|
21
|
+
'bundle exec rubocop --fail-level C'
|
22
|
+
],
|
23
|
+
'gemfile' => Dir.glob('gemfiles/*.gemfile'),
|
24
|
+
)
|
25
|
+
|
26
|
+
::File.open('.travis.yml', 'w+') do |file|
|
27
|
+
file.write(travis)
|
28
|
+
end
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/bolshakov/active_record-sequence.svg?branch=master)](https://travis-ci.org/bolshakov/active_record-sequence)
|
2
|
+
|
3
|
+
# ActiveRecord::Sequence
|
4
|
+
|
5
|
+
Access to [PostgreSQL's Sequences](https://www.postgresql.org/docs/8.1/static/sql-createsequence.html)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'active_record-sequence'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install active_record-sequence
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
By default new sequence starts from `1`:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
sequence = ActiveRecord::Sequence.create('numbers')
|
29
|
+
```
|
30
|
+
|
31
|
+
`#next` returns next value in the sequence:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
sequence.next #=> 1
|
35
|
+
sequence.next #=> 2
|
36
|
+
```
|
37
|
+
|
38
|
+
`#peek` returns current value:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
sequence.peek #=> 2
|
42
|
+
```
|
43
|
+
|
44
|
+
You can start a sequence with specific value:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
sequence = ActiveRecord::Sequence.create('numbers', start: 42)
|
48
|
+
sequence.next #=> 42
|
49
|
+
sequence.next #=> 43
|
50
|
+
```
|
51
|
+
|
52
|
+
Specify custom increment value:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
sequence = ActiveRecord::Sequence.create('numbers', increment: 3)
|
56
|
+
sequence.next #=> 1
|
57
|
+
sequence.next #=> 4
|
58
|
+
```
|
59
|
+
|
60
|
+
If you pass negative increment, a sequence will be decreasing:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
sequence = ActiveRecord::Sequence.create('numbers', increment: -3)
|
64
|
+
sequence.next #=> -1
|
65
|
+
sequence.next #=> -4
|
66
|
+
```
|
67
|
+
|
68
|
+
To limit number of elements in a sequence specify `max` value:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
sequence = ActiveRecord::Sequence.create('numbers', max: 2)
|
72
|
+
sequence.next #=> 1
|
73
|
+
sequence.next #=> 2
|
74
|
+
sequence.next #=> fail with StopIteration
|
75
|
+
```
|
76
|
+
|
77
|
+
Decreasing sequence may be limited as well:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
sequence = ActiveRecord::Sequence.create('numbers', min: -2, increment: -1)
|
81
|
+
sequence.next #=> -1
|
82
|
+
sequence.next #=> -2
|
83
|
+
sequence.next #=> fail with StopIteration
|
84
|
+
```
|
85
|
+
|
86
|
+
To define infinite sequence, use `cycle` option:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
sequence = ActiveRecord::Sequence.create('numbers', max: 2, cycle: true)
|
90
|
+
sequence.next #=> 1
|
91
|
+
sequence.next #=> 2
|
92
|
+
sequence.next #=> 1
|
93
|
+
sequence.next #=> 2
|
94
|
+
# etc.
|
95
|
+
```
|
96
|
+
|
97
|
+
You con use previously created sequence by instantiating `Sequence` class:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
ActiveRecord::Sequence.create('numbers', max: 2, cycle: true)
|
101
|
+
sequence = ActiveRecord::Sequence.new('numbers')
|
102
|
+
sequence.next #=> 1
|
103
|
+
sequence.next #=> 2
|
104
|
+
sequence.next #=> 1
|
105
|
+
```
|
106
|
+
|
107
|
+
To destroy a sequence:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
ActiveRecord::Sequence.drop('numbers')
|
111
|
+
```
|
112
|
+
|
113
|
+
## Development
|
114
|
+
|
115
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests.
|
116
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
117
|
+
|
118
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a
|
119
|
+
new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
120
|
+
which will create a git tag for the version, push git commits and tags, and push the `.gem`
|
121
|
+
file to [rubygems.org](https://rubygems.org).
|
122
|
+
|
123
|
+
|
124
|
+
We test this gem against different versions of `ActiveRecord` using [appraisal](https://github.com/thoughtbot/appraisal) gem.
|
125
|
+
To regenerate gemfiles run:
|
126
|
+
|
127
|
+
$ appraisal install
|
128
|
+
|
129
|
+
To run specs against all versions:
|
130
|
+
|
131
|
+
$ appraisal rake spec
|
132
|
+
|
133
|
+
## Contributing
|
134
|
+
|
135
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bolshakov/active_record-sequence.
|
136
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_record/sequence/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'active_record-sequence'
|
8
|
+
spec.version = ActiveRecord::Sequence::VERSION
|
9
|
+
spec.authors = ['Tema Bolshakov']
|
10
|
+
spec.email = ['abolshakov@spbtv.com']
|
11
|
+
|
12
|
+
spec.summary = "Provide access to PostgreSQL's sequences"
|
13
|
+
spec.description = "Provide access to PostgreSQL's sequences"
|
14
|
+
spec.homepage = 'https://github.com/bolshakov/active_record-sequence'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'activerecord', '>= 3.2', '< 6.0.0'
|
22
|
+
spec.add_development_dependency 'appraisal', '2.1.0'
|
23
|
+
spec.add_development_dependency 'bundler', '1.13.1'
|
24
|
+
spec.add_development_dependency 'pg', '0.19.0'
|
25
|
+
spec.add_development_dependency 'rake', '11.3.0'
|
26
|
+
spec.add_development_dependency 'rspec', '3.5.0'
|
27
|
+
spec.add_development_dependency 'spbtv_code_style', '1.4.1'
|
28
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'active_record/sequence'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
active_record-sequence (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (4.2.7)
|
10
|
+
activesupport (= 4.2.7)
|
11
|
+
builder (~> 3.1)
|
12
|
+
activerecord (4.2.7)
|
13
|
+
activemodel (= 4.2.7)
|
14
|
+
activesupport (= 4.2.7)
|
15
|
+
arel (~> 6.0)
|
16
|
+
activesupport (4.2.7)
|
17
|
+
i18n (~> 0.7)
|
18
|
+
json (~> 1.7, >= 1.7.7)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
appraisal (2.1.0)
|
23
|
+
bundler
|
24
|
+
rake
|
25
|
+
thor (>= 0.14.0)
|
26
|
+
arel (6.0.3)
|
27
|
+
ast (2.3.0)
|
28
|
+
builder (3.2.2)
|
29
|
+
diff-lcs (1.2.5)
|
30
|
+
i18n (0.7.0)
|
31
|
+
json (1.8.3)
|
32
|
+
minitest (5.9.0)
|
33
|
+
parser (2.3.1.2)
|
34
|
+
ast (~> 2.2)
|
35
|
+
pg (0.19.0)
|
36
|
+
powerpack (0.1.1)
|
37
|
+
rainbow (2.1.0)
|
38
|
+
rake (11.3.0)
|
39
|
+
rspec (3.5.0)
|
40
|
+
rspec-core (~> 3.5.0)
|
41
|
+
rspec-expectations (~> 3.5.0)
|
42
|
+
rspec-mocks (~> 3.5.0)
|
43
|
+
rspec-core (3.5.3)
|
44
|
+
rspec-support (~> 3.5.0)
|
45
|
+
rspec-expectations (3.5.0)
|
46
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
+
rspec-support (~> 3.5.0)
|
48
|
+
rspec-mocks (3.5.0)
|
49
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
50
|
+
rspec-support (~> 3.5.0)
|
51
|
+
rspec-support (3.5.0)
|
52
|
+
rspec_junit_formatter (0.2.3)
|
53
|
+
builder (< 4)
|
54
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
55
|
+
rubocop (0.40.0)
|
56
|
+
parser (>= 2.3.1.0, < 3.0)
|
57
|
+
powerpack (~> 0.1)
|
58
|
+
rainbow (>= 1.99.1, < 3.0)
|
59
|
+
ruby-progressbar (~> 1.7)
|
60
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
61
|
+
rubocop-checkstyle_formatter (0.3.0)
|
62
|
+
rubocop (>= 0.30.1)
|
63
|
+
ruby-progressbar (1.8.1)
|
64
|
+
spbtv_code_style (1.4.1)
|
65
|
+
rspec_junit_formatter
|
66
|
+
rubocop (= 0.40.0)
|
67
|
+
rubocop-checkstyle_formatter
|
68
|
+
thor (0.19.1)
|
69
|
+
thread_safe (0.3.5)
|
70
|
+
tzinfo (1.2.2)
|
71
|
+
thread_safe (~> 0.1)
|
72
|
+
unicode-display_width (1.1.1)
|
73
|
+
|
74
|
+
PLATFORMS
|
75
|
+
ruby
|
76
|
+
|
77
|
+
DEPENDENCIES
|
78
|
+
active_record-sequence!
|
79
|
+
activerecord (= 4.2.7)
|
80
|
+
appraisal (= 2.1.0)
|
81
|
+
bundler (= 1.13.1)
|
82
|
+
pg (= 0.19.0)
|
83
|
+
rake (= 11.3.0)
|
84
|
+
rspec (= 3.5.0)
|
85
|
+
spbtv_code_style (= 1.4.1)
|
86
|
+
|
87
|
+
BUNDLED WITH
|
88
|
+
1.13.1
|
@@ -0,0 +1,86 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
active_record-sequence (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (5.0.0)
|
10
|
+
activesupport (= 5.0.0)
|
11
|
+
activerecord (5.0.0)
|
12
|
+
activemodel (= 5.0.0)
|
13
|
+
activesupport (= 5.0.0)
|
14
|
+
arel (~> 7.0)
|
15
|
+
activesupport (5.0.0)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (~> 0.7)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
appraisal (2.1.0)
|
21
|
+
bundler
|
22
|
+
rake
|
23
|
+
thor (>= 0.14.0)
|
24
|
+
arel (7.1.1)
|
25
|
+
ast (2.3.0)
|
26
|
+
builder (3.2.2)
|
27
|
+
concurrent-ruby (1.0.2)
|
28
|
+
diff-lcs (1.2.5)
|
29
|
+
i18n (0.7.0)
|
30
|
+
minitest (5.9.0)
|
31
|
+
parser (2.3.1.2)
|
32
|
+
ast (~> 2.2)
|
33
|
+
pg (0.19.0)
|
34
|
+
powerpack (0.1.1)
|
35
|
+
rainbow (2.1.0)
|
36
|
+
rake (11.3.0)
|
37
|
+
rspec (3.5.0)
|
38
|
+
rspec-core (~> 3.5.0)
|
39
|
+
rspec-expectations (~> 3.5.0)
|
40
|
+
rspec-mocks (~> 3.5.0)
|
41
|
+
rspec-core (3.5.3)
|
42
|
+
rspec-support (~> 3.5.0)
|
43
|
+
rspec-expectations (3.5.0)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.5.0)
|
46
|
+
rspec-mocks (3.5.0)
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
48
|
+
rspec-support (~> 3.5.0)
|
49
|
+
rspec-support (3.5.0)
|
50
|
+
rspec_junit_formatter (0.2.3)
|
51
|
+
builder (< 4)
|
52
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
53
|
+
rubocop (0.40.0)
|
54
|
+
parser (>= 2.3.1.0, < 3.0)
|
55
|
+
powerpack (~> 0.1)
|
56
|
+
rainbow (>= 1.99.1, < 3.0)
|
57
|
+
ruby-progressbar (~> 1.7)
|
58
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
59
|
+
rubocop-checkstyle_formatter (0.3.0)
|
60
|
+
rubocop (>= 0.30.1)
|
61
|
+
ruby-progressbar (1.8.1)
|
62
|
+
spbtv_code_style (1.4.1)
|
63
|
+
rspec_junit_formatter
|
64
|
+
rubocop (= 0.40.0)
|
65
|
+
rubocop-checkstyle_formatter
|
66
|
+
thor (0.19.1)
|
67
|
+
thread_safe (0.3.5)
|
68
|
+
tzinfo (1.2.2)
|
69
|
+
thread_safe (~> 0.1)
|
70
|
+
unicode-display_width (1.1.1)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
active_record-sequence!
|
77
|
+
activerecord (= 5.0.0)
|
78
|
+
appraisal (= 2.1.0)
|
79
|
+
bundler (= 1.13.1)
|
80
|
+
pg (= 0.19.0)
|
81
|
+
rake (= 11.3.0)
|
82
|
+
rspec (= 3.5.0)
|
83
|
+
spbtv_code_style (= 1.4.1)
|
84
|
+
|
85
|
+
BUNDLED WITH
|
86
|
+
1.13.1
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Sequence
|
3
|
+
Error = Class.new(StandardError)
|
4
|
+
# Sequence is already exists and thus could not be created.
|
5
|
+
AlreadyExist = Class.new(Error)
|
6
|
+
# To obtain current value, you have to call `#next` first.
|
7
|
+
CurrentValueUndefined = Class.new(Error)
|
8
|
+
# Sequence is not exists and thus could not be deleted or accessed.
|
9
|
+
NotExist = Class.new(Error)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Sequence # rubocop:disable Style/Documentation
|
3
|
+
# Builds SQL statement for creating sequence
|
4
|
+
# @api private
|
5
|
+
class SequenceSQLBuilder
|
6
|
+
attr_reader :options
|
7
|
+
attr_reader :parts
|
8
|
+
|
9
|
+
def initialize(name, options = {})
|
10
|
+
@options = options
|
11
|
+
@parts = [format('CREATE SEQUENCE %s', name)]
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_sql
|
15
|
+
configure_increment
|
16
|
+
configure_min_value
|
17
|
+
configure_max_value
|
18
|
+
configure_start_value
|
19
|
+
configure_cycle
|
20
|
+
parts.join(' ')
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def configure_increment
|
26
|
+
parts << format('INCREMENT BY %s', options[:increment]) if options[:increment]
|
27
|
+
end
|
28
|
+
|
29
|
+
def configure_min_value
|
30
|
+
parts << format('MINVALUE %s', options[:min]) if options[:min]
|
31
|
+
end
|
32
|
+
|
33
|
+
def configure_max_value
|
34
|
+
parts << format('MAXVALUE %s', options[:max]) if options[:max]
|
35
|
+
end
|
36
|
+
|
37
|
+
def configure_start_value
|
38
|
+
parts << format('START %s', options[:start]) if options[:start]
|
39
|
+
end
|
40
|
+
|
41
|
+
def configure_cycle
|
42
|
+
parts << (options.fetch(:cycle, false) ? 'CYCLE' : 'NO CYCLE')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private_constant(:SequenceSQLBuilder)
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/sequence/version'
|
3
|
+
require 'active_record/sequence/error'
|
4
|
+
require 'active_support/core_ext/module/delegation'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
# Usage
|
8
|
+
# sequence = Sequence.new('numbers')
|
9
|
+
# sequence.next #=> 1
|
10
|
+
# sequence.peek #=> 1
|
11
|
+
# sequence.next #=> 2
|
12
|
+
#
|
13
|
+
class Sequence
|
14
|
+
autoload :SequenceSQLBuilder, 'active_record/sequence/sequence_sql_builder'
|
15
|
+
|
16
|
+
class << self
|
17
|
+
CREATE_ERRORS = {
|
18
|
+
'PG::DuplicateTable' => ActiveRecord::Sequence::AlreadyExist,
|
19
|
+
}.freeze
|
20
|
+
# Create sequence
|
21
|
+
# @param name [String]
|
22
|
+
# @param options [{}]
|
23
|
+
# @option options [Integer] :start (1)
|
24
|
+
# @option options [Integer] :increment (1) specifies which value is added to
|
25
|
+
# the current sequence value to create a new value. A positive value will
|
26
|
+
# make an ascending sequence, a negative one a descending sequence.
|
27
|
+
# @option options [Integer] :min determines the minimum value a sequence can generate.
|
28
|
+
# The defaults are 1 and -2^63-1 for ascending and descending sequences, respectively.
|
29
|
+
# @option options [Integer] :max determines the maximum value for the sequence.
|
30
|
+
# The defaults are 2^63-1 and -1 for ascending and descending sequences, respectively.
|
31
|
+
# @option options [Boolean] :cycle (false) allows the sequence to wrap around when the
|
32
|
+
# max value or min value has been reached by an ascending or descending sequence respectively.
|
33
|
+
# If the limit is reached, the next number generated will be the min value or max value, respectively.
|
34
|
+
# @return [Sequence]
|
35
|
+
# @see https://www.postgresql.org/docs/8.1/static/sql-createsequence.html
|
36
|
+
def create(name, options = {})
|
37
|
+
create_sql = SequenceSQLBuilder.new(name, options).to_sql
|
38
|
+
handle_postgres_errors(CREATE_ERRORS) do
|
39
|
+
connection.execute(create_sql)
|
40
|
+
end
|
41
|
+
new(name)
|
42
|
+
end
|
43
|
+
|
44
|
+
DROP_ERRORS = {
|
45
|
+
'PG::UndefinedTable' => NotExist,
|
46
|
+
}.freeze
|
47
|
+
|
48
|
+
# @param name [String]
|
49
|
+
# @return [void]
|
50
|
+
def drop(name)
|
51
|
+
drop_sql = format('DROP SEQUENCE %s', name)
|
52
|
+
handle_postgres_errors(DROP_ERRORS) do
|
53
|
+
connection.execute(drop_sql)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def connection
|
58
|
+
@connection ||= ActiveRecord::Base.retrieve_connection
|
59
|
+
end
|
60
|
+
|
61
|
+
# @param mappings [{}] from PG errors to library errors
|
62
|
+
# @api private
|
63
|
+
def handle_postgres_errors(mappings)
|
64
|
+
yield
|
65
|
+
rescue ActiveRecord::StatementInvalid => error
|
66
|
+
library_error = mappings.fetch(error.cause.class.name) { raise }
|
67
|
+
raise library_error
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
attr_reader :name
|
72
|
+
|
73
|
+
# @param name [String]
|
74
|
+
def initialize(name)
|
75
|
+
@name = name
|
76
|
+
@connection = self.class.connection
|
77
|
+
end
|
78
|
+
|
79
|
+
NEXT_ERRORS = {
|
80
|
+
'PG::ObjectNotInPrerequisiteState' => StopIteration,
|
81
|
+
'PG::UndefinedTable' => ActiveRecord::Sequence::NotExist,
|
82
|
+
}.freeze
|
83
|
+
|
84
|
+
# @return [Integer]
|
85
|
+
def next
|
86
|
+
next_sql = 'SELECT nextval(%s)'.freeze
|
87
|
+
handle_postgres_errors(NEXT_ERRORS) do
|
88
|
+
execute(next_sql, name)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
PEEK_ERRORS = {
|
93
|
+
'PG::ObjectNotInPrerequisiteState' => ActiveRecord::Sequence::CurrentValueUndefined,
|
94
|
+
'PG::UndefinedTable' => ActiveRecord::Sequence::NotExist,
|
95
|
+
}.freeze
|
96
|
+
|
97
|
+
# @return [Integer]
|
98
|
+
def peek
|
99
|
+
current_sql = 'SELECT currval(%s)'.freeze
|
100
|
+
handle_postgres_errors(PEEK_ERRORS) do
|
101
|
+
execute(current_sql, name)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
attr_reader :connection
|
108
|
+
delegate :handle_postgres_errors, to: :class
|
109
|
+
|
110
|
+
def execute(sql, *args)
|
111
|
+
quoted_args = args.map { |arg| connection.quote(arg) }
|
112
|
+
formatted_sql = format(sql, *quoted_args)
|
113
|
+
|
114
|
+
connection.select_value(formatted_sql).to_i
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record-sequence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tema Bolshakov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.0.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 6.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: appraisal
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 2.1.0
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.1.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.13.1
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.13.1
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pg
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.19.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.19.0
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rake
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 11.3.0
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 11.3.0
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 3.5.0
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 3.5.0
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: spbtv_code_style
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.4.1
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 1.4.1
|
117
|
+
description: Provide access to PostgreSQL's sequences
|
118
|
+
email:
|
119
|
+
- abolshakov@spbtv.com
|
120
|
+
executables: []
|
121
|
+
extensions: []
|
122
|
+
extra_rdoc_files: []
|
123
|
+
files:
|
124
|
+
- ".gitignore"
|
125
|
+
- ".rspec"
|
126
|
+
- ".rubocop.yml"
|
127
|
+
- ".travis.yml"
|
128
|
+
- Appraisals
|
129
|
+
- Gemfile
|
130
|
+
- README.md
|
131
|
+
- Rakefile
|
132
|
+
- active_record-sequence.gemspec
|
133
|
+
- bin/console
|
134
|
+
- bin/setup
|
135
|
+
- gemfiles/rails_4.2.7.gemfile
|
136
|
+
- gemfiles/rails_4.2.7.gemfile.lock
|
137
|
+
- gemfiles/rails_5.0.0.gemfile
|
138
|
+
- gemfiles/rails_5.0.0.gemfile.lock
|
139
|
+
- lib/active_record/sequence.rb
|
140
|
+
- lib/active_record/sequence/error.rb
|
141
|
+
- lib/active_record/sequence/sequence_sql_builder.rb
|
142
|
+
- lib/active_record/sequence/version.rb
|
143
|
+
homepage: https://github.com/bolshakov/active_record-sequence
|
144
|
+
licenses: []
|
145
|
+
metadata: {}
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 2.5.1
|
163
|
+
signing_key:
|
164
|
+
specification_version: 4
|
165
|
+
summary: Provide access to PostgreSQL's sequences
|
166
|
+
test_files: []
|