Skip to content

Commit def02c6

Browse files
committed
Add support for drop/rollback operations for collections and indexes.
1 parent d6ae940 commit def02c6

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

lib/arangodb_ecto/migration.ex

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,44 @@ defmodule ArangoDB.Ecto.Migration do
5252
Arangoex.Collection.create(endpoint, %Arangoex.Collection{name: name, type: collection_type})
5353
end
5454

55+
defp execute(endpoint, {cmd, %Ecto.Migration.Table{name: name}}, _opts)
56+
when cmd in [:drop, :drop_if_exists]
57+
do
58+
Arangoex.Collection.drop(endpoint, %Arangoex.Collection{name: name})
59+
end
60+
5561
defp execute(endpoint, {cmd, %Ecto.Migration.Index{table: collection, columns: fields} = index}, _opts)
56-
when cmd in [:create, :create_if_not_exists]
62+
when cmd in [:create, :create_if_not_exists]
5763
do
5864
body = make_index(index)
5965
Arangoex.Index.create_general(endpoint, collection, Map.put(body, :fields, fields))
6066
end
6167

68+
defp execute(endpoint, {cmd, %Ecto.Migration.Index{table: collection, columns: fields} = index}, _opts)
69+
when cmd in [:drop, :drop_if_exists]
70+
do
71+
body = make_index(index)
72+
|> Map.put(:fields, fields)
73+
|> Poison.encode!()
74+
|> Poison.decode!()
75+
|> MapSet.new()
76+
77+
{:ok, %{"error" => false, "indexes" => indexes}} = Arangoex.Index.indexes(endpoint, collection)
78+
matching_index = Enum.filter(indexes, &MapSet.subset?(body, MapSet.new(&1)))
79+
case {cmd, matching_index} do
80+
{_, [index]} -> Arangoex.Index.delete(endpoint, index["id"])
81+
{:drop_if_exists, []} -> :ok
82+
{:drop, []} -> raise "No index found matching #{inspect index}"
83+
end
84+
end
85+
6286
defp execute(_, {:alter, _, _}, options) do
6387
if options[:log], do: Logger.warn "ALTER command has no effect in ArangoDB."
6488
{:ok, nil}
6589
end
6690

67-
defp execute(_, {cmd, _, _}, _) do
68-
raise "{inspect __MODULE__}: unspported DDL operation #{inspect cmd}"
91+
defp execute(_, cmd, _) do
92+
raise "#{inspect __MODULE__}: unspported DDL operation #{inspect cmd}"
6993
end
7094

7195
#

test/integration/migrator_test.exs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
Code.require_file "../../deps/ecto/integration_test/cases/migrator.exs", __DIR__
1+
Code.require_file "../../deps/ecto/integration_test/cases/migrator.exs", __DIR__
2+
3+
defmodule ArangoDB.Ecto.Integration.MigratorTest do
4+
5+
use Ecto.Integration.Case
6+
7+
alias Ecto.Integration.PoolRepo
8+
alias Ecto.Migration.SchemaMigration
9+
10+
import Ecto.Migrator
11+
12+
setup do
13+
PoolRepo.delete_all(SchemaMigration)
14+
:ok
15+
end
16+
17+
defmodule CollectionMigration do
18+
use Ecto.Migration
19+
20+
def change do
21+
create table(:dummy_collection)
22+
end
23+
end
24+
25+
defmodule IndexMigration do
26+
use Ecto.Migration
27+
28+
def change do
29+
create index(:posts, :author_id)
30+
end
31+
end
32+
33+
test "run and rollback collection migration" do
34+
endpoint = ArangoDB.Ecto.Utils.get_endpoint(PoolRepo)
35+
36+
assert up(PoolRepo, 20100906120000, CollectionMigration, log: false) == :ok
37+
{:ok, collections} = Arangoex.Collection.collections(endpoint)
38+
assert [_] = collections |> Enum.filter(&match?(%{name: "dummy_collection"}, &1))
39+
40+
assert down(PoolRepo, 20100906120000, CollectionMigration, log: false) == :ok
41+
{:ok, collections} = Arangoex.Collection.collections(endpoint)
42+
assert [] = collections |> Enum.filter(&match?(%{name: "dummy_collection"}, &1))
43+
end
44+
45+
test "run and rollback index migration" do
46+
endpoint = ArangoDB.Ecto.Utils.get_endpoint(PoolRepo)
47+
48+
assert up(PoolRepo, 20100906120000, IndexMigration, log: false) == :ok
49+
{:ok, %{"error" => false, "indexes" => indexes}} = Arangoex.Index.indexes(endpoint, "posts")
50+
assert [_index] = indexes |> Enum.filter(&match?(%{"fields" => ["author_id"]}, &1))
51+
52+
assert down(PoolRepo, 20100906120000, IndexMigration, log: false) == :ok
53+
{:ok, %{"error" => false, "indexes" => indexes}} = Arangoex.Index.indexes(endpoint, "posts")
54+
assert [] = indexes |> Enum.filter(&match?(%{"fields" => ["author_id"]}, &1))
55+
end
56+
end

0 commit comments

Comments
 (0)