Skip to content

Commit

Permalink
adds all required colmns, only if they are missing, #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Danwhy committed Feb 19, 2019
1 parent 54187b8 commit c0dadca
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/alog/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ defmodule Alog.Connection do
Ecto.Adapters.Postgres.Connection.execute_ddl({c, table, columns})

_ ->
Ecto.Adapters.Postgres.Connection.execute_ddl(
{c, table, columns ++ [{:add, :cid, :varchar, [primary_key: true]}]}
)
Ecto.Adapters.Postgres.Connection.execute_ddl({c, table, update_columns(columns)})
end
end

Expand Down Expand Up @@ -69,4 +67,21 @@ defmodule Alog.Connection do
end

defdelegate execute_ddl(command), to: Ecto.Adapters.Postgres.Connection

# Add required columns if they are missing
defp update_columns(columns) do
[
{:add, :cid, :string, [primary_key: true]},
{:add, :entry_id, :string, []},
{:add, :deleted, :boolean, []},
{:add, :inserted_at, :naive_datetime_usec, []},
{:add, :updated_at, :naive_datetime_usec, []}
]
|> Enum.reduce(columns, fn {_, c, _, _} = col, acc ->
case Enum.find(acc, fn {_, a, _, _} -> a == c end) do
nil -> acc ++ [col]
_ -> acc
end
end)
end
end

0 comments on commit c0dadca

Please sign in to comment.