logidze 0.12.0 → 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 +29 -5
- data/LICENSE.txt +1 -1
- data/README.md +263 -103
- 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 +58 -1
- 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 +49 -13
- 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 +27 -14
- data/lib/logidze/history.rb +1 -10
- data/lib/logidze/ignore_log_data.rb +1 -4
- data/lib/logidze/model.rb +48 -35
- data/lib/logidze/version.rb +1 -1
- metadata +48 -73
- data/.gitattributes +0 -3
- data/.github/ISSUE_TEMPLATE.md +0 -20
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -29
- data/.gitignore +0 -40
- data/.rubocop.yml +0 -55
- data/.travis.yml +0 -46
- data/Gemfile +0 -15
- data/Rakefile +0 -28
- data/assets/pg_log_data_chart.png +0 -0
- data/bench/performance/README.md +0 -109
- data/bench/performance/diff_bench.rb +0 -38
- data/bench/performance/insert_bench.rb +0 -22
- data/bench/performance/memory_profile.rb +0 -56
- data/bench/performance/setup.rb +0 -315
- data/bench/performance/update_bench.rb +0 -38
- 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 -6
- data/gemfiles/rails5.gemfile +0 -6
- data/gemfiles/rails52.gemfile +0 -6
- data/gemfiles/rails6.gemfile +0 -6
- data/gemfiles/railsmaster.gemfile +0 -7
- data/lib/logidze/ignore_log_data/ignored_columns.rb +0 -46
- data/lib/logidze/migration.rb +0 -20
- data/logidze.gemspec +0 -41
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,8 +1,30 @@
|
|
1
|
-
|
2
1
|
# Change log
|
3
2
|
|
4
3
|
## master (unreleased)
|
5
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
|
+
|
6
28
|
## 0.12.0 (2020-01-02)
|
7
29
|
|
8
30
|
- PR [#143](https://github.com/palkan/logidze/pull/143) Add `:transactional` option to `#with_meta` and `#with_responsible` ([@oleg-kiviljov][])
|
@@ -16,7 +38,9 @@ Logidze.with_meta({ip: request.ip}, transactional: false) do
|
|
16
38
|
post.save!
|
17
39
|
end
|
18
40
|
```
|
41
|
+
|
19
42
|
or
|
43
|
+
|
20
44
|
```ruby
|
21
45
|
Logidze.with_responsible(user.id, transactional: false) do
|
22
46
|
post.save!
|
@@ -122,7 +146,7 @@ Please run `rails generate logidze:install --update` to regenerate stored functi
|
|
122
146
|
This feature checks if several logs came in within a debounce time period then only keep the latest one
|
123
147
|
by merging the latest in previous others.
|
124
148
|
|
125
|
-
The concept is similar to https://underscorejs.org/#debounce
|
149
|
+
The concept is similar to [https://underscorejs.org/#debounce](https://underscorejs.org/#debounce).
|
126
150
|
|
127
151
|
without `debounce_time`
|
128
152
|
|
@@ -183,7 +207,7 @@ with `debounce_time` of `10ms`
|
|
183
207
|
|
184
208
|
## 0.7.0 (2018-08-29)
|
185
209
|
|
186
|
-
- [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][])
|
187
211
|
|
188
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][])
|
189
213
|
|
@@ -251,7 +275,7 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
|
|
251
275
|
|
252
276
|
## 0.5.1 (2017-06-15)
|
253
277
|
|
254
|
-
- _(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][])
|
255
279
|
|
256
280
|
## 0.5.0 (2017-03-28)
|
257
281
|
|
@@ -301,7 +325,7 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
|
|
301
325
|
[@charlie-wasp]: https://github.com/charlie-wasp
|
302
326
|
[@akxcv]: https://github.com/akxcv
|
303
327
|
[@vassilevsky]: https://github.com/vassilevsky
|
304
|
-
[@
|
328
|
+
[@amalagaura]: https://github.com/amalagaura
|
305
329
|
[@dmitrytsepelev]: https://github.com/DmitryTsepelev
|
306
330
|
[@zocoi]: https://github.com/zocoi
|
307
331
|
[@duderman]: https://github.com/duderman
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,37 +1,68 @@
|
|
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
18
|
|
17
|
-
- Ruby ~> 2.
|
18
|
-
- Rails >=
|
19
|
+
- Ruby ~> 2.5
|
20
|
+
- Rails >= 5.0 (for Rails 4.2 use version <=0.12.0)
|
19
21
|
|
20
22
|
<a href="https://evilmartians.com/">
|
21
23
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
22
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
|
+
|
23
54
|
## Installation
|
24
55
|
|
25
56
|
Add Logidze to your application's Gemfile:
|
26
57
|
|
27
58
|
```ruby
|
28
|
-
gem "logidze"
|
59
|
+
gem "logidze", "1.0.0.rc1"
|
29
60
|
```
|
30
61
|
|
31
62
|
Install required DB extensions and create trigger function:
|
32
63
|
|
33
64
|
```sh
|
34
|
-
rails generate logidze:install
|
65
|
+
bundle exec rails generate logidze:install
|
35
66
|
```
|
36
67
|
|
37
68
|
This creates a migration for adding trigger function and enabling the hstore extension.
|
@@ -39,95 +70,119 @@ This creates a migration for adding trigger function and enabling the hstore ext
|
|
39
70
|
Run migrations:
|
40
71
|
|
41
72
|
```sh
|
42
|
-
|
73
|
+
bundle exec rails db:migrate
|
43
74
|
```
|
44
75
|
|
45
|
-
**NOTE:** you
|
76
|
+
**NOTE:** Logidze uses DB functions and triggers, hence you need to use SQL format for a schema dump:
|
46
77
|
|
47
78
|
```ruby
|
48
79
|
# application.rb
|
49
80
|
config.active_record.schema_format = :sql
|
50
81
|
```
|
51
82
|
|
52
|
-
|
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:
|
53
96
|
|
54
97
|
```sh
|
55
|
-
rails generate logidze:model Post
|
56
|
-
|
98
|
+
bundle exec rails generate logidze:model Post
|
99
|
+
bundle exec rails db:migrate
|
57
100
|
```
|
58
101
|
|
59
102
|
This also adds `has_logidze` line to your model, which adds methods for working with logs.
|
60
103
|
|
61
|
-
|
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:
|
62
105
|
|
63
106
|
```sh
|
64
|
-
rails generate logidze:model Post --
|
107
|
+
bundle exec rails generate logidze:model Post --path "app/models/custom/post.rb"
|
65
108
|
```
|
66
109
|
|
67
|
-
|
110
|
+
### Backfill data
|
111
|
+
|
112
|
+
To backfill table data (i.e., create initial snapshots) add `backfill` option to the generator:
|
68
113
|
|
69
114
|
```sh
|
70
|
-
rails generate logidze:model Post --backfill
|
115
|
+
bundle exec rails generate logidze:model Post --backfill
|
71
116
|
```
|
72
117
|
|
73
|
-
|
118
|
+
Now your migration should contain and `UPDATE ...` statement to populate the `log_data` column with the current state.
|
74
119
|
|
75
|
-
|
76
|
-
# track all columns, except `created_at` and `active`
|
77
|
-
rails generate logidze:model Post --blacklist=created_at,active
|
78
|
-
# track only `title` and `body` columns
|
79
|
-
rails generate logidze:model Post --whitelist=title,body
|
80
|
-
```
|
120
|
+
Otherwise a full snapshot will be created the first time the record is updated.
|
81
121
|
|
82
|
-
|
122
|
+
You can create a snapshot manually by performing the following query:
|
83
123
|
|
84
|
-
```
|
85
|
-
|
124
|
+
```sql
|
125
|
+
UPDATE <my_table> as t
|
126
|
+
SET log_data = logidze_snapshot(to_jsonb(t))
|
86
127
|
```
|
87
128
|
|
88
|
-
|
89
|
-
your model does not have that column, Logidze will gracefully fall back to `statement_timestamp()`.
|
90
|
-
To change the column name or disable this feature completely, you can use the `timestamp_column` option:
|
129
|
+
Or by using the following methods:
|
91
130
|
|
92
|
-
```
|
93
|
-
|
94
|
-
rails generate logidze:model Post --timestamp_column time
|
95
|
-
# will always set version timestamp to `statement_timestamp()`
|
96
|
-
rails generate logidze:model Post --timestamp_column nil # "null" and "false" will also work
|
97
|
-
```
|
131
|
+
```ruby
|
132
|
+
Model.create_logidze_snapshot
|
98
133
|
|
99
|
-
|
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)
|
100
136
|
|
101
|
-
|
102
|
-
|
103
|
-
|
137
|
+
# filter columns
|
138
|
+
Model.create_logidze_snapshot(only: %(name))
|
139
|
+
Model.create_logidze_snapshot(except: %(password))
|
104
140
|
|
105
|
-
|
106
|
-
in the [wiki](https://github.com/palkan/logidze/wiki/Associations-versioning).
|
141
|
+
# or call a similar method (but with !) on a record
|
107
142
|
|
108
|
-
|
143
|
+
my_model = Model.find(params[:id])
|
144
|
+
my_model.create_logidze_snapshot!(timestamp: :created_at)
|
145
|
+
```
|
109
146
|
|
110
|
-
|
111
|
-
Logidze requires at least database owner privileges (which is not always possible).
|
147
|
+
A snapshot is only created if `log_data` is null.
|
112
148
|
|
113
|
-
|
149
|
+
### Log size limits
|
114
150
|
|
115
|
-
|
151
|
+
You can provide the `limit` option to `generate` to limit the size of the log (by default it's unlimited):
|
116
152
|
|
117
|
-
|
153
|
+
```sh
|
154
|
+
bundle exec rails generate logidze:model Post --limit=10
|
155
|
+
```
|
118
156
|
|
119
|
-
|
157
|
+
### Tracking only selected columns
|
120
158
|
|
121
|
-
|
159
|
+
You can log only particular columns changes. There are mutually exclusive `except` and `only` options for this:
|
122
160
|
|
123
161
|
```sh
|
124
|
-
|
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
|
125
166
|
```
|
126
167
|
|
127
|
-
|
168
|
+
### Logs timestamps
|
169
|
+
|
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()`.
|
172
|
+
|
173
|
+
To change the column name or disable this feature completely, you can use the `timestamp_column` option:
|
174
|
+
|
175
|
+
```sh
|
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
|
180
|
+
```
|
128
181
|
|
129
182
|
## Usage
|
130
183
|
|
184
|
+
### Basic API
|
185
|
+
|
131
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:
|
@@ -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
|
@@ -210,46 +270,7 @@ Alternatively, you can configure Logidze always to default to `append: true`.
|
|
210
270
|
Logidze.append_on_undo = true
|
211
271
|
```
|
212
272
|
|
213
|
-
###
|
214
|
-
|
215
|
-
By default, Active Record _selects_ all the table columns when no explicit `select` statement specified.
|
216
|
-
|
217
|
-
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.
|
218
|
-
|
219
|
-
If you do not use compaction (`generate logidze:model ... --limit N`) for `log_data`, you're likely to face this problem.
|
220
|
-
|
221
|
-
Logidze provides a way to avoid loading `log_data` by default (and load it on demand):
|
222
|
-
|
223
|
-
```ruby
|
224
|
-
class User < ActiveRecord::Base
|
225
|
-
# Add `ignore_log_data` option to macros
|
226
|
-
has_logidze ignore_log_data: true
|
227
|
-
end
|
228
|
-
```
|
229
|
-
|
230
|
-
If you want Logidze to behave this way by default, configure the global option:
|
231
|
-
|
232
|
-
```ruby
|
233
|
-
# config/initializers/logidze.rb
|
234
|
-
Logidze.ignore_log_data_by_default = true
|
235
|
-
|
236
|
-
# or
|
237
|
-
|
238
|
-
# config/application.rb
|
239
|
-
config.logidze.ignore_log_data_by_default = true
|
240
|
-
```
|
241
|
-
|
242
|
-
However, you can override it by explicitly passing `ignore_log_data: false` to the `ignore_log_data`.
|
243
|
-
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
|
244
|
-
the _users_ with `log_data` included.
|
245
|
-
|
246
|
-
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.)
|
247
|
-
|
248
|
-
![](./assets/pg_log_data_chart.png)
|
249
|
-
|
250
|
-
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.
|
251
|
-
|
252
|
-
## Track meta information
|
273
|
+
### Track meta information
|
253
274
|
|
254
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:
|
255
276
|
|
@@ -269,7 +290,7 @@ Logidze.with_meta({ip: request.ip}, transactional: false) do
|
|
269
290
|
end
|
270
291
|
```
|
271
292
|
|
272
|
-
|
293
|
+
### Track responsibility
|
273
294
|
|
274
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.
|
275
296
|
|
@@ -319,7 +340,8 @@ Logidze.with_responsible(user.id, transactional: false) do
|
|
319
340
|
post.save!
|
320
341
|
end
|
321
342
|
```
|
322
|
-
|
343
|
+
|
344
|
+
### Disable logging temporary
|
323
345
|
|
324
346
|
If you want to make update without logging (e.g., mass update), you can turn it off the following way:
|
325
347
|
|
@@ -331,7 +353,7 @@ Logidze.without_logging { Post.update_all(seen: true) }
|
|
331
353
|
Post.without_logging { Post.update_all(seen: true) }
|
332
354
|
```
|
333
355
|
|
334
|
-
|
356
|
+
### Reset log
|
335
357
|
|
336
358
|
Reset the history for a record (or records):
|
337
359
|
|
@@ -343,6 +365,114 @@ record.reset_log_data
|
|
343
365
|
User.where(active: true).reset_log_data
|
344
366
|
```
|
345
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
|
+
|
346
476
|
## Log format
|
347
477
|
|
348
478
|
The `log_data` column has the following format:
|
@@ -371,19 +501,49 @@ The `log_data` column has the following format:
|
|
371
501
|
|
372
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.
|
373
503
|
|
374
|
-
##
|
504
|
+
## Troubleshooting
|
375
505
|
|
376
|
-
|
506
|
+
### `log_data` is nil when using Rails fixtures
|
377
507
|
|
378
|
-
|
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).
|
379
521
|
|
380
|
-
|
522
|
+
### `PG::UntranslatableCharacter: ERROR`
|
381
523
|
|
382
|
-
|
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)`.
|
383
526
|
|
384
|
-
|
385
|
-
|
527
|
+
Related issues: [#155](https://github.com/palkan/logidze/issues/155).
|
528
|
+
|
529
|
+
### `pg_restore` fails to restore a dump
|
530
|
+
|
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).
|
386
544
|
|
387
545
|
## License
|
388
546
|
|
389
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
|