Skip to content

Commit

Permalink
[Fixes #70] Ignore missing columns in diff and past versions
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 30, 2018
1 parent a31bd89 commit e12e61a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/logidze/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def diff_from(ts = nil, version: nil, time: nil)
time = parse_time(time) if time
changes = log_data.diff_from(time: time, version: version).tap do |v|
deserialize_changes!(v)
end
end.delete_if { |k, _v| deleted_column?(k) }

{ "id" => id, "changes" => changes }
end
Expand Down Expand Up @@ -203,6 +203,7 @@ def apply_diff(version, diff)
end

def apply_column_diff(column, value)
return if deleted_column?(column)
write_attribute column, deserialize_value(column, value)
end

Expand All @@ -225,6 +226,10 @@ def deserialize_value(column, value)
end
end

def deleted_column?(column)
!@attributes.key?(column)
end

def deserialize_changes!(diff)
diff.each do |k, v|
v["old"] = deserialize_value(k, v["old"])
Expand Down
43 changes: 41 additions & 2 deletions spec/logidze/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
end
end

describe "Versioned associations" do
context "Versioned associations" do
before(:all) { Logidze.associations_versioning = true }

let(:user) do
Expand Down Expand Up @@ -447,7 +447,7 @@
end
end

describe "Serialized types" do
context "Serialized types" do
let(:user) do
User.create!(
name: 'test',
Expand Down Expand Up @@ -495,4 +495,43 @@
end
end
end

context "Schema changes" do
let(:user) do
User.create!(
name: 'test',
log_data: {
'v' => 3,
'h' =>
[
{ 'v' => 1, 'ts' => time(100), 'c' => { 'age' => nil } },
{ 'v' => 2, 'ts' => time(120), 'c' => { 'age' => 1, 'last_name' => 'Harry' } },
{ 'v' => 3, 'ts' => time(200), 'c' => { 'name' => 'Harry', 'age' => 10 } }
]
}
)
end

describe "#at" do
it "returns version at specified time", :aggregate_failures do
user_old = user.at(time: time(150))
expect(user_old.name).to eq 'test'
expect(user_old.age).to eq 1
end
end

describe "#diff_from" do
it "returns diff from specified time" do
expect(user.diff_from(version: 1))
.to eq(
"id" => user.id,
"changes" =>
{
"age" => { "old" => nil, "new" => 10 },
"name" => { "old" => nil, "new" => "Harry" }
}
)
end
end
end
end

0 comments on commit e12e61a

Please sign in to comment.