Skip to content

Commit

Permalink
Add support for drop/rollback operations for collections and indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoeter committed Mar 19, 2018
1 parent d6ae940 commit def02c6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
30 changes: 27 additions & 3 deletions lib/arangodb_ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,44 @@ defmodule ArangoDB.Ecto.Migration do
Arangoex.Collection.create(endpoint, %Arangoex.Collection{name: name, type: collection_type})
end

defp execute(endpoint, {cmd, %Ecto.Migration.Table{name: name}}, _opts)
when cmd in [:drop, :drop_if_exists]
do
Arangoex.Collection.drop(endpoint, %Arangoex.Collection{name: name})
end

defp execute(endpoint, {cmd, %Ecto.Migration.Index{table: collection, columns: fields} = index}, _opts)
when cmd in [:create, :create_if_not_exists]
when cmd in [:create, :create_if_not_exists]
do
body = make_index(index)
Arangoex.Index.create_general(endpoint, collection, Map.put(body, :fields, fields))
end

defp execute(endpoint, {cmd, %Ecto.Migration.Index{table: collection, columns: fields} = index}, _opts)
when cmd in [:drop, :drop_if_exists]
do
body = make_index(index)
|> Map.put(:fields, fields)
|> Poison.encode!()
|> Poison.decode!()
|> MapSet.new()

{:ok, %{"error" => false, "indexes" => indexes}} = Arangoex.Index.indexes(endpoint, collection)
matching_index = Enum.filter(indexes, &MapSet.subset?(body, MapSet.new(&1)))
case {cmd, matching_index} do
{_, [index]} -> Arangoex.Index.delete(endpoint, index["id"])
{:drop_if_exists, []} -> :ok
{:drop, []} -> raise "No index found matching #{inspect index}"
end
end

defp execute(_, {:alter, _, _}, options) do
if options[:log], do: Logger.warn "ALTER command has no effect in ArangoDB."
{:ok, nil}
end

defp execute(_, {cmd, _, _}, _) do
raise "{inspect __MODULE__}: unspported DDL operation #{inspect cmd}"
defp execute(_, cmd, _) do
raise "#{inspect __MODULE__}: unspported DDL operation #{inspect cmd}"
end

#
Expand Down
57 changes: 56 additions & 1 deletion test/integration/migrator_test.exs
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
Code.require_file "../../deps/ecto/integration_test/cases/migrator.exs", __DIR__
Code.require_file "../../deps/ecto/integration_test/cases/migrator.exs", __DIR__

defmodule ArangoDB.Ecto.Integration.MigratorTest do

use Ecto.Integration.Case

alias Ecto.Integration.PoolRepo
alias Ecto.Migration.SchemaMigration

import Ecto.Migrator

setup do
PoolRepo.delete_all(SchemaMigration)
:ok
end

defmodule CollectionMigration do
use Ecto.Migration

def change do
create table(:dummy_collection)
end
end

defmodule IndexMigration do
use Ecto.Migration

def change do
create index(:posts, :author_id)
end
end

test "run and rollback collection migration" do
endpoint = ArangoDB.Ecto.Utils.get_endpoint(PoolRepo)

assert up(PoolRepo, 20100906120000, CollectionMigration, log: false) == :ok
{:ok, collections} = Arangoex.Collection.collections(endpoint)
assert [_] = collections |> Enum.filter(&match?(%{name: "dummy_collection"}, &1))

assert down(PoolRepo, 20100906120000, CollectionMigration, log: false) == :ok
{:ok, collections} = Arangoex.Collection.collections(endpoint)
assert [] = collections |> Enum.filter(&match?(%{name: "dummy_collection"}, &1))
end

test "run and rollback index migration" do
endpoint = ArangoDB.Ecto.Utils.get_endpoint(PoolRepo)

assert up(PoolRepo, 20100906120000, IndexMigration, log: false) == :ok
{:ok, %{"error" => false, "indexes" => indexes}} = Arangoex.Index.indexes(endpoint, "posts")
assert [_index] = indexes |> Enum.filter(&match?(%{"fields" => ["author_id"]}, &1))

assert down(PoolRepo, 20100906120000, IndexMigration, log: false) == :ok
{:ok, %{"error" => false, "indexes" => indexes}} = Arangoex.Index.indexes(endpoint, "posts")
assert [] = indexes |> Enum.filter(&match?(%{"fields" => ["author_id"]}, &1))
end
end

0 comments on commit def02c6

Please sign in to comment.