Skip to content

Commit

Permalink
fixing erro 'database in use'
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaric committed Feb 26, 2017
1 parent 86bf3ca commit 68ad522
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lib/tds_ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ defmodule Tds.Ecto do
~s(CREATE DATABASE [#{database}])
|> concat_if(opts[:lc_collate], &"COLLATE=#{&1}")

case run_query(opts, command) do
case run_query(Keyword.put(opts, :database, "master"), command) do
{:ok, _} -> :ok
{:error, %Tds.Error{mssql: %{number: 1801}}} ->
{:error, :already_up}
Expand All @@ -109,7 +109,7 @@ defmodule Tds.Ecto do
def storage_down(opts) do
database = Keyword.fetch!(opts, :database) || raise ":database is nil in repository configuration"

case run_query(opts, "DROP DATABASE [#{database}]") do
case run_query(Keyword.put(opts, :database, "master"), "DROP DATABASE [#{database}]") do
{:ok, _} ->
:ok
{:error, %Tds.Error{mssql: %{number: 3701}}} ->
Expand Down
55 changes: 36 additions & 19 deletions lib/tds_ecto/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if Code.ensure_loaded?(Tds) do
opts = Keyword.put(opts, :parameters, params)

case DBConnection.prepare_execute(pid, query, params, opts) do
{:ok, _, %Tds.Result{columns: nil, command: nil, num_rows: 1, rows: []}} ->
{:ok, %Tds.Result{columns: nil, command: nil, num_rows: 1, rows: nil}}
{:ok, _, query} -> {:ok, query}
{:error, _} = err -> err
end
Expand Down Expand Up @@ -206,32 +208,47 @@ if Code.ensure_loaded?(Tds) do

assemble([delete, from, join, where])
end

# def insert(prefix, table, fields, returning) do
# values =
# if fields == [] do
# returning(returning, "INSERTED") <>
# "DEFAULT VALUES"
# else
# "(" <> Enum.map_join(fields, ", ", &quote_name/1) <> ")" <>
# " " <> returning(returning, "INSERTED") <>
# "VALUES (" <> Enum.map_join(1..length(fields), ", ", &"@#{&1}") <> ")"
# end
# "INSERT INTO #{quote_table(prefix, table)} " <> values
# end
def insert(prefix, table, header, rows, on_conflict, returning) do
Logger.info(inspect(["OVDE", prefix, table, header, rows, on_conflict, returning]))
end
def insert(prefix, table, fields, returning) do
[] = on_conflict(on_conflict, header)
values =
if fields == [] do
if header == [] do
returning(returning, "INSERTED") <>
"DEFAULT VALUES"
"DEFAULT VALUES"
else
"(" <> Enum.map_join(fields, ", ", &quote_name/1) <> ")" <>
" " <> returning(returning, "INSERTED") <>
"VALUES (" <> Enum.map_join(1..length(fields), ", ", &"@#{&1}") <> ")"
"(" <> Enum.map_join(header, ", ", &quote_name/1) <> ")" <>
" " <> returning(returning, "INSERTED") <>
"VALUES " <> insert_all(rows, 1, "")
end
"INSERT INTO #{quote_table(prefix, table)} " <> values
end
def insert(prefix, table, header, rows, returning) do
values =
if header == [] do
returning(returning, "INSERTED") <>
"DEFAULT VALUES"
else
"(" <> Enum.map_join(header, ", ", &quote_name/1) <> ")" <>
" " <> returning(returning, "INSERTED") <>
"VALUES " <> insert_all(rows, 1, "")
end
"INSERT INTO #{quote_table(prefix, table)} " <> values

defp on_conflict({_, _, [_ | _]}, _header) do
error!(nil, "The :conflict_target option is not supported in insert/insert_all by TDS")
end
defp on_conflict({:raise, _, []}, _header) do
[]
end
defp on_conflict({:nothing, _, []}, [field | _]) do
error!(nil, "The :nothing option is not supported in insert/insert_all by TDS")
end
defp on_conflict({:replace_all, _, []}, header) do
error!(nil, "The :replace_all option is not supported in insert/insert_all by TDS")
end
defp on_conflict({query, _, []}, _header) do
error!(nil, "The query as option for on_conflict is not supported in insert/insert_all by TDS yet.")
end

defp insert_all([row|rows], counter, acc) do
Expand Down
46 changes: 23 additions & 23 deletions test/storage_test.exs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
defmodule Tds.Ecto.StorageTest do
use ExUnit.Case, async: false
use ExUnit.Case, async: true

alias Tds.Ecto

def params do
[database: "storage_mgt",
pool: Ecto.Adapters.SQL.Sandbox,
pool: Ecto.Adapters.SQL.Sandbox,
username: "mssql",
password: "mssql",
hostname: "localhost"]
end

def wrong_params do
def wrong_params() do
Keyword.merge params(),
[username: "randomuser",
password: "password1234"]
end

def drop_database do
database = params()[:database]
run_sqlcmd("DROP DATABASE [#{database}];")
def drop_database(params) do
database = params[:database]
run_sqlcmd(params, "DROP DATABASE [#{database}];", ["-d", "master"])
end

def create_database do
database = params()[:database]
run_sqlcmd("CREATE DATABASE [#{database}];")
def create_database(params) do
database = params[:database]
run_sqlcmd(params, "CREATE DATABASE [#{database}];", ["-d", "master"])
end

def create_posts do
run_sqlcmd("CREATE TABLE posts (title nvarchar(20));", ["-d", params()[:database]])
def create_posts(params) do
run_sqlcmd(params, "CREATE TABLE posts (title nvarchar(20));", ["-d", params[:database]])
end

def run_sqlcmd(sql, args \\ []) do
def run_sqlcmd(params, sql, args \\ []) do
args = [
"-U", params()[:username],
"-P", params()[:password],
"-H", params()[:hostname],
"-U", params[:username],
"-P", params[:password],
"-H", params[:hostname],
"-Q", ~s(#{sql}) | args]
# IO.puts(Enum.map_join(args, " ", &"#{&1}"))
System.cmd "sqlcmd", args
Expand All @@ -47,21 +47,21 @@ defmodule Tds.Ecto.StorageTest do
# end

test "storage up (twice in a row)" do
assert Tds.Ecto.storage_up(params()) == :ok
assert Tds.Ecto.storage_up(params()) == {:error, :already_up}
drop_database()
assert :ok == Tds.Ecto.storage_up(params())
assert {:error, :already_up} == Tds.Ecto.storage_up(params())
{_, 0} = drop_database(params())
end

test "storage down (twice in a row)" do
create_database()
assert Tds.Ecto.storage_down(params()) == :ok
assert Tds.Ecto.storage_down(params()) == {:error, :already_down}
{_, 0} = create_database(params())
assert :ok == Tds.Ecto.storage_down(params())
assert {:error, :already_down} == Tds.Ecto.storage_down(params())
end

test "storage up and down (wrong credentials)" do
refute Tds.Ecto.storage_up(wrong_params()) == :ok
create_database()
{_, 0} = create_database(params())
refute Tds.Ecto.storage_down(wrong_params()) == :ok
drop_database()
{_, 0} = drop_database(params())
end
end

0 comments on commit 68ad522

Please sign in to comment.