paper_trail-bulk_writes 0.1.0 → 0.3.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 +7 -1
- data/CODE_OF_CONDUCT.md +128 -0
- data/README.md +17 -2
- data/lib/paper_trail/bulk_writes/insert.rb +60 -6
- data/lib/paper_trail/bulk_writes/model.rb +2 -2
- data/lib/paper_trail/bulk_writes/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5af6edbca4b17762cc0f647c69ee04b52284cd924b826b6a997a65b4b92925ba
|
|
4
|
+
data.tar.gz: 2baef624ade4173c84c5b54252ee67930f625ee1d97d5e2ed70788d75891900e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 46b8fe04d97334c518edd7344f1a2b2e0a771bb3a6c524611e3fe47000c627dd3cfda33358c88c1889ff9ec654fb8e7d8fd2c8aa732a76959410ca2a0f85669e
|
|
7
|
+
data.tar.gz: 8b16c7ea07f84084e99f2de9a062e1500e3d018ce024d9d8f48302975a72c9791b3472597d1a51d3b1f72babd7c4187fca3bfb3b11a95c0744ee2aac1da85ccf
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
- Added: `audited_insert_all` takes `returning:`, a list of column names, and returns an `ActiveRecord::Result` of those columns in input order, like `insert_all`. Without it, it still returns the ids. Works on MySQL too, by reading the rows back.
|
|
6
|
+
|
|
7
|
+
## 0.2.0
|
|
8
|
+
|
|
9
|
+
- Fixed: on databases without `INSERT ... RETURNING`, `audited_insert_all` raised `KeyError` when the caller supplied ids typed differently to the column, such as strings out of a CSV or a JSON payload.
|
|
4
10
|
|
|
5
11
|
## 0.1.0
|
|
6
12
|
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
samrobinsonwork@outlook.com.
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
|
122
|
+
enforcement ladder](https://github.com/mozilla/diversity).
|
|
123
|
+
|
|
124
|
+
[homepage]: https://www.contributor-covenant.org
|
|
125
|
+
|
|
126
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
127
|
+
https://www.contributor-covenant.org/faq. Translations are available at
|
|
128
|
+
https://www.contributor-covenant.org/translations.
|
data/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
PaperTrail versions for `update_all`, `delete_all` and `insert_all`.
|
|
4
4
|
|
|
5
|
+
Working on PostgreSQL (full support), SQLite 3.35+ and MySQL 5-7+, MariaDB 10.5+.
|
|
6
|
+
|
|
5
7
|
## The problem
|
|
6
8
|
|
|
7
9
|
[PaperTrail](https://github.com/paper-trail-gem/paper_trail) records versions from ActiveRecord callbacks. Bulk writes skip callbacks, so they leave no audit trail:
|
|
@@ -58,6 +60,9 @@ Shift.where(id: ids).audited_delete_all
|
|
|
58
60
|
|
|
59
61
|
# Returns the new ids, in input order. Rows are inserted with insert_all!.
|
|
60
62
|
Shift.audited_insert_all([{ name: 'a', status: 'draft' }, { name: 'b', status: 'draft' }])
|
|
63
|
+
|
|
64
|
+
# Pass returning: for other columns; returns an ActiveRecord::Result, like insert_all.
|
|
65
|
+
Shift.audited_insert_all(rows, returning: %w[id status])
|
|
61
66
|
```
|
|
62
67
|
|
|
63
68
|
Batches default to 1,000 rows:
|
|
@@ -120,7 +125,6 @@ end
|
|
|
120
125
|
|
|
121
126
|
## Limitations
|
|
122
127
|
|
|
123
|
-
- **Postgres and SQLite (3.35+) only.** `audited_insert_all` relies on `INSERT ... RETURNING`. MySQL is untested.
|
|
124
128
|
- **`object` is not written**, only `object_changes`. `version.reify` won't work on bulk-written versions; use `where_object_changes` and friends.
|
|
125
129
|
- **`has_paper_trail on:`** is not consulted — every bulk write is versioned.
|
|
126
130
|
- Requires Ruby 3.2+, ActiveRecord 7.1+, PaperTrail 15+.
|
|
@@ -130,11 +134,22 @@ end
|
|
|
130
134
|
```sh
|
|
131
135
|
bundle install
|
|
132
136
|
bundle exec rspec # SQLite in memory
|
|
133
|
-
DB=postgres DATABASE_URL=postgres://localhost/paper_trail_bulk_writes_test bundle exec rspec
|
|
134
137
|
bundle exec appraisal install && bundle exec appraisal rspec # every Rails × PaperTrail combination
|
|
135
138
|
bundle exec rubocop
|
|
136
139
|
```
|
|
137
140
|
|
|
141
|
+
`pg` and `mysql2` build against client libraries the SQLite suite does not need, so they sit in optional groups. Opt in per driver to run the suite against Postgres or MySQL:
|
|
142
|
+
|
|
143
|
+
```sh
|
|
144
|
+
bundle config set --local with postgres
|
|
145
|
+
bundle install
|
|
146
|
+
DB=postgres DATABASE_URL=postgres://localhost/paper_trail_bulk_writes_test bundle exec rspec
|
|
147
|
+
|
|
148
|
+
bundle config set --local with mysql
|
|
149
|
+
bundle install
|
|
150
|
+
DB=mysql DATABASE_URL=mysql2://root:root@127.0.0.1:3306/paper_trail_bulk_writes_test bundle exec rspec
|
|
151
|
+
```
|
|
152
|
+
|
|
138
153
|
## License
|
|
139
154
|
|
|
140
155
|
MIT. Extracted from production code at [Florence](https://www.florence.co.uk).
|
|
@@ -5,32 +5,86 @@ module PaperTrail
|
|
|
5
5
|
class Insert
|
|
6
6
|
include Versioned
|
|
7
7
|
|
|
8
|
-
def initialize(model:, rows:, whodunnit: nil, batch_size: nil)
|
|
8
|
+
def initialize(model:, rows:, whodunnit: nil, batch_size: nil, returning: nil)
|
|
9
9
|
@model = model
|
|
10
10
|
@rows = rows
|
|
11
11
|
@whodunnit = whodunnit
|
|
12
12
|
@batch_size = batch_size || default_batch_size
|
|
13
|
+
@returning = returning && Array(returning).map(&:to_s)
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def call
|
|
16
|
-
|
|
17
|
+
inserted = insert_all
|
|
18
|
+
return inserted.pluck('id') unless returning
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
ActiveRecord::Result.new(returning, inserted.map { |attributes| attributes.values_at(*returning) })
|
|
19
21
|
end
|
|
20
22
|
|
|
21
23
|
private
|
|
22
24
|
|
|
23
|
-
attr_reader :model, :rows, :whodunnit, :batch_size
|
|
25
|
+
attr_reader :model, :rows, :whodunnit, :batch_size, :returning
|
|
24
26
|
|
|
25
27
|
def tracked
|
|
26
28
|
@tracked ||= adapter.tracked(model.column_names) - ['id']
|
|
27
29
|
end
|
|
28
30
|
|
|
31
|
+
def fetched
|
|
32
|
+
@fetched ||= ['id', *tracked] | returning.to_a
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def insert_all
|
|
36
|
+
return [] if rows.empty?
|
|
37
|
+
|
|
38
|
+
model.transaction { rows.each_slice(batch_size).flat_map { |batch| write(batch) } }
|
|
39
|
+
end
|
|
40
|
+
|
|
29
41
|
def write(batch)
|
|
30
|
-
inserted =
|
|
42
|
+
inserted = insert_and_fetch(batch)
|
|
31
43
|
changed = changed_columns(batch)
|
|
32
44
|
adapter.insert(inserted.map { |attributes| version_row(attributes, changed) })
|
|
33
|
-
inserted
|
|
45
|
+
inserted
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Databases with RETURNING (Postgres, SQLite 3.35+, MariaDB 10.5+) hand the
|
|
49
|
+
# inserted rows back.
|
|
50
|
+
def insert_and_fetch(batch)
|
|
51
|
+
if model.connection.supports_insert_returning?
|
|
52
|
+
model.insert_all!(batch, returning: fetched).to_a
|
|
53
|
+
else
|
|
54
|
+
supplied = supplied_ids(batch)
|
|
55
|
+
model.insert_all!(batch)
|
|
56
|
+
fetch_rows(supplied || derive_ids(batch.size))
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# MySQL only - it doesn't give us the inserted rows back for free.
|
|
61
|
+
def supplied_ids(batch)
|
|
62
|
+
ids = batch.map { |row| row[:id] || row['id'] }
|
|
63
|
+
return nil if ids.all?(&:nil?)
|
|
64
|
+
raise ArgumentError, 'rows must all supply an id or none of them' unless ids.none?(&:nil?)
|
|
65
|
+
|
|
66
|
+
cast_ids(ids)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def cast_ids(ids)
|
|
70
|
+
type = model.type_for_attribute('id')
|
|
71
|
+
ids.map { |id| type.cast(id) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def derive_ids(count)
|
|
75
|
+
first = select_integer('SELECT LAST_INSERT_ID()')
|
|
76
|
+
step = select_integer('SELECT @@auto_increment_increment')
|
|
77
|
+
Array.new(count) { |i| first + (i * step) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def select_integer(sql)
|
|
81
|
+
model.connection.select_value(sql).to_i
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def fetch_rows(ids)
|
|
85
|
+
relation = model.unscoped.where(id: ids).select(*fetched)
|
|
86
|
+
by_id = model.connection.select_all(relation).to_a.index_by { |row| row['id'] }
|
|
87
|
+
ids.map { |id| by_id.fetch(id) }
|
|
34
88
|
end
|
|
35
89
|
|
|
36
90
|
def changed_columns(batch)
|
|
@@ -8,8 +8,8 @@ module PaperTrail
|
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
module ClassMethods
|
|
11
|
-
def audited_insert_all(rows, whodunnit: PaperTrail.request.whodunnit)
|
|
12
|
-
Insert.call(model: self, rows: rows, whodunnit: whodunnit)
|
|
11
|
+
def audited_insert_all(rows, whodunnit: PaperTrail.request.whodunnit, returning: nil)
|
|
12
|
+
Insert.call(model: self, rows: rows, whodunnit: whodunnit, returning: returning)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def audited_update_all(attributes:, whodunnit: PaperTrail.request.whodunnit, touch: true)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: paper_trail-bulk_writes
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sam Robinson
|
|
@@ -50,6 +50,7 @@ extensions: []
|
|
|
50
50
|
extra_rdoc_files: []
|
|
51
51
|
files:
|
|
52
52
|
- CHANGELOG.md
|
|
53
|
+
- CODE_OF_CONDUCT.md
|
|
53
54
|
- LICENSE.txt
|
|
54
55
|
- README.md
|
|
55
56
|
- Rakefile
|