ZennerIoT / ex_audit

Ecto auditing library that transparently tracks changes and can revert them.
MIT License
368 stars 110 forks source link

Getting microseconds error #14

Open spapas opened 5 years ago

spapas commented 5 years ago

Hello, I am trying to integrate ex_audit wit my application using more or less the default configuration.

However when I try to save a model with changes I get the following error:

:utc_datetime expects microseconds to be empty, got: #DateTime<2019-05-02 05:19:39.935000Z>
Use `DateTime.truncate(utc_datetime, :second)` (available in Elixir v1.6+) to remove microseconds.

lib/ecto/type.ex
  end
  defp check_no_usec!(%{microsecond: {0, 0}} = datetime, _kind), do: datetime
  defp check_no_usec!(%struct{} = datetime, kind) do
    raise ArgumentError, """
    #{inspect(kind)} expects microseconds to be empty, got: #{inspect(datetime)}
    Use `#{inspect(struct)}.truncate(#{kind}, :second)` (available in Elixir v1.6+) to remove microseconds.
    """
  end

Any suggestions on how to fix ?

Here are the versions I use:

...  
  ecto 3.1.1
  ecto_sql 3.1.1
  ex_audit 0.6.0
  phoenix 1.4.3
  phoenix_ecto 4.0.0

Also here's the whole ST:

 ecto lib/ecto/type.ex:1224 Ecto.Type.check_no_usec!/2
 ecto lib/ecto/type.ex:412 Ecto.Type.dump_utc_datetime/1
 ecto lib/ecto/type.ex:817 Ecto.Type.process_dumpers/3
 ecto lib/ecto/repo/schema.ex:925 Ecto.Repo.Schema.dump_field!/6
 ecto lib/ecto/repo/schema.ex:109 anonymous fn/5 in Ecto.Repo.Schema.init_mapper/3
 elixir lib/enum.ex:1437 anonymous fn/3 in Enum.map_reduce/3
 stdlib maps.erl:257 :maps.fold_1/3
 elixir lib/enum.ex:1956 Enum.map_reduce/3
 ecto lib/ecto/repo/schema.ex:81 anonymous fn/5 in Ecto.Repo.Schema.extract_header_and_fields/5
 elixir lib/enum.ex:1431 Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
 ecto lib/ecto/repo/schema.ex:80 Ecto.Repo.Schema.extract_header_and_fields/5
 ecto lib/ecto/repo/schema.ex:44 Ecto.Repo.Schema.do_insert_all/6
 ex_audit lib/repo/schema.ex:34 anonymous fn/4 in ExAudit.Schema.update/4
 ex_audit lib/repo/schema.ex:155 ExAudit.Schema.run_in_multi/4
 ecto lib/ecto/multi.ex:579 Ecto.Multi.apply_operation/5
 elixir lib/enum.ex:1940 Enum."-reduce/3-lists^foldl/2-0-"/3
 ecto lib/ecto/multi.ex:563 anonymous fn/5 in Ecto.Multi.apply_operations/5
 ecto_sql lib/ecto/adapters/sql.ex:874 anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
 db_connection lib/db_connection.ex:1415 DBConnection.run_transaction/4
 ecto lib/ecto/repo/transaction.ex:15 Ecto.Repo.Transaction.transaction/4

Thanks !

spapas commented 5 years ago

Please notice that this issue can be resolved by changing line 56 of ex_audit/lib/tracking/tracking.ex:

from now = DateTime.utc_now() to now = DateTime.utc_now() |> DateTime.truncate(:second)

narrowtux commented 5 years ago

I see, the problem is it needs to be configurable because some people are using the _usec variants of the datetimes.

spapas commented 5 years ago

Hello @narrowtux, do you have any timeline on when will this bug be fixed?

TIA

aerosol commented 5 years ago

You can solve this by migrating your recorded_at to :utc_datetime_usec

spapas commented 5 years ago

Thanks @aerosol ... I was solving it till now by actually implementing the solution I described in my previous comment (https://github.com/ZennerIoT/ex_audit/issues/14#issuecomment-488563208)

ashneyderman commented 5 years ago

@spapas - @aerosol suggestion required no code change at all in the library's code base - you only change the schema in your code base. As was noted in https://github.com/ZennerIoT/ex_audit/issues/14#issuecomment-488592623 this needs to be configurable. I.e some people will want microseconds others will not (although, I think a smart thing to do here is to stick with as much precision as possible).

spapas commented 5 years ago

Well at least you need to add a small clarification in the README to help any new users having this issue.