Skip to content

Proposal for Better HistoryTracker storage schema #67

Open
@johnnyshields

Description

@johnnyshields

I have a use case to build a human-readable audit trail. I find the association chain syntax to be difficult to work with. I'd propose to combine :modified, :action, and :association_chain into one tree structure.

Assuming we have these models:

class Post
  ...
  field :p1
  field :p2
  embeds_many :comments
  embeds_one :section
end

class Comment
  ...
  field :c1
  field :c2
  embeds_many :links 
end

class Link
  ...
  field :l1
end

class Section
  ...
  field :s1
end

Doing an edit might generate:

modified: {
  _action: :update,
  _version: 2,
  _id: '#P1',
  p1: ___,
  p2: ___,
  comments: [{
    _id: '#C1',
    _action: :update,
    _version: 2,
    c1: ___,
    c2: ___
  },{
    _id: '#C2',
    _action: :create,
    _version: 1,
    c1: ___,
    links: [{
      _id: '#L1',
      _action: :create,
      _version: 1,
      l1: ___
    }]
  }],
  section: {
    _id: '#S2',
    _action: :delete,
    _version: 2,
    s1: ___
  }
}

If I modify only an embedded model, the audit should still save from the parent downward:

modified: {
  _action: :update,
  _id: '#P1',
  _version: 3,
  comment: [{
    _id: '#C2',
    _action: :update,
    _version: 2,
    links: [{
      _id: '#L1',
      _action: :update,
      _version: 2,
      l1: ___
    }]
  }]
}

A drawback is that this structure makes it slightly difficult to follow history on embedded models. We may want to support "breakout mode" to where you can force embedded updates to be stored on a separately from their parents (i.e. the current structure).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions