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

Update to Ecto 3.0 #728

Merged
merged 14 commits into from Oct 17, 2018

Keep conn manipulation outside of the transaction

  • Loading branch information...
wojtekmach committed Oct 10, 2018
commit 0929cd9e09f5511652db4c90a3ba451a752a1428
@@ -278,12 +278,34 @@ defmodule Hexpm.Web.Dashboard.OrganizationController do
end
def create(conn, params) do
case do_create(conn.assigns.current_user, params, audit_data(conn)) do
{:ok, organization} ->
conn
|> put_flash(:info, "Organization created with one month free trial period active.")
|> redirect(to: Routes.organization_path(conn, :show, organization))
{:error, {:organization, changeset}} ->
conn
|> put_status(400)
|> render_new(changeset: changeset, params: params)
{:error, {:billing, reason}} ->
changeset = Organization.changeset(%Organization{}, params["organization"])
conn
|> put_status(400)
|> put_flash(:error, "Oops, something went wrong! Please check the errors below.")
|> render_new(
changeset: changeset,
params: params,
errors: reason["errors"]
)
end
end
defp do_create(user, params, audit_data) do
Hexpm.Repo.transaction(fn ->
case Organizations.create(
conn.assigns.current_user,
params["organization"],
audit: audit_data(conn)
) do
case Organizations.create(user, params["organization"], audit: audit_data) do
{:ok, organization} ->
billing_params =
Map.take(params, ["email", "person", "company"])
@@ -294,32 +316,16 @@ defmodule Hexpm.Web.Dashboard.OrganizationController do
case Hexpm.Billing.create(billing_params) do
{:ok, _} ->
conn
|> put_flash(:info, "Organization created with one month free trial period active.")
|> redirect(to: Routes.organization_path(conn, :show, organization))
organization
{:error, reason} ->
changeset = Organization.changeset(%Organization{}, params["organization"])
conn
|> put_status(400)
|> put_flash(:error, "Oops, something went wrong! Please check the errors below.")
|> render_new(
changeset: changeset,
params: params,
errors: reason["errors"]
)
|> Hexpm.Repo.rollback()
Hexpm.Repo.rollback({:billing, reason})
end
{:error, changeset} ->
conn
|> put_status(400)
|> render_new(changeset: changeset, params: params)
|> Hexpm.Repo.rollback()
Hexpm.Repo.rollback({:organization, changeset})
end
end)
|> elem(1)
end
defp update_billing(conn, organization, params, fun) do
View
@@ -24,7 +24,7 @@ defmodule Hexpm.Web.Session do
session = Session.build(data)
session =
if run?() and Repo.write_mode?() do
if Repo.write_mode?() do
Repo.insert!(session)
else
Ecto.Changeset.apply_changes(session)
@@ -62,18 +62,4 @@ defmodule Hexpm.Web.Session do
defp build_cookie(id, token) do
"#{id}++#{Base.url_encode64(token)}"
end
# Hack around transaction being rolled back in tests
if Mix.env() == :test do
defp run?() do
conn = Process.get({Ecto.Adapters.SQL, Hexpm.RepoBase.Pool})
case conn && Process.get({DBConnection, conn.conn_ref}) do
{:failed, _} -> false
_ -> true
end
end
else
defp run?(), do: true
end
end
ProTip! Use n and p to navigate between commits in a pull request.