dwyl / github-backup

:octocat: :back: 🆙 Backup your GitHub Issues so you can still work when (they/you are) offline.
https://github-backup.herokuapp.com
GNU General Public License v2.0
31 stars 3 forks source link

Use timestamp when issue/comment is created #93

Open SimonLab opened 6 years ago

SimonLab commented 6 years ago

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

nelsonic commented 6 years ago

related to #91

SimonLab commented 6 years ago

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 commented 6 years ago

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