Skip to content

Commit 5f8831e

Browse files
committed
WIP
1 parent 34935bb commit 5f8831e

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

lib/ecto/adapters/postgres/connection.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,9 @@ if Code.ensure_loaded?(Postgrex) do
12391239
table_name = quote_name(table.prefix, table.name)
12401240

12411241
query = [
1242-
"CREATE TABLE ",
1242+
"CREATE ",
1243+
if_do(table.unlogged, "UNLOGGED "),
1244+
"TABLE ",
12431245
if_do(command == :create_if_not_exists, "IF NOT EXISTS "),
12441246
table_name,
12451247
?\s,

lib/ecto/adapters/sql.ex

+3
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ defmodule Ecto.Adapters.SQL do
871871
IO.warn(message)
872872
end
873873

874+
# TODO: warn if :create_unlogged_tables and not in tests
875+
# TODO: warn if :create_unlogged_tables and adapter other than Postgrex
876+
874877
config
875878
|> Keyword.delete(:name)
876879
|> Keyword.update(:pool, DBConnection.ConnectionPool, &normalize_pool/1)

lib/ecto/migration.ex

+13-2
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,22 @@ defmodule Ecto.Migration do
416416
417417
To define a table in a migration, see `Ecto.Migration.table/2`.
418418
"""
419-
defstruct name: nil, prefix: nil, comment: nil, primary_key: true, engine: nil, options: nil
419+
defstruct name: nil,
420+
prefix: nil,
421+
comment: nil,
422+
primary_key: true,
423+
engine: nil,
424+
options: nil,
425+
unlogged: false
420426

421427
@type t :: %__MODULE__{
422428
name: String.t(),
423429
prefix: String.t() | nil,
424430
comment: String.t() | nil,
425431
primary_key: boolean | keyword(),
426432
engine: atom,
427-
options: String.t()
433+
options: String.t(),
434+
unlogged: boolean
428435
}
429436
end
430437

@@ -560,6 +567,8 @@ defmodule Ecto.Migration do
560567
defp expand_create(object, command, block) do
561568
quote do
562569
table = %Table{} = unquote(object)
570+
unlogged = Runner.repo_config(:create_unlogged_tables, false)
571+
table = %Table{table | unlogged: unlogged}
563572
Runner.start_command({unquote(command), Ecto.Migration.__prefix__(table)})
564573

565574
if primary_key = Ecto.Migration.__primary_key__(table) do
@@ -623,6 +632,8 @@ defmodule Ecto.Migration do
623632
end
624633

625634
def create(%Table{} = table) do
635+
unlogged = Runner.repo_config(:create_unlogged_tables, table.unlogged)
636+
table = %Table{table | unlogged: unlogged}
626637
do_create(table, :create)
627638
table
628639
end

lib/ecto/migration/runner.ex

+14-4
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,21 @@ defmodule Ecto.Migration.Runner do
432432
defp command(ddl) when is_binary(ddl) or is_list(ddl),
433433
do: "execute #{inspect(ddl)}"
434434

435-
defp command({:create, %Table{} = table, _}),
436-
do: "create table #{quote_name(table.prefix, table.name)}"
435+
defp command({:create, %Table{} = table, _}) do
436+
if repo_config(:create_unlogged_tables, false) do
437+
"create unlogged table #{quote_name(table.prefix, table.name)}"
438+
else
439+
"create table #{quote_name(table.prefix, table.name)}"
440+
end
441+
end
437442

438-
defp command({:create_if_not_exists, %Table{} = table, _}),
439-
do: "create table if not exists #{quote_name(table.prefix, table.name)}"
443+
defp command({:create_if_not_exists, %Table{} = table, _}) do
444+
if repo_config(:create_unlogged_tables, false) do
445+
"create unlogged table if not exists #{quote_name(table.prefix, table.name)}"
446+
else
447+
"create table if not exists #{quote_name(table.prefix, table.name)}"
448+
end
449+
end
440450

441451
defp command({:alter, %Table{} = table, _}),
442452
do: "alter table #{quote_name(table.prefix, table.name)}"

test/ecto/migration_test.exs

+19
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,16 @@ defmodule Ecto.MigrationTest do
461461
{:create, table, [{:add, :id, :bigserial, [primary_key: true]}]}
462462
end
463463

464+
@tag repo_config: [create_unlogged_tables: true]
465+
test "create unlogged table" do
466+
create table = table(:posts)
467+
flush()
468+
469+
assert last_command() ==
470+
{:create, %Table{table | unlogged: true},
471+
[{:add, :id, :bigserial, [primary_key: true]}]}
472+
end
473+
464474
test "alters a table" do
465475
alter table(:posts) do
466476
add :summary, :text
@@ -721,6 +731,15 @@ defmodule Ecto.MigrationTest do
721731
end
722732
end
723733

734+
@tag repo_config: [create_unlogged_tables: true]
735+
test "creates unlogged table" do
736+
create(table(:posts))
737+
flush()
738+
739+
{_, table, _} = last_command()
740+
assert table.unlogged == true
741+
end
742+
724743
test "drops a table with prefix from migration" do
725744
drop(table(:posts, prefix: "foo"))
726745
flush()

0 commit comments

Comments
 (0)