-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Columns filtering: blacklisting or whitelisting #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,15 @@ class <%= @migration_class_name %> < ActiveRecord::Migration | |
SQL | ||
end | ||
|
||
<% if update? %> | ||
execute <<-SQL | ||
CREATE OR REPLACE FUNCTION logidze_version(v bigint, data jsonb) RETURNS jsonb AS $body$ | ||
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb); | ||
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb); | ||
SQL | ||
<% end %> | ||
|
||
execute <<-SQL | ||
CREATE OR REPLACE FUNCTION logidze_version(v bigint, data jsonb, blacklist text[] DEFAULT '{}') RETURNS jsonb AS $body$ | ||
DECLARE | ||
buf jsonb; | ||
BEGIN | ||
|
@@ -26,7 +33,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration | |
'v', | ||
v, | ||
'c', | ||
logidze_exclude_keys(data, 'log_data') | ||
logidze_exclude_keys(data, VARIADIC array_append(blacklist, 'log_data')) | ||
); | ||
IF coalesce(#{current_setting('logidze.responsible')}, '') <> '' THEN | ||
buf := jsonb_set(buf, ARRAY['r'], to_jsonb(current_setting('logidze.responsible'))); | ||
|
@@ -36,12 +43,12 @@ class <%= @migration_class_name %> < ActiveRecord::Migration | |
$body$ | ||
LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION logidze_snapshot(item jsonb) RETURNS jsonb AS $body$ | ||
CREATE OR REPLACE FUNCTION logidze_snapshot(item jsonb, blacklist text[] DEFAULT '{}') RETURNS jsonb AS $body$ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same goes here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, didn't think about it. Added |
||
BEGIN | ||
return json_build_object( | ||
'v', 1, | ||
'h', jsonb_build_array( | ||
logidze_version(1, item) | ||
logidze_version(1, item, blacklist) | ||
) | ||
); | ||
END; | ||
|
@@ -103,20 +110,22 @@ class <%= @migration_class_name %> < ActiveRecord::Migration | |
merged jsonb; | ||
iterator integer; | ||
item record; | ||
columns_blacklist text[]; | ||
BEGIN | ||
columns_blacklist := TG_ARGV[1]; | ||
|
||
IF TG_OP = 'INSERT' THEN | ||
|
||
NEW.log_data := logidze_snapshot(to_jsonb(NEW.*)); | ||
NEW.log_data := logidze_snapshot(to_jsonb(NEW.*), columns_blacklist); | ||
|
||
ELSIF TG_OP = 'UPDATE' THEN | ||
|
||
IF OLD.log_data is NULL OR OLD.log_data = '{}'::jsonb THEN | ||
NEW.log_data := logidze_snapshot(to_jsonb(NEW.*)); | ||
NEW.log_data := logidze_snapshot(to_jsonb(NEW.*), columns_blacklist); | ||
RETURN NEW; | ||
END IF; | ||
|
||
history_limit := TG_ARGV[0]; | ||
history_limit := NULLIF(TG_ARGV[0], 'null'); | ||
current_version := (NEW.log_data->>'v')::int; | ||
|
||
IF NEW = OLD THEN | ||
|
@@ -149,7 +158,7 @@ class <%= @migration_class_name %> < ActiveRecord::Migration | |
NEW.log_data := jsonb_set( | ||
NEW.log_data, | ||
ARRAY['h', size::text], | ||
logidze_version(new_v, changes), | ||
logidze_version(new_v, changes, columns_blacklist), | ||
true | ||
); | ||
|
||
|
@@ -174,9 +183,9 @@ class <%= @migration_class_name %> < ActiveRecord::Migration | |
def down | ||
<% unless update? %> | ||
execute <<-SQL | ||
DROP FUNCTION logidze_version(bigint, jsonb) CASCADE; | ||
DROP FUNCTION logidze_version(bigint, jsonb, text[]) CASCADE; | ||
DROP FUNCTION logidze_compact_history(jsonb) CASCADE; | ||
DROP FUNCTION logidze_snapshot(jsonb) CASCADE; | ||
DROP FUNCTION logidze_snapshot(jsonb, text[]) CASCADE; | ||
DROP FUNCTION logidze_logger() CASCADE; | ||
SQL | ||
<% end %> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When upgrading we have to drop previously defined function, 'cause we change the signature.
So, we should add here
DROP FUNCTION IF EXISTS ...
in case of upgrade (useupdate?
helper).