activerecord-scoped_sequence 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1f4343e352f4a5cc1b9d051f5d7c9a70b2002d0a2fcc370d4c1279c2c940cf27
4
+ data.tar.gz: 71978321b77654636cf975ce5a8d02af28423657f5de455e7c3f14ba6546fb5b
5
+ SHA512:
6
+ metadata.gz: 1698c8de3e04185ee467e6c405cfd2aebce1e1f890f0f0f9f3ce3d7eab4ce3a5baaada28f7e38bea9229b60ec3ff43c8d327554bbf4b6d42dbfbaa8714db1130
7
+ data.tar.gz: e38cc4f43975d0f99224bdcf09097c52ddec74ce415197e25b4b05b7af018b6f1b5b4c17178ac8985249f340c3a62986cec231a650592778547bb35b808952a1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 activerecord-scoped_sequence contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Active Record Scoped Sequence
2
+
3
+ `activerecord-scoped_sequence` assigns increasing integer values within the
4
+ scope of a `belongs_to` association. It serializes allocation by locking the
5
+ associated row, so concurrent inserts for one scope cannot receive the same
6
+ generated value.
7
+
8
+ ## Installation
9
+
10
+ Add the gem to your application:
11
+
12
+ ```ruby
13
+ gem "activerecord-scoped_sequence"
14
+ ```
15
+
16
+ Then run `bundle install`.
17
+
18
+ ## Usage
19
+
20
+ Add the sequence column and an index covering its scope:
21
+
22
+ ```ruby
23
+ add_column :invoices, :number, :bigint
24
+ add_index :invoices, [:organization_id, :number], unique: true
25
+ ```
26
+
27
+ Declare the sequence on the model:
28
+
29
+ ```ruby
30
+ class Invoice < ApplicationRecord
31
+ belongs_to :organization
32
+
33
+ scoped_sequence :number, scope: :organization
34
+ end
35
+ ```
36
+
37
+ The organization is locked while the next number is assigned by querying the
38
+ maximum invoice number currently persisted for it and adding one. An explicitly
39
+ supplied number is preserved.
40
+
41
+ ## Concurrency and indexes
42
+
43
+ The lock is acquired in `before_create`, inside Active Record's implicit save
44
+ transaction. Database row locks live until that transaction commits or rolls
45
+ back, so the lock remains held through the insert even after the callback
46
+ returns.
47
+
48
+ Every writer must use callbacks for allocation. Bulk APIs such as `insert_all`
49
+ bypass the macro. A composite unique index remains strongly recommended as a
50
+ database invariant; it is defensive and is not used as a retry mechanism.
51
+
52
+ The gem currently supports non-polymorphic `belongs_to` associations with
53
+ single-column keys.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module ScopedSequence
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "active_record/scoped_sequence/version"
5
+
6
+ module ActiveRecord
7
+ module ScopedSequence
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ class_attribute :scoped_sequence_definitions, instance_writer: false, default: {}.freeze
12
+ end
13
+
14
+ class_methods do
15
+ # Defines an integer sequence on +attribute+ scoped to a +belongs_to+
16
+ # association. The associated row is locked through the record insert.
17
+ #
18
+ # The next value is one greater than the maximum value currently persisted
19
+ # in the scope.
20
+ def scoped_sequence(attribute, scope:)
21
+ attribute = attribute.to_s
22
+ scope = scope.to_sym
23
+
24
+ self.scoped_sequence_definitions = scoped_sequence_definitions.merge(
25
+ attribute => { scope: scope }.freeze
26
+ ).freeze
27
+
28
+ before_create do
29
+ assign_scoped_sequence(attribute, scope)
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+ def assign_scoped_sequence(attribute, scope)
36
+ reflection = self.class.reflect_on_association(scope)
37
+
38
+ unless reflection&.belongs_to?
39
+ raise ArgumentError, "Scoped sequence scope #{scope.inspect} must name a belongs_to association"
40
+ end
41
+ if reflection.polymorphic?
42
+ raise ArgumentError, "Scoped sequences do not support polymorphic associations"
43
+ end
44
+ if reflection.foreign_key.is_a?(Array) || reflection.association_primary_key.is_a?(Array)
45
+ raise ArgumentError, "Scoped sequences do not support composite association keys"
46
+ end
47
+
48
+ foreign_key = reflection.foreign_key
49
+ scope_id = _read_attribute(foreign_key)
50
+ raise ArgumentError, "Scoped sequence association #{scope.inspect} must be persisted" if scope_id.nil?
51
+
52
+ primary_key = reflection.association_primary_key
53
+ reflection.klass.unscoped.where(primary_key => scope_id).lock.take!
54
+
55
+ if _read_attribute(attribute).nil?
56
+ relation = self.class.unscoped.where(foreign_key => scope_id)
57
+ _write_attribute(attribute, relation.maximum(attribute).to_i + 1)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ ActiveSupport.on_load(:active_record) do
64
+ include ActiveRecord::ScopedSequence
65
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-scoped_sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.1'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ - !ruby/object:Gem::Dependency
61
+ name: sqlite3
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ description: 'Auto increments an integer column on a model, scoped to an association.
75
+ Example: sequential invoice_number column for invoices, scoped to a company (each
76
+ company has their own invoice 1, invoice 2, etc).'
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - LICENSE.txt
82
+ - README.md
83
+ - lib/active_record/scoped_sequence.rb
84
+ - lib/active_record/scoped_sequence/version.rb
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '3.1'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 4.0.16
103
+ specification_version: 4
104
+ summary: Association-scoped integer sequences for Active Record
105
+ test_files: []