Open SimonLab opened 6 years ago
related to #91
We can add the inserted_at
value into the issue changeset like this:
changeset = Issue.changeset(%Issue{}, i)
time = NaiveDateTime.from_iso8601!(i.created_at)
changeset = Changeset.put_change(changeset, :inserted_at, time)
Howerver we need to do the same for each comments of the issue. So we will need to break down the changeset to create a comment changeset which we will update the inserted_at
value then we will need to associate this changeset to the issue
An easier solution was to update the changeset to accept inserted_at
and updated_at
but this field are optional so when there are not in the params of the changeset function we let the Postgres define them:
def changeset(%Issue{} = issue, attrs) do
issue
|> cast(attrs, [:issue_id, :title, :inserted_at, :updated_at])
|> cast_assoc(:comments, require: true)
|> validate_required([:issue_id, :title])
We can see that neither of the two timestamps are passed to validate_required
tested with iex -S mix
:
Issue.changeset(%Issue{}, %{issue_id: 1, title: "bob", inserted_at: NaiveDateTime.from_iso8601!("2017-05-30 16:59:00")}
App.Repo.insert!(i)
[debug] QUERY OK db=28.1ms
commit []
%App.Issue{
__meta__: #Ecto.Schema.Metadata<:loaded, "issues">,
comments: #Ecto.Association.NotLoaded<association :comments is not loaded>,
id: 21,
inserted_at: ~N[2017-05-30 16:59:00],
issue_id: 1,
title: "bob",
updated_at: ~N[2018-03-14 15:52:15.483779]
}
65 (Save all issues/comments on install) let postgres save automatically the
created_at
timestamp value. Instead we want this value to be the one when the issue/comment was created to be make sure the history display the content in the correct orderWe might need to update the changeset of the issue and comment schema to allow a
created_at
params to be passed on insert. I also need to test if the genereated timestamp by postgres can be overwritten