poly 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b553ab920c023d3f1e23a5de23c35bc75798827f6dfe5db2bd9369e0875586b0
4
- data.tar.gz: 7ce045f76258951d9b3a281f38c369c81b9decc2cbfaca7ce9a73279605ae792
3
+ metadata.gz: ba17496c0d7031f1877d3aa61200842efd24cb1381c12ecb597e5b4c80ac65d7
4
+ data.tar.gz: 9bf0d37b9106badbf5672f3b1534e5c8e8161e3934ead590b56415a17196eda5
5
5
  SHA512:
6
- metadata.gz: 7242fafe809c07f8b801ed5fd049519ffc7d403dd8254e1323fa7d794dffa549dd8c5070317a1ff6f2dd8f185b603baef121c2ab6eccced5dfd9e542dad006b3
7
- data.tar.gz: 519f7047228088054991bd4288eb52f1e39b98839976dfaadb1c68b3f46a0b0d97c7f1c088543be9f58838978e989b048d622bef4a768e4df39a9733dee83098
6
+ metadata.gz: bf46bda44abfaccfb137c84cf1a6da3a6d3a6d0ad901e5354b3a3031769454777a90d03b2b09f0a5d5f81d4d26fd91458a58ea1de4cb083756fa4cfae6328684
7
+ data.tar.gz: 62b7997d9a53c0ee472f745edaae46e78b6df6d54ff77af8ca4465bbb85afd642636c164863cf77eff4aaa537ed00c3779582203c935b34fa692853457c5b239
data/CHANGELOG.md CHANGED
@@ -5,7 +5,37 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [Unreleased]
8
+ ## [1.2.0] - 2026-08-16
9
+
10
+ ### Added
11
+
12
+ - `Poly::PolymorphicJoinError` (`lib/poly/polymorphic_join_error.rb`) — the
13
+ join-validation error raised from `lib/poly/joins.rb` is now namespaced
14
+ under `Poly`. The bare top-level `PolymorphicJoinError` constant remains as
15
+ a deprecated alias, so existing `rescue PolymorphicJoinError` call sites
16
+ keep working.
17
+ - CI now covers a database axis (SQLite and PostgreSQL, the latter via a GitHub
18
+ Actions `services:` Postgres container) and an ActiveRecord/Rails version axis
19
+ (7.1, 7.2, 8.x), each matrix cell pinning the loaded AR version via the new
20
+ `ACTIVERECORD_VERSION` env var consumed by the `Gemfile`.
21
+ - `spec/spec_helper.rb`'s database adapter is now parameterized via
22
+ `POLY_TEST_ADAPTER` (`sqlite3` default, or `postgresql`) instead of
23
+ hardcoding an in-memory SQLite connection.
24
+ - README "Supported Databases" section documenting that MySQL is explicitly
25
+ unsupported (`AbstractMysqlAdapter` doesn't implement `supports_partial_index?`,
26
+ silently degrading `poly_prime_index` to a full-table unique index).
27
+ - README and new specs (`spec/models/poly/stack_spec.rb`) documenting
28
+ `Poly::Stack`'s concurrency boundary — the `poly_stack_seize_prime`
29
+ demote-then-insert sequence in `lib/poly/stack.rb`, and the
30
+ `ActiveRecord::RecordNotUnique` failure mode it can hit under concurrent
31
+ writers. No new public API was added.
32
+
33
+ ### Fixed
34
+
35
+ - README §5 ("Poly::Stack")'s "append-only" wording corrected: the contract
36
+ is immutable payload with mutable linkage/index metadata (`is_prime`,
37
+ `superseded_by_id` are mutated in place on supersession), not literally
38
+ immutable/append-only rows.
9
39
 
10
40
  ## [1.1.0] - 2026-07-07
11
41
 
@@ -70,7 +100,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
70
100
  Creates methods like `joins_commentable(ClassName)` that validate the reverse
71
101
  `has_many`/`has_one` association before building the join SQL.
72
102
 
73
- [Unreleased]: https://github.com/whittakertech/poly/compare/v1.1.0...HEAD
103
+ [1.2.0]: https://github.com/whittakertech/poly/compare/v1.1.0...v1.2.0
74
104
  [1.1.0]: https://github.com/whittakertech/poly/compare/v1.0.0...v1.1.0
75
105
  [1.0.0]: https://github.com/whittakertech/poly/compare/v0.2.0...v1.0.0
76
106
  [0.2.0]: https://github.com/whittakertech/poly/compare/v0.1.0...v0.2.0
data/README.md CHANGED
@@ -63,6 +63,28 @@ bundle install
63
63
 
64
64
  ---
65
65
 
66
+ ## Supported Databases
67
+
68
+ Poly is tested in CI against **SQLite** and **PostgreSQL** (both Ruby x
69
+ ActiveRecord 7.1/7.2/8.x combinations -- see `.github/workflows/ci.yml`).
70
+
71
+ **MySQL is explicitly not supported.** `Poly::Migration#poly_prime_index`
72
+ (see below) relies on a partial/conditional unique index --
73
+ `add_index table, [...], unique: true, where: 'is_prime'` -- to enforce
74
+ "exactly one prime row per resource+role, many non-primes allowed."
75
+ PostgreSQL's and SQLite3's ActiveRecord adapters both override
76
+ `supports_partial_index?` to `true`; `ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter`
77
+ does **not** override it, so it inherits the abstract default of `false`.
78
+ Because `schema_creation.rb` only emits the index's `WHERE` clause when
79
+ `supports_partial_index?` is true, MySQL silently drops the clause instead
80
+ of raising -- producing a full-table unique index instead of a partial one,
81
+ and quietly breaking the single-prime-per-role invariant (it would instead
82
+ forbid more than one row total per resource+role). This is a silent
83
+ correctness bug, not just reduced support, so running Poly against MySQL is
84
+ unsupported rather than merely uncautioned-against.
85
+
86
+ ---
87
+
66
88
  # Quickstart
67
89
 
68
90
  ### Migration
@@ -143,7 +165,7 @@ AND "comments"."commentable_type" = 'Post'
143
165
  > # has_one :comment, as: :commentable — also valid
144
166
  > ```
145
167
  >
146
- > Otherwise `PolymorphicJoinError` is raised.
168
+ > Otherwise `Poly::PolymorphicJoinError` is raised.
147
169
 
148
170
  ### Join Flow
149
171
 
@@ -332,13 +354,17 @@ Supports:
332
354
  # 5. Poly::Stack
333
355
 
334
356
  A polymorphic, role-discriminated **history** where one entry is the current
335
- **prime** — the top of an append-only stack. `Poly::Role` is
357
+ **prime** — the top of the stack. `Poly::Role` is
336
358
  the same idea at cardinality 1; `Poly::Stack` opens it up to many entries per
337
359
  `(resource, role)`, with the most-recently created always prime.
338
360
 
339
- It is **append-only** and **payload agnostic**: it manages the prime marker and
340
- an audit edge only. The payload column, the actor, and the reason belong to your
341
- model — Poly::Stack never reads them.
361
+ It is **payload agnostic** with an **immutable payload, mutable linkage/index
362
+ metadata** contract: it manages the prime marker and an audit edge only. The
363
+ payload column, the actor, and the reason belong to your model — Poly::Stack
364
+ never reads or rewrites them. What Poly::Stack *does* mutate in place, on
365
+ supersession, are the prior prime row's own `is_prime` and `superseded_by_id`
366
+ columns — historical rows are never deleted, but their linkage/index metadata
367
+ is updated, so this is not a literally append-only/immutable-row contract.
342
368
 
343
369
  ## Schema
344
370
 
@@ -474,6 +500,36 @@ scope :trashed, -> { where_status(:trash) }
474
500
  > [!NOTE]
475
501
  > Prefer explicit scopes over `default_scope` for soft-delete.
476
502
 
503
+ ## Concurrency Boundary
504
+
505
+ `poly_stack_seize_prime` (the `before_create` callback that demotes the prior
506
+ prime and claims the new one) is **not** wrapped in an explicit row lock or
507
+ transaction. Two writers racing the same `(resource, role)` at the same time
508
+ can both read the same prior prime, both demote it, and both attempt to
509
+ insert with `is_prime: true`.
510
+
511
+ When that happens, it is the partial unique index (`add_index ..., unique:
512
+ true, where: 'is_prime'`, built by `poly_prime_index`) — not the
513
+ callback — that enforces "at most one prime per `(resource, role)`". The
514
+ losing writer's `INSERT` raises `ActiveRecord::RecordNotUnique`, at the
515
+ `INSERT` itself, after the callback has already run.
516
+
517
+ Poly::Stack does not catch or retry this internally. **Callers that may write
518
+ concurrently to the same `(resource, role)` should be prepared to rescue
519
+ `ActiveRecord::RecordNotUnique` around the create call and retry** — e.g.
520
+ re-fetch the current prime and re-attempt the create — rather than assuming a
521
+ single `create!` is race-safe:
522
+
523
+ ```ruby
524
+ begin
525
+ post.statuses.create!(state: 'public')
526
+ rescue ActiveRecord::RecordNotUnique
527
+ # another writer won the race for this (resource, role); re-fetch and
528
+ # decide whether to retry, merge, or surface a conflict to the caller.
529
+ retry_or_handle_conflict
530
+ end
531
+ ```
532
+
477
533
  ## Priming Flow
478
534
 
479
535
  ```mermaid
data/lib/poly/joins.rb CHANGED
@@ -19,12 +19,12 @@ module Poly::Joins
19
19
  next if singleton_class.method_defined?(method_name)
20
20
 
21
21
  define_singleton_method(method_name) do |klass|
22
- raise PolymorphicJoinError, 'Expected an ActiveRecord model' unless klass <= ActiveRecord::Base
22
+ raise Poly::PolymorphicJoinError, 'Expected an ActiveRecord model' unless klass <= ActiveRecord::Base
23
23
 
24
24
  base_klass = klass.base_class
25
25
 
26
26
  unless join_allowed?(klass, as: assoc_name)
27
- raise PolymorphicJoinError,
27
+ raise Poly::PolymorphicJoinError,
28
28
  "Polymorphic join requires #{base_klass} to declare: " \
29
29
  "has_many :#{name.underscore.pluralize}, as: :#{assoc_name}"
30
30
  end
@@ -1,3 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class PolymorphicJoinError < StandardError; end
3
+ class Poly::PolymorphicJoinError < StandardError; end
4
+
5
+ # Deprecated: use Poly::PolymorphicJoinError. Kept as an alias so existing
6
+ # `rescue PolymorphicJoinError` / `is_a?(PolymorphicJoinError)` callers keep
7
+ # working across the 1.2.0 rename.
8
+ PolymorphicJoinError = Poly::PolymorphicJoinError
data/lib/poly/stack.rb CHANGED
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Card-side concern. Turns a polymorphic, role-discriminated model (the "card"
4
- # model, e.g. Status) into an append-only stack where the most-recently created
5
- # card per (resource, role) is the current "prime" — the top of the stack.
4
+ # model, e.g. Status) into a stack where the most-recently created card per
5
+ # (resource, role) is the current "prime" — the top of the stack. The
6
+ # contract is immutable payload, mutable linkage/index metadata: prior rows
7
+ # are never deleted, but their `is_prime` and `superseded_by_id` columns are
8
+ # mutated in place when a new card supersedes them — this is not a literally
9
+ # append-only/immutable-row stack.
6
10
  #
7
11
  # class Status < ApplicationRecord
8
12
  # belongs_to :resource, polymorphic: true
@@ -50,6 +54,18 @@ module Poly::Stack
50
54
  # remember it so the supersession edge can be linked once we have an id.
51
55
  # Runs before insert: at INSERT time there is exactly one is_prime row, so
52
56
  # the partial unique index is satisfied (zero primes momentarily is legal).
57
+ #
58
+ # Concurrency boundary: this demote-then-insert sequence is NOT wrapped in
59
+ # an explicit row lock or transaction. Two concurrent writers racing the
60
+ # same (resource, role) can both read the same prior prime, both demote it
61
+ # via #update_columns, and both attempt to insert with is_prime: true. The
62
+ # second writer's INSERT then raises ActiveRecord::RecordNotUnique — not
63
+ # here in the callback, but afterward, once ActiveRecord issues the actual
64
+ # INSERT. It is the partial unique index (`poly_prime_index` /
65
+ # `index_#{table}_prime`, see lib/poly/migration.rb), not this callback,
66
+ # that actually enforces "at most one prime per (resource, role)" under a
67
+ # race. Poly::Stack does not retry internally; see README's "Poly::Stack"
68
+ # section for the recommended caller-side rescue-and-retry pattern.
53
69
  def poly_stack_seize_prime
54
70
  cols = self.class.poly_stack_columns
55
71
  @poly_stack_superseded = self.class
data/lib/poly/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Poly
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Whittaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-07-07 00:00:00.000000000 Z
11
+ date: 2026-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord