retcon 0.0.1 → 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 +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +67 -21
- data/docs/01-getting-started.md +94 -0
- data/docs/02-models.md +104 -0
- data/docs/03-changesets-and-actors.md +97 -0
- data/docs/04-conflicts.md +72 -0
- data/docs/05-proposals.md +86 -0
- data/docs/06-reverts.md +62 -0
- data/docs/07-drift.md +47 -0
- data/docs/08-redaction.md +60 -0
- data/docs/09-edges.md +42 -0
- data/docs/10-schema-evolution.md +51 -0
- data/docs/11-internals.md +51 -0
- data/docs/12-reference.md +64 -0
- data/lib/retcon/actor.rb +86 -0
- data/lib/retcon/bulk.rb +60 -0
- data/lib/retcon/changeset.rb +76 -0
- data/lib/retcon/config.rb +94 -0
- data/lib/retcon/context.rb +103 -0
- data/lib/retcon/drift.rb +151 -0
- data/lib/retcon/edge.rb +80 -0
- data/lib/retcon/history.rb +122 -0
- data/lib/retcon/model.rb +210 -0
- data/lib/retcon/projection.rb +51 -0
- data/lib/retcon/proposal.rb +224 -0
- data/lib/retcon/redaction.rb +81 -0
- data/lib/retcon/revert.rb +130 -0
- data/lib/retcon/schema.rb +125 -0
- data/lib/retcon/value.rb +45 -0
- data/lib/retcon/version.rb +1 -1
- data/lib/retcon.rb +189 -2
- metadata +59 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6e8f992935a823db0ae51d7cb33579f4cb05053bd1c8a54f5d640c7699122e06
|
|
4
|
+
data.tar.gz: 946d37c1a98ff76b6d98d431d605e41fe61397d1d77e8af3b01ed8738b645f82
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5de42fd31efd8ab469abf5a2f6cdd068163e3ebfd8426591583485288938e46495617f1749317dd1bc564b6624862e78714c2afed97492c634525da5e9bbd5b1
|
|
7
|
+
data.tar.gz: 49780f2bb0610fd1809e66402f0bf58e4e4f38eaa034356b11bd33f8136e5627974eb4427b0e0dfd7d91423dc58af57018108b624bc4172af1a8a8c7ca58121a
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.0 (unreleased)
|
|
4
|
+
|
|
5
|
+
The first working release.
|
|
6
|
+
|
|
7
|
+
- Field-level history tables (`version_table`), with live tables kept as a projection in the same transaction.
|
|
8
|
+
- Changesets with actors (records, named systems, agents acting on behalf of someone) and a global order of application.
|
|
9
|
+
- Per-field conflict checks against the loaded or explicit base version: raise, or skip and report.
|
|
10
|
+
- Proposals: update, create and destroy recorded against base versions; apply, reject, withdraw, amend. `Retcon.edit`
|
|
11
|
+
with a commit/propose/deny policy. `:set_union` merges for array fields.
|
|
12
|
+
- Reverts of changesets or of everything an actor authored, keeping later work and sending same-field overlaps to a
|
|
13
|
+
review proposal. Revive and rebuild single records.
|
|
14
|
+
- Drift: a trigger marks rows written outside retcon; catch-up on write, sweeps, full digest sweeps, deletes outside
|
|
15
|
+
retcon, genesis for pre-retcon rows. `Retcon.bypass` and `Retcon::Bulk.upsert`.
|
|
16
|
+
- Redaction of field values with salted commitments, editor pseudonyms, and deletion requests (`erase`).
|
|
17
|
+
- Versioned edges (`Retcon::Edge`) that revive by triple.
|
|
18
|
+
- Schema versions with upcasters (`rename_field`, `upcast`).
|
|
19
|
+
- ActiveSupport::Notifications events and the guide at https://lwm-luminx.github.io/retcon/.
|
|
20
|
+
|
|
3
21
|
## 0.0.1 (2026-11-25)
|
|
4
22
|
|
|
5
23
|
Reserves the name. No functionality yet; the design is in review.
|
data/README.md
CHANGED
|
@@ -5,30 +5,76 @@
|
|
|
5
5
|
|
|
6
6
|
**Retroactive continuity for Active Record:** change the past without breaking the story.
|
|
7
7
|
|
|
8
|
-
retcon adds a VCS-style layer to Active Record so many people can edit shared records with full
|
|
9
|
-
damage (bad deletes, misinformation, bad actors) can be repaired without losing the good work that
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
8
|
+
retcon adds a VCS-style layer to Active Record on PostgreSQL, so many people can edit shared records with full
|
|
9
|
+
attribution, and damage (bad deletes, misinformation, bad actors) can be repaired without losing the good work that
|
|
10
|
+
came after it.
|
|
11
|
+
|
|
12
|
+
**Guide:** <https://lwm-luminx.github.io/retcon/>
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
class Venue < ApplicationRecord
|
|
16
|
+
include Retcon::Model
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
Retcon.as(current_user) { venue.update!(name: 'The Eagle') } # an attributed, applied changeset
|
|
20
|
+
|
|
21
|
+
proposal = Retcon.propose(actor: member) { |p| p.update(venue, phone: '555-0100') }
|
|
22
|
+
proposal.apply!(committer: reviewer) # or reject!, withdraw!, amend!
|
|
23
|
+
|
|
24
|
+
Retcon.revert_actor(vandal, actor: staff) # drops their changes, keeps later work
|
|
25
|
+
Retcon::Redaction.field(venue, :phone, actor: staff, reason: 'privacy') # scrubs a value from history
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How it works
|
|
29
|
+
|
|
30
|
+
- **History is the source of truth.** Each versioned table gets a history table of immutable field-level versions,
|
|
31
|
+
each in a changeset with an actor, a base version and a place in one global order.
|
|
32
|
+
- **Live tables stay fast.** They are a projection of history, written in the same transaction, so reads don't
|
|
33
|
+
change and any live row can be rebuilt from history.
|
|
34
|
+
- **Changesets group changes.** A direct edit is an applied changeset; a proposal is an unapplied one, reviewed before
|
|
35
|
+
it applies, like a pull request. A policy you write decides who commits and who proposes.
|
|
36
|
+
- **Field-level conflicts.** A stale edit or proposal conflicts only on fields someone else changed since its base.
|
|
37
|
+
Array fields can merge instead.
|
|
38
|
+
- **Tombstones, not lost rows.** Deleted rows leave the live table, and come back with their id and history.
|
|
24
39
|
- **Repair.** Reverting an actor's changes drops them and keeps later changes; only same-field overlaps go to review.
|
|
25
40
|
Reverts are changesets too, so they can be undone.
|
|
26
|
-
- **
|
|
27
|
-
|
|
28
|
-
- **
|
|
41
|
+
- **Drift is caught.** Writes made outside retcon (`update_all`, SQL, the console) are found by a trigger and recorded
|
|
42
|
+
before anything builds on them.
|
|
43
|
+
- **Redaction.** Scrub a value from every version, pseudonymize an editor, or run a deletion request. Salted
|
|
44
|
+
commitments keep history verifiable after the value is gone.
|
|
45
|
+
- **Edges.** Graph edges (subject, predicate, object) are versioned rows that revive by triple, and removed edges
|
|
46
|
+
leave the live table, so [graph_query](https://github.com/lwm-luminx/graph_query) walks only live ones.
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
gem 'retcon'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class InstallRetcon < ActiveRecord::Migration[8.0]
|
|
56
|
+
include Retcon::Schema
|
|
57
|
+
|
|
58
|
+
def change
|
|
59
|
+
create_retcon_tables
|
|
60
|
+
version_table :venues
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Requires Ruby 3.3+, Active Record 7.1+ and PostgreSQL 15+. See
|
|
66
|
+
[Getting started](https://lwm-luminx.github.io/retcon/#getting-started).
|
|
67
|
+
|
|
68
|
+
## Development
|
|
69
|
+
|
|
70
|
+
The specs need a PostgreSQL database:
|
|
29
71
|
|
|
30
|
-
|
|
31
|
-
|
|
72
|
+
```sh
|
|
73
|
+
createdb retcon_test
|
|
74
|
+
RETCON_DATABASE_URL=postgres:///retcon_test bundle exec rspec
|
|
75
|
+
bundle exec rubocop
|
|
76
|
+
bin/build-docs site # the guide, from docs/*.md (needs pandoc)
|
|
77
|
+
```
|
|
32
78
|
|
|
33
79
|
## License
|
|
34
80
|
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Getting started
|
|
2
|
+
|
|
3
|
+
retcon keeps a full, field-level history of the Active Record models you choose, on PostgreSQL. Every save is
|
|
4
|
+
attributed to someone and grouped into a **changeset**. Changes can also be **proposed** and applied later after
|
|
5
|
+
review, like a pull request. When something goes wrong (a bad delete, a vandal, a privacy request), you repair it with
|
|
6
|
+
a new changeset instead of restoring a backup, and the good work that came after it stays.
|
|
7
|
+
|
|
8
|
+
Your live tables don't move and don't get slower. They become a **projection** of history: written in the same
|
|
9
|
+
transaction as the history rows, and rebuildable from them at any time.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
# Gemfile
|
|
15
|
+
gem 'retcon'
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
retcon needs Ruby 3.3+, Active Record 7.1+ and PostgreSQL 15+ through the `pg` adapter.
|
|
19
|
+
|
|
20
|
+
## Create the tables
|
|
21
|
+
|
|
22
|
+
One migration creates retcon's shared tables, and one call per table versions it:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
class InstallRetcon < ActiveRecord::Migration[8.0]
|
|
26
|
+
include Retcon::Schema
|
|
27
|
+
|
|
28
|
+
def change
|
|
29
|
+
create_retcon_tables # retcon_actors, retcon_changesets and the retcon_seq sequence
|
|
30
|
+
version_table :venues # retcon_venues, three columns on venues, and a trigger
|
|
31
|
+
version_table :events
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`version_table` creates the history table `retcon_venues`, adds `retcon_seq`, `retcon_digest` and `retcon_dirty` to
|
|
37
|
+
`venues`, and installs a trigger that marks rows written outside retcon (see [Drift and bulk writes](#drift)).
|
|
38
|
+
|
|
39
|
+
## Version a model
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class Venue < ApplicationRecord
|
|
43
|
+
include Retcon::Model
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
That's all a model needs. Every column is versioned except the primary key, timestamps, `lock_version`, counter caches
|
|
48
|
+
(`*_count`) and retcon's own columns. [Models](#models) covers choosing fields.
|
|
49
|
+
|
|
50
|
+
Rows that existed before retcon have no history yet. Give them a starting point once, after the migration:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
Venue.retcon_genesis! # one "Genesis" changeset per batch of 500 rows
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Make attributed changes
|
|
57
|
+
|
|
58
|
+
Wrap changes in `Retcon.as` to say who made them:
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
Retcon.as(current_user) do
|
|
62
|
+
venue.update!(name: 'The Eagle', phone: '555-0100')
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Saves work exactly as before: validations and callbacks run, and `save` still returns `true` or `false`. retcon adds
|
|
67
|
+
a changeset with the user as its author, a version row for each changed field, and the record's new `retcon_seq`.
|
|
68
|
+
|
|
69
|
+
Group several saves into one changeset with a message:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
Retcon.changeset(actor: current_user, message: 'New season') do
|
|
73
|
+
venue.update!(hours: { 'tue' => '4pm-2am' })
|
|
74
|
+
venue.events.create!(name: 'Drag Bingo')
|
|
75
|
+
end
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Read history
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
venue.retcon_versions # every version row, oldest first
|
|
82
|
+
venue.retcon_heads['phone'] # the current version of a field: seq, value, row_id (blame)
|
|
83
|
+
Retcon::Changeset.last.author # who made the last change
|
|
84
|
+
Retcon::Changeset.last.changes # {[Venue, id] => {'name' => 'The Eagle', ...}}
|
|
85
|
+
Venue.retcon_state(venue.id) # the record as history says it is
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Where to go next
|
|
89
|
+
|
|
90
|
+
- [Changesets and actors](#changesets-and-actors): attribution, agents, events.
|
|
91
|
+
- [Conflicts](#conflicts): what happens when two people edit the same record.
|
|
92
|
+
- [Proposals](#proposals): changes that wait for review.
|
|
93
|
+
- [Reverts](#reverts): undoing an actor's changes without losing later work.
|
|
94
|
+
- [Redaction](#redaction): removing what history must not keep.
|
data/docs/02-models.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Models
|
|
2
|
+
|
|
3
|
+
Include `Retcon::Model` in any Active Record model whose table was versioned with `version_table`. Settings go in a
|
|
4
|
+
`versioned` block:
|
|
5
|
+
|
|
6
|
+
```ruby
|
|
7
|
+
class Venue < ApplicationRecord
|
|
8
|
+
include Retcon::Model
|
|
9
|
+
|
|
10
|
+
versioned do
|
|
11
|
+
ignore :view_count, :search_vector
|
|
12
|
+
merge :tags, :set_union
|
|
13
|
+
scope(&:audience_id)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Versioned fields
|
|
19
|
+
|
|
20
|
+
By default every column is versioned except:
|
|
21
|
+
|
|
22
|
+
- the primary key and retcon's own columns (`retcon_seq`, `retcon_digest`, `retcon_dirty`);
|
|
23
|
+
- `created_at`, `updated_at` and `lock_version`;
|
|
24
|
+
- counter caches: any column ending in `_count`.
|
|
25
|
+
|
|
26
|
+
`ignore` leaves more out. `only` names the versioned fields outright instead:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
versioned do
|
|
30
|
+
only :name, :address, :phone
|
|
31
|
+
end
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`Venue.retcon_config.tracked_fields` lists the result. A save that changes only untracked fields writes no history and
|
|
35
|
+
no changeset, so view counters and search vectors cost nothing.
|
|
36
|
+
|
|
37
|
+
Fields keep their types through history. Values are stored as JSON: strings, numbers, booleans, arrays and `jsonb`
|
|
38
|
+
as they are; dates, times and decimals as strings; anything else through `as_json`. They are cast back through the
|
|
39
|
+
attribute's type when read.
|
|
40
|
+
|
|
41
|
+
## Scope
|
|
42
|
+
|
|
43
|
+
`scope` gives every changeset about a record a scope string, such as the tenant or audience it belongs to. Reverts
|
|
44
|
+
and reviews can then be limited to it:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
versioned do
|
|
48
|
+
scope { |venue| venue.audience_id }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
Retcon::Changeset.where(scope: audience.id.to_s)
|
|
52
|
+
Retcon.revert_actor(vandal, actor: staff, scope: audience.id.to_s)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
A changeset takes the scope of the first record it writes, unless `Retcon.changeset(scope:)` sets one.
|
|
56
|
+
|
|
57
|
+
## Merge strategies
|
|
58
|
+
|
|
59
|
+
When a proposal changes a field that someone else changed after the proposal's base, it conflicts. A merge strategy
|
|
60
|
+
can settle that instead:
|
|
61
|
+
|
|
62
|
+
- `:replace` (the default): it conflicts.
|
|
63
|
+
- `:set_union`: for array fields, the result keeps every element either side has. Two people tagging the same venue
|
|
64
|
+
both get their tags.
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
versioned do
|
|
68
|
+
merge :tags, :set_union
|
|
69
|
+
end
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The merged value is written as one more version in the applied changeset, after the proposed one.
|
|
73
|
+
|
|
74
|
+
## Schema versions
|
|
75
|
+
|
|
76
|
+
`schema_version`, `rename_field` and `upcast` keep old history readable when columns change. See
|
|
77
|
+
[Schema evolution](#schema-evolution).
|
|
78
|
+
|
|
79
|
+
## Associations and callbacks
|
|
80
|
+
|
|
81
|
+
Saves and destroys are wrapped whole, so autosaved associations and `dependent: :destroy` children join the same
|
|
82
|
+
changeset as their parent. Your callbacks run on ordinary saves. When retcon writes live rows itself (applying a
|
|
83
|
+
proposal, reverting, rebuilding), it validates the record but skips callbacks. Check `Retcon.replaying?` in a callback
|
|
84
|
+
that has side effects you never want to repeat, such as sending a notification:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
after_save :notify_followers, unless: -> { Retcon.replaying? }
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Instance and class methods
|
|
91
|
+
|
|
92
|
+
| Method | Returns |
|
|
93
|
+
| --- | --- |
|
|
94
|
+
| `record.retcon_versions` | every version row of the record, oldest first, proposals included |
|
|
95
|
+
| `record.retcon_heads` | `{field => Head}` with the newest applied version of each field |
|
|
96
|
+
| `record.retcon_drifted?` | whether the live row was written outside retcon |
|
|
97
|
+
| `record.retcon_base = seq` | the base version the next save is checked against |
|
|
98
|
+
| `record.retcon_on_conflict = :skip` | leave conflicting fields out of the next save |
|
|
99
|
+
| `record.retcon_conflicts` | the fields the last save left out |
|
|
100
|
+
| `Model.retcon_state(id)` | `{'exists', 'seq', 'attributes'}` as history says, or nil |
|
|
101
|
+
| `Model.retcon_revive!(id, actor:)` | brings a deleted record back with its id |
|
|
102
|
+
| `Model.retcon_rebuild!(id)` | rewrites the live row from history |
|
|
103
|
+
| `Model.retcon_sweep!` | catches up rows written outside retcon |
|
|
104
|
+
| `Model.retcon_genesis!` | gives pre-retcon rows their first history |
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Changesets and actors
|
|
2
|
+
|
|
3
|
+
## Changesets
|
|
4
|
+
|
|
5
|
+
A changeset is one logical change: version rows across any number of records and tables, with an author, a message
|
|
6
|
+
and, once someone applies or decides it, a committer. Changesets are never deleted. Reverts and redactions are
|
|
7
|
+
changesets too, so they can be inspected and undone like anything else.
|
|
8
|
+
|
|
9
|
+
| Column | Meaning |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `kind` | `edit`, `proposal`, `genesis`, `catch_up`, `import`, `revert`, `redaction` |
|
|
12
|
+
| `state` | `applied`, `proposed`, `rejected`, `withdrawn`, `amended` |
|
|
13
|
+
| `author` / `committer` | who wrote it, and who applied or decided it |
|
|
14
|
+
| `applied_seq` | its place in retcon's global order of application |
|
|
15
|
+
| `parent` | the changeset this one amends, or the revert a review proposal came from |
|
|
16
|
+
| `scope` | the tenant or audience it belongs to (see [Models](#models)) |
|
|
17
|
+
| `message`, `metadata` | free text, and a jsonb hash for your own keys |
|
|
18
|
+
|
|
19
|
+
Every save outside `Retcon.changeset` gets a changeset of its own. Inside one, every save joins it, nested blocks
|
|
20
|
+
included, and the whole block runs in one database transaction:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
changeset = Retcon.changeset(actor: staff, message: 'Merge duplicate venues', metadata: { 'ticket' => 'OPS-12' }) do
|
|
24
|
+
keep.update!(phone: dupe.phone)
|
|
25
|
+
dupe.destroy!
|
|
26
|
+
end
|
|
27
|
+
changeset.records # [[Venue, keep.id], [Venue, dupe.id]]
|
|
28
|
+
changeset.versions # the version rows, in write order
|
|
29
|
+
changeset.changes # {[Venue, keep.id] => {'phone' => '555-0100'}}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
A block that changes nothing leaves no changeset and returns nil.
|
|
33
|
+
|
|
34
|
+
### Global order
|
|
35
|
+
|
|
36
|
+
`applied_seq` comes from one PostgreSQL sequence, `retcon_seq`, shared by every versioned table. Every version row of
|
|
37
|
+
an applied changeset carries the same `seq`, and the live row's `retcon_seq` is the seq of the last changeset that
|
|
38
|
+
wrote it. That is what conflicts are checked against, and what lets reverts and rebuilds replay history in order.
|
|
39
|
+
|
|
40
|
+
## Actors
|
|
41
|
+
|
|
42
|
+
History stores actor ids, never names or emails. An actor stands for a record, or is named:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
Retcon.as(current_user) { ... } # a record: a User, a device grant, an API client
|
|
46
|
+
Retcon.as(Retcon::Actor.named('import', 'facebook')) { ... }
|
|
47
|
+
Retcon.as('nightly-cleanup') { ... } # a String names a system actor
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`Retcon::Actor.resolve` turns any of these into an `Actor` row, creating it the first time. Record actors are of kind
|
|
51
|
+
`user` unless you map the class:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
Retcon::Actor.kinds = { 'VenueDeviceGrant' => 'device' }
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`actor.record` finds the record again; `actor.label` is how to show it (its pseudonym once redacted).
|
|
58
|
+
|
|
59
|
+
### Agents
|
|
60
|
+
|
|
61
|
+
When software acts for a person (an assistant, a moderation bot acting on a report), attribute the change to both:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
Retcon.as(glam_bot, on_behalf_of: host) { event.update!(status: 'cancelled') }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The changeset's author is an `agent` actor that points at the person it acted for (`on_behalf_of`). Reverting the
|
|
68
|
+
agent's changes, or the person's, finds them either way.
|
|
69
|
+
|
|
70
|
+
### Unattributed saves
|
|
71
|
+
|
|
72
|
+
Saves with no actor set are attributed to `Retcon::Actor.system` ("unattributed"), so nothing breaks while you add
|
|
73
|
+
`Retcon.as` around your write paths. Turn that off once they're covered:
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
Retcon.allow_unattributed = false # saves with no actor raise Retcon::Error
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
A common place for `Retcon.as` is a controller `around_action`, or the GraphQL context for mutations.
|
|
80
|
+
|
|
81
|
+
## Events
|
|
82
|
+
|
|
83
|
+
retcon publishes through `ActiveSupport::Notifications` as `<name>.retcon`, with the changeset in the payload:
|
|
84
|
+
|
|
85
|
+
| Event | When |
|
|
86
|
+
| --- | --- |
|
|
87
|
+
| `proposal_opened` | a proposal (or an amended one) is opened |
|
|
88
|
+
| `changeset_applied` | a proposal is applied |
|
|
89
|
+
| `proposal_rejected` | a proposal is rejected |
|
|
90
|
+
| `revert_conflict` | a revert left same-field overlaps in a review proposal |
|
|
91
|
+
| `redacted` | a redaction is written |
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
Retcon.on(:proposal_opened) { |changeset| ReviewQueue.route(changeset) }
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
This is how you route proposals to reviewers, for example to a moderation host or a staff queue.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Conflicts
|
|
2
|
+
|
|
3
|
+
Two people open the same venue. One fixes the phone number, the other the hours. Both save. With retcon both changes
|
|
4
|
+
stay: conflicts are checked **per field**, and only when someone else changed that same field to a different value
|
|
5
|
+
after the version you started from.
|
|
6
|
+
|
|
7
|
+
## The base version
|
|
8
|
+
|
|
9
|
+
Every live row carries `retcon_seq`, the seq of the last changeset that wrote it. A record remembers the
|
|
10
|
+
`retcon_seq` it was loaded with; that is its **base**. On save, retcon:
|
|
11
|
+
|
|
12
|
+
1. locks the live row (`SELECT ... FOR UPDATE`) for the rest of the transaction;
|
|
13
|
+
2. catches up any change written outside retcon since (see [Drift](#drift));
|
|
14
|
+
3. compares each changed field's newest version with the base.
|
|
15
|
+
|
|
16
|
+
A field conflicts when its newest version is newer than the base, came from someone else's change, and holds a value
|
|
17
|
+
different from the one being saved.
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
mine = Venue.find(id) # base: seq 41
|
|
21
|
+
theirs = Venue.find(id)
|
|
22
|
+
Retcon.as(bob) { theirs.update!(phone: '555-0199') } # seq 42
|
|
23
|
+
|
|
24
|
+
Retcon.as(alice) { mine.update!(hours: '4pm-2am') } # fine: hours didn't move
|
|
25
|
+
Retcon.as(alice) { mine.update!(phone: '555-0100') } # Retcon::Conflict: phone changed at seq 42
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Saving the value that's already there never conflicts.
|
|
29
|
+
|
|
30
|
+
## Web forms: an explicit base
|
|
31
|
+
|
|
32
|
+
A record loaded inside the request that saves it has a base from moments ago. For an edit form, the base that matters
|
|
33
|
+
is the one the person saw when the form was rendered. Put the record's `retcon_seq` in the form and give it back:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
# the form: hidden_field :retcon_seq
|
|
37
|
+
venue.retcon_base = params[:venue][:retcon_seq].to_i
|
|
38
|
+
venue.update!(venue_params)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`retcon_base` also takes `{field => seq}` for clients that track versions per field. It applies to the next save
|
|
42
|
+
only.
|
|
43
|
+
|
|
44
|
+
## Handling a conflict
|
|
45
|
+
|
|
46
|
+
By default a conflict raises `Retcon::Conflict` and nothing is saved:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
rescue Retcon::Conflict => e
|
|
50
|
+
e.conflicts.each do |c|
|
|
51
|
+
c.field # 'phone'
|
|
52
|
+
c.current # what is there now: '555-0199'
|
|
53
|
+
c.proposed # what this save wanted: '555-0100'
|
|
54
|
+
c.base_seq # the version the edit started from
|
|
55
|
+
c.head_seq # the version that moved it
|
|
56
|
+
end
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Show the person both values and let them choose. Or save what you can and report the rest:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
venue.retcon_on_conflict = :skip
|
|
63
|
+
venue.update!(phone: '555-0100', hours: '4pm-2am')
|
|
64
|
+
venue.retcon_conflicts # [FieldConflict(phone)]: hours saved, phone left as it was
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Skipped fields are put back to their database value on the record.
|
|
68
|
+
|
|
69
|
+
## Proposals
|
|
70
|
+
|
|
71
|
+
Proposals record their base when they're opened and are checked when they're applied, days later perhaps. The same
|
|
72
|
+
rules apply, plus [merge strategies](#models) for fields like tags. See [Proposals](#proposals).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Proposals
|
|
2
|
+
|
|
3
|
+
A proposal is a changeset that hasn't been applied: updates, creates and destroys recorded against base versions,
|
|
4
|
+
reviewed, then applied or rejected. It touches no live table until it is applied, so readers never see it.
|
|
5
|
+
|
|
6
|
+
## Opening one
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
proposal = Retcon.propose(actor: member, message: 'New hours') do |p|
|
|
10
|
+
p.update(venue, hours: 'Tue-Sun 4pm-2am')
|
|
11
|
+
p.create(Event, venue_id: venue.id, name: 'Drag Bingo')
|
|
12
|
+
p.destroy(old_event)
|
|
13
|
+
end
|
|
14
|
+
proposal.state # 'proposed'
|
|
15
|
+
proposal.changes # the diff to show a reviewer
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- `p.update(record, fields)` proposes new values. Its base is the version the record was loaded at, or pass
|
|
19
|
+
`base: seq` (or `{field => seq}`) for the version the proposer saw.
|
|
20
|
+
- `p.create(Model, attributes)` proposes a new record. Its id is chosen now (a UUID, or the next value of the table's
|
|
21
|
+
sequence), so other parts of the same proposal can refer to it. It returns the unsaved record.
|
|
22
|
+
- `p.destroy(record)` proposes deleting it.
|
|
23
|
+
|
|
24
|
+
Only versioned fields can be proposed. A proposal that changes nothing raises `ArgumentError`.
|
|
25
|
+
|
|
26
|
+
## Reviewing
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
proposal.apply!(committer: reviewer)
|
|
30
|
+
proposal.reject!(by: reviewer, reason: 'Hours are wrong')
|
|
31
|
+
proposal.withdraw! # the author takes it back
|
|
32
|
+
amended = proposal.amend!(by: reviewer) do |p|
|
|
33
|
+
p.update(venue, hours: 'Tue-Sun 5pm-2am') # replaces the original's value for this field
|
|
34
|
+
end
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`apply!` runs in one transaction: it locks the proposal, locks and catches up each record, checks every field
|
|
38
|
+
against its base, writes the live rows, and marks the version rows applied with a fresh seq. Live rows are validated
|
|
39
|
+
(an invalid result raises `Retcon::Invalid`), and model callbacks don't run (see `Retcon.replaying?` in
|
|
40
|
+
[Models](#models)).
|
|
41
|
+
|
|
42
|
+
If fields conflict, `apply!` raises `Retcon::Conflict` and applies nothing. To apply the rest:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
result = proposal.apply!(committer: reviewer, on_conflict: :skip)
|
|
46
|
+
result[:conflicts] # [FieldConflict]; also kept in the changeset's metadata as 'skipped'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Skipped fields stay in the applied changeset unapplied, so history shows what was proposed and not taken.
|
|
50
|
+
|
|
51
|
+
`amend!` keeps the author, makes the reviewer the committer, points the new proposal's `parent` at the original and
|
|
52
|
+
marks the original `amended`.
|
|
53
|
+
|
|
54
|
+
## Policy: commit, propose or deny
|
|
55
|
+
|
|
56
|
+
`Retcon.edit` sends an edit through a policy you define. Trusted editors commit directly, others open a proposal:
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
Retcon.policy = lambda do |actor, record, changes|
|
|
60
|
+
next :deny if actor.record&.banned?
|
|
61
|
+
next :commit if actor.record&.staff? || record.try(:owner_id) == actor.record&.id
|
|
62
|
+
|
|
63
|
+
:propose
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
Retcon.edit(venue, { phone: '555-0100' }, actor: current_user, base: params[:retcon_seq].to_i)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`changes` is `{field => [from, to]}`. The policy returns `:commit` (an applied changeset), `:propose` (a proposal;
|
|
70
|
+
the record is left unchanged) or `:deny` (raises `Retcon::Denied`). Trust tiers, matching rules and rate limits all
|
|
71
|
+
fit here.
|
|
72
|
+
|
|
73
|
+
## Routing reviews
|
|
74
|
+
|
|
75
|
+
Subscribe to `proposal_opened` to send proposals where they belong:
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
Retcon.on(:proposal_opened) do |proposal|
|
|
79
|
+
ModerationQueue.enqueue(proposal, scope: proposal.scope)
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Proposed rows in history
|
|
84
|
+
|
|
85
|
+
A proposal's version rows sit in the history tables with `applied` false and no seq. Heads, state and rebuilds only
|
|
86
|
+
count applied rows, so an open proposal never changes what history says a record is.
|
data/docs/06-reverts.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Reverts
|
|
2
|
+
|
|
3
|
+
A revert undoes changesets the way an interactive rebase drops commits, without rewriting history: one new changeset
|
|
4
|
+
of kind `revert` puts back what the dropped changesets replaced, and later work by others stays.
|
|
5
|
+
|
|
6
|
+
```ruby
|
|
7
|
+
Retcon.revert(changeset, actor: staff)
|
|
8
|
+
changeset.revert!(actor: staff) # the same
|
|
9
|
+
Retcon.revert_actor(vandal, actor: staff) # everything they authored
|
|
10
|
+
Retcon.revert_actor(vandal, actor: staff, since: 2.days.ago, scope: audience.id.to_s)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`revert_actor` takes every applied changeset the actor authored, leaving out their own reverts and redactions.
|
|
14
|
+
|
|
15
|
+
## What a revert does
|
|
16
|
+
|
|
17
|
+
For each field the dropped changesets set:
|
|
18
|
+
|
|
19
|
+
- if a dropped changeset wrote the field's current value, the field goes back to the newest value written by a
|
|
20
|
+
changeset that isn't dropped (nil when there is none);
|
|
21
|
+
- if someone else changed it afterwards, their value stays, and restoring the earlier value goes into a **review
|
|
22
|
+
proposal** instead. This is a same-field overlap: a person decides.
|
|
23
|
+
|
|
24
|
+
Records the dropped changesets created are deleted (tombstoned). Records they deleted are revived, with their id and
|
|
25
|
+
history. Fields nobody else touched are restored directly.
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
result = Retcon.revert_actor(vandal, actor: staff)
|
|
29
|
+
result.revert # the applied revert changeset, or nil when there was nothing to undo
|
|
30
|
+
result.review # a proposal with the overlaps, or nil; its parent is the revert
|
|
31
|
+
result.flagged # [Actor] reviewers who applied the vandal's proposals
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`flagged` lists committers who applied a dropped changeset someone else authored. They approved the damage, so
|
|
35
|
+
whoever cleans up may want to look at what else they approved.
|
|
36
|
+
|
|
37
|
+
The review proposal fires `revert_conflict` as well as `proposal_opened`, so it can be routed like any proposal.
|
|
38
|
+
|
|
39
|
+
## Undoing a revert
|
|
40
|
+
|
|
41
|
+
A revert is a changeset, so it can be reverted. That puts the dropped changes back, with the same rules about later
|
|
42
|
+
work.
|
|
43
|
+
|
|
44
|
+
## Reviving one record
|
|
45
|
+
|
|
46
|
+
To bring back a single deleted record without reverting anything else:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
Venue.retcon_revive!(venue_id, actor: staff)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
It reinserts the row with its id, every column from the tombstone snapshot and each field's newest version, as a
|
|
53
|
+
changeset of its own (or in the open one). Reviving a record that is live raises `Retcon::Error`.
|
|
54
|
+
|
|
55
|
+
## Rebuilding
|
|
56
|
+
|
|
57
|
+
Because live rows are a projection, any row can be rewritten from history:
|
|
58
|
+
|
|
59
|
+
```ruby
|
|
60
|
+
Venue.retcon_rebuild!(venue_id) # inserts, updates or deletes the live row to match history
|
|
61
|
+
Retcon.verify(Venue, sample: 500) # [[id, field, live, history], ...] for rows that disagree; empty when they agree
|
|
62
|
+
```
|