Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert "top2" to "top1" for host url #23

Merged
merged 1 commit into from
Mar 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/fly_postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Fly.Postgres do
end

@doc """
Rewrite the `:url` value to include DNS helpers of "top2.nearest.of" to find
Rewrite the `:url` value to include DNS helpers of "top1.nearest.of" to find
the closes database to target. If the host already contains that, leave it
unchanged. If it is missing, add it and return the updated the url in the
config.
Expand All @@ -42,13 +42,20 @@ defmodule Fly.Postgres do
uri = URI.parse(Keyword.get(config, :url))

# if detected DNS helpers in the URI, return unchanged
if String.contains?(uri.host, ".nearest.of.") do
config
else
# Not detected. Add them to the Host and return new config with replaced
# host that includes DNS helpers
updated_uri = %URI{uri | host: "top2.nearest.of.#{uri.host}"} |> URI.to_string()
Keyword.put(config, :url, updated_uri)
cond do
String.contains?(uri.host, "top1.nearest.of.") ->
config

String.contains?(uri.host, "top2.nearest.of.") ->
new_host = String.replace(uri.host, "top2.nearest.of.", "top1.nearest.of.", global: false)
updated_uri = %URI{uri | host: new_host} |> URI.to_string()
Keyword.put(config, :url, updated_uri)

true ->
# Not detected. Add them to the Host and return new config with replaced
# host that includes DNS helpers
updated_uri = %URI{uri | host: "top1.nearest.of.#{uri.host}"} |> URI.to_string()
Keyword.put(config, :url, updated_uri)
end
end

Expand Down
30 changes: 19 additions & 11 deletions test/fly_postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Fly.PostgresTest do

doctest Fly.Postgres

@url_dns "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5432/some_app"
@url_dns "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5432/some_app"
@url_base "postgres://some-user:some-pass@my-app-db.internal:5432/some_app"

setup do
Expand All @@ -24,13 +24,13 @@ defmodule Fly.PostgresTest do
config = [stuff: "THINGS", url: @url_base]
System.put_env([{"FLY_REGION", "xyz"}, {"DATABASE_URL", @url_base}])
config = Fly.Postgres.rewrite_database_url(config)
assert Keyword.get(config, :url) |> String.contains?("top2.nearest.of.")
assert Keyword.get(config, :url) |> String.contains?("top1.nearest.of.")
end

test "changes port when not in primary region" do
config = [stuff: "THINGS", url: System.get_env("DATABASE_URL")]
# NOTE: Only the port number changed
expected = "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5433/some_app"
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5433/some_app"
updated = Fly.Postgres.rewrite_database_url(config)
assert expected == Keyword.get(updated, :url)
# other things are altered
Expand All @@ -40,7 +40,7 @@ defmodule Fly.PostgresTest do
test "changes port and adds DNS helpers if missing when not in primary region" do
config = [stuff: "THINGS", url: @url_base]
# NOTE: Port number changed and DNS parts added to host
expected = "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5433/some_app"
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5433/some_app"
updated = Fly.Postgres.rewrite_database_url(config)
assert Keyword.get(updated, :url) == expected
end
Expand All @@ -49,15 +49,23 @@ defmodule Fly.PostgresTest do
describe "rewrite_host/1" do
test "adds dns helpers if missing from host" do
config = [stuff: "THINGS", url: @url_base]
expected = "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5432/some_app"
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5432/some_app"
updated = Fly.Postgres.rewrite_host(config)
assert Keyword.get(updated, :url) == expected
end

test "returns unmodified if dns helpers detected" do
config = [stuff: "THINGS", url: @url_dns]
test "returns unmodified if dns top1 helper detected" do
no_change = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5432/some_app"
config = [stuff: "THINGS", url: no_change]
updated = Fly.Postgres.rewrite_host(config)
assert Keyword.get(updated, :url) == @url_dns
assert Keyword.get(updated, :url) == no_change
end

test "changes to top1 if top2 is detected" do
config = [stuff: "THINGS", url: "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5432/some_app"]
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5432/some_app"
updated = Fly.Postgres.rewrite_host(config)
assert Keyword.get(updated, :url) == expected
end
end

Expand All @@ -77,7 +85,7 @@ defmodule Fly.PostgresTest do
config = [stuff: "THINGS", url: @url_dns]
updated = Fly.Postgres.rewrite_replica_port(config)
# NOTE: Port number should change
expected = "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5433/some_app"
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5433/some_app"
assert Keyword.get(updated, :url) == expected
assert @url_dns != expected
end
Expand All @@ -93,15 +101,15 @@ defmodule Fly.PostgresTest do
System.put_env([{"FLY_REGION", "xyz"}])
config = [stuff: "THINGS", url: @url_base]
# NOTE: Only the port number changed
expected = "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5432/some_app"
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5432/some_app"
{:ok, updated} = Fly.Postgres.config_repo_url(config)
assert expected == Keyword.get(updated, :url)
end

test "update url for replica PORT when given" do
config = [stuff: "THINGS", url: System.get_env("DATABASE_URL")]
# NOTE: Only the port number changed
expected = "postgres://some-user:some-pass@top2.nearest.of.my-app-db.internal:5433/some_app"
expected = "postgres://some-user:some-pass@top1.nearest.of.my-app-db.internal:5433/some_app"
{:ok, updated} = Fly.Postgres.config_repo_url(config)
assert expected == Keyword.get(updated, :url)
end
Expand Down