logidze 0.10.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +81 -4
  3. data/LICENSE.txt +1 -1
  4. data/README.md +291 -106
  5. data/lib/generators/logidze/fx_helper.rb +17 -0
  6. data/lib/generators/logidze/inject_sql.rb +18 -0
  7. data/lib/generators/logidze/install/USAGE +6 -1
  8. data/lib/generators/logidze/install/functions/logidze_compact_history.sql +38 -0
  9. data/lib/generators/logidze/install/functions/logidze_filter_keys.sql +27 -0
  10. data/lib/generators/logidze/install/functions/logidze_logger.sql +150 -0
  11. data/lib/generators/logidze/install/functions/logidze_snapshot.sql +32 -0
  12. data/lib/generators/logidze/install/functions/logidze_version.sql +20 -0
  13. data/lib/generators/logidze/install/install_generator.rb +43 -1
  14. data/lib/generators/logidze/install/templates/hstore.rb.erb +1 -1
  15. data/lib/generators/logidze/install/templates/migration.rb.erb +19 -232
  16. data/lib/generators/logidze/install/templates/migration_fx.rb.erb +41 -0
  17. data/lib/generators/logidze/model/model_generator.rb +53 -13
  18. data/lib/generators/logidze/model/templates/migration.rb.erb +57 -36
  19. data/lib/generators/logidze/model/triggers/logidze.sql +6 -0
  20. data/lib/logidze.rb +37 -31
  21. data/lib/logidze/engine.rb +11 -0
  22. data/lib/logidze/has_logidze.rb +5 -7
  23. data/lib/logidze/history.rb +1 -10
  24. data/lib/logidze/ignore_log_data.rb +5 -40
  25. data/lib/logidze/meta.rb +43 -16
  26. data/lib/logidze/model.rb +50 -43
  27. data/lib/logidze/utils/check_pending.rb +57 -0
  28. data/lib/logidze/utils/function_definitions.rb +49 -0
  29. data/lib/logidze/utils/pending_migration_error.rb +25 -0
  30. data/lib/logidze/version.rb +1 -1
  31. metadata +65 -76
  32. data/.gitattributes +0 -3
  33. data/.github/ISSUE_TEMPLATE.md +0 -20
  34. data/.github/PULL_REQUEST_TEMPLATE.md +0 -29
  35. data/.gitignore +0 -40
  36. data/.rubocop.yml +0 -55
  37. data/.travis.yml +0 -42
  38. data/Gemfile +0 -15
  39. data/Rakefile +0 -28
  40. data/assets/pg_log_data_chart.png +0 -0
  41. data/bench/performance/README.md +0 -109
  42. data/bench/performance/diff_bench.rb +0 -38
  43. data/bench/performance/insert_bench.rb +0 -22
  44. data/bench/performance/memory_profile.rb +0 -56
  45. data/bench/performance/setup.rb +0 -315
  46. data/bench/performance/update_bench.rb +0 -38
  47. data/bench/triggers/Makefile +0 -56
  48. data/bench/triggers/Readme.md +0 -58
  49. data/bench/triggers/bench.sql +0 -6
  50. data/bench/triggers/hstore_trigger_setup.sql +0 -38
  51. data/bench/triggers/jsonb_minus_2_setup.sql +0 -47
  52. data/bench/triggers/jsonb_minus_setup.sql +0 -49
  53. data/bench/triggers/keys2_trigger_setup.sql +0 -44
  54. data/bench/triggers/keys_trigger_setup.sql +0 -50
  55. data/bin/console +0 -8
  56. data/bin/setup +0 -9
  57. data/gemfiles/rails42.gemfile +0 -6
  58. data/gemfiles/rails5.gemfile +0 -6
  59. data/gemfiles/rails52.gemfile +0 -6
  60. data/gemfiles/rails6.gemfile +0 -6
  61. data/gemfiles/railsmaster.gemfile +0 -7
  62. data/lib/logidze/ignore_log_data/association.rb +0 -11
  63. data/lib/logidze/ignore_log_data/default_scope_patch.rb +0 -25
  64. data/lib/logidze/ignore_log_data/ignored_columns.rb +0 -46
  65. data/lib/logidze/ignore_log_data/missing_attribute_patch.rb +0 -18
  66. data/lib/logidze/migration.rb +0 -20
  67. data/logidze.gemspec +0 -41
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 56aa14f81cc160897376ed2b226664fc3a93c5f4becedb7d5a45abd197d03388
4
- data.tar.gz: 5e196a2ef847e0e1d6069e793fb726f35166f05be007d9c4e4cfde2318b2cd3a
3
+ metadata.gz: e6c441d5e6e60ef695eb354e4bfb5be9fa182bd8b70755d829aee6eb2781e401
4
+ data.tar.gz: d64374a0d6327f2f713fd7341c31de93472f32d045cb5c825c35874c756307d4
5
5
  SHA512:
6
- metadata.gz: f8871956c3f1439fdd5cc3bb810e612a75145a49cf7f43a039b92915a2edc2ab57bb49a7bd5a4a14a48863696f37d8afcce4f052b2c620b01ffd5a179398fc30
7
- data.tar.gz: 15440ace63434523644340155bd26bdb55acb906993b4b6014d1dd8d0f0e498d4e34709bc144bb3da260c8e509f336dc68c9100cc37568d3354d025a9d88cf39
6
+ metadata.gz: f3f453e410de263ed8b0704aece35e6274dfc19c9074a8f75450e84033318b149ae81c9e1f48cc9d515e523b95c98c14cbd3453a7c41db1690febfe8060f640d
7
+ data.tar.gz: dbf4a22b357889bc0d2aaeda52bffc8bc245cdc12d2cb34d0f7c9ccec7c0f60807ecc3cbd5272fee567420024309f8be05f6e1e8c9f45334ab3d2ad155a97a12
data/CHANGELOG.md CHANGED
@@ -2,6 +2,81 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 1.1.0 (2021-03-31)
6
+
7
+ - Add pending upgrade checks [Experimental]. ([@skryukov][])
8
+
9
+ Now Logidze can check for a pending upgrade. Use `Logidze.pending_upgrade = :warn` to be notified by warning, or `Logidze.pending_upgrade = :error` if you want Logidze to raise an error.
10
+
11
+ - [Fixes [#171](https://github.com/palkan/logidze/issues/171)] Stringify jsonb column values within snapshots. ([@skryukov][])
12
+
13
+ - [Fixes [#175](https://github.com/palkan/logidze/issues/175)] Set dynamic ActiveRecord version for migrations. ([@skryukov][])
14
+
15
+ - [Fixes [#184](https://github.com/palkan/logidze/issues/184)] Remove Rails meta-gem dependency ([@bf4][])
16
+
17
+ ## 1.0.0 (2020-11-09)
18
+
19
+ - Add `--name` option to model generator to specify the migration name. ([@palkan][])
20
+
21
+ When you update Logidze installation for a model multiple times, you might hit the `DuplicateMigrationNameError` (see [#167](https://github.com/palkan/logidze/issues/167)).
22
+
23
+ - Add `.with_full_snapshot` to add full snapshots to the log instead of diffs. ([@palkan][])
24
+
25
+ Useful in combination with `.without_logging`: first, you perform multiple updates without logging, then
26
+ you do something like `with_full_snapshot { record.touch }` to create a log entry with the current state.
27
+
28
+ - Add `#create_logidze_snapshot!` and `.create_logidze_snapshot` methods. ([@palkan][])
29
+
30
+ - Add integration with `fx` gem. ([@palkan][])
31
+
32
+ Now it's possible to use Logidze with `schema.rb`. Add `fx` gem to the project, and new migrations will be
33
+ using Fx `create_function` / `create_trigger` functions.
34
+
35
+ - Refactored columns filtering. ([@palkan][])
36
+
37
+ Renamed `--whitelist/--blacklist` to `--only/--except` correspondingly.
38
+
39
+ The _only_-logic has been changed: previously we collected the list of columns to ignore at the migration generation time,
40
+ now we filter the columns within the trigger function (thus, schema changes do not affect the columns being tracked).
41
+
42
+ - **Dropped support for Rails 4.2, Ruby 2.4 and PostgreSQL 9.5**. ([@palkan][])
43
+
44
+ ## 0.12.0 (2020-01-02)
45
+
46
+ - PR [#143](https://github.com/palkan/logidze/pull/143) Add `:transactional` option to `#with_meta` and `#with_responsible` ([@oleg-kiviljov][])
47
+
48
+ 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`.
49
+
50
+ Usage:
51
+
52
+ ```ruby
53
+ Logidze.with_meta({ip: request.ip}, transactional: false) do
54
+ post.save!
55
+ end
56
+ ```
57
+
58
+ or
59
+
60
+ ```ruby
61
+ Logidze.with_responsible(user.id, transactional: false) do
62
+ post.save!
63
+ end
64
+ ```
65
+
66
+ ## 0.11.0 (2019-08-15)
67
+
68
+ - **Breaking** Return `nil` when `log_data` is not loaded instead of raising an exception. ([@palkan][])
69
+
70
+ We cannot distinguish between not loaded `log_data` and not-yet-created (i.e. for new records).
71
+ The latter could be used in frameworks/gems ([example](https://github.com/palkan/logidze/issues/127#issuecomment-518798640)).
72
+
73
+ - **Breaking** Only allow specifying `ignore_log_data` at boot time without runtime modifications. ([@palkan][])
74
+
75
+ Playing with ActiveRecord default scopes wasn't a good idea. We fallback to a more explicit way of _telling_ AR
76
+ when to load or ignore the `log_data` column.
77
+
78
+ This change removes `Logidze.with_log_data` method.
79
+
5
80
  ## 0.10.0 (2019-05-15)
6
81
 
7
82
  - **Ruby >= 2.4 is required**
@@ -87,7 +162,7 @@ Please run `rails generate logidze:install --update` to regenerate stored functi
87
162
  This feature checks if several logs came in within a debounce time period then only keep the latest one
88
163
  by merging the latest in previous others.
89
164
 
90
- The concept is similar to https://underscorejs.org/#debounce
165
+ The concept is similar to [https://underscorejs.org/#debounce](https://underscorejs.org/#debounce).
91
166
 
92
167
  without `debounce_time`
93
168
 
@@ -148,7 +223,7 @@ with `debounce_time` of `10ms`
148
223
 
149
224
  ## 0.7.0 (2018-08-29)
150
225
 
151
- - [Fixes [#75](https://github.com/palkan/logidze/issues/70)] Fix association versioning with an optional belongs to ([@ankursethi-uscis][])
226
+ - [Fixes [#75](https://github.com/palkan/logidze/issues/70)] Fix association versioning with an optional belongs to ([@amalagaura][])
152
227
 
153
228
  - [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][])
154
229
 
@@ -216,7 +291,7 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
216
291
 
217
292
  ## 0.5.1 (2017-06-15)
218
293
 
219
- - _(Fix)_ Drop _all_ created functions upon rolling back (https://github.com/palkan/logidze/commit/b8e150cc18b3316a8cf0c78f7117339481fb49c6). ([@vassilevsky][])
294
+ - _(Fix)_ Drop _all_ created functions upon rolling back ([commit](https://github.com/palkan/logidze/commit/b8e150cc18b3316a8cf0c78f7117339481fb49c6)). ([@vassilevsky][])
220
295
 
221
296
  ## 0.5.0 (2017-03-28)
222
297
 
@@ -266,7 +341,9 @@ This is a quick fix for a more general problem (see [#59](https://github.com/pal
266
341
  [@charlie-wasp]: https://github.com/charlie-wasp
267
342
  [@akxcv]: https://github.com/akxcv
268
343
  [@vassilevsky]: https://github.com/vassilevsky
269
- [@ankursethi-uscis]: https://github.com/ankursethi-uscis
344
+ [@amalagaura]: https://github.com/amalagaura
270
345
  [@dmitrytsepelev]: https://github.com/DmitryTsepelev
271
346
  [@zocoi]: https://github.com/zocoi
272
347
  [@duderman]: https://github.com/duderman
348
+ [@oleg-kiviljov]: https://github.com/oleg-kiviljov
349
+ [@skryukov]: https://github.com/skryukov
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016-2019 palkan
3
+ Copyright (c) 2016-2020 Vladimir Dementyev
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,36 +1,66 @@
1
- [![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](http://cultofmartians.com) [![Gem Version](https://badge.fury.io/rb/logidze.svg)](https://rubygems.org/gems/logidze) [![Build Status](https://travis-ci.org/palkan/logidze.svg?branch=master)](https://travis-ci.org/palkan/logidze) [![Open Source Helpers](https://www.codetriage.com/palkan/logidze/badges/users.svg)](https://www.codetriage.com/palkan/logidze)
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)](https://github.com/palkan/logidze/actions)
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
- [Read the story behind Logidze](https://evilmartians.com/chronicles/introducing-logidze?utm_source=logidze)
12
-
13
- [How is Logidze pronounced?](https://github.com/palkan/logidze/issues/73)
13
+ 🤔 [How is Logidze pronounced?](https://github.com/palkan/logidze/issues/73)
14
14
 
15
15
  Other requirements:
16
- - Ruby ~> 2.1
17
- - Rails >= 4.2 (**Rails 6 is supported**)
16
+
17
+ - Ruby ~> 2.5
18
+ - Rails >= 5.0 (for Rails 4.2 use version <=0.12.0)
18
19
 
19
20
  <a href="https://evilmartians.com/">
20
21
  <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
21
22
 
23
+ ## Links
24
+
25
+ - [Logidze 1.0: Active Record, Postgres, Rails, and time travel](https://evilmartians.com/chronicles/logidze-1-0-active-record-postgresql-rails-and-time-travel?utm_source=logidze)
26
+ - [Logidze: for all those tired of versioning data](https://evilmartians.com/chronicles/introducing-logidze?utm_source=logidze)
27
+
28
+ ## Table of contents
29
+
30
+ - [Installation & Configuration](#installation)
31
+ - [Using with schema.rb](#using-with-schemarb)
32
+ - [Configuring models](#configuring-models)
33
+ - [Backfill data](#backfill-data)
34
+ - [Log size limits](#log-size-limits)
35
+ - [Tracking only selected columns](#tracking-only-selected-columns)
36
+ - [Logs timestamps](#logs-timestamps)
37
+ - [Usage](#usage)
38
+ - [Basic API](#basic-api)
39
+ - [Track meta information](#track-meta-information)
40
+ - [Track responsibility](#track-responsibility)
41
+ - [Disable logging temporary](#disable-logging-temporary)
42
+ - [Reset log](#reset-log)
43
+ - [Creating full snapshot instead of diffs](#full-snapshots)
44
+ - [Associations versioning](#associations-versioning)
45
+ - [Dealing with large logs](#dealing-with-large-logs)
46
+ - [Handling records deletion](#handling-records-deletion)
47
+ - [Upgrading](#upgrading)
48
+ - [Log format](#log-format)
49
+ - [Troubleshooting 🚨](#troubleshooting)
50
+ - [Development](#development)
51
+
22
52
  ## Installation
23
53
 
24
54
  Add Logidze to your application's Gemfile:
25
55
 
26
56
  ```ruby
27
- gem "logidze"
57
+ gem "logidze", "~> 1.0.0"
28
58
  ```
29
59
 
30
60
  Install required DB extensions and create trigger function:
31
61
 
32
62
  ```sh
33
- rails generate logidze:install
63
+ bundle exec rails generate logidze:install
34
64
  ```
35
65
 
36
66
  This creates a migration for adding trigger function and enabling the hstore extension.
@@ -38,96 +68,119 @@ This creates a migration for adding trigger function and enabling the hstore ext
38
68
  Run migrations:
39
69
 
40
70
  ```sh
41
- rake db:migrate
71
+ bundle exec rails db:migrate
42
72
  ```
43
73
 
44
- **NOTE:** you **must** use SQL schema format since Logidze uses DB functions and triggers:
74
+ **NOTE:** Logidze uses DB functions and triggers, hence you need to use SQL format for a schema dump:
45
75
 
46
76
  ```ruby
47
77
  # application.rb
48
78
  config.active_record.schema_format = :sql
49
79
  ```
50
80
 
51
- 3. Add log column and triggers to the model:
81
+ ### Using with schema.rb
82
+
83
+ Logidze seamlessly integrates with [fx][] gem to make it possible to continue using `schema.rb` for the database schema dump.
84
+
85
+ Add `fx` gem to your Gemfile and run the same Logidze generators: `rails g logidze:install` or `rails g logidze:model`.
86
+
87
+ 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.
88
+
89
+ On the other hand, if you have `fx` gem but don't want Logidze to use it—pass `--no-fx` option.
90
+
91
+ ### Configuring models
92
+
93
+ Run the following migration to enable changes tracking for an Active Record model and adding a `log_data::jsonb` column to the table:
52
94
 
53
95
  ```sh
54
- rails generate logidze:model Post
55
- rake db:migrate
96
+ bundle exec rails generate logidze:model Post
97
+ bundle exec rails db:migrate
56
98
  ```
57
99
 
58
100
  This also adds `has_logidze` line to your model, which adds methods for working with logs.
59
101
 
60
- You can provide the `limit` option to `generate` to limit the size of the log (by default it's unlimited):
102
+ 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
103
 
62
104
  ```sh
63
- rails generate logidze:model Post --limit=10
105
+ bundle exec rails generate logidze:model Post --path "app/models/custom/post.rb"
64
106
  ```
65
107
 
66
- To backfill table data (i.e., create initial snapshots) add `backfill` option:
108
+ ### Backfill data
109
+
110
+ To backfill table data (i.e., create initial snapshots) add `backfill` option to the generator:
67
111
 
68
112
  ```sh
69
- rails generate logidze:model Post --backfill
113
+ bundle exec rails generate logidze:model Post --backfill
70
114
  ```
71
115
 
72
- You can log only particular columns changes. There are mutually exclusive `blacklist` and `whitelist` options for this:
116
+ Now your migration should contain and `UPDATE ...` statement to populate the `log_data` column with the current state.
73
117
 
74
- ```sh
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
- ```
118
+ Otherwise a full snapshot will be created the first time the record is updated.
80
119
 
81
- 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:
120
+ You can create a snapshot manually by performing the following query:
82
121
 
83
- ```sh
84
- rails generate logidze:model Post --path "app/models/custom/post.rb"
122
+ ```sql
123
+ UPDATE <my_table> as t
124
+ SET log_data = logidze_snapshot(to_jsonb(t))
85
125
  ```
86
126
 
87
- By default, Logidze tries to get a timestamp for a version from record's `updated_at` field whenever appropriate. If
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:
127
+ Or by using the following methods:
90
128
 
91
- ```sh
92
- # will try to get the timestamp value from `time` column
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
- ```
129
+ ```ruby
130
+ Model.create_logidze_snapshot
97
131
 
98
- If you want to update Logidze settings for the model, run migration with `--update` flag:
132
+ # specify the timestamp column to use for the initial version (by default the current time is used)
133
+ Model.create_logidze_snapshot(timestamp: :created_at)
99
134
 
100
- ```sh
101
- rails generate logidze:model Post --update --whitelist=title body rating
135
+ # filter columns
136
+ Model.create_logidze_snapshot(only: %(name))
137
+ Model.create_logidze_snapshot(except: %(password))
138
+
139
+ # or call a similar method (but with !) on a record
140
+
141
+ my_model = Model.find(params[:id])
142
+ my_model.create_logidze_snapshot!(timestamp: :created_at)
102
143
  ```
103
144
 
104
- Logidze also supports associations versioning. It is an experimental feature and disabled by default. You can learn more
105
- in the [wiki](https://github.com/palkan/logidze/wiki/Associations-versioning).
145
+ A snapshot is only created if `log_data` is null.
106
146
 
107
- ## Troubleshooting
147
+ ### Log size limits
108
148
 
109
- The most common problem is `"permission denied to set parameter "logidze.xxx"` caused by `ALTER DATABASE ...` query.
110
- Logidze requires at least database owner privileges (which is not always possible).
149
+ You can provide the `limit` option to `generate` to limit the size of the log (by default it's unlimited):
111
150
 
112
- Here is a quick and straightforward [workaround](https://github.com/palkan/logidze/issues/11#issuecomment-260703464) by [@nobodyzzz](https://github.com/nobodyzzz).
151
+ ```sh
152
+ bundle exec rails generate logidze:model Post --limit=10
153
+ ```
113
154
 
114
- **NOTE**: if you're using PostgreSQL >= 9.6 you need neither the workaround nor owner privileges because Logidze (>= 0.3.1) can work without `ALTER DATABASE ...`.
155
+ ### Tracking only selected columns
115
156
 
116
- Nevertheless, you still need super-user privileges to enable `hstore` extension (or you can use [PostgreSQL Extension Whitelisting](https://github.com/dimitri/pgextwlist)).
157
+ You can log only particular columns changes. There are mutually exclusive `except` and `only` options for this:
117
158
 
159
+ ```sh
160
+ # track all columns, except `created_at` and `active`
161
+ bundle exec rails generate logidze:model Post --except=created_at,active
162
+ # track only `title` and `body` columns
163
+ bundle exec rails generate logidze:model Post --only=title,body
164
+ ```
118
165
 
119
- ## Upgrade from previous versions
166
+ ### Logs timestamps
120
167
 
121
- We try to make an upgrade process as simple as possible. For now, the only required action is to create and run a migration:
168
+ By default, Logidze tries to get a timestamp for a version from record's `updated_at` field whenever appropriate. If
169
+ your model does not have that column, Logidze will gracefully fall back to `statement_timestamp()`.
170
+
171
+ To change the column name or disable this feature completely, you can use the `timestamp_column` option:
122
172
 
123
173
  ```sh
124
- rails generate logidze:install --update
174
+ # will try to get the timestamp value from `time` column
175
+ bundle exec rails generate logidze:model Post --timestamp_column time
176
+ # will always set version timestamp to `statement_timestamp()`
177
+ bundle exec rails generate logidze:model Post --timestamp_column nil # "null" and "false" will also work
125
178
  ```
126
179
 
127
- This updates core `logdize_logger` DB function. No need to update tables or triggers.
128
-
129
180
  ## Usage
130
181
 
182
+ ### Basic API
183
+
131
184
  Your model now has `log_data` column, which stores changes log.
132
185
 
133
186
  To retrieve record version at a given time use `#at` or `#at!` methods:
@@ -157,6 +210,9 @@ You can also get revision by version number:
157
210
  post.at(version: 2)
158
211
  ```
159
212
 
213
+ **NOTE:** If `log_data` is nil, `#at(time:)` returns self and `#at(version:)` returns `nil`.
214
+ You can opt-in to return `nil` for time-based `#at` as well by setting `Logidze.return_self_if_log_data_is_empty = false`.
215
+
160
216
  It is also possible to get version for relations:
161
217
 
162
218
  ```ruby
@@ -173,6 +229,8 @@ post.diff_from(time: 1.hour.ago)
173
229
  Post.where(created_at: Time.zone.today.all_day).diff_from(time: 1.hour.ago)
174
230
  ```
175
231
 
232
+ **NOTE:** If `log_data` is nil, `#diff_from` returns an empty Hash as `"changes"`.
233
+
176
234
  There are also `#undo!` and `#redo!` options (and more general `#switch_to!`):
177
235
 
178
236
  ```ruby
@@ -198,8 +256,8 @@ create a new version with old data. Caveat: when switching to a newer version, `
198
256
 
199
257
  ```ruby
200
258
  post = Post.create!(title: "first post") # v1
201
- post.update!(title: "new title") # v2
202
- post.undo!(append: true) # v3 (with same attributes as v1)
259
+ post.update!(title: "new title") # v2
260
+ post.undo!(append: true) # v3 (with same attributes as v1)
203
261
  ```
204
262
 
205
263
  Note that `redo!` will not work after `undo!(append: true)` because the latter will create a new version
@@ -210,60 +268,27 @@ Alternatively, you can configure Logidze always to default to `append: true`.
210
268
  Logidze.append_on_undo = true
211
269
  ```
212
270
 
213
- ### How to not load log data by default, or dealing with large logs
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 always to behave this way - you can set up a global configuration option:
231
-
232
- ```ruby
233
- Rails.application.config.logidze.ignore_log_data_by_default = true
234
- ```
271
+ ### Track meta information
235
272
 
236
- However, you can override it by explicitly passing `ignore_log_data: false` to the `ignore_log_data`. Also, it's possible to change it temporary inside the block:
273
+ 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:
237
274
 
238
275
  ```ruby
239
- Logidze.with_log_data do
240
- Post.find(params[:id]).log_data
276
+ Logidze.with_meta(ip: request.ip) do
277
+ post.save!
241
278
  end
242
279
  ```
243
280
 
244
- When `ignore_log_data` is turned on, each time you use `User.all` (or any other Relation method) `log_data` won't be loaded from the DB.
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 `ActiveModel::MissingAttributeError`, but if you really need it (e.g., during the console debugging) - use **`user.reload_log_data`**, which forces loading the column from the DB.
251
-
252
- If you need to select `log_data` during the initial load-use a special scope `User.with_log_data`.
253
-
254
- ## Track meta information
281
+ Meta expects a hash to be passed so you won't need to encode and decode JSON manually.
255
282
 
256
- 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:
283
+ 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.
257
284
 
258
285
  ```ruby
259
- Logidze.with_meta(ip: request.ip) do
286
+ Logidze.with_meta({ip: request.ip}, transactional: false) do
260
287
  post.save!
261
288
  end
262
289
  ```
263
290
 
264
- Meta expects a hash to be passed so you won't need to encode and decode JSON manually.
265
-
266
- ## Track responsibility (aka _whodunnit_)
291
+ ### Track responsibility
267
292
 
268
293
  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.
269
294
 
@@ -306,7 +331,15 @@ class ApplicationController < ActionController::Base
306
331
  end
307
332
  ```
308
333
 
309
- ## Disable logging temporary
334
+ 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.
335
+
336
+ ```ruby
337
+ Logidze.with_responsible(user.id, transactional: false) do
338
+ post.save!
339
+ end
340
+ ```
341
+
342
+ ### Disable logging temporary
310
343
 
311
344
  If you want to make update without logging (e.g., mass update), you can turn it off the following way:
312
345
 
@@ -318,7 +351,7 @@ Logidze.without_logging { Post.update_all(seen: true) }
318
351
  Post.without_logging { Post.update_all(seen: true) }
319
352
  ```
320
353
 
321
- ## Reset log
354
+ ### Reset log
322
355
 
323
356
  Reset the history for a record (or records):
324
357
 
@@ -330,6 +363,128 @@ record.reset_log_data
330
363
  User.where(active: true).reset_log_data
331
364
  ```
332
365
 
366
+ ### Full snapshots
367
+
368
+ You can instruct Logidze to create a full snapshot instead of a diff for a particular log entry.
369
+
370
+ It could be useful in combination with `.without_logging`: first, you perform multiple updates without logging, then
371
+ you want to create a log entry with the current state. To do that, you should use the `Logidze.with_full_snapshot` method:
372
+
373
+ ```ruby
374
+ record = Model.find(params[:id])
375
+
376
+ Logidze.without_logging do
377
+ # perform multiple write operations with record
378
+ end
379
+
380
+ Logidze.with_full_snapshot do
381
+ record.touch
382
+ end
383
+ ```
384
+
385
+ ### Associations versioning
386
+
387
+ Logidze also supports associations versioning. This feature is disabled by default (due to the number of edge cases). You can learn more
388
+ in the [wiki](https://github.com/palkan/logidze/wiki/Associations-versioning).
389
+
390
+ ## Dealing with large logs
391
+
392
+ By default, Active Record _selects_ all the table columns when no explicit `select` statement specified.
393
+
394
+ 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.
395
+
396
+ If you do not use compaction (`generate logidze:model ... --limit N`) for `log_data`, you're likely to face this problem.
397
+
398
+ Logidze provides a way to avoid loading `log_data` by default (and load it on demand):
399
+
400
+ ```ruby
401
+ class User < ActiveRecord::Base
402
+ # Add `ignore_log_data` option to macros
403
+ has_logidze ignore_log_data: true
404
+ end
405
+ ```
406
+
407
+ If you want Logidze to behave this way by default, configure the global option:
408
+
409
+ ```ruby
410
+ # config/initializers/logidze.rb
411
+ Logidze.ignore_log_data_by_default = true
412
+
413
+ # or
414
+
415
+ # config/application.rb
416
+ config.logidze.ignore_log_data_by_default = true
417
+ ```
418
+
419
+ However, you can override it by explicitly passing `ignore_log_data: false` to the `ignore_log_data`.
420
+ 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
421
+ the _users_ with `log_data` included.
422
+
423
+ 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.)
424
+
425
+ ![](./assets/pg_log_data_chart.png)
426
+
427
+ 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.
428
+
429
+ ## Handling records deletion
430
+
431
+ Unlike, for example, PaperTrail, Logidze is designed to **only track changes**. If the record has been deleted, **everything is lost**.
432
+
433
+ 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).
434
+
435
+ See also the discussion: [#61](https://github.com/palkan/logidze/issues/61).
436
+
437
+ ## Upgrading
438
+
439
+ We try to make an upgrade process as simple as possible. For now, the only required action is to create and run a migration:
440
+
441
+ ```sh
442
+ bundle exec rails generate logidze:install --update
443
+ ```
444
+
445
+ This updates core `logdize_logger` DB function. No need to update tables or triggers.
446
+
447
+ **NOTE:** When using `fx`, you can omit the `--update` flag. The migration containing only the updated functions would be created.
448
+
449
+ If you want to update Logidze settings for the model, run migration with `--update` flag:
450
+
451
+ ```sh
452
+ bundle exec rails generate logidze:model Post --update --only=title,body,rating
453
+ ```
454
+
455
+ You can also use the `--name` option to specify the migration name to avoid duplicate migration names:
456
+
457
+ ```sh
458
+ $ bundle exec rails generate logidze:model Post --update --only=title,body,rating --name add_only_filter_to_posts_log_data
459
+
460
+ create db/migrate/20202309142344_add_only_filter_to_posts_log_data.rb
461
+ ```
462
+
463
+ ### Pending upgrade check [Experimental]
464
+
465
+ Logidze can check for a pending upgrade. Use `Logidze.pending_upgrade = :warn` to be notified by warning, or `Logidze.pending_upgrade = :error` if you want Logidze to raise an error.
466
+
467
+ ### Upgrading from 0.x to 1.0 (edge)
468
+
469
+ #### Schema and migrations
470
+
471
+ Most SQL function definitions have changed without backward compatibility.
472
+ Perform the following steps to upgrade:
473
+
474
+ 1. Re-install Logidze: `bundle exec rails generate logidze:install --update`.
475
+
476
+ 1. Re-install Logidze triggers **for all models**: `bundle exec rails generate logidze:model <model> --update`.
477
+
478
+ **NOTE:** If you had previously specified whitelist/blacklist attributes, you will need to include the `--only`/`--except` [option](#tracking-only-selected-columns) as appropriate. You can easily copy these column lists from the previous logidze migration for the model.
479
+
480
+ 1. Remove the `include Logidze::Migration` line from the old migration files (if any)—this module has been removed.
481
+
482
+ Rewrite legacy logidze 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).
483
+
484
+ #### API changes
485
+
486
+ 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)`.
487
+
333
488
  ## Log format
334
489
 
335
490
  The `log_data` column has the following format:
@@ -358,19 +513,49 @@ The `log_data` column has the following format:
358
513
 
359
514
  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.
360
515
 
361
- ## Development
516
+ ## Troubleshooting
362
517
 
363
- For development setup run `./bin/setup`. This runs `bundle install` and creates test DB.
518
+ ### `log_data` is nil when using Rails fixtures
364
519
 
365
- ## Contributing
520
+ Rails fixtures are populated with triggers disabled. Thus, `log_data` is null initially for all records.
521
+ You can use `#create_logidze_snapshot` manually to build initial snapshots.
522
+
523
+ ### How to make this work with Apartment 🤔
524
+
525
+ 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).
526
+
527
+ Secondly, set `config.use_sql = true` in the Apartment configuration.
528
+
529
+ Finally, when using `fx` along with `schema.rb`, you might face a problem with duplicate trigger definitions (for different schemas).
530
+ Here is a patch to fix this: [dump_triggers.rake](etc/dump_triggers.rake).
531
+
532
+ Related issues: [#50](https://github.com/palkan/logidze/issues/50).
366
533
 
367
- Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/logidze.
534
+ ### `PG::UntranslatableCharacter: ERROR`
368
535
 
369
- ## Future ideas
536
+ That could happen when your row data contain null bytes. You should sanitize the data before writing to the database.
537
+ 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)`.
370
538
 
371
- - Enhance update_all to support mass-logging.
372
- - Other DB adapters.
539
+ Related issues: [#155](https://github.com/palkan/logidze/issues/155).
540
+
541
+ ### `pg_restore` fails to restore a dump
542
+
543
+ First, when restoring data dumps you should consider using `--disable-triggers` option (unless you have a strong reason to invoke the triggers).
544
+
545
+ 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)).
546
+
547
+ ## Development
548
+
549
+ 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.
550
+
551
+ If you prefer developing on your local machine, make user you have Postgres installed and run `./bin/setup`.
552
+
553
+ ## Contributing
554
+
555
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/palkan/logidze](https://github.com/palkan/logidze).
373
556
 
374
557
  ## License
375
558
 
376
559
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
560
+
561
+ [fx]: https://github.com/teoljungberg/fx