palkan / logidze

Database changes log for Rails
MIT License
1.59k stars 74 forks source link

Association versioning with ignore_log_data and at(version: ) doesn't return expected results #214

Open jasonlor opened 2 years ago

jasonlor commented 2 years ago

Using Logidze 1.2.2

Associations versioning using at(version: ##) and doesn't return the expected records.

Model.find(#).at(version: #).associated_model.with_log_data

Logidze.associations_versioning = true
Logidze.ignore_log_data_by_default = true
palkan commented 2 years ago

Hey, could you please provide more details? Ideally, the records log_data values, what you see and what you expect.

jasonlor commented 2 years ago

I'm trying to retrieve the log_data from associated records using the following:

Initially, I have a setup where ParentModel's log_version=3. The updated_at was modified twice when the two ChildModel's were associated to it.

class ParentModel
  has_logidze
  has_many :child_models
end

class ChildModel
  has_logidze
  belongs_to :parent_model, touch: true
end

ParentModel - log_version = 3 -


ParentModel - log_version = 4


I'm trying to retrieve the the value of ChildModel A when the ParentModel is at log_version=3. For ChildModel A I'm expecting a return value of 1. But when I'm seeing

ParentModel.find(#).at(version: 3).associated_model.with_log_data.child_models.find(ChildModelA.id)

ChildModelA's quantity field returns 3 (the latest value) instead of the 1 from log_data.

I've also tried:

lookup_time = ParentModel.find(#).at(version: 3).updated_at ParentModel.find(#).at(time: lookup_time).associated_model.with_log_data.child_models

palkan commented 1 year ago

Thanks for the details! Will take a look ASAP (or will try to find someone to fix this 🙂)

jerometremblay commented 1 year ago

I am not sure this is the exact same problem, however there is something wrong withe latest version.

Let say i have this

when i read parent.at(version: 1).child it will return child v1 if i update child, giving

then parent.at(version: 1).child will return child v2

However, after i update parent again, leading to

now, reading parent.at(version: 1).child will correctly return child v1 that was there at the creation of parent v1.

palkan commented 1 year ago

Under the hood, we use at(time:) when loading associations. I think, that can be the reason for inconsistency.

That's why I need log_data contents to figure out what went wrong.