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

keep inserted_at value to the original value #36

Merged
merged 8 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: elixir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

elixir:
- 1.7
otp_release:
- 20.2.4
env:
- MIX_ENV=test
Copy link
Member

@samhstn samhstn Feb 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think you need this env, you've already written it below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, thanks @samhstn 👍

cache:
directories:
- _build
- deps
services:
- postgresql
env:
global:
- MIX_ENV=test
before_script:
- mix do ecto.create, ecto.migrate
script:
- mix test
9 changes: 4 additions & 5 deletions lib/alog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ defmodule Alog do
from(
m in __MODULE__,
where: m.entry_id == ^entry_id,
order_by: [desc: :inserted_at],
order_by: [desc: :updated_at],
limit: 1,
select: m
)
Expand Down Expand Up @@ -179,7 +179,7 @@ defmodule Alog do
end
end)
end).()
|> order_by([m], desc: m.inserted_at)
|> order_by([m], desc: m.updated_at)
|> distinct([m], m.entry_id)
|> select([m], m)

Expand Down Expand Up @@ -209,7 +209,6 @@ defmodule Alog do
|> Map.get(:data)
|> @repo.preload(__MODULE__.__schema__(:associations))
|> Map.put(:id, nil)
|> Map.put(:inserted_at, nil)
|> Map.put(:updated_at, nil)

changeset
Expand Down Expand Up @@ -251,7 +250,7 @@ defmodule Alog do
sub =
from(m in __MODULE__,
distinct: m.entry_id,
order_by: [desc: :inserted_at],
order_by: [desc: :updated_at],
select: m
)

Expand Down Expand Up @@ -346,7 +345,7 @@ defmodule Alog do
sub =
from(mod in Map.get(module.__schema__(:association, assoc), :queryable),
distinct: mod.entry_id,
order_by: [desc: :inserted_at],
order_by: [desc: :updated_at],
select: mod
)

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Alog.MixProject do
def project do
[
app: :alog,
version: "0.4.2",
version: "0.4.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be 0.5.0 now, as we've changed the behaviour of the module

Copy link
Member Author

@SimonLab SimonLab Feb 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about the version. For me it's a patch fix as the API hasn't changed and I don't think this PR add a new functionality, I see keeping the inserted_at value as a bug fix

MAJOR version when you make incompatible API changes,
MINOR version when you add functionality in a backwards-compatible manner, and
PATCH version when you make backwards-compatible bug fixes.

but happy to to update to 0.5.0 if you think the logic of the code is different 👍 , let me know what you prefer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see how this could be considered a bug, but I think it changes enough that it could introduce unexpected errors into code that uses it, eg. someone could have been using inserted_by to get the most recent items in their database, and now that won't work the same.

I think just to be safe we should bump to 0.5.0

https://xkcd.com/1172/

elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
9 changes: 9 additions & 0 deletions test/all_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ defmodule AlogTest.AllTest do

assert length(User.all()) == 2
end

test "all return inserted_at original value" do
{:ok, user} = %User{} |> User.changeset(Helpers.user_3_params()) |> User.insert()
{:ok, user_updated} = user |> User.changeset(%{postcode: "W2 3EC"}) |> User.update()

[user_all] = User.all()
assert user_all.inserted_at == user.inserted_at
assert user_all.postcode == user_updated.postcode
end
end
end
4 changes: 2 additions & 2 deletions test/get_by_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule AlogTest.GetByTest do

test "does not retrieve outdated results" do
{:ok, user} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert()
{:ok, updated_user} = user |> User.changeset(%{postcode: "EC3 RST"}) |> User.update()
{:ok, _updated_user} = user |> User.changeset(%{postcode: "EC3 RST"}) |> User.update()

assert User.get_by(postcode: "E2 0SY") == nil
end
Expand All @@ -58,7 +58,7 @@ defmodule AlogTest.GetByTest do
|> User.changeset(Map.put(Helpers.user_2_params(), :postcode, "E2 0SY"))
|> User.insert()

{:ok, updated_user_2} = user_2 |> User.changeset(%{postcode: "EC3 RST"}) |> User.update()
{:ok, _} = user_2 |> User.changeset(%{postcode: "EC3 RST"}) |> User.update()

assert User.get_by(postcode: "E2 0SY") == user
end
Expand Down
2 changes: 2 additions & 0 deletions test/support/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Alog.TestApp.Helpers do

def user_2_params(), do: %{name: "Loki", username: "mschfmkr", postcode: "E1 6DR"}

def user_3_params(), do: %{name: "Bob", username: "bobuser", postcode: "E1 7DR"}

def seed_data() do
{:ok, item_type} = %ItemType{} |> ItemType.changeset(%{type: "Weapon"}) |> ItemType.insert()

Expand Down