palkan / logidze

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

Associations versioning - accessing versions with `at(version: #)` #230

Closed adas172002 closed 1 year ago

adas172002 commented 1 year ago

Hi,

I am trying to set versioning of belongs_to relation between Production and Decision models, both with has_logidze macro. I created a new Production without Decision, then updated Production with a fresh Decision instance, so my production.log_data right now looks like this:

#<Logidze::History:0x0000000127d793f8
 @data=
  {"h"=>
    [{"c"=>
       {"id"=>3,
        "status"=>0,
        "task_id"=>1,
        "issuer_id"=>37,
        "created_at"=>"2023-03-08T15:22:30.54541",
        "machine_id"=>64,
        "updated_at"=>"2023-03-08T15:22:30.54541",
        "activity_id"=>5,
        "description"=>"bla bla bla.",
        "qty_detected"=>"15",
        "originator_id"=>nil,
        "catalogue_check"=>false},
      "m"=>{"_r"=>37},
      "v"=>1,
      "ts"=>1678288950545},
     {"c"=>{"updated_at"=>"2023-03-08 15:26:48.573198", "originator_id"=>34},
      "m"=>{"_r"=>37},
      "v"=>2,
      "ts"=>1678289208573}],
   "v"=>2},
 @versions=
  [#<Logidze::History::Version:0x00000001300826a0
    @data=
     {"c"=>
       {"id"=>3,
        "status"=>0,
        "task_id"=>1,
        "issuer_id"=>37,
        "created_at"=>"2023-03-08T15:22:30.54541",
        "machine_id"=>64,
        "updated_at"=>"2023-03-08T15:22:30.54541",
        "activity_id"=>5,
        "description"=>"bla bla bla.",
        "qty_detected"=>"15",
        "originator_id"=>nil,
        "catalogue_check"=>false},
      "m"=>{"_r"=>37},
      "v"=>1,
      "ts"=>1678288950545}>,
   #<Logidze::History::Version:0x0000000130082678
    @data=
     {"c"=>{"updated_at"=>"2023-03-08 15:26:48.573198", "originator_id"=>34},
      "m"=>{"_r"=>37},
      "v"=>2,
      "ts"=>1678289208573}>]>

and production.decision.log_data

#<Logidze::History:0x0000000126ea54a8
 @data=
  {"h"=>
    [{"c"=>
       {"id"=>3,
        "date"=>"2023-03-08",
        "reason"=>"ble ble ble",
        "action_type"=>1,
        "production_id"=>3,
        "instant_action"=>"la la la"},
      "m"=>{"_r"=>37},
      "v"=>1,
      "ts"=>1678289208579}],
   "v"=>1}>

I expect that getting production.at(version:1).decision should give nil, but I am getting same instance of Decision as for production.at(version: 2).decision. Am I missing something?

palkan commented 1 year ago

Let's see.

The expected behaviour should be similar to the following code:

production = Production.find(42)

production_v1 = production.at(version: 1)

decision_v1 = production.decision.at(time: 1678288950545) # the timestamp of the v1 log for production

The decision object has a timestamp equal to 1678289208579 == '2023-03-08 10:26:48 -0500', while 1678288950545 == '2023-03-08 10:22:30 -0500'. So, decision_v1 must be nil.

I think, if you set Logidze.return_self_if_log_data_is_empty = false, it would work as expected.

Probably, we should make this behaviour a default for associations.

adas172002 commented 1 year ago

Hi @palkan it works as you described. Thanks for your support.