|
| 1 | +defmodule PurgeTest do |
| 2 | + use CouchTestCase |
| 3 | + |
| 4 | + @moduletag :purge |
| 5 | + |
| 6 | + @tag :with_db |
| 7 | + test "purge documents", context do |
| 8 | + db_name = context[:db_name] |
| 9 | + |
| 10 | + design_doc = %{ |
| 11 | + _id: "_design/test", |
| 12 | + language: "javascript", |
| 13 | + views: %{ |
| 14 | + all_docs_twice: %{ |
| 15 | + map: "function(doc) { emit(doc.integer, null); emit(doc.integer, null) }" |
| 16 | + }, |
| 17 | + single_doc: %{ |
| 18 | + map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}" |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + {:ok, _} = create_doc(db_name, design_doc) |
| 24 | + |
| 25 | + num_docs = 10 |
| 26 | + bulk_save(db_name, make_docs(1..(num_docs + 1))) |
| 27 | + |
| 28 | + test_all_docs_twice(db_name, num_docs, 1) |
| 29 | + |
| 30 | + info = info(db_name) |
| 31 | + |
| 32 | + doc1 = open_doc(db_name, 1) |
| 33 | + doc2 = open_doc(db_name, 2) |
| 34 | + |
| 35 | + resp = |
| 36 | + Couch.post("/#{db_name}/_purge", |
| 37 | + body: %{"1": [doc1["_rev"]], "2": [doc2["_rev"]]} |
| 38 | + ) |
| 39 | + |
| 40 | + assert resp.status_code == 201 |
| 41 | + result = resp.body |
| 42 | + |
| 43 | + assert Enum.at(result["purged"]["1"], 0) == doc1["_rev"] |
| 44 | + assert Enum.at(result["purged"]["2"], 0) == doc2["_rev"] |
| 45 | + |
| 46 | + open_doc(db_name, 1, 404) |
| 47 | + open_doc(db_name, 2, 404) |
| 48 | + |
| 49 | + purged_info = info(db_name) |
| 50 | + |
| 51 | + assert purged_info["purge_seq"] != info["purge_seq"] |
| 52 | + |
| 53 | + test_all_docs_twice(db_name, num_docs, 0, 2) |
| 54 | + |
| 55 | + # purge sequences are preserved after compaction (COUCHDB-1021) |
| 56 | + resp = Couch.post("/#{db_name}/_compact") |
| 57 | + assert resp.status_code == 202 |
| 58 | + |
| 59 | + retry_until(fn -> |
| 60 | + info(db_name)["compact_running"] == false |
| 61 | + end) |
| 62 | + |
| 63 | + compacted_info = info(db_name) |
| 64 | + assert compacted_info["purge_seq"] == purged_info["purge_seq"] |
| 65 | + |
| 66 | + # purge documents twice in a row without loading views |
| 67 | + # (causes full view rebuilds) |
| 68 | + |
| 69 | + doc3 = open_doc(db_name, 3) |
| 70 | + doc4 = open_doc(db_name, 4) |
| 71 | + |
| 72 | + resp = |
| 73 | + Couch.post("/#{db_name}/_purge", |
| 74 | + body: %{"3": [doc3["_rev"]]} |
| 75 | + ) |
| 76 | + |
| 77 | + assert resp.status_code == 201 |
| 78 | + |
| 79 | + resp = |
| 80 | + Couch.post("/#{db_name}/_purge", |
| 81 | + body: %{"4": [doc4["_rev"]]} |
| 82 | + ) |
| 83 | + |
| 84 | + assert resp.status_code == 201 |
| 85 | + |
| 86 | + test_all_docs_twice(db_name, num_docs, 0, 4) |
| 87 | + end |
| 88 | + |
| 89 | + @tag :with_db |
| 90 | + test "COUCHDB-1065", context do |
| 91 | + db_name_a = context[:db_name] |
| 92 | + db_name_b = random_db_name() |
| 93 | + {:ok, _} = create_db(db_name_b) |
| 94 | + |
| 95 | + {:ok, doc_a_resp} = create_doc(db_name_a, %{_id: "test", a: 1}) |
| 96 | + {:ok, doc_b_resp} = create_doc(db_name_b, %{_id: "test", a: 2}) |
| 97 | + replicate(db_name_a, db_name_b) |
| 98 | + |
| 99 | + open_rev(db_name_b, "test", doc_a_resp.body["rev"], 200) |
| 100 | + open_rev(db_name_b, "test", doc_b_resp.body["rev"], 200) |
| 101 | + |
| 102 | + resp = |
| 103 | + Couch.post("/#{db_name_b}/_purge", |
| 104 | + body: %{test: [doc_a_resp.body["rev"]]} |
| 105 | + ) |
| 106 | + |
| 107 | + assert resp.status_code == 201 |
| 108 | + |
| 109 | + open_rev(db_name_b, "test", doc_a_resp.body["rev"], 404) |
| 110 | + |
| 111 | + resp = |
| 112 | + Couch.post("/#{db_name_b}/_purge", |
| 113 | + body: %{test: [doc_b_resp.body["rev"]]} |
| 114 | + ) |
| 115 | + |
| 116 | + assert resp.status_code == 201 |
| 117 | + |
| 118 | + open_rev(db_name_b, "test", doc_b_resp.body["rev"], 404) |
| 119 | + |
| 120 | + resp = |
| 121 | + Couch.post("/#{db_name_b}/_purge", |
| 122 | + body: %{test: [doc_a_resp.body["rev"], doc_b_resp.body["rev"]]} |
| 123 | + ) |
| 124 | + |
| 125 | + assert resp.status_code == 201 |
| 126 | + |
| 127 | + delete_db(db_name_b) |
| 128 | + end |
| 129 | + |
| 130 | + def replicate(src, tgt, options \\ []) do |
| 131 | + defaults = [headers: [], body: %{}, timeout: 30_000] |
| 132 | + options = defaults |> Keyword.merge(options) |> Enum.into(%{}) |
| 133 | + |
| 134 | + %{body: body} = options |
| 135 | + body = [source: src, target: tgt] |> Enum.into(body) |
| 136 | + options = Map.put(options, :body, body) |
| 137 | + |
| 138 | + resp = Couch.post("/_replicate", Enum.to_list(options)) |
| 139 | + assert HTTPotion.Response.success?(resp), "#{inspect(resp)}" |
| 140 | + resp.body |
| 141 | + end |
| 142 | + |
| 143 | + defp open_doc(db_name, id, expect \\ 200) do |
| 144 | + resp = Couch.get("/#{db_name}/#{id}") |
| 145 | + assert resp.status_code == expect |
| 146 | + resp.body |
| 147 | + end |
| 148 | + |
| 149 | + defp open_rev(db_name, id, rev, expect) do |
| 150 | + resp = Couch.get("/#{db_name}/#{id}?rev=#{rev}") |
| 151 | + assert resp.status_code == expect |
| 152 | + resp.body |
| 153 | + end |
| 154 | + |
| 155 | + defp test_all_docs_twice(db_name, num_docs, sigle_doc_expect, offset \\ 0) do |
| 156 | + resp = Couch.get("/#{db_name}/_design/test/_view/all_docs_twice") |
| 157 | + assert resp.status_code == 200 |
| 158 | + rows = resp.body["rows"] |
| 159 | + |
| 160 | + for x <- 0..(num_docs - offset) do |
| 161 | + assert Map.get(Enum.at(rows, 2 * x), "key") == x + offset + 1 |
| 162 | + assert Map.get(Enum.at(rows, 2 * x + 1), "key") == x + offset + 1 |
| 163 | + end |
| 164 | + |
| 165 | + resp = Couch.get("/#{db_name}/_design/test/_view/single_doc") |
| 166 | + assert resp.body["total_rows"] == sigle_doc_expect |
| 167 | + end |
| 168 | +end |
0 commit comments