This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squash accounts balance history table
- Loading branch information
1 parent
2f83aa2
commit 657aec8
Showing
11 changed files
with
123 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule ExMoney.Accounts do | ||
@moduledoc """ | ||
The Accounts context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias ExMoney.Repo | ||
|
||
alias ExMoney.Accounts.BalanceHistory | ||
|
||
def get_account_history_balance(from, to) do | ||
{:ok, from} = Date.from_iso8601(from) | ||
erl_from = Date.to_erl(from) | ||
{:ok, naive_from} = NaiveDateTime.from_erl({erl_from, {0, 0, 0}}) | ||
|
||
{:ok, to} = Date.from_iso8601(to) | ||
erl_to = Date.to_erl(to) | ||
{:ok, naive_to} = NaiveDateTime.from_erl({erl_to, {0, 0, 0}}) | ||
|
||
query = | ||
from h in BalanceHistory, | ||
where: h.inserted_at >= ^naive_from, | ||
where: h.inserted_at <= ^naive_to | ||
|
||
Repo.all(query) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule ExMoney.Accounts.BalanceHistory do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "accounts_balance_history" do | ||
field :state, :map | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, ~w(state)a) | ||
|> validate_required(~w(state)a) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule Mix.Tasks.ExMoney.AccountsHistoryBalanceState do | ||
use Mix.Task | ||
import Mix.Ecto | ||
import Ecto.Query, warn: false | ||
|
||
alias ExMoney.{Repo, Accounts.BalanceHistory} | ||
|
||
@shortdoc "Move balance history entries into `state` map field to reduce number of rows" | ||
|
||
def run(_args) do | ||
ensure_repo(ExMoney.Repo, []) | ||
ensure_started(ExMoney.Repo, []) | ||
|
||
query = from h in BalanceHistory, | ||
group_by: fragment("inserted_at::timestamp::date"), | ||
select: %{ | ||
min_id: min(h.id), | ||
grouped_json: fragment("json_agg(json_build_object('id', id, 'account_id', account_id, 'balance', balance))") | ||
} | ||
|
||
h = Repo.all(query) | ||
|
||
Enum.each h, fn(%{min_id: id, grouped_json: grouped_json}) -> | ||
accounts_state = Enum.reduce grouped_json, %{}, fn(row, acc) -> | ||
Map.put(acc, row["account_id"], row["balance"]) | ||
end | ||
|
||
ids_to_remove = Enum.reduce grouped_json, [], fn(row, acc) -> | ||
if row["id"] != id do | ||
[row["id"] | acc] | ||
else | ||
acc | ||
end | ||
end | ||
|
||
Repo.get(BalanceHistory, id) | ||
|> BalanceHistory.changeset(%{state: accounts_state}) | ||
|> Repo.update! | ||
|
||
(from h in BalanceHistory, where: h.id in ^ids_to_remove) |> Repo.delete_all | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20180211210647_add_state_to_balance_history.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule ExMoney.Repo.Migrations.AddStateToBalanceHistory do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:accounts_balance_history) do | ||
add :state, :map | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters