-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: race condition compiling migrations when concurrently creating n…
…ew tenants (#406)
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
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,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 -> | ||
Check warning on line 22 in lib/migration_compile_cache.ex GitHub Actions / ash-ci (15) / mix credo --strict
Check warning on line 22 in lib/migration_compile_cache.ex GitHub Actions / ash-ci (14) / mix credo --strict
|
||
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 |
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