Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use timestamp when issue/comment is created #93

Open
SimonLab opened this issue Mar 14, 2018 · 3 comments
Open

Use timestamp when issue/comment is created #93

SimonLab opened this issue Mar 14, 2018 · 3 comments

Comments

@SimonLab
Copy link
Member

#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 order

We 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

@SimonLab SimonLab added this to the MVP milestone Mar 14, 2018
@SimonLab SimonLab self-assigned this Mar 14, 2018
@nelsonic
Copy link
Member

related to #91

@SimonLab
Copy link
Member Author

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

@SimonLab
Copy link
Member Author

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]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants