logidze 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 937e0b13769d464f7bb14dc1440951ea6a1462db
4
- data.tar.gz: e7d117211dc9e24854b0171158996066aa56ade1
3
+ metadata.gz: d1a840d3c86c64317abe7c5b69a8dcc978e9efc7
4
+ data.tar.gz: 221465ea78ebb99cf8335415d5c378484d4ddb9a
5
5
  SHA512:
6
- metadata.gz: fcdfe2828fa0fa360177a7839de761bc91b7e9bb81e9ce0d69482e28b3d2c1194a91e71dca9e13975d7eafc31839db3990f7b082fa37f132c43b51bad3245010
7
- data.tar.gz: 3fd4562dd287fdcf8c1321e5ff8e225931cbc9b76913f1c90f9a4566f2610446f86febddfed60407988746a4ad765d397d469bef75eb06591938b2978198866c
6
+ metadata.gz: 414193540e8570e67f735664e42f09b97fc3e736d8110d0373192ab41b651cf6b597b863e194bc99fbba71a73500d0a96c3ab5205da55e23a709ac4eb76bc2e8
7
+ data.tar.gz: 7883e8e97309016d2b11b43744fdd07c15aea256408cb65d2d72c55ce1405d461ba6d3d684d5010ca00a8fa634122fe2efb5a7893e0d875dcaff3e783d8eaf4c
data/README.md CHANGED
@@ -60,6 +60,18 @@ 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
+ ## Troubleshooting
64
+
65
+ The most common problem is `"permission denied to set parameter "logidze.xxx"` caused by `ALTER DATABASE ...` query.
66
+ Logidze requires at least database owner privileges (which is not always possible).
67
+
68
+ Here is a quick and straightforward [workaround](https://github.com/palkan/logidze/issues/11#issuecomment-260703464) by [@nobodyzzz](https://github.com/nobodyzzz).
69
+
70
+ **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 ...`.
71
+
72
+ Nevertheless, you still need super-user privileges to enable `hstore` extension (or you can use [PostgreSQL Extension Whitelisting](https://github.com/dimitri/pgextwlist)).
73
+
74
+
63
75
  ## Upgrade from previous versions
64
76
 
65
77
  We try to make upgrade process as simple as possible. For now, the only required action is to create and run a migration:
@@ -202,9 +214,9 @@ The `log_data` column has the following format:
202
214
  "c": {
203
215
  "attr": "new value", // updated fields with new values
204
216
  "attr2": "new value"
205
- }
206
- },
217
+ },
207
218
  "r": 42 // Resposibility ID (if provided)
219
+ }
208
220
  ]
209
221
  }
210
222
  ```
data/bin/console CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
+ require "rails"
4
5
  require "logidze"
5
6
 
6
7
  require "pry"
@@ -1,14 +1,19 @@
1
1
  class <%= @migration_class_name %> < ActiveRecord::Migration
2
+ require 'logidze/migration'
3
+ include Logidze::Migration
4
+
2
5
  def up
3
- execute <<-SQL
4
- DO $$
5
- BEGIN
6
- EXECUTE 'ALTER DATABASE ' || current_database() || ' SET logidze.disabled TO off';
7
- EXECUTE 'ALTER DATABASE ' || current_database() || ' SET logidze.responsible TO off';
8
- END;
9
- $$
10
- LANGUAGE plpgsql;
11
- SQL
6
+ unless current_setting_missing_supported?
7
+ execute <<-SQL
8
+ DO $$
9
+ BEGIN
10
+ EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET logidze.disabled=' || quote_literal('');
11
+ EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET logidze.responsible=' || quote_literal('');
12
+ END;
13
+ $$
14
+ LANGUAGE plpgsql;
15
+ SQL
16
+ end
12
17
 
13
18
  execute <<-SQL
14
19
  CREATE OR REPLACE FUNCTION logidze_version(v bigint, data jsonb) RETURNS jsonb AS $body$
@@ -23,7 +28,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
23
28
  'c',
24
29
  logidze_exclude_keys(data, 'log_data')
25
30
  );
26
- IF current_setting('logidze.responsible') <> 'off' THEN
31
+ IF coalesce(#{current_setting('logidze.responsible')}, '') <> '' THEN
27
32
  buf := jsonb_set(buf, ARRAY['r'], to_jsonb(current_setting('logidze.responsible')));
28
33
  END IF;
29
34
  RETURN buf;
@@ -1,4 +1,7 @@
1
1
  class <%= @migration_class_name %> < ActiveRecord::Migration
2
+ require 'logidze/migration'
3
+ include Logidze::Migration
4
+
2
5
  def up
3
6
  <% unless only_trigger? %>
4
7
  add_column :<%= table_name %>, :log_data, :jsonb
@@ -7,7 +10,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration
7
10
  execute <<-SQL
8
11
  CREATE TRIGGER logidze_on_<%= table_name %>
9
12
  BEFORE UPDATE OR INSERT ON <%= table_name %> FOR EACH ROW
10
- WHEN (current_setting('logidze.disabled') <> 'on')
13
+ WHEN (coalesce(#{current_setting('logidze.disabled')}, '') <> 'on')
11
14
  EXECUTE PROCEDURE logidze_logger(<%= limit || '' %>);
12
15
  SQL
13
16
 
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
+ require 'active_support/core_ext/module/delegation'
3
+
2
4
  module Logidze
3
5
  # Log data wrapper
4
6
  class History
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ module Logidze
3
+ # Contains helpers for handling different PG versions
4
+ module Migration
5
+ # Checks whether pg function `current_setting` support `missing_ok` argument
6
+ # (since 9.6)
7
+ def current_setting_missing_supported?
8
+ ActiveRecord::Base.connection.send(:postgresql_version) >= 90_600
9
+ end
10
+
11
+ def current_setting(name)
12
+ if current_setting_missing_supported?
13
+ "current_setting('#{name}', true)"
14
+ else
15
+ "current_setting('#{name}')"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -9,7 +9,7 @@ module Logidze # :nodoc:
9
9
  "SET LOCAL logidze.responsible = #{ActiveRecord::Base.connection.quote(responsible_id)};"
10
10
  )
11
11
  res = yield
12
- ActiveRecord::Base.connection.execute "SET LOCAL logidze.responsible = DEFAULT;"
12
+ ActiveRecord::Base.connection.execute "SET LOCAL logidze.responsible TO DEFAULT;"
13
13
  res
14
14
  end
15
15
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Logidze
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
data/lib/logidze.rb CHANGED
@@ -19,9 +19,9 @@ module Logidze
19
19
  # Logidze.without_logging { Post.update_all(active: true) }
20
20
  def self.without_logging
21
21
  ActiveRecord::Base.transaction do
22
- ActiveRecord::Base.connection.execute "SET LOCAL logidze.disabled = 'on';"
22
+ ActiveRecord::Base.connection.execute "SET LOCAL logidze.disabled TO on;"
23
23
  res = yield
24
- ActiveRecord::Base.connection.execute "SET LOCAL logidze.disabled = DEFAULT;"
24
+ ActiveRecord::Base.connection.execute "SET LOCAL logidze.disabled TO DEFAULT;"
25
25
  res
26
26
  end
27
27
  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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-11 00:00:00.000000000 Z
11
+ date: 2016-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -185,6 +185,7 @@ files:
185
185
  - lib/logidze/history.rb
186
186
  - lib/logidze/history/type.rb
187
187
  - lib/logidze/history/version.rb
188
+ - lib/logidze/migration.rb
188
189
  - lib/logidze/model.rb
189
190
  - lib/logidze/responsible.rb
190
191
  - lib/logidze/version.rb