@@ -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 #
0 commit comments