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

Experiment: Support for creating unlogged tables to speed up tests #601

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,9 @@ if Code.ensure_loaded?(Postgrex) do
table_name = quote_name(table.prefix, table.name)

query = [
"CREATE TABLE ",
"CREATE ",
if_do(table.unlogged, "UNLOGGED "),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for the future: we should probably call this "table.modifier" or something, because there are other options, such as temporary tables in MySQL, global/local, etc. It should be a plain string.

"TABLE ",
if_do(command == :create_if_not_exists, "IF NOT EXISTS "),
table_name,
?\s,
Expand Down
3 changes: 3 additions & 0 deletions lib/ecto/adapters/sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ defmodule Ecto.Adapters.SQL do
IO.warn(message)
end

# TODO: warn if :create_unlogged_tables and not in tests
# TODO: warn if :create_unlogged_tables and adapter other than Postgrex

config
|> Keyword.delete(:name)
|> Keyword.update(:pool, DBConnection.ConnectionPool, &normalize_pool/1)
Expand Down
15 changes: 13 additions & 2 deletions lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,22 @@ defmodule Ecto.Migration do

To define a table in a migration, see `Ecto.Migration.table/2`.
"""
defstruct name: nil, prefix: nil, comment: nil, primary_key: true, engine: nil, options: nil
defstruct name: nil,
prefix: nil,
comment: nil,
primary_key: true,
engine: nil,
options: nil,
unlogged: false

@type t :: %__MODULE__{
name: String.t(),
prefix: String.t() | nil,
comment: String.t() | nil,
primary_key: boolean | keyword(),
engine: atom,
options: String.t()
options: String.t(),
unlogged: boolean
}
end

Expand Down Expand Up @@ -560,6 +567,8 @@ defmodule Ecto.Migration do
defp expand_create(object, command, block) do
quote do
table = %Table{} = unquote(object)
unlogged = Runner.repo_config(:create_unlogged_tables, false)
table = %Table{table | unlogged: unlogged}
Runner.start_command({unquote(command), Ecto.Migration.__prefix__(table)})

if primary_key = Ecto.Migration.__primary_key__(table) do
Expand Down Expand Up @@ -623,6 +632,8 @@ defmodule Ecto.Migration do
end

def create(%Table{} = table) do
unlogged = Runner.repo_config(:create_unlogged_tables, table.unlogged)
table = %Table{table | unlogged: unlogged}
do_create(table, :create)
table
end
Expand Down
18 changes: 14 additions & 4 deletions lib/ecto/migration/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,21 @@ defmodule Ecto.Migration.Runner do
defp command(ddl) when is_binary(ddl) or is_list(ddl),
do: "execute #{inspect(ddl)}"

defp command({:create, %Table{} = table, _}),
do: "create table #{quote_name(table.prefix, table.name)}"
defp command({:create, %Table{} = table, _}) do
if repo_config(:create_unlogged_tables, false) do
"create unlogged table #{quote_name(table.prefix, table.name)}"
else
"create table #{quote_name(table.prefix, table.name)}"
end
end

defp command({:create_if_not_exists, %Table{} = table, _}),
do: "create table if not exists #{quote_name(table.prefix, table.name)}"
defp command({:create_if_not_exists, %Table{} = table, _}) do
if repo_config(:create_unlogged_tables, false) do
"create unlogged table if not exists #{quote_name(table.prefix, table.name)}"
else
"create table if not exists #{quote_name(table.prefix, table.name)}"
end
end

defp command({:alter, %Table{} = table, _}),
do: "alter table #{quote_name(table.prefix, table.name)}"
Expand Down
19 changes: 19 additions & 0 deletions test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ defmodule Ecto.MigrationTest do
{:create, table, [{:add, :id, :bigserial, [primary_key: true]}]}
end

@tag repo_config: [create_unlogged_tables: true]
test "create unlogged table" do
create table = table(:posts)
flush()

assert last_command() ==
{:create, %Table{table | unlogged: true},
[{:add, :id, :bigserial, [primary_key: true]}]}
end

test "alters a table" do
alter table(:posts) do
add :summary, :text
Expand Down Expand Up @@ -721,6 +731,15 @@ defmodule Ecto.MigrationTest do
end
end

@tag repo_config: [create_unlogged_tables: true]
test "creates unlogged table" do
create(table(:posts))
flush()

{_, table, _} = last_command()
assert table.unlogged == true
end

test "drops a table with prefix from migration" do
drop(table(:posts, prefix: "foo"))
flush()
Expand Down