Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions lib/migration_compile_cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule AshPostgres.MigrationCompileCache do
@moduledoc """
A cache for the compiled migrations.

This is used to avoid recompiling the migration files
every time a migration is run, as well as ensuring that
migrations are compiled sequentially.

This is important because otherwise there is a race condition
where two invocations could be compiling the same migration at
once, which would error out.
"""

def start_link(opts \\ %{}) do
Agent.start_link(fn -> opts end, name: __MODULE__)
end

@doc """
Compile a file, caching the result for future calls.
"""
def compile_file(file) do
Agent.get_and_update(__MODULE__, fn state ->
new_state = ensure_compiled(state, file)
{Map.get(new_state, file), new_state}
end)
end

defp ensure_compiled(state, file) do
case Map.get(state, file) do
nil ->
compiled = Code.compile_file(file)
Map.put(state, file, compiled)
_ ->
state
end
end

end
7 changes: 6 additions & 1 deletion lib/multitenancy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule AshPostgres.MultiTenancy do
end

defp load_migration!({version, _, file}) when is_binary(file) do
loaded_modules = file |> Code.compile_file() |> Enum.map(&elem(&1, 0))
loaded_modules = file |> compile_file() |> Enum.map(&elem(&1, 0))

if mod = Enum.find(loaded_modules, &migration?/1) do
{version, mod}
Expand All @@ -70,6 +70,11 @@ defmodule AshPostgres.MultiTenancy do
"file #{Path.relative_to_cwd(file)} does not define an Ecto.Migration"
end
end

defp compile_file(file) do
AshPostgres.MigrationCompileCache.start_link()
AshPostgres.MigrationCompileCache.compile_file(file)
end

defp migration?(mod) do
function_exported?(mod, :__migration__, 0)
Expand Down