forked from bors-ng/bors-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When creating an elixir release, the database adapter is hardcoded into `sys.config`. With the hardcoded database, the docker image is strictly set to Postgres. This change allows to set the database url to a MySQL host by environment variable, so the current docker image can connect to MySQL and use the appropiate Ecto Adapter. Co-authored-by: Björn Brauer <[email protected]> Co-authored-by: Theodor Fiedler <[email protected]> Co-authored-by: Philipp Hinrichsen <[email protected]> Co-authored-by: Markus Wolf <[email protected]>
- Loading branch information
1 parent
dda0128
commit 54ab7b5
Showing
19 changed files
with
278 additions
and
82 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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,48 +1,55 @@ | ||
defmodule BorsNG.Database.Repo do | ||
defmodule DynamicEctoRepoWrapper do | ||
@moduledoc """ | ||
An ecto data repository; | ||
this process interacts with your persistent database. | ||
Do not confuse this with a GitHub repo. | ||
We call those `Project`s internally. | ||
Exposes a macro to define Ecto.Repo functions dynamically. | ||
""" | ||
defmacro create_ecto_repo_callback(args, name) do | ||
quote bind_quoted: [args: args, name: name] do | ||
def unquote(name)(unquote_splicing(args)) do | ||
repo = :persistent_term.get(:db_repo) | ||
apply(repo, unquote(name), unquote(args)) | ||
end | ||
end | ||
end | ||
|
||
use Ecto.Repo, | ||
otp_app: :bors, | ||
adapter: Application.get_env(:bors, BorsNG.Database.Repo)[:adapter] | ||
def create_ecto_repo_callback_args(_, 0) do | ||
[] | ||
end | ||
|
||
def init(_, config) do | ||
# Backwards compatibility hack: if POSTGRES_HOST is set, and the database URL is left at default, | ||
# use the older configuration. | ||
config = Confex.Resolver.resolve!(config) | ||
no_host = is_nil(System.get_env("POSTGRES_HOST")) | ||
def create_ecto_repo_callback_args(module, arity) do | ||
Enum.map(1..arity, &Macro.var(:"arg#{&1}", module)) | ||
end | ||
end | ||
|
||
config = | ||
case config[:url] do | ||
_ when no_host -> | ||
config | ||
defmodule BorsNG.Database.Repo do | ||
@moduledoc """ | ||
This is an Ecto.Repo wrapper that defines all callback functions. | ||
""" | ||
import DynamicEctoRepoWrapper | ||
|
||
"postgresql://postgres:Postgres1234@localhost/bors_dev" -> | ||
[ | ||
adapter: Ecto.Adapters.Postgres, | ||
username: "postgres", | ||
password: "Postgres1234", | ||
database: "bors_dev", | ||
hostname: {:system, "POSTGRES_HOST", "localhost"}, | ||
pool_size: 10 | ||
] | ||
@ecto_repo_callbacks Path.join([__DIR__, "repo_callbacks.txt"]) | ||
|> File.read!() | ||
|> String.trim() | ||
|> String.split("\n") | ||
|> Enum.map(fn line -> | ||
[line, arity] = line |> String.split("/") |> Enum.map(&String.trim(&1)) | ||
{String.to_atom(line), String.to_integer(arity)} | ||
end) | ||
|
||
"postgresql://postgres:Postgres1234@localhost/bors_test" -> | ||
[ | ||
adapter: Ecto.Adapters.Postgres, | ||
username: "postgres", | ||
password: "Postgres1234", | ||
database: "bors_test", | ||
hostname: {:system, "POSTGRES_HOST", "localhost"}, | ||
pool_size: 10 | ||
] | ||
end | ||
@ecto_repo_callbacks | ||
|> Enum.flat_map(fn | ||
{callback, arity} when arity >= 1 -> | ||
is_defined = Enum.any?(@ecto_repo_callbacks, fn fun -> fun == {callback, arity - 1} end) | ||
|
||
{:ok, config} | ||
end | ||
if is_defined, | ||
do: [{callback, arity}], | ||
else: [{callback, arity}, {callback, arity - 1}] | ||
|
||
{callback, arity} -> | ||
[{callback, arity}] | ||
end) | ||
|> Enum.map(fn {callback, arity} -> | ||
__MODULE__ | ||
|> create_ecto_repo_callback_args(arity) | ||
|> create_ecto_repo_callback(callback) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
__adapter__/0 | ||
aggregate/3 | ||
aggregate/4 | ||
all/2 | ||
checked_out?/0 | ||
checkout/2 | ||
config/0 | ||
default_options/1 | ||
delete/2 | ||
delete!/2 | ||
delete_all/2 | ||
exists?/2 | ||
get/3 | ||
get!/3 | ||
get_by/3 | ||
get_by!/3 | ||
get_dynamic_repo/0 | ||
in_transaction?/0 | ||
init/2 | ||
insert/2 | ||
insert!/2 | ||
insert_all/3 | ||
insert_or_update/2 | ||
insert_or_update!/2 | ||
load/2 | ||
one/2 | ||
one!/2 | ||
preload/3 | ||
prepare_query/3 | ||
put_dynamic_repo/1 | ||
reload/2 | ||
reload!/2 | ||
rollback/1 | ||
start_link/1 | ||
stop/1 | ||
stream/2 | ||
transaction/2 | ||
update/2 | ||
update!/2 | ||
update_all/3 |
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,19 @@ | ||
defmodule BorsNG.Database.RepoMysql do | ||
@moduledoc """ | ||
An ecto data repository; | ||
this process interacts with your persistent database. | ||
Do not confuse this with a GitHub repo. | ||
We call those `Project`s internally. | ||
""" | ||
|
||
use Ecto.Repo, | ||
otp_app: :bors, | ||
adapter: Ecto.Adapters.MyXQL | ||
|
||
def init(_, config) do | ||
config = Confex.Resolver.resolve!(config) | ||
|
||
{:ok, config} | ||
end | ||
end |
Oops, something went wrong.