logidze 0.8.1 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +124 -5
- data/LICENSE.txt +1 -1
- data/README.md +310 -80
- data/lib/generators/logidze/fx_helper.rb +17 -0
- data/lib/generators/logidze/inject_sql.rb +18 -0
- data/lib/generators/logidze/install/USAGE +6 -1
- data/lib/generators/logidze/install/functions/logidze_compact_history.sql +38 -0
- data/lib/generators/logidze/install/functions/logidze_filter_keys.sql +27 -0
- data/lib/generators/logidze/install/functions/logidze_logger.sql +150 -0
- data/lib/generators/logidze/install/functions/logidze_snapshot.sql +24 -0
- data/lib/generators/logidze/install/functions/logidze_version.sql +20 -0
- data/lib/generators/logidze/install/install_generator.rb +61 -3
- data/lib/generators/logidze/install/templates/hstore.rb.erb +1 -1
- data/lib/generators/logidze/install/templates/migration.rb.erb +19 -232
- data/lib/generators/logidze/install/templates/migration_fx.rb.erb +41 -0
- data/lib/generators/logidze/model/model_generator.rb +56 -20
- data/lib/generators/logidze/model/templates/migration.rb.erb +57 -36
- data/lib/generators/logidze/model/triggers/logidze.sql +6 -0
- data/lib/logidze.rb +43 -20
- data/lib/logidze/engine.rb +4 -1
- data/lib/logidze/has_logidze.rb +12 -3
- data/lib/logidze/history.rb +7 -15
- data/lib/logidze/history/type.rb +1 -1
- data/lib/logidze/history/version.rb +6 -5
- data/lib/logidze/ignore_log_data.rb +22 -0
- data/lib/logidze/ignore_log_data/cast_attribute_patch.rb +17 -0
- data/lib/logidze/meta.rb +44 -17
- data/lib/logidze/model.rb +65 -37
- data/lib/logidze/version.rb +2 -1
- data/lib/logidze/versioned_association.rb +0 -1
- metadata +47 -103
- data/.gitignore +0 -40
- data/.hound.yml +0 -3
- data/.rubocop.yml +0 -94
- data/.travis.yml +0 -39
- data/Gemfile +0 -13
- data/Rakefile +0 -28
- data/bench/performance/README.md +0 -109
- data/bench/performance/diff_bench.rb +0 -36
- data/bench/performance/insert_bench.rb +0 -20
- data/bench/performance/memory_profile.rb +0 -53
- data/bench/performance/setup.rb +0 -308
- data/bench/performance/update_bench.rb +0 -36
- data/bench/triggers/Makefile +0 -56
- data/bench/triggers/Readme.md +0 -58
- data/bench/triggers/bench.sql +0 -6
- data/bench/triggers/hstore_trigger_setup.sql +0 -38
- data/bench/triggers/jsonb_minus_2_setup.sql +0 -47
- data/bench/triggers/jsonb_minus_setup.sql +0 -49
- data/bench/triggers/keys2_trigger_setup.sql +0 -44
- data/bench/triggers/keys_trigger_setup.sql +0 -50
- data/bin/console +0 -8
- data/bin/setup +0 -9
- data/gemfiles/rails42.gemfile +0 -5
- data/gemfiles/rails5.gemfile +0 -6
- data/gemfiles/rails52.gemfile +0 -6
- data/gemfiles/railsmaster.gemfile +0 -7
- data/lib/logidze/migration.rb +0 -19
- data/logidze.gemspec +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 747454e7c38a1969f39ed1f4b9d9142b2afc48d7ed7c34534b82abcac88dc4d7
|
4
|
+
data.tar.gz: 2522172435f01b7d4441479f5f0c3e1bf08cde1f36e6ac9c745c8c8a20bd1d6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbbcbe791ca4f682f75c188c0b65db6f8244dea84a6cb5390a85c980921f1f84bf3676962dbb5792cf5ac2d7a24f0dbf1ac6b916f7bf03a1a13a1820ebc89507
|
7
|
+
data.tar.gz: 4514014a739bc452aa7ad8fb26495a96aa651bfa3c8c4784021c1e2d13874c003c57e24b28ac6397f5ca5239e4e65dd76f0565df7af69ad29bbc8dd63630cc8b
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,123 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
-
## master
|
3
|
+
## master (unreleased)
|
4
|
+
|
5
|
+
## 1.0.0.rc1 (2020-09-01)
|
6
|
+
|
7
|
+
- Add `.with_full_snapshot` to add full snapshots to the log instead of diffs. ([@palkan][])
|
8
|
+
|
9
|
+
Useful in combination with `.without_logging`: first, you perform multiple updates without logging, then
|
10
|
+
you do something like `with_full_snapshot { record.touch }` to create a log entry with the current state.
|
11
|
+
|
12
|
+
- Add `#create_logidze_snapshot!` and `.create_logidze_snapshot` methods. ([@palkan][])
|
13
|
+
|
14
|
+
- Add integration with `fx` gem. ([@palkan][])
|
15
|
+
|
16
|
+
Now it's possible to use Logidze with `schema.rb`. Add `fx` gem to the project, and new migrations will be
|
17
|
+
using Fx `create_function` / `create_trigger` functions.
|
18
|
+
|
19
|
+
- Refactored columns filtering. ([@palkan][])
|
20
|
+
|
21
|
+
Renamed `--whitelist/--blacklist` to `--only/--except` correspondingly.
|
22
|
+
|
23
|
+
The _only_-logic has been changed: previously we collected the list of columns to ignore at the migration generation time,
|
24
|
+
now we filter the columns within the trigger function (thus, schema changes do not affect the columns being tracked).
|
25
|
+
|
26
|
+
- **Dropped support for Rails 4.2, Ruby 2.4 and PostgreSQL 9.5**. ([@palkan][])
|
27
|
+
|
28
|
+
## 0.12.0 (2020-01-02)
|
29
|
+
|
30
|
+
- PR [#143](https://github.com/palkan/logidze/pull/143) Add `:transactional` option to `#with_meta` and `#with_responsible` ([@oleg-kiviljov][])
|
31
|
+
|
32
|
+
Now it's possible to set meta and responsible without wrapping the block into a DB transaction. For backward compatibility `:transactional` option by default is set to `true`.
|
33
|
+
|
34
|
+
Usage:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Logidze.with_meta({ip: request.ip}, transactional: false) do
|
38
|
+
post.save!
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
or
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Logidze.with_responsible(user.id, transactional: false) do
|
46
|
+
post.save!
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
## 0.11.0 (2019-08-15)
|
51
|
+
|
52
|
+
- **Breaking** Return `nil` when `log_data` is not loaded instead of raising an exception. ([@palkan][])
|
53
|
+
|
54
|
+
We cannot distinguish between not loaded `log_data` and not-yet-created (i.e. for new records).
|
55
|
+
The latter could be used in frameworks/gems ([example](https://github.com/palkan/logidze/issues/127#issuecomment-518798640)).
|
56
|
+
|
57
|
+
- **Breaking** Only allow specifying `ignore_log_data` at boot time without runtime modifications. ([@palkan][])
|
58
|
+
|
59
|
+
Playing with ActiveRecord default scopes wasn't a good idea. We fallback to a more explicit way of _telling_ AR
|
60
|
+
when to load or ignore the `log_data` column.
|
61
|
+
|
62
|
+
This change removes `Logidze.with_log_data` method.
|
63
|
+
|
64
|
+
## 0.10.0 (2019-05-15)
|
65
|
+
|
66
|
+
- **Ruby >= 2.4 is required**
|
67
|
+
|
68
|
+
- PR [#111](https://github.com/palkan/logidze/pull/111) Global configuration for `:ignore_log_data` option ([@dmitrytsepelev][])
|
69
|
+
|
70
|
+
Now it's possible to avoid loading `log_data` from the DB by default with
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Logidze.ignore_log_data_by_default = true
|
74
|
+
```
|
75
|
+
|
76
|
+
In cases when `ignore_log_data: false` is explicitly passed to the `ignore_log_data` the default setting is being overriden. Also, it's possible to change it inside the block:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
Logidze.with_log_data do
|
80
|
+
Post.find(params[:id]).log_data
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
- PR [#110](https://github.com/palkan/logidze/pull/110) Add `reset_log_data` API to nullify log_data column ([@Arkweid][])
|
85
|
+
|
86
|
+
Usage:
|
87
|
+
|
88
|
+
Reset the history for a record (or records):
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
# for single record
|
92
|
+
record.reset_log_data
|
93
|
+
|
94
|
+
# for relation
|
95
|
+
User.where(active: true).reset_log_data
|
96
|
+
```
|
97
|
+
|
98
|
+
## 0.9.0 (2018-11-28)
|
99
|
+
|
100
|
+
- PR [#98](https://github.com/palkan/logidze/pull/98) Add `:ignore_log_data` option to `#has_logidze` ([@dmitrytsepelev][])
|
101
|
+
|
102
|
+
Usage:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class User < ActiveRecord::Base
|
106
|
+
has_logidze ignore_log_data: true
|
107
|
+
end
|
108
|
+
|
109
|
+
User.all #=> SELECT id, name FROM users
|
110
|
+
|
111
|
+
User.with_log_data #=> SELECT id, name, log_data FROM users
|
112
|
+
|
113
|
+
user = User.find(params[:id])
|
114
|
+
user.log_data #=> ActiveModel::MissingAttributeError
|
115
|
+
user.reload_log_data #=> Logidze::History
|
116
|
+
```
|
117
|
+
|
118
|
+
## 0.8.1 (2018-10-22)
|
119
|
+
|
120
|
+
- [PR #93](https://github.com/palkan/logidze/pull/93)] Return 0 for log size when log_data is nil ([@duderman][])
|
4
121
|
|
5
122
|
## 0.8.0 (2018-10-01)
|
6
123
|
|
@@ -29,7 +146,7 @@ Please run `rails generate logidze:install --update` to regenerate stored functi
|
|
29
146
|
This feature checks if several logs came in within a debounce time period then only keep the latest one
|
30
147
|
by merging the latest in previous others.
|
31
148
|
|
32
|
-
The concept is similar to https://underscorejs.org/#debounce
|
149
|
+
The concept is similar to [https://underscorejs.org/#debounce](https://underscorejs.org/#debounce).
|
33
150
|
|
34
151
|
without `debounce_time`
|
35
152
|
|
@@ -90,7 +207,7 @@ with `debounce_time` of `10ms`
|
|
90
207
|
|
91
208
|
## 0.7.0 (2018-08-29)
|
92
209
|
|
93
|
-
- [Fixes [#75](https://github.com/palkan/logidze/issues/70)] Fix association versioning with an optional belongs to ([@
|
210
|
+
- [Fixes [#75](https://github.com/palkan/logidze/issues/70)] Fix association versioning with an optional belongs to ([@amalagaura][])
|
94
211
|
|
95
212
|
- [PR [#79](https://github.com/palkan/logidze/pull/13)] Allow adding meta information to versions using `with_meta` (addressed [Issue [#60]](https://github.com/palkan/logidze/issues/60)). ([@DmitryTsepelev][])
|
96
213
|
|
@@ -158,7 +275,7 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
|
|
158
275
|
|
159
276
|
## 0.5.1 (2017-06-15)
|
160
277
|
|
161
|
-
- _(Fix)_ Drop _all_ created functions upon rolling back (https://github.com/palkan/logidze/commit/b8e150cc18b3316a8cf0c78f7117339481fb49c6). ([@vassilevsky][])
|
278
|
+
- _(Fix)_ Drop _all_ created functions upon rolling back ([commit](https://github.com/palkan/logidze/commit/b8e150cc18b3316a8cf0c78f7117339481fb49c6)). ([@vassilevsky][])
|
162
279
|
|
163
280
|
## 0.5.0 (2017-03-28)
|
164
281
|
|
@@ -208,6 +325,8 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
|
|
208
325
|
[@charlie-wasp]: https://github.com/charlie-wasp
|
209
326
|
[@akxcv]: https://github.com/akxcv
|
210
327
|
[@vassilevsky]: https://github.com/vassilevsky
|
211
|
-
[@
|
328
|
+
[@amalagaura]: https://github.com/amalagaura
|
212
329
|
[@dmitrytsepelev]: https://github.com/DmitryTsepelev
|
213
330
|
[@zocoi]: https://github.com/zocoi
|
331
|
+
[@duderman]: https://github.com/duderman
|
332
|
+
[@oleg-kiviljov]: https://github.com/oleg-kiviljov
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,134 +1,189 @@
|
|
1
|
-
[![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](http://cultofmartians.com)
|
1
|
+
[![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](http://cultofmartians.com)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/logidze.svg)](https://rubygems.org/gems/logidze)
|
3
|
+
![Build](https://github.com/palkan/logidze/workflows/Build/badge.svg)
|
4
|
+
[![Open Source Helpers](https://www.codetriage.com/palkan/logidze/badges/users.svg)](https://www.codetriage.com/palkan/logidze)
|
2
5
|
|
3
6
|
# Logidze
|
4
7
|
|
5
|
-
Logidze provides tools for logging DB records changes. Just like [audited](https://github.com/collectiveidea/audited) and [paper_trail](https://github.com/airblade/paper_trail) do (but [faster](bench/performance)).
|
8
|
+
Logidze provides tools for logging DB records changes when using PostgreSQL (>=9.6). Just like [audited](https://github.com/collectiveidea/audited) and [paper_trail](https://github.com/airblade/paper_trail) do (but [faster](bench/performance)).
|
6
9
|
|
7
10
|
Logidze allows you to create a DB-level log (using triggers) and gives you an API to browse this log.
|
8
11
|
The log is stored with the record itself in JSONB column. No additional tables required.
|
9
|
-
Currently, only PostgreSQL 9.5+ is supported (for PostgreSQL 9.4 try [jsonbx](http://www.pgxn.org/dist/jsonbx/1.0.0/) extension).
|
10
12
|
|
11
|
-
|
13
|
+
**❗️ IMPORTANT:** This page contains documentation for the upcoming v1.0. For the latest release documentation see the [0-stable branch](https://github.com/palkan/logidze/tree/0-stable). If you're just starting with Logidze, we recommend using the latest release candidate (`1.0.0.rc1`).
|
12
14
|
|
13
|
-
[How is Logidze pronounced?](https://github.com/palkan/logidze/issues/73)
|
15
|
+
🤔 [How is Logidze pronounced?](https://github.com/palkan/logidze/issues/73)
|
14
16
|
|
15
17
|
Other requirements:
|
16
|
-
|
17
|
-
-
|
18
|
+
|
19
|
+
- Ruby ~> 2.5
|
20
|
+
- Rails >= 5.0 (for Rails 4.2 use version <=0.12.0)
|
18
21
|
|
19
22
|
<a href="https://evilmartians.com/">
|
20
23
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
21
24
|
|
25
|
+
## Links
|
26
|
+
|
27
|
+
- [Logidze: for all those tired of versioning data](https://evilmartians.com/chronicles/introducing-logidze?utm_source=logidze)
|
28
|
+
|
29
|
+
## Table of contents
|
30
|
+
|
31
|
+
- [Main concepts](#main-concepts)
|
32
|
+
- [Installation & Configuration](#installation)
|
33
|
+
- [Using with schema.rb](#using-with-schemarb)
|
34
|
+
- [Configuration models](#configuration-models)
|
35
|
+
- [Backfill data](#backfill-data)
|
36
|
+
- [Log size limit](#log-size-limit)
|
37
|
+
- [Tracking only selected columns](#tracking-only-selected-columns)
|
38
|
+
- [Logs timestamps](#logs-timestamps)
|
39
|
+
- [Usage](#usage)
|
40
|
+
- [Basic API](#basic-api)
|
41
|
+
- [Track meta information](#track-meta-information)
|
42
|
+
- [Track responsibility](#track-responsibility)
|
43
|
+
- [Disable logging temporary](#disable-logging-temporary)
|
44
|
+
- [Reset log](#reset-log)
|
45
|
+
- [Creating full snapshot instead of diffs](#full-snapshots)
|
46
|
+
- [Associations versioning](#associations-versioning)
|
47
|
+
- [Dealing with large logs](#dealing-with-large-logs)
|
48
|
+
- [Handling records deletion](#handling-records-deletion)
|
49
|
+
- [Upgrading](#upgrading)
|
50
|
+
- [Log format](#log-format)
|
51
|
+
- [Troubleshooting 🚨](#troubleshooting)
|
52
|
+
- [Development](#development)
|
53
|
+
|
22
54
|
## Installation
|
23
55
|
|
24
56
|
Add Logidze to your application's Gemfile:
|
25
57
|
|
26
58
|
```ruby
|
27
|
-
gem
|
59
|
+
gem "logidze", "1.0.0.rc1"
|
28
60
|
```
|
29
61
|
|
30
62
|
Install required DB extensions and create trigger function:
|
31
63
|
|
32
64
|
```sh
|
33
|
-
rails generate logidze:install
|
65
|
+
bundle exec rails generate logidze:install
|
34
66
|
```
|
35
67
|
|
36
|
-
This creates migration for adding trigger function and enabling hstore extension.
|
68
|
+
This creates a migration for adding trigger function and enabling the hstore extension.
|
37
69
|
|
38
70
|
Run migrations:
|
39
71
|
|
40
72
|
```sh
|
41
|
-
|
73
|
+
bundle exec rails db:migrate
|
42
74
|
```
|
43
75
|
|
44
|
-
**NOTE:** you
|
76
|
+
**NOTE:** Logidze uses DB functions and triggers, hence you need to use SQL format for a schema dump:
|
45
77
|
|
46
78
|
```ruby
|
47
79
|
# application.rb
|
48
80
|
config.active_record.schema_format = :sql
|
49
81
|
```
|
50
82
|
|
51
|
-
|
83
|
+
### Using with schema.rb
|
84
|
+
|
85
|
+
Logidze seamlessly integrates with [fx][] gem to make it possible to continue using `schema.rb` for the database schema dump.
|
86
|
+
|
87
|
+
Add `fx` gem to your Gemfile and run the same Logidze generators: `rails g logidze:install` or `rails g logidze:model`.
|
88
|
+
|
89
|
+
If for some reason Logidze couldn't detect the presence of Fx in your bundle, you can enforce it by passing `--fx` option to generators.
|
90
|
+
|
91
|
+
On the other hand, if you have `fx` gem but don't want Logidze to use it—pass `--no-fx` option.
|
92
|
+
|
93
|
+
### Configuring models
|
94
|
+
|
95
|
+
Run the following migration to enable changes tracking for an Active Record model and adding a `log_data::jsonb` column to the table:
|
52
96
|
|
53
97
|
```sh
|
54
|
-
rails generate logidze:model Post
|
55
|
-
|
98
|
+
bundle exec rails generate logidze:model Post
|
99
|
+
bundle exec rails db:migrate
|
56
100
|
```
|
57
101
|
|
58
102
|
This also adds `has_logidze` line to your model, which adds methods for working with logs.
|
59
103
|
|
60
|
-
|
104
|
+
By default, Logidze tries to infer the path to the model file from the model name and may fail, for example, if you have unconventional project structure. In that case, you should specify the path explicitly:
|
61
105
|
|
62
106
|
```sh
|
63
|
-
rails generate logidze:model Post --
|
107
|
+
bundle exec rails generate logidze:model Post --path "app/models/custom/post.rb"
|
64
108
|
```
|
65
109
|
|
66
|
-
|
110
|
+
### Backfill data
|
111
|
+
|
112
|
+
To backfill table data (i.e., create initial snapshots) add `backfill` option to the generator:
|
67
113
|
|
68
114
|
```sh
|
69
|
-
rails generate logidze:model Post --backfill
|
115
|
+
bundle exec rails generate logidze:model Post --backfill
|
70
116
|
```
|
71
117
|
|
72
|
-
|
118
|
+
Now your migration should contain and `UPDATE ...` statement to populate the `log_data` column with the current state.
|
73
119
|
|
74
|
-
|
75
|
-
# track all columns, except `created_at` and `active`
|
76
|
-
rails generate logidze:model Post --blacklist=created_at active
|
77
|
-
# track only `title` and `body` columns
|
78
|
-
rails generate logidze:model Post --whitelist=title body
|
79
|
-
```
|
120
|
+
Otherwise a full snapshot will be created the first time the record is updated.
|
80
121
|
|
81
|
-
|
122
|
+
You can create a snapshot manually by performing the following query:
|
82
123
|
|
83
|
-
```
|
84
|
-
|
124
|
+
```sql
|
125
|
+
UPDATE <my_table> as t
|
126
|
+
SET log_data = logidze_snapshot(to_jsonb(t))
|
85
127
|
```
|
86
128
|
|
87
|
-
|
88
|
-
your model does not have that column, Logidze will gracefully fall back to `statement_timestamp()`.
|
89
|
-
To change the column name or disable this feature completely, you can use the `timestamp_column` option:
|
129
|
+
Or by using the following methods:
|
90
130
|
|
91
|
-
```
|
92
|
-
|
93
|
-
rails generate logidze:model Post --timestamp_column time
|
94
|
-
# will always set version timestamp to `statement_timestamp()`
|
95
|
-
rails generate logidze:model Post --timestamp_column nil # "null" and "false" will also work
|
96
|
-
```
|
131
|
+
```ruby
|
132
|
+
Model.create_logidze_snapshot
|
97
133
|
|
98
|
-
|
134
|
+
# specify the timestamp column to use for the initial version (by default the current time is used)
|
135
|
+
Model.create_logidze_snapshot(timestamp: :created_at)
|
99
136
|
|
100
|
-
|
101
|
-
|
137
|
+
# filter columns
|
138
|
+
Model.create_logidze_snapshot(only: %(name))
|
139
|
+
Model.create_logidze_snapshot(except: %(password))
|
140
|
+
|
141
|
+
# or call a similar method (but with !) on a record
|
142
|
+
|
143
|
+
my_model = Model.find(params[:id])
|
144
|
+
my_model.create_logidze_snapshot!(timestamp: :created_at)
|
102
145
|
```
|
103
146
|
|
104
|
-
|
105
|
-
in the [wiki](https://github.com/palkan/logidze/wiki/Associations-versioning).
|
147
|
+
A snapshot is only created if `log_data` is null.
|
106
148
|
|
107
|
-
|
149
|
+
### Log size limits
|
108
150
|
|
109
|
-
|
110
|
-
Logidze requires at least database owner privileges (which is not always possible).
|
151
|
+
You can provide the `limit` option to `generate` to limit the size of the log (by default it's unlimited):
|
111
152
|
|
112
|
-
|
153
|
+
```sh
|
154
|
+
bundle exec rails generate logidze:model Post --limit=10
|
155
|
+
```
|
113
156
|
|
114
|
-
|
157
|
+
### Tracking only selected columns
|
115
158
|
|
116
|
-
|
159
|
+
You can log only particular columns changes. There are mutually exclusive `except` and `only` options for this:
|
117
160
|
|
161
|
+
```sh
|
162
|
+
# track all columns, except `created_at` and `active`
|
163
|
+
bundle exec rails generate logidze:model Post --except=created_at,active
|
164
|
+
# track only `title` and `body` columns
|
165
|
+
bundle exec rails generate logidze:model Post --only=title,body
|
166
|
+
```
|
167
|
+
|
168
|
+
### Logs timestamps
|
118
169
|
|
119
|
-
|
170
|
+
By default, Logidze tries to get a timestamp for a version from record's `updated_at` field whenever appropriate. If
|
171
|
+
your model does not have that column, Logidze will gracefully fall back to `statement_timestamp()`.
|
120
172
|
|
121
|
-
|
173
|
+
To change the column name or disable this feature completely, you can use the `timestamp_column` option:
|
122
174
|
|
123
175
|
```sh
|
124
|
-
|
176
|
+
# will try to get the timestamp value from `time` column
|
177
|
+
bundle exec rails generate logidze:model Post --timestamp_column time
|
178
|
+
# will always set version timestamp to `statement_timestamp()`
|
179
|
+
bundle exec rails generate logidze:model Post --timestamp_column nil # "null" and "false" will also work
|
125
180
|
```
|
126
181
|
|
127
|
-
This updates core `logdize_logger` DB function. No need to update tables or triggers.
|
128
|
-
|
129
182
|
## Usage
|
130
183
|
|
131
|
-
|
184
|
+
### Basic API
|
185
|
+
|
186
|
+
Your model now has `log_data` column, which stores changes log.
|
132
187
|
|
133
188
|
To retrieve record version at a given time use `#at` or `#at!` methods:
|
134
189
|
|
@@ -142,13 +197,13 @@ post.log_version #=> 3
|
|
142
197
|
post.log_size #=> 3
|
143
198
|
|
144
199
|
# Get copy of a record at a given time
|
145
|
-
|
200
|
+
post.at(time: 2.days.ago)
|
146
201
|
|
147
202
|
# or revert the record itself to the previous state (without committing to DB)
|
148
|
-
post.at!(time:
|
203
|
+
post.at!(time: "2018-04-15 12:00:00")
|
149
204
|
|
150
205
|
# If no version found
|
151
|
-
post.at(time:
|
206
|
+
post.at(time: "1945-05-09 09:00:00") #=> nil
|
152
207
|
```
|
153
208
|
|
154
209
|
You can also get revision by version number:
|
@@ -157,6 +212,9 @@ You can also get revision by version number:
|
|
157
212
|
post.at(version: 2)
|
158
213
|
```
|
159
214
|
|
215
|
+
**NOTE:** If `log_data` is nil, `#at(time:)` returns self and `#at(version:)` returns `nil`.
|
216
|
+
You can opt-in to return `nil` for time-based `#at` as well by setting `Logidze.return_self_if_log_data_is_empty = false`.
|
217
|
+
|
160
218
|
It is also possible to get version for relations:
|
161
219
|
|
162
220
|
```ruby
|
@@ -173,6 +231,8 @@ post.diff_from(time: 1.hour.ago)
|
|
173
231
|
Post.where(created_at: Time.zone.today.all_day).diff_from(time: 1.hour.ago)
|
174
232
|
```
|
175
233
|
|
234
|
+
**NOTE:** If `log_data` is nil, `#diff_from` returns an empty Hash as `"changes"`.
|
235
|
+
|
176
236
|
There are also `#undo!` and `#redo!` options (and more general `#switch_to!`):
|
177
237
|
|
178
238
|
```ruby
|
@@ -186,28 +246,33 @@ post.redo!
|
|
186
246
|
post.switch_to!(2)
|
187
247
|
```
|
188
248
|
|
189
|
-
|
249
|
+
You can initiate reloading of `log_data` from the DB:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
post.reload_log_data # => returns the latest log data value
|
253
|
+
```
|
254
|
+
|
255
|
+
Typically, if you update record after `#undo!` or `#switch_to!` you lose all "future" versions and `#redo!` is no
|
190
256
|
longer possible. However, you can provide an `append: true` option to `#undo!` or `#switch_to!`, which will
|
191
257
|
create a new version with old data. Caveat: when switching to a newer version, `append` will have no effect.
|
192
258
|
|
193
259
|
```ruby
|
194
|
-
post = Post.create!(title:
|
195
|
-
post.update!(title:
|
196
|
-
post.undo!(append: true)
|
260
|
+
post = Post.create!(title: "first post") # v1
|
261
|
+
post.update!(title: "new title") # v2
|
262
|
+
post.undo!(append: true) # v3 (with same attributes as v1)
|
197
263
|
```
|
198
264
|
|
199
265
|
Note that `redo!` will not work after `undo!(append: true)` because the latter will create a new version
|
200
266
|
instead of rolling back to an old one.
|
201
|
-
Alternatively, you can configure Logidze to
|
267
|
+
Alternatively, you can configure Logidze always to default to `append: true`.
|
202
268
|
|
203
269
|
```ruby
|
204
270
|
Logidze.append_on_undo = true
|
205
271
|
```
|
206
272
|
|
273
|
+
### Track meta information
|
207
274
|
|
208
|
-
|
209
|
-
|
210
|
-
You can store any meta information you want inside your version (it could be IP address, user agent etc). In order to add it you should wrap your code with a block:
|
275
|
+
You can store any meta information you want inside your version (it could be IP address, user agent, etc.). To add it you should wrap your code with a block:
|
211
276
|
|
212
277
|
```ruby
|
213
278
|
Logidze.with_meta(ip: request.ip) do
|
@@ -217,7 +282,15 @@ end
|
|
217
282
|
|
218
283
|
Meta expects a hash to be passed so you won't need to encode and decode JSON manually.
|
219
284
|
|
220
|
-
|
285
|
+
By default `.with_meta` wraps the block into a DB transaction. That could lead to an unexpected behavior, especially, when using `.with_meta` within an around_action. To avoid wrapping the block into a DB transaction use `transactional: false` option.
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
Logidze.with_meta({ip: request.ip}, transactional: false) do
|
289
|
+
post.save!
|
290
|
+
end
|
291
|
+
```
|
292
|
+
|
293
|
+
### Track responsibility
|
221
294
|
|
222
295
|
A special application of meta information is storing the author of the change, which is called _Responsible ID_. There is more likely that you would like to store the `current_user.id` that way.
|
223
296
|
|
@@ -235,7 +308,7 @@ And then to retrieve `responsible_id`:
|
|
235
308
|
post.log_data.responsible_id
|
236
309
|
```
|
237
310
|
|
238
|
-
Logidze does not require `responsible_id` to be `SomeModel` ID. It can be anything. Thus Logidze does not provide methods for retrieving the corresponding object. However, you can
|
311
|
+
Logidze does not require `responsible_id` to be `SomeModel` ID. It can be anything. Thus Logidze does not provide methods for retrieving the corresponding object. However, you can easily write it yourself:
|
239
312
|
|
240
313
|
```ruby
|
241
314
|
class Post < ActiveRecord::Base
|
@@ -252,17 +325,25 @@ And in your controller:
|
|
252
325
|
|
253
326
|
```ruby
|
254
327
|
class ApplicationController < ActionController::Base
|
255
|
-
around_action :
|
328
|
+
around_action :use_logidze_responsible, only: %i[create update]
|
256
329
|
|
257
|
-
def
|
330
|
+
def use_logidze_responsible(&block)
|
258
331
|
Logidze.with_responsible(current_user&.id, &block)
|
259
332
|
end
|
260
333
|
end
|
261
334
|
```
|
262
335
|
|
263
|
-
|
336
|
+
By default `.with_responsible` wraps the block into a DB transaction. That could lead to an unexpected behavior, especially, when using `.with_responsible` within an around_action. To avoid wrapping the block into a DB transaction use `transactional: false` option.
|
264
337
|
|
265
|
-
|
338
|
+
```ruby
|
339
|
+
Logidze.with_responsible(user.id, transactional: false) do
|
340
|
+
post.save!
|
341
|
+
end
|
342
|
+
```
|
343
|
+
|
344
|
+
### Disable logging temporary
|
345
|
+
|
346
|
+
If you want to make update without logging (e.g., mass update), you can turn it off the following way:
|
266
347
|
|
267
348
|
```ruby
|
268
349
|
Logidze.without_logging { Post.update_all(seen: true) }
|
@@ -272,6 +353,126 @@ Logidze.without_logging { Post.update_all(seen: true) }
|
|
272
353
|
Post.without_logging { Post.update_all(seen: true) }
|
273
354
|
```
|
274
355
|
|
356
|
+
### Reset log
|
357
|
+
|
358
|
+
Reset the history for a record (or records):
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
# for a single record
|
362
|
+
record.reset_log_data
|
363
|
+
|
364
|
+
# for relation
|
365
|
+
User.where(active: true).reset_log_data
|
366
|
+
```
|
367
|
+
|
368
|
+
### Full snapshots
|
369
|
+
|
370
|
+
You can instruct Logidze to create a full snapshot instead of a diff for a particular log entry.
|
371
|
+
|
372
|
+
It could be useful in combination with `.without_logging`: first, you perform multiple updates without logging, then
|
373
|
+
you want to create a log entry with the current state. To do that, you should use the `Logidze.with_full_snapshot` method:
|
374
|
+
|
375
|
+
```ruby
|
376
|
+
record = Model.find(params[:id])
|
377
|
+
|
378
|
+
Logidze.without_logging do
|
379
|
+
# perform multiple write operations with record
|
380
|
+
end
|
381
|
+
|
382
|
+
Logidze.with_full_snapshot do
|
383
|
+
record.touch
|
384
|
+
end
|
385
|
+
```
|
386
|
+
|
387
|
+
### Associations versioning
|
388
|
+
|
389
|
+
Logidze also supports associations versioning. This feature is disabled by default (due to the number of edge cases). You can learn more
|
390
|
+
in the [wiki](https://github.com/palkan/logidze/wiki/Associations-versioning).
|
391
|
+
|
392
|
+
## Dealing with large logs
|
393
|
+
|
394
|
+
By default, Active Record _selects_ all the table columns when no explicit `select` statement specified.
|
395
|
+
|
396
|
+
That could slow down queries execution if you have field values which exceed the size of the data block (typically 8KB). PostgreSQL turns on its [TOAST](https://wiki.postgresql.org/wiki/TOAST) mechanism), which requires reading from multiple physical locations for fetching the row's data.
|
397
|
+
|
398
|
+
If you do not use compaction (`generate logidze:model ... --limit N`) for `log_data`, you're likely to face this problem.
|
399
|
+
|
400
|
+
Logidze provides a way to avoid loading `log_data` by default (and load it on demand):
|
401
|
+
|
402
|
+
```ruby
|
403
|
+
class User < ActiveRecord::Base
|
404
|
+
# Add `ignore_log_data` option to macros
|
405
|
+
has_logidze ignore_log_data: true
|
406
|
+
end
|
407
|
+
```
|
408
|
+
|
409
|
+
If you want Logidze to behave this way by default, configure the global option:
|
410
|
+
|
411
|
+
```ruby
|
412
|
+
# config/initializers/logidze.rb
|
413
|
+
Logidze.ignore_log_data_by_default = true
|
414
|
+
|
415
|
+
# or
|
416
|
+
|
417
|
+
# config/application.rb
|
418
|
+
config.logidze.ignore_log_data_by_default = true
|
419
|
+
```
|
420
|
+
|
421
|
+
However, you can override it by explicitly passing `ignore_log_data: false` to the `ignore_log_data`.
|
422
|
+
You can also enforce loading `log_data` in-place by using the `.with_log_data` scope, e.g. `User.all.with_log_data` loads all
|
423
|
+
the _users_ with `log_data` included.
|
424
|
+
|
425
|
+
The chart below shows the difference in PG query time before and after turning `ignore_log_data` on. (Special thanks to [@aderyabin](https://github.com/aderyabin) for sharing it.)
|
426
|
+
|
427
|
+
![](./assets/pg_log_data_chart.png)
|
428
|
+
|
429
|
+
If you try to call `#log_data` on the model loaded in such way, you'll get `nil`. If you want to fetch log data (e.g., during the console debugging)–use **`user.reload_log_data`**, which forces loading this column from the DB.
|
430
|
+
|
431
|
+
## Handling records deletion
|
432
|
+
|
433
|
+
Unlike, for example, PaperTrail, Logidze is designed to **only track changes**. If the record has been deleted, **everything is lost**.
|
434
|
+
|
435
|
+
If you want to keep changes history after records deletion as well, consider using specialized tools for soft-delete, such as, [Discard](https://github.com/jhawthorn/discard) or [Paranoia](https://github.com/rubysherpas/paranoia).
|
436
|
+
|
437
|
+
See also the discussion: [#61](https://github.com/palkan/logidze/issues/61).
|
438
|
+
|
439
|
+
## Upgrading
|
440
|
+
|
441
|
+
We try to make an upgrade process as simple as possible. For now, the only required action is to create and run a migration:
|
442
|
+
|
443
|
+
```sh
|
444
|
+
rails generate logidze:install --update
|
445
|
+
```
|
446
|
+
|
447
|
+
This updates core `logdize_logger` DB function. No need to update tables or triggers.
|
448
|
+
|
449
|
+
**NOTE:** When using `fx`, you can omit the `--update` flag. The migration containing only the updated functions would be created.
|
450
|
+
|
451
|
+
If you want to update Logidze settings for the model, run migration with `--update` flag:
|
452
|
+
|
453
|
+
```sh
|
454
|
+
rails generate logidze:model Post --update --only=title,body,rating
|
455
|
+
```
|
456
|
+
|
457
|
+
### Upgrading from 0.x to 1.0 (edge)
|
458
|
+
|
459
|
+
#### Schema and migrations
|
460
|
+
|
461
|
+
Most SQL functions definitions has changed without backward compatibility.
|
462
|
+
Perform the following steps to upgrade:
|
463
|
+
|
464
|
+
1. Re-install Logidze: `rails generate logidze:install --update`.
|
465
|
+
|
466
|
+
1. Re-install Logidze triggers **for all models**: `rails generate logidze:model <model> --update`.
|
467
|
+
|
468
|
+
1. Remove the `include Logidze::Migration` line from the old migration files (if any)—this module has been removed.
|
469
|
+
|
470
|
+
Rewrite the migrations to not use the `#current_setting(name)` and `#current_setting_missing_supported?` methods or copy them from the latest [0.x release](https://github.com/palkan/logidze/blob/0-stable/lib/logidze/migration.rb).
|
471
|
+
|
472
|
+
#### API changes
|
473
|
+
|
474
|
+
The deprecated `time` positional argument has been removed from `#at` and `#diff_from` methods. Now you need to use keyword arguments, i.e., `model.at(some_tome) -> model.at(time: some_time)`.
|
475
|
+
|
275
476
|
## Log format
|
276
477
|
|
277
478
|
The `log_data` column has the following format:
|
@@ -298,22 +499,51 @@ The `log_data` column has the following format:
|
|
298
499
|
}
|
299
500
|
```
|
300
501
|
|
301
|
-
If you specify the limit in the trigger definition then log size will not exceed the specified size. When a new change occurs, and there is no more room for it, the two oldest changes will be merged.
|
502
|
+
If you specify the limit in the trigger definition, then log size will not exceed the specified size. When a new change occurs, and there is no more room for it, the two oldest changes will be merged.
|
302
503
|
|
303
|
-
##
|
504
|
+
## Troubleshooting
|
304
505
|
|
305
|
-
|
506
|
+
### `log_data` is nil when using Rails fixtures
|
306
507
|
|
307
|
-
|
508
|
+
Rails fixtures are populated with triggers disabled. Thus, `log_data` is null initially for all records.
|
509
|
+
You can use `#create_logidze_snapshot` manually to build initial snapshots.
|
510
|
+
|
511
|
+
### How to make this work with Apartment 🤔
|
512
|
+
|
513
|
+
First, read [Apartment docs](https://github.com/influitive/apartment#installing-extensions-into-persistent-schemas) on installing PostgreSQL extensions. You need to use the described approach to install Hstore (and drop the migration provided by Logidze during installation).
|
514
|
+
|
515
|
+
Secondly, set `config.use_sql = true` in the Apartment configuration.
|
516
|
+
|
517
|
+
Finally, when using `fx` along with `schema.rb`, you might face a problem with duplicate trigger definitions (for different schemas).
|
518
|
+
Here is a patch to fix this: [dump_triggers.rake](etc/dump_triggers.rake).
|
519
|
+
|
520
|
+
Related issues: [#50](https://github.com/palkan/logidze/issues/50).
|
521
|
+
|
522
|
+
### `PG::UntranslatableCharacter: ERROR`
|
308
523
|
|
309
|
-
|
524
|
+
That could happen when your row data contain null bytes. You should sanitize the data before writing to the database.
|
525
|
+
From the [PostgreSQL docs](https://www.postgresql.org/docs/current/datatype-json.html): `jsonb type also rejects \u0000 (because that cannot be represented in PostgreSQL's text type)`.
|
310
526
|
|
527
|
+
Related issues: [#155](https://github.com/palkan/logidze/issues/155).
|
311
528
|
|
312
|
-
|
529
|
+
### `pg_restore` fails to restore a dump
|
313
530
|
|
314
|
-
-
|
315
|
-
|
531
|
+
First, when restoring data dumps you should consider using `--disable-triggers` option (unless you have a strong reason to invoke the triggers).
|
532
|
+
|
533
|
+
When restoring data dumps for a particular PostgreSQL schema (e.g., when using Apartment), you may encounter the issue with non-existent Logidze functions. That happens because `pg_dump` adds `SELECT pg_catalog.set_config('search_path', '', false);`, and, thus, breaks our existing triggers/functions, because they live either in "public" or in a tenant's namespace (see [this thread](https://postgrespro.com/list/thread-id/2448092)).
|
534
|
+
|
535
|
+
## Development
|
536
|
+
|
537
|
+
We use [Dip](https://github.com/bibendi/dip) for development. Provision the project by running `dip provision` and then use `dip bundle`, `dip rspec` or `dip bash` to interact with a Docker development environment.
|
538
|
+
|
539
|
+
If you prefer developing on your local machine, make user you have Postgres installed and run `./bin/setup`.
|
540
|
+
|
541
|
+
## Contributing
|
542
|
+
|
543
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/palkan/logidze](https://github.com/palkan/logidze).
|
316
544
|
|
317
545
|
## License
|
318
546
|
|
319
547
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
548
|
+
|
549
|
+
[fx]: https://github.com/teoljungberg/fx
|