paper_trail-bulk_writes 0.1.0 → 0.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: 055ac91334d2baf03ac6070aa705f234296a25eebf790e05e52b7248f2be61b5
4
- data.tar.gz: f5739caa8f59629cebd71a0dd16c8c9dafb7fb92e4b45051eddfff9e36e587a6
3
+ metadata.gz: d95e8ca4943e9946044920ed171b6ac8b2453d3f6679d7d33d22295de5a5ad0e
4
+ data.tar.gz: a23c12b374cbff0625383b6d18f2585480f3aca36f517288a4eb5d4ba054a0e0
5
5
  SHA512:
6
- metadata.gz: 20a07a365217a304a6920df25fdd6cb6d55da74f09d0fe79d33ca86adefb5e2aa140d4b51fc96f77f59cfc0d97eea40abfdcad87943ea0f2faa83ef87967f7c2
7
- data.tar.gz: 349d1dd5cc22cfe873d731243540e8bda174d38cfd8893a5ab19edc7a6ddbb7f960dd039bff032b80bf7c163690fddd0bfca2d0196386acef46034b5e0f28a8a
6
+ metadata.gz: b33946ae81b24bc77d3f3ef25040e5af6d8605003ab5d8ad37f9638a19e680c48e85aa5ff68589ddbc1b0a507ad6781f4d0b64c1b2c62494f03073f1816b0fc6
7
+ data.tar.gz: 5d3bef3c3c4abd5700ca9d1a30d9aae3f4348ce0381f89c20d136e50ba37668172f4b0a0b73ca0c547dfe2372d300a3607b5036e71a88cadef2184b7b0967d44
data/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - 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.
6
+
5
7
  ## 0.1.0
6
8
 
7
9
  - `PaperTrail::BulkWrites::Model` with `audited_update_all`, `audited_delete_all` and `audited_insert_all`.
@@ -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:
@@ -120,7 +122,6 @@ end
120
122
 
121
123
  ## Limitations
122
124
 
123
- - **Postgres and SQLite (3.35+) only.** `audited_insert_all` relies on `INSERT ... RETURNING`. MySQL is untested.
124
125
  - **`object` is not written**, only `object_changes`. `version.reify` won't work on bulk-written versions; use `where_object_changes` and friends.
125
126
  - **`has_paper_trail on:`** is not consulted — every bulk write is versioned.
126
127
  - Requires Ruby 3.2+, ActiveRecord 7.1+, PaperTrail 15+.
@@ -130,11 +131,22 @@ end
130
131
  ```sh
131
132
  bundle install
132
133
  bundle exec rspec # SQLite in memory
133
- DB=postgres DATABASE_URL=postgres://localhost/paper_trail_bulk_writes_test bundle exec rspec
134
134
  bundle exec appraisal install && bundle exec appraisal rspec # every Rails × PaperTrail combination
135
135
  bundle exec rubocop
136
136
  ```
137
137
 
138
+ `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:
139
+
140
+ ```sh
141
+ bundle config set --local with postgres
142
+ bundle install
143
+ DB=postgres DATABASE_URL=postgres://localhost/paper_trail_bulk_writes_test bundle exec rspec
144
+
145
+ bundle config set --local with mysql
146
+ bundle install
147
+ DB=mysql DATABASE_URL=mysql2://root:root@127.0.0.1:3306/paper_trail_bulk_writes_test bundle exec rspec
148
+ ```
149
+
138
150
  ## License
139
151
 
140
152
  MIT. Extracted from production code at [Florence](https://www.florence.co.uk).
@@ -27,12 +27,54 @@ module PaperTrail
27
27
  end
28
28
 
29
29
  def write(batch)
30
- inserted = model.insert_all!(batch, returning: ['id', *tracked]).to_a
30
+ inserted = insert_and_fetch(batch)
31
31
  changed = changed_columns(batch)
32
32
  adapter.insert(inserted.map { |attributes| version_row(attributes, changed) })
33
33
  inserted.pluck('id')
34
34
  end
35
35
 
36
+ # Databases with RETURNING (Postgres, SQLite 3.35+, MariaDB 10.5+) hand the
37
+ # inserted rows back.
38
+ def insert_and_fetch(batch)
39
+ if model.connection.supports_insert_returning?
40
+ model.insert_all!(batch, returning: ['id', *tracked]).to_a
41
+ else
42
+ supplied = supplied_ids(batch)
43
+ model.insert_all!(batch)
44
+ fetch_rows(supplied || derive_ids(batch.size))
45
+ end
46
+ end
47
+
48
+ # MySQL only - it doesn't give us the inserted rows back for free.
49
+ def supplied_ids(batch)
50
+ ids = batch.map { |row| row[:id] || row['id'] }
51
+ return nil if ids.all?(&:nil?)
52
+ raise ArgumentError, 'rows must all supply an id or none of them' unless ids.none?(&:nil?)
53
+
54
+ cast_ids(ids)
55
+ end
56
+
57
+ def cast_ids(ids)
58
+ type = model.type_for_attribute('id')
59
+ ids.map { |id| type.cast(id) }
60
+ end
61
+
62
+ def derive_ids(count)
63
+ first = select_integer('SELECT LAST_INSERT_ID()')
64
+ step = select_integer('SELECT @@auto_increment_increment')
65
+ Array.new(count) { |i| first + (i * step) }
66
+ end
67
+
68
+ def select_integer(sql)
69
+ model.connection.select_value(sql).to_i
70
+ end
71
+
72
+ def fetch_rows(ids)
73
+ relation = model.unscoped.where(id: ids).select('id', *tracked)
74
+ by_id = model.connection.select_all(relation).to_a.index_by { |row| row['id'] }
75
+ ids.map { |id| by_id.fetch(id) }
76
+ end
77
+
36
78
  def changed_columns(batch)
37
79
  supplied = batch.flat_map { |row| row.keys.map(&:to_s) }.uniq
38
80
  (supplied + %w[id created_at updated_at]) & ['id', *tracked]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PaperTrail
4
4
  module BulkWrites
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
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.1.0
4
+ version: 0.2.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