anchor.diff:      view_cleanup(DbName),
       ?_assertEqual(0, count_index_files(DbName)).
   
  -
  -create_doc(DbName) ->
  +couchdb_1138(DbName) ->
  +    ?_test(begin
  +        {ok, IndexerPid} = couch_index_server:get_index(
  +            couch_mrview_index, DbName, <<"_design/foo">>),
  +        ?assert(is_pid(IndexerPid)),
  +        ?assert(is_process_alive(IndexerPid)),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +
  +        Rows0 = query_view(DbName, "foo", "bar"),
  +        ?assertEqual(3, length(Rows0)),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +        ?assert(is_process_alive(IndexerPid)),
  +
  +        create_doc(DbName, "doc1000"),
  +        Rows1 = query_view(DbName, "foo", "bar"),
  +        ?assertEqual(4, length(Rows1)),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +        ?assert(is_process_alive(IndexerPid)),
  +
  +        Ref1 = get_db_ref_counter(DbName),
  +        compact_db(DbName),
  +        Ref2 = get_db_ref_counter(DbName),
  +        ?assertEqual(2, couch_ref_counter:count(Ref2)),
  +        ?assertNotEqual(Ref2, Ref1),
  +        ?assertNot(is_process_alive(Ref1)),
  +        ?assert(is_process_alive(IndexerPid)),
  +
  +        compact_view_group(DbName, "foo"),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +        Ref3 = get_db_ref_counter(DbName),
  +        ?assertEqual(Ref3, Ref2),
  +        ?assert(is_process_alive(IndexerPid)),
  +
  +        create_doc(DbName, "doc1001"),
  +        Rows2 = query_view(DbName, "foo", "bar"),
  +        ?assertEqual(5, length(Rows2)),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +        ?assert(is_process_alive(IndexerPid))
  +    end).
  +
  +couchdb_1309(DbName) ->
  +    ?_test(begin
  +        {ok, IndexerPid} = couch_index_server:get_index(
  +            couch_mrview_index, DbName, <<"_design/foo">>),
  +        ?assert(is_pid(IndexerPid)),
  +        ?assert(is_process_alive(IndexerPid)),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +
  +        create_doc(DbName, "doc1001"),
  +        Rows0 = query_view(DbName, "foo", "bar"),
  +        check_rows_value(Rows0, null),
  +        ?assertEqual(4, length(Rows0)),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +        ?assert(is_process_alive(IndexerPid)),
  +
  +        update_design_doc(DbName,  <<"_design/foo">>, <<"bar">>),
  +        {ok, NewIndexerPid} = couch_index_server:get_index(
  +            couch_mrview_index, DbName, <<"_design/foo">>),
  +        ?assert(is_pid(NewIndexerPid)),
  +        ?assert(is_process_alive(NewIndexerPid)),
  +        ?assertNotEqual(IndexerPid, NewIndexerPid),
  +        ?assertEqual(2, count_db_refs(DbName)),
  +
  +        Rows1 = query_view(DbName, "foo", "bar", ok),
  +        ?assertEqual(0, length(Rows1)),
  +        Rows2 = query_view(DbName, "foo", "bar"),
  +        check_rows_value(Rows2, 1),
  +        ?assertEqual(4, length(Rows2)),
  +
  +        MonRef0 = erlang:monitor(process, IndexerPid),
  +        receive
  +            {'DOWN', MonRef0, _, _, _} ->
  +                ok
  +        after ?TIMEOUT ->
  +            erlang:error(
  +                {assertion_failed,
  +                 [{module, ?MODULE}, {line, ?LINE},
  +                  {reason, "old view group is not dead after ddoc update"}]})
  +        end,
  +
  +        MonRef1 = erlang:monitor(process, NewIndexerPid),
  +        ok = couch_server:delete(DbName, [?ADMIN_USER]),
  +        receive
  +            {'DOWN', MonRef1, _, _, _} ->
  +                ok
  +        after ?TIMEOUT ->
  +            erlang:error(
  +                {assertion_failed,
  +                 [{module, ?MODULE}, {line, ?LINE},
  +                  {reason, "new view group did not die after DB deletion"}]})
  +        end
  +    end).
  +
  +create_doc(DbName, DocId) when is_list(DocId) ->
  +    create_doc(DbName, ?l2b(DocId));
  +create_doc(DbName, DocId) when is_binary(DocId) ->
       {ok, Db} = couch_db:open(DbName, [?ADMIN_USER]),
       Doc666 = couch_doc:from_json_obj({[
  -        {<<"_id">>, <<"doc666">>},
  +        {<<"_id">>, DocId},
           {<<"value">>, 999}
  -
       ]}),
       {ok, _} = couch_db:update_docs(Db, [Doc666]),
       couch_db:ensure_full_commit(Db),
  
positive.code: 
      restore_backup_db_file(DbName),
  
      Body1 = query_view(DbName, "foo", "bar"),
      ViewJson1 = ejson:decode(Body1),
      Rows1 = couch_util:get_nested_json_value(ViewJson1, [<<"rows">>]),
      ?assert(has_doc("doc1", Rows1)),
      ?assert(has_doc("doc2", Rows1)),
      ?assert(has_doc("doc3", Rows1)),
  
negative.code: 
  setup() ->
      DbName = ?tempdb(),
      {ok, Db} = couch_db:create(DbName, [?ADMIN_USER]),
      ok = couch_db:close(Db),
      FooRev = create_design_doc(DbName, <<"_design/foo">>, <<"bar">>),
      query_view(DbName, "foo", "bar"),
      BooRev = create_design_doc(DbName, <<"_design/boo">>, <<"baz">>),
  