-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1d03ef
commit 873dc13
Showing
2 changed files
with
162 additions
and
0 deletions.
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,33 @@ | ||
defmodule Support.ConnectionHelpers do | ||
@doc """ | ||
Reduces and intersperses a list in one pass. | ||
""" | ||
def intersperse_reduce(list, separator, user_acc, reducer, acc \\ []) | ||
|
||
def intersperse_reduce([], _separator, user_acc, _reducer, acc), | ||
do: {acc, user_acc} | ||
|
||
def intersperse_reduce([elem], _separator, user_acc, reducer, acc) do | ||
{elem, user_acc} = reducer.(elem, user_acc) | ||
{[acc | elem], user_acc} | ||
end | ||
|
||
def intersperse_reduce([elem | rest], separator, user_acc, reducer, acc) do | ||
{elem, user_acc} = reducer.(elem, user_acc) | ||
intersperse_reduce(rest, separator, user_acc, reducer, [acc, elem, separator]) | ||
end | ||
|
||
@doc """ | ||
Maps and intersperses at list in one pass. | ||
""" | ||
def intersperse_map(list, separator, mapper, acc \\ []) | ||
|
||
def intersperse_map([], _separator, _mapper, acc), | ||
do: acc | ||
|
||
def intersperse_map([elem], _separator, mapper, acc), | ||
do: [acc | mapper.(elem)] | ||
|
||
def intersperse_map([elem | rest], separator, mapper, acc), | ||
do: intersperse_map(rest, separator, mapper, [acc, mapper.(elem), separator]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
defmodule MigrationsAgent do | ||
use Agent | ||
|
||
def start_link(versions) do | ||
Agent.start_link(fn -> versions end, name: __MODULE__) | ||
end | ||
|
||
def get do | ||
Agent.get(__MODULE__, & &1) | ||
end | ||
|
||
def up(version, opts) do | ||
Agent.update(__MODULE__, &[{version, opts[:prefix]} | &1]) | ||
end | ||
|
||
def down(version, opts) do | ||
Agent.update(__MODULE__, &List.delete(&1, {version, opts[:prefix]})) | ||
end | ||
end | ||
|
||
defmodule EctoSQL.TestAdapter do | ||
@behaviour Ecto.Adapter | ||
@behaviour Ecto.Adapter.Queryable | ||
@behaviour Ecto.Adapter.Schema | ||
@behaviour Ecto.Adapter.Transaction | ||
@behaviour Ecto.Adapter.Migration | ||
|
||
defmacro __before_compile__(_opts), do: :ok | ||
def ensure_all_started(_, _), do: {:ok, []} | ||
|
||
def init(_opts) do | ||
child_spec = Supervisor.child_spec({Task, fn -> :timer.sleep(:infinity) end}, []) | ||
{:ok, child_spec, %{meta: :meta}} | ||
end | ||
|
||
def checkout(_, _, _), do: raise("not implemented") | ||
def checked_out?(_), do: raise("not implemented") | ||
def delete(_, _, _, _, _), do: raise("not implemented") | ||
def insert_all(_, _, _, _, _, _, _, _), do: raise("not implemented") | ||
def rollback(_, _), do: raise("not implemented") | ||
def stream(_, _, _, _, _), do: raise("not implemented") | ||
def update(_, _, _, _, _, _), do: raise("not implemented") | ||
|
||
## Types | ||
|
||
def loaders(_primitive, type), do: [type] | ||
def dumpers(_primitive, type), do: [type] | ||
def autogenerate(_), do: nil | ||
|
||
## Queryable | ||
|
||
def prepare(operation, query), do: {:nocache, {operation, query}} | ||
|
||
# Migration emulation | ||
|
||
def execute(_, _, {:nocache, {:all, query}}, _, opts) do | ||
%{from: %{source: {"schema_migrations", _}}} = query | ||
true = opts[:schema_migration] | ||
versions = MigrationsAgent.get() | ||
{length(versions), Enum.map(versions, &[elem(&1, 0)])} | ||
end | ||
|
||
def execute(_, _, {:nocache, {:delete_all, query}}, params, opts) do | ||
%{from: %{source: {"schema_migrations", _}}} = query | ||
[version] = params | ||
true = opts[:schema_migration] | ||
MigrationsAgent.down(version, opts) | ||
{1, nil} | ||
end | ||
|
||
def insert(_, %{source: "schema_migrations"}, val, _, _, opts) do | ||
true = opts[:schema_migration] | ||
version = Keyword.fetch!(val, :version) | ||
MigrationsAgent.up(version, opts) | ||
{:ok, []} | ||
end | ||
|
||
def in_transaction?(_), do: Process.get(:in_transaction?) || false | ||
|
||
def transaction(mod, _opts, fun) do | ||
Process.put(:in_transaction?, true) | ||
send(test_process(), {:transaction, mod, fun}) | ||
{:ok, fun.()} | ||
after | ||
Process.put(:in_transaction?, false) | ||
end | ||
|
||
## Migrations | ||
|
||
def lock_for_migrations(mod, opts, fun) do | ||
send(test_process(), {:lock_for_migrations, mod, fun, opts}) | ||
fun.() | ||
end | ||
|
||
def execute_ddl(_, command, _) do | ||
Process.put(:last_command, command) | ||
{:ok, [{:info, "execute ddl", %{command: command}}]} | ||
end | ||
|
||
def supports_ddl_transaction? do | ||
get_config(:supports_ddl_transaction?, false) | ||
end | ||
|
||
defp test_process do | ||
get_config(:test_process, self()) | ||
end | ||
|
||
defp get_config(name, default) do | ||
:ecto_sql | ||
|> Application.get_env(__MODULE__, []) | ||
|> Keyword.get(name, default) | ||
end | ||
end | ||
|
||
defmodule EctoSQL.TestRepo do | ||
use Ecto.Repo, otp_app: :ecto_sql, adapter: EctoSQL.TestAdapter | ||
|
||
def default_options(_operation) do | ||
Process.get(:repo_default_options, []) | ||
end | ||
end | ||
|
||
defmodule EctoSQL.MigrationTestRepo do | ||
use Ecto.Repo, otp_app: :ecto_sql, adapter: EctoSQL.TestAdapter | ||
end | ||
|
||
EctoSQL.TestRepo.start_link() | ||
EctoSQL.TestRepo.start_link(name: :tenant_db) | ||
EctoSQL.MigrationTestRepo.start_link() |