Skip to content
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

Stringify snapshot jsonb values #173

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/generators/logidze/install/functions/logidze_snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
CREATE OR REPLACE FUNCTION logidze_snapshot(item jsonb, ts_column text DEFAULT NULL, columns text[] DEFAULT NULL, include_columns boolean DEFAULT false) RETURNS jsonb AS $body$
DECLARE
ts timestamp with time zone;
k text;
BEGIN
IF ts_column IS NULL THEN
ts := statement_timestamp();
Expand All @@ -13,6 +14,13 @@ CREATE OR REPLACE FUNCTION logidze_snapshot(item jsonb, ts_column text DEFAULT N
item := logidze_filter_keys(item, columns, include_columns);
END IF;

FOR k IN (SELECT key FROM jsonb_each(item))
LOOP
IF jsonb_typeof(item->k) = 'object' THEN
item := jsonb_set(item, ARRAY[k], to_jsonb(item->>k));
END IF;
END LOOP;

return json_build_object(
'v', 1,
'h', jsonb_build_array(
Expand Down
14 changes: 12 additions & 2 deletions spec/logidze/create_snapshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
end

let(:now) { Time.local(1989, 7, 10, 18, 23, 33) }
let(:user) { Logidze.without_logging { User.create!(time: now, name: "test", age: 10, active: false) } }
let(:user) do
Logidze.without_logging do
User.create!(time: now, name: "test", age: 10, active: false, extra: {gender: "X"})
end
end

describe "#create_logidze_snapshot!" do
specify "without arguments" do
Expand All @@ -27,7 +31,13 @@
expect(user.log_data).not_to be_nil
expect(user.log_data.version).to eq 1
expect(Time.at(user.log_data.current_version.time / 1000) - now).to be > 1.year
expect(user.log_data.current_version.changes).to include({"name" => "test", "age" => 10, "active" => false})
expect(user.log_data.current_version.changes)
.to include({
"name" => "test",
"age" => 10,
"active" => false,
"extra" => '{"gender": "X"}'
})
end

specify "timestamp column" do
Expand Down
6 changes: 3 additions & 3 deletions spec/sql/snapshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require "acceptance_helper"

describe "logidze_snapshot" do
let(:now) { Time.local(1989, 7, 10, 18, 23, 33) }
let(:now) { Time.zone.local(1989, 7, 10, 18, 23, 33) }

let(:data) { %('{"title": "Feel me", "rating": 42, "name": "Jack", "updated_at": "#{now.to_s(:db)}"}'::jsonb) }
let(:data) { %('{"title": "Feel me", "rating": 42, "name": "Jack", "extra": {"gender": "X"}, "updated_at": "#{now.to_s(:db)}"}'::jsonb) }

specify "without optional args" do
res = sql "select logidze_snapshot(#{data})"
Expand All @@ -20,7 +20,7 @@
expect(version).to match({
"ts" => an_instance_of(Integer),
"v" => 1,
"c" => a_hash_including({"title" => "Feel me", "rating" => 42, "name" => "Jack"})
"c" => a_hash_including({"title" => "Feel me", "rating" => 42, "name" => "Jack", "extra" => '{"gender": "X"}'})
})

expect(Time.at(version["ts"] / 1000) - now).to be > 1.year
Expand Down