Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mgibowski committed Jun 14, 2024
1 parent 34935bb commit 5f8831e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
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 "),
"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

0 comments on commit 5f8831e

Please sign in to comment.