Skip to content

Commit

Permalink
Merge pull request apache#3118 from apache/dreyfus-cleanup-with-inval…
Browse files Browse the repository at this point in the history
…id-ddoc

Allow to continue to cleanup search index even if there is invalid design document
  • Loading branch information
jiangphcn authored Sep 1, 2020
2 parents 0c3c4b6 + 253d64a commit 27eefab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/dreyfus/src/dreyfus_fabric_cleanup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ go(DbName) ->
ok.

active_sigs(#doc{body={Fields}}=Doc) ->
{RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
{IndexNames, _} = lists:unzip(RawIndexes),
[begin
{ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
Index#index.sig
end || IndexName <- IndexNames].
try
{RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
{IndexNames, _} = lists:unzip(RawIndexes),
[begin
{ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
Index#index.sig
end || IndexName <- IndexNames]
catch error:{badmatch, _Error} ->
[]
end.

cleanup_local_purge_doc(DbName, ActiveSigs) ->
{ok, BaseDir} = clouseau_rpc:get_root_dir(),
Expand Down
25 changes: 25 additions & 0 deletions src/dreyfus/test/elixir/test/search_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ defmodule SearchTest do
assert Map.has_key?(resp.body, "ok") == true
end

def create_invalid_ddoc(db_name, opts \\ %{}) do
invalid_ddoc = %{
:indexes => [
%{"name" => "foo", "ddoc" => "bar", "type" => "text"},
]
}

ddoc = Enum.into(opts, invalid_ddoc)

resp = Couch.put("/#{db_name}/_design/search", body: ddoc)
assert resp.status_code in [201, 202]
assert Map.has_key?(resp.body, "ok") == true
end

def get_items (resp) do
%{:body => %{"rows" => rows}} = resp
Enum.map(rows, fn row -> row["doc"]["item"] end)
Expand Down Expand Up @@ -198,4 +212,15 @@ defmodule SearchTest do
ids = get_items(resp)
assert Enum.sort(ids) == ["apple"]
end

@tag :with_db
test "clean up search index with invalid design document", context do
db_name = context[:db_name]
create_search_docs(db_name)
create_ddoc(db_name)
create_invalid_ddoc(db_name)

resp = Couch.post("/#{db_name}/_search_cleanup")
assert resp.status_code in [201, 202]
end
end

0 comments on commit 27eefab

Please sign in to comment.