logidze 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1a840d3c86c64317abe7c5b69a8dcc978e9efc7
4
- data.tar.gz: 221465ea78ebb99cf8335415d5c378484d4ddb9a
3
+ metadata.gz: 34c35c39f7c5dc042b1c13298bdb0347f6d2c811
4
+ data.tar.gz: c4e3a0f86bdb6fcffa45286d55a4d42a59343074
5
5
  SHA512:
6
- metadata.gz: 414193540e8570e67f735664e42f09b97fc3e736d8110d0373192ab41b651cf6b597b863e194bc99fbba71a73500d0a96c3ab5205da55e23a709ac4eb76bc2e8
7
- data.tar.gz: 7883e8e97309016d2b11b43744fdd07c15aea256408cb65d2d72c55ce1405d461ba6d3d684d5010ca00a8fa634122fe2efb5a7893e0d875dcaff3e783d8eaf4c
6
+ metadata.gz: 29f671a66d9fe528c17baa1416f803a4aba38f0e58e5a4cad5398c28641212c4b956a29004f225babe10ca35c9690e17b1c9d6e5576339d1358ae56f1b4e728a
7
+ data.tar.gz: 438261164cf4ea8c792ab089cc0cbafdff31c3086c12025f0fe35907b3a5efbbd5882563eebeec50ce8954b83d788c3a688077246077a90f65fe05845465640d
data/.travis.yml CHANGED
@@ -1,27 +1,17 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
 
4
+ dist: trusty
5
+ sudo: false
6
+
7
+ addons:
8
+ postgresql: "9.6"
9
+
4
10
  env:
5
11
  global:
6
12
  - LOGIDZE_DB_USER=postgres
7
13
  - LOGIDZE_DB_NAME=logidze
8
14
 
9
- before_install:
10
- - sudo /etc/init.d/postgresql stop
11
- - sudo apt-get -y remove --purge postgresql-9.1
12
- - sudo apt-get -y remove --purge postgresql-9.2
13
- - sudo apt-get -y remove --purge postgresql-9.3
14
- - sudo apt-get -y remove --purge postgresql-9.4
15
- - sudo apt-get -y autoremove
16
- - sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 7FCC7D46ACCC4CF8
17
- - sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main 9.5" >> /etc/apt/sources.list.d/postgresql.list'
18
- - sudo apt-get update
19
- - sudo apt-get -y install postgresql-9.5
20
- - sudo sh -c 'echo "local all postgres trust" > /etc/postgresql/9.5/main/pg_hba.conf'
21
- - sudo sh -c 'echo -n "host all all 127.0.0.1/32 trust" >> /etc/postgresql/9.5/main/pg_hba.conf'
22
- - sudo /etc/init.d/postgresql restart
23
- - psql --version
24
-
25
15
  before_script:
26
16
  - bundle exec rake dummy:db:create
27
17
  - psql -U postgres -d logidze -c 'CREATE EXTENSION IF NOT EXISTS hstore;'
data/CHANGELOG.md CHANGED
@@ -1,18 +1,36 @@
1
- # 0.3.0
2
- - Add `--update` option to install migration generator
3
- - Add `--only-trigger` option to model migration generator
4
- - Add [Responsibility](https://github.com/palkan/logidze/issues/4) feature
1
+ # Change log
5
2
 
6
- # 0.2.3
7
- - Support Ruby >= 2.1
3
+ ## master
8
4
 
9
- # 0.2.2
10
- - Add `--backfill` option to model migration
11
- - Handle legacy data (that doesn't have log data)
5
+ ## 0.4.0 (2017-01-14)
12
6
 
13
- # 0.2.1
14
- - Support both Rails 4 and 5
7
+ - Add `--blacklist` and `--whitelist` options to model migration generator. ([@charlie-wasp][])
15
8
 
16
- # 0.2.0 (**Incompatible with 0.1.0**)
9
+ ## 0.3.0
17
10
 
18
- - Rails 5 support
11
+ - Add `--update` option to install migration generator. ([@palkan][])
12
+
13
+ - Add `--only-trigger` option to model migration generator. ([@palkan][])
14
+
15
+ - Add [Responsibility](https://github.com/palkan/logidze/issues/4) feature. ([@palkan][])
16
+
17
+ ## 0.2.3
18
+
19
+ - Support Ruby >= 2.1. ([@palkan][])
20
+
21
+ ## 0.2.2
22
+
23
+ - Add `--backfill` option to model migration. ([@palkan][])
24
+
25
+ - Handle legacy data (that doesn't have log data). ([@palkan][])
26
+
27
+ ## 0.2.1
28
+
29
+ - Support both Rails 4 and 5. ([@palkan][])
30
+
31
+ ## 0.2.0 (**Incompatible with 0.1.0**)
32
+
33
+ - Rails 5 support. ([@palkan][])
34
+
35
+ [@palkan]: https://github.com/palkan
36
+ [@charlie-wasp]: https://github.com/charlie-wasp
data/README.md CHANGED
@@ -60,6 +60,15 @@ To backfill table data (i.e. create initial snapshots) add `backfill` option:
60
60
  rails generate logidze:model Post --backfill
61
61
  ```
62
62
 
63
+ You can log only particular columns changes. There are mutually exclusive `blacklist` and `whitelist` options for this:
64
+
65
+ ```ruby
66
+ # track all columns, except `created_at` and `active`
67
+ rails generate logidze:model Post --blacklist=created_at active
68
+ # track only `title` and `body` columns
69
+ rails generate logidze:model Post --whitelist=title body
70
+ ```
71
+
63
72
  ## Troubleshooting
64
73
 
65
74
  The most common problem is `"permission denied to set parameter "logidze.xxx"` caused by `ALTER DATABASE ...` query.
@@ -234,7 +243,6 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/palkan
234
243
 
235
244
  ## TODO
236
245
 
237
- - Exclude columns from the log.
238
246
  - Enhance update_all to support mass-logging.
239
247
  - Other DB adapters.
240
248
 
@@ -15,8 +15,15 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
15
15
  SQL
16
16
  end
17
17
 
18
+ <% if update? %>
18
19
  execute <<-SQL
19
- CREATE OR REPLACE FUNCTION logidze_version(v bigint, data jsonb) RETURNS jsonb AS $body$
20
+ DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb);
21
+ DROP FUNCTION IF EXISTS logidze_snapshot(jsonb);
22
+ SQL
23
+ <% end %>
24
+
25
+ execute <<-SQL
26
+ CREATE OR REPLACE FUNCTION logidze_version(v bigint, data jsonb, blacklist text[] DEFAULT '{}') RETURNS jsonb AS $body$
20
27
  DECLARE
21
28
  buf jsonb;
22
29
  BEGIN
@@ -26,7 +33,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
26
33
  'v',
27
34
  v,
28
35
  'c',
29
- logidze_exclude_keys(data, 'log_data')
36
+ logidze_exclude_keys(data, VARIADIC array_append(blacklist, 'log_data'))
30
37
  );
31
38
  IF coalesce(#{current_setting('logidze.responsible')}, '') <> '' THEN
32
39
  buf := jsonb_set(buf, ARRAY['r'], to_jsonb(current_setting('logidze.responsible')));
@@ -36,12 +43,12 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
36
43
  $body$
37
44
  LANGUAGE plpgsql;
38
45
 
39
- CREATE OR REPLACE FUNCTION logidze_snapshot(item jsonb) RETURNS jsonb AS $body$
46
+ CREATE OR REPLACE FUNCTION logidze_snapshot(item jsonb, blacklist text[] DEFAULT '{}') RETURNS jsonb AS $body$
40
47
  BEGIN
41
48
  return json_build_object(
42
49
  'v', 1,
43
50
  'h', jsonb_build_array(
44
- logidze_version(1, item)
51
+ logidze_version(1, item, blacklist)
45
52
  )
46
53
  );
47
54
  END;
@@ -103,20 +110,22 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
103
110
  merged jsonb;
104
111
  iterator integer;
105
112
  item record;
113
+ columns_blacklist text[];
106
114
  BEGIN
115
+ columns_blacklist := TG_ARGV[1];
107
116
 
108
117
  IF TG_OP = 'INSERT' THEN
109
118
 
110
- NEW.log_data := logidze_snapshot(to_jsonb(NEW.*));
119
+ NEW.log_data := logidze_snapshot(to_jsonb(NEW.*), columns_blacklist);
111
120
 
112
121
  ELSIF TG_OP = 'UPDATE' THEN
113
122
 
114
123
  IF OLD.log_data is NULL OR OLD.log_data = '{}'::jsonb THEN
115
- NEW.log_data := logidze_snapshot(to_jsonb(NEW.*));
124
+ NEW.log_data := logidze_snapshot(to_jsonb(NEW.*), columns_blacklist);
116
125
  RETURN NEW;
117
126
  END IF;
118
127
 
119
- history_limit := TG_ARGV[0];
128
+ history_limit := NULLIF(TG_ARGV[0], 'null');
120
129
  current_version := (NEW.log_data->>'v')::int;
121
130
 
122
131
  IF NEW = OLD THEN
@@ -149,7 +158,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
149
158
  NEW.log_data := jsonb_set(
150
159
  NEW.log_data,
151
160
  ARRAY['h', size::text],
152
- logidze_version(new_v, changes),
161
+ logidze_version(new_v, changes, columns_blacklist),
153
162
  true
154
163
  );
155
164
 
@@ -174,9 +183,9 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
174
183
  def down
175
184
  <% unless update? %>
176
185
  execute <<-SQL
177
- DROP FUNCTION logidze_version(bigint, jsonb) CASCADE;
186
+ DROP FUNCTION logidze_version(bigint, jsonb, text[]) CASCADE;
178
187
  DROP FUNCTION logidze_compact_history(jsonb) CASCADE;
179
- DROP FUNCTION logidze_snapshot(jsonb) CASCADE;
188
+ DROP FUNCTION logidze_snapshot(jsonb, text[]) CASCADE;
180
189
  DROP FUNCTION logidze_logger() CASCADE;
181
190
  SQL
182
191
  <% end %>
@@ -15,7 +15,14 @@ module Logidze
15
15
  class_option :only_trigger, type: :boolean, optional: true,
16
16
  desc: "Create trigger-only migration"
17
17
 
18
+ class_option :blacklist, type: :array, optional: true
19
+ class_option :whitelist, type: :array, optional: true
20
+
18
21
  def generate_migration
22
+ if options[:blacklist] && options[:whitelist]
23
+ $stderr.puts "Use only one: --whitelist or --blacklist"
24
+ exit(1)
25
+ end
19
26
  migration_template "migration.rb.erb", "db/migrate/#{migration_file_name}"
20
27
  end
21
28
 
@@ -45,6 +52,38 @@ module Logidze
45
52
  def only_trigger?
46
53
  options[:only_trigger]
47
54
  end
55
+
56
+ def columns_blacklist
57
+ array = if !options[:whitelist]
58
+ options[:blacklist]
59
+ else
60
+ class_name.constantize.column_names - options[:whitelist]
61
+ end
62
+
63
+ array || []
64
+ end
65
+
66
+ def logidze_logger_parameters
67
+ if limit.nil? && columns_blacklist.empty?
68
+ ''
69
+ elsif !limit.nil? && columns_blacklist.empty?
70
+ limit
71
+ elsif !limit.nil? && !columns_blacklist.empty?
72
+ "#{limit}, #{format_pgsql_array(columns_blacklist)}"
73
+ elsif limit.nil? && !columns_blacklist.empty?
74
+ "null, #{format_pgsql_array(columns_blacklist)}"
75
+ end
76
+ end
77
+
78
+ def logidze_snapshot_parameters
79
+ return 'to_jsonb(t)' if columns_blacklist.empty?
80
+
81
+ "to_jsonb(t), #{format_pgsql_array(columns_blacklist)}"
82
+ end
83
+
84
+ def format_pgsql_array(ruby_array)
85
+ "'{" + ruby_array.join(', ') + "}'"
86
+ end
48
87
  end
49
88
 
50
89
  private
@@ -11,13 +11,13 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
11
11
  CREATE TRIGGER logidze_on_<%= table_name %>
12
12
  BEFORE UPDATE OR INSERT ON <%= table_name %> FOR EACH ROW
13
13
  WHEN (coalesce(#{current_setting('logidze.disabled')}, '') <> 'on')
14
- EXECUTE PROCEDURE logidze_logger(<%= limit || '' %>);
14
+ EXECUTE PROCEDURE logidze_logger(<%= logidze_logger_parameters %>);
15
15
  SQL
16
16
 
17
17
  <% if backfill? %>
18
18
  execute <<-SQL
19
19
  UPDATE <%= table_name %> as t
20
- SET log_data = logidze_snapshot(to_jsonb(t));
20
+ SET log_data = logidze_snapshot(<%= logidze_snapshot_parameters %>);
21
21
  SQL
22
22
  <% end %>
23
23
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Logidze
3
- VERSION = "0.3.1"
3
+ VERSION = "0.4.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logidze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-17 00:00:00.000000000 Z
11
+ date: 2017-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails