Skip to content

Commit

Permalink
fix dialects in migration message
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetised committed May 14, 2024
1 parent a22aa96 commit 1ff9f4d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
8 changes: 5 additions & 3 deletions components/electric/lib/electric/postgres/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ defmodule Electric.Postgres.Migration do
@spec to_ops([String.t()], SchemaLoader.Version.t()) ::
{Changes.Migration.Ops.t(), [Electric.Postgres.relation()]}
def to_ops(stmts, schema_version) do
ops = {Changes.Migration.empty_ops(), MapSet.new()}

stmts
|> Enum.reduce({%Changes.Migration.Ops{}, MapSet.new()}, fn stmt, {ops, relations} ->
|> Enum.reduce(ops, fn stmt, {ops, relations} ->
Changes.Migration.dialects()
|> Enum.reduce(ops, fn {key, dialect}, ops ->
|> Enum.reduce({ops, relations}, fn dialect, {ops, relations} ->
{:ok, new_ops, new_relations} = to_op(stmt, schema_version, dialect)

{Map.update!(ops, key, &(&1 ++ new_ops)), Enum.into(new_relations, relations)}
{Map.update!(ops, dialect, &(&1 ++ new_ops)), Enum.into(new_relations, relations)}
end)
end)
|> then(fn {ops, relations} -> {ops, MapSet.to_list(relations)} end)
Expand Down
28 changes: 15 additions & 13 deletions components/electric/lib/electric/replication/changes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,10 @@ defmodule Electric.Replication.Changes do

@relation Electric.Postgres.Extension.ddl_relation()

defmodule Ops do
defstruct sqlite: [], postgres: []

@type t() :: %__MODULE__{
sqlite: [%Electric.Satellite.SatOpMigrate{}],
postgres: [%Electric.Satellite.SatOpMigrate{}]
}
end
@dialects [
Postgres.Dialect.Postgresql,
Postgres.Dialect.SQLite
]

defstruct [
:version,
Expand All @@ -272,20 +268,26 @@ defmodule Electric.Replication.Changes do
relation: @relation
]

@type ops() :: %{
Electric.Postgres.Dialect.t() => [%Electric.Satellite.SatOpMigrate{}]
}

@type t() :: %__MODULE__{
version: SchemaLoader.version(),
schema: SchemaLoader.Version.t(),
ddl: [String.t(), ...],
ops: Ops.t(),
ops: ops(),
relations: [Postgres.relation()],
relation: Postgres.relation()
}

@spec dialects() :: [{atom(), Electric.Postgres.Dialect.t()}]
@spec dialects() :: [Electric.Postgres.Dialect.t()]
def dialects do
[
sqlite: Electric.Postgres.Dialect.SQLite
]
@dialects
end

def empty_ops do
Map.new(@dialects, fn dialect -> {dialect, []} end)
end
end

Expand Down
5 changes: 3 additions & 2 deletions components/electric/lib/electric/satellite/serialization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ defmodule Electric.Satellite.Serialization do

defp serialize_change(%Migration{} = migration, state) do
Logger.info("Serializing migration #{inspect(migration.version)}")
%{known_relations: known_relations, sql_dialect: sql_dialect} = state

known_relations =
Enum.reduce(migration.relations, state.known_relations, fn relation, known ->
Enum.reduce(migration.relations, known_relations, fn relation, known ->
{_relation_id, _columns, _, known} =
load_new_relation(relation, known)

Expand All @@ -122,7 +123,7 @@ defmodule Electric.Satellite.Serialization do

ops =
migration.ops
|> Map.fetch!(:sqlite)
|> Map.fetch!(sql_dialect)
|> Enum.reduce(state.ops, fn op, ops ->
[%SatTransOp{op: {:migrate, op}} | ops]
end)
Expand Down

0 comments on commit 1ff9f4d

Please sign in to comment.