Skip to content

Commit

Permalink
creates a clause to ensure migrations to not fail because of schema_m…
Browse files Browse the repository at this point in the history
…igrations table #45
  • Loading branch information
RobStallion committed Mar 1, 2019
1 parent 7124449 commit ff3f1d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/alog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule Alog do
defdelegate storage_down(opts), to: EAP

# overrides insert/6 defined in Ecto.Adapters.SQL
def insert(adapter_meta, %{source: "schema_migrations", prefix: prefix}, params, on_conflict, returning, opts) do
insert_logic(adapter_meta, "schema_migrations", prefix, params, on_conflict, returning, opts)
end

def insert(adapter_meta, %{source: source, prefix: prefix}, params, on_conflict, returning, opts) do
# converts params from a keyword list to a map
params_map = Enum.into(params, %{})
Expand All @@ -41,10 +45,7 @@ defmodule Alog do
|> Map.put(:entry_id, entry_id)
|> Enum.into([])

{kind, conflict_params, _} = on_conflict
{fields, values} = :lists.unzip(params)
sql = @conn.insert(prefix, source, fields, [fields], on_conflict, returning)
Ecto.Adapters.SQL.struct(adapter_meta, @conn, sql, :insert, source, [], values ++ conflict_params, kind, returning, opts)
insert_logic(adapter_meta, source, prefix, params, on_conflict, returning, opts)
end

# I think that this step need to also make sure that the data is not an exact copy.
Expand All @@ -70,4 +71,11 @@ defmodule Alog do
|> Map.put_new(:inserted_at, NaiveDateTime.utc_now())
|> Map.put_new(:updated_at, NaiveDateTime.utc_now())
end

defp insert_logic(adapter_meta, source, prefix, params, on_conflict, returning, opts) do
{kind, conflict_params, _} = on_conflict
{fields, values} = :lists.unzip(params)
sql = @conn.insert(prefix, source, fields, [fields], on_conflict, returning)
Ecto.Adapters.SQL.struct(adapter_meta, @conn, sql, :insert, source, [], values ++ conflict_params, kind, returning, opts)
end
end
2 changes: 1 addition & 1 deletion test/support/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Alog.TestApp.Comment do
use Ecto.Schema
import Ecto.Changeset

# I'd imagine we'll change string as the type but
# I'd imagine we'll change string as the type
@primary_key {:cid, :string, autogenerate: false}
schema "comments" do
field(:entry_id, :string)
Expand Down

0 comments on commit ff3f1d3

Please sign in to comment.