Skip to content

Commit cf79daa

Browse files
committed
migration naming fixes, child_of/descendant_of imprv
1 parent a74c25c commit cf79daa

6 files changed

Lines changed: 34 additions & 31 deletions

File tree

lib/nested_sets.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ defmodule NestedSets do
6666
@type depth :: non_neg_integer() | nil
6767
@type delete_result :: {:ok, non_neg_integer()} | {:error, error_reason()}
6868

69+
@typep position :: :prepend | :append | :before | :after
70+
@typep validation_result :: :ok | {:error, String.t()}
71+
6972
@doc false
7073
def config(node) when is_struct(node), do: config(node.__struct__)
7174

@@ -179,7 +182,9 @@ defmodule NestedSets do
179182
180183
{:ok, count} = NestedSets.delete_with_children(Repo, node)
181184
"""
182-
@spec delete_with_children(Ecto.Repo.t(), ns_node()) :: {:ok, integer()} | {:error, term()}
185+
186+
@spec delete_with_children(Ecto.Repo.t(), ns_node()) ::
187+
{:ok, non_neg_integer()} | {:error, term()}
183188
def delete_with_children(repo, node) do
184189
cfg = config(node)
185190

@@ -214,6 +219,7 @@ defmodule NestedSets do
214219
"""
215220
@spec delete_node(Ecto.Repo.t(), ns_node()) :: {:ok, ns_node()} | {:error, term()}
216221
def delete_node(repo, node) do
222+
# @review: root node deletion
217223
if root?(node) do
218224
{:error, :cannot_delete_root}
219225
else
@@ -303,14 +309,14 @@ defmodule NestedSets do
303309
"""
304310
@spec descendant_of?(ns_node(), ns_node()) :: boolean()
305311
def descendant_of?(node, potential_parent) do
306-
cfg = config(node)
307-
308312
if node.__struct__ != potential_parent.__struct__ do
309313
raise ArgumentError,
310314
"descendant_of?/2 expects both arguments to be structs of the same Schema, " <>
311315
"got #{inspect(node.__struct__)} and #{inspect(potential_parent.__struct__)}"
312316
end
313317

318+
cfg = config(node)
319+
314320
node_lft = Map.get(node, cfg.lft)
315321
node_rgt = Map.get(node, cfg.rgt)
316322
parent_lft = Map.get(potential_parent, cfg.lft)
@@ -330,14 +336,14 @@ defmodule NestedSets do
330336
"""
331337
@spec child_of?(ns_node(), ns_node()) :: boolean()
332338
def child_of?(node, potential_parent) do
333-
cfg = config(node)
334-
335339
if node.__struct__ != potential_parent.__struct__ do
336340
raise ArgumentError,
337341
"child_of?/2 expects both arguments to be structs of the same Schema, " <>
338342
"got #{inspect(node.__struct__)} and #{inspect(potential_parent.__struct__)}"
339343
end
340344

345+
cfg = config(node)
346+
341347
same_scope? =
342348
if cfg.tree != false do
343349
Map.get(node, cfg.tree) == Map.get(potential_parent, cfg.tree)
@@ -370,9 +376,6 @@ defmodule NestedSets do
370376

371377
# Private section
372378

373-
@typep position :: :prepend | :append | :before | :after
374-
@typep validation_result :: :ok | {:error, String.t()}
375-
376379
@spec loaded?(ns_node()) :: boolean()
377380
defp loaded?(%{__meta__: %{state: :loaded}}), do: true
378381
defp loaded?(_), do: false

lib/nested_sets/migration.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule NestedSets.Migration do
2626

2727
use Ecto.Migration
2828

29-
defmacro nested_sets_fields(opts \\ []) do
29+
defmacro nested_sets_columns(opts \\ []) do
3030
quote bind_quoted: [opts: opts] do
3131
add :lft, :integer, null: false
3232
add :rgt, :integer, null: false

test/nested_sets/migration_test.exs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ defmodule NestedSets.MigrationTest do
55

66
alias NestedSets.Test.Repos
77

8-
describe "nested_sets_fields/1" do
8+
describe "nested_sets_columns/1" do
99
for {db_type, repo, tags} <- Repos.list() do
1010
@describetag tags
1111

1212
@tag db: db_type
13-
test "creates basic nested set fields without tree (#{db_type})" do
13+
test "creates basic nested set columns without tree (#{db_type})" do
1414
repo = unquote(repo)
1515
db_type = unquote(db_type)
1616
table = unique_table_name()
@@ -19,7 +19,7 @@ defmodule NestedSets.MigrationTest do
1919

2020
create_table(repo, table, fn ->
2121
quote do
22-
NestedSets.Migration.nested_sets_fields()
22+
NestedSets.Migration.nested_sets_columns()
2323
end
2424
end)
2525

@@ -41,7 +41,7 @@ defmodule NestedSets.MigrationTest do
4141

4242
create_table(repo, table, fn ->
4343
quote do
44-
NestedSets.Migration.nested_sets_fields(use_tree: true)
44+
NestedSets.Migration.nested_sets_columns(use_tree: true)
4545
end
4646
end)
4747

@@ -63,7 +63,7 @@ defmodule NestedSets.MigrationTest do
6363

6464
create_table(repo, table, fn ->
6565
quote do
66-
NestedSets.Migration.nested_sets_fields(use_tree: :organization_id)
66+
NestedSets.Migration.nested_sets_columns(use_tree: :organization_id)
6767
end
6868
end)
6969

@@ -83,7 +83,7 @@ defmodule NestedSets.MigrationTest do
8383

8484
create_table(repo, table, fn ->
8585
quote do
86-
NestedSets.Migration.nested_sets_fields(use_tree: :tenant_id, tree_null: false)
86+
NestedSets.Migration.nested_sets_columns(use_tree: :tenant_id, tree_null: false)
8787
end
8888
end)
8989

@@ -108,7 +108,7 @@ defmodule NestedSets.MigrationTest do
108108

109109
create_table(repo, table, fn ->
110110
quote do
111-
NestedSets.Migration.nested_sets_fields(
111+
NestedSets.Migration.nested_sets_columns(
112112
use_tree: :organization_id,
113113
tree_type: :id,
114114
tree_references: unquote(ref_table)
@@ -140,7 +140,7 @@ defmodule NestedSets.MigrationTest do
140140

141141
create_table(repo, table, fn ->
142142
quote do
143-
NestedSets.Migration.nested_sets_fields(use_tree: false)
143+
NestedSets.Migration.nested_sets_columns(use_tree: false)
144144
end
145145
end)
146146

@@ -158,7 +158,7 @@ defmodule NestedSets.MigrationTest do
158158

159159
create_table(repo, table, fn ->
160160
quote do
161-
NestedSets.Migration.nested_sets_fields(use_tree: nil)
161+
NestedSets.Migration.nested_sets_columns(use_tree: nil)
162162
end
163163
end)
164164

@@ -175,7 +175,7 @@ defmodule NestedSets.MigrationTest do
175175

176176
def change do
177177
create table(:test_invalid) do
178-
NestedSets.Migration.nested_sets_fields(use_tree: 123)
178+
NestedSets.Migration.nested_sets_columns(use_tree: 123)
179179
end
180180
end
181181
end
@@ -194,7 +194,7 @@ defmodule NestedSets.MigrationTest do
194194

195195
def change do
196196
create table(:test_missing_ref) do
197-
NestedSets.Migration.nested_sets_fields(use_tree: :org_id, tree_type: :id)
197+
NestedSets.Migration.nested_sets_columns(use_tree: :org_id, tree_type: :id)
198198
end
199199
end
200200
end
@@ -213,10 +213,10 @@ defmodule NestedSets.MigrationTest do
213213
:"#{prefix}_#{System.unique_integer([:positive])}"
214214
end
215215

216-
defp create_table(repo, table_name, fields_fn) do
216+
defp create_table(repo, table_name, columns_fn) do
217217
reset_migrations!(repo)
218218

219-
fields_ast = fields_fn.()
219+
columns_ast = columns_fn.()
220220

221221
migration_module_name =
222222
String.to_atom("Elixir.NestedSets.TestMigration#{System.unique_integer([:positive])}")
@@ -232,7 +232,7 @@ defmodule NestedSets.MigrationTest do
232232
def change do
233233
create table(unquote(table_name)) do
234234
add :name, :string
235-
unquote(fields_ast)
235+
unquote(columns_ast)
236236
end
237237
end
238238
end,
@@ -331,8 +331,8 @@ defmodule NestedSets.MigrationTest do
331331
%{name: name, type: normalize_type(type, :postgres), nullable: nullable == "YES"}
332332

333333
Ecto.Adapters.SQLite3 ->
334-
[_cid, name, type, notnull, _default, _pk] = row
335-
%{name: name, type: normalize_type(type, :sqlite), nullable: notnull == 0}
334+
[_cid, name, type, not_null, _default, _pk] = row
335+
%{name: name, type: normalize_type(type, :sqlite), nullable: not_null == 0}
336336

337337
Ecto.Adapters.MyXQL ->
338338
[name, type, nullable] = row

test/support/mysql/migrations/00_create_test_tables.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule NestedSets.Test.MysqlRepo.Migrations.CreateTestTables do
99
add :name, :string, null: false
1010
add :active, :boolean, default: true, null: false
1111

12-
nested_sets_fields()
12+
nested_sets_columns()
1313

1414
timestamps()
1515
end
@@ -21,7 +21,7 @@ defmodule NestedSets.Test.MysqlRepo.Migrations.CreateTestTables do
2121
add :name, :string, null: false
2222
add :active, :boolean, default: true, null: false
2323

24-
nested_sets_fields(use_tree: true)
24+
nested_sets_columns(use_tree: true)
2525

2626
timestamps()
2727
end

test/support/postgres/migrations/00_create_test_tables.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule NestedSets.Test.Repo.Migrations.CreateTestTables do
99
add :name, :string, null: false
1010
add :active, :boolean, default: true, null: false
1111

12-
nested_sets_fields()
12+
nested_sets_columns()
1313

1414
timestamps()
1515
end
@@ -21,7 +21,7 @@ defmodule NestedSets.Test.Repo.Migrations.CreateTestTables do
2121
add :name, :string, null: false
2222
add :active, :boolean, default: true, null: false
2323

24-
nested_sets_fields(use_tree: true)
24+
nested_sets_columns(use_tree: true)
2525

2626
timestamps()
2727
end

test/support/sqlite/migrations/00_create_test_tables.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule NestedSets.Test.SqliteRepo.Migrations.CreateTestTables do
99
add :name, :string, null: false
1010
add :active, :boolean, default: true, null: false
1111

12-
nested_sets_fields()
12+
nested_sets_columns()
1313

1414
timestamps()
1515
end
@@ -21,7 +21,7 @@ defmodule NestedSets.Test.SqliteRepo.Migrations.CreateTestTables do
2121
add :name, :string, null: false
2222
add :active, :boolean, default: true, null: false
2323

24-
nested_sets_fields(use_tree: true)
24+
nested_sets_columns(use_tree: true)
2525

2626
timestamps()
2727
end

0 commit comments

Comments
 (0)