Skip to content

Commit

Permalink
Use raw queries for table_exists? check
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed May 5, 2019
1 parent 04cf272 commit dd88ed6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 33 deletions.
10 changes: 1 addition & 9 deletions lib/ecto/adapters/mysql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,7 @@ if Code.ensure_loaded?(Mariaex) do

@impl true
def table_exists_query(table) do
require Ecto.Query

Ecto.Query.from(
t in "tables",
prefix: "information_schema",
where: t.table_name == ^table and t.table_schema == fragment("DATABASE()"),
select: t.table_name,
limit: 1
)
{"SELECT true FROM information_schema.tables WHERE table_name = ? AND table_schema = DATABASE() LIMIT 1", [table]}
end

defp pk_definitions(columns, prefix) do
Expand Down
10 changes: 1 addition & 9 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,7 @@ if Code.ensure_loaded?(MyXQL) do

@impl true
def table_exists_query(table) do
require Ecto.Query

Ecto.Query.from(
t in "tables",
prefix: "information_schema",
where: t.table_name == ^table and t.table_schema == fragment("DATABASE()"),
select: t.table_name,
limit: 1
)
{"SELECT true FROM information_schema.tables WHERE table_name = ? AND table_schema = DATABASE() LIMIT 1", [table]}
end

defp pk_definitions(columns, prefix) do
Expand Down
10 changes: 1 addition & 9 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -801,15 +801,7 @@ if Code.ensure_loaded?(Postgrex) do

@impl true
def table_exists_query(table) do
require Ecto.Query

Ecto.Query.from(
t in "tables",
prefix: "information_schema",
where: t.table_name == ^table and t.table_schema == fragment("current_schema()"),
select: t.table_name,
limit: 1
)
{"SELECT true FROM information_schema.tables WHERE table_name = $1 AND table_schema = current_schema() LIMIT 1", [table]}
end

# From https://www.postgresql.org/docs/9.3/static/protocol-error-fields.html.
Expand Down
10 changes: 4 additions & 6 deletions lib/ecto/adapters/sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,13 @@ defmodule Ecto.Adapters.SQL do
Check if the given `table` exists.
Returns `true` if the `table` exists in the `repo`, otherwise `false`.
The table is checked against the current database/schema in the connection.
"""
@spec table_exists?(Ecto.Repo.t, table :: String.t) :: boolean
def table_exists?(repo, table) when is_atom(repo) do
table_exists?(repo, Ecto.Adapter.lookup_meta(repo), table)
end

defp table_exists?(repo, %{sql: sql}, table) do
sql_query = sql.table_exists_query(table)
repo.one(sql_query) != nil
%{sql: sql} = adapter_meta = Ecto.Adapter.lookup_meta(repo)
{query, params} = sql.table_exists_query(table)
query!(adapter_meta, query, params, []).num_rows != 0
end

## Callbacks
Expand Down

0 comments on commit dd88ed6

Please sign in to comment.