PULL after PURGE no docs are returned

#1

I created some documents via the Sync Gateway API.
I PUSHED them into the DB via Sync Gateway (worked)
Then I PURGEd all documents in the LOCAL DB (on Android)
After I started PULL I expected the docs to be resend to the local DB again. It worked once, after that PULL had no effect.

Any idea why PURGED docs are not PULLED again ?
I do not define any channels for PULL.
Channel “*” is assigned to the doc, so I expect it to be pulled as described here: http://developer.couchbase.com/mobile/develop/guides/sync-gateway/channels/intro/index.html

Here is such a document.
{

 "_id": "e41de1d4-1b83-4eec-b169-e973cf09910a",
  "_rev": "1-0c8566cf0d3fdaf275d29cd1ee9bcc6c",
  "_sync": {
    "rev": "1-0c8566cf0d3fdaf275d29cd1ee9bcc6c",
    "sequence": 11,
    "history": {
      "revs": [
        "1-0c8566cf0d3fdaf275d29cd1ee9bcc6c"
      ],
      "parents": [
        -1
      ],
      "bodies": [
        ""
      ],
      "channels": [
        [
          "*"
        ]
      ]
    },
    "channels": {
      "*": null,
      "skipxxx": {
        "seq": 11,
        "rev": "1-0c8566cf0d3fdaf275d29cd1ee9bcc6c"
      }
    },
    "access": {
      "skipxxx": {
        "skipxxx": 11
      }
    },
    "time_saved": "2015-06-12T15:18:51.1175041-07:00"
  },
  "checked": false,
  "created_at": "2015-06-12T22:18:38.053Z",
  "creator": "skipxxx",
  "title": "UsersTestDocumenttitle",
  "type": "UsersTestDocument",
  "writers": "[skipxxx]"
}
#2

From the documentation it says:

Purging a document is different from deleting it; it’s more like forgetting it. The purge method removes all trace of a document (and all its revisions and their attachments) from the local database. It has no effect on replication or on remote databases, though.

Purging is mostly a way to save disk space by forgetting about replicated documents that you don’t need anymore. It has some slightly weird interactions with replication, though. For example, if you purge a document, and then later the document is updated on the remote server, the next replication will pull the document into your database again.

Indicating that a document would only be synced “back” to the client if it changes.

http://developer.couchbase.com/mobile/develop/guides/couchbase-lite/native-api/document/index.html

#3

Thank you martinesman. I was going crazy about this behaviour.

Another question:
Is there a possibility to resync ALL documents, even the purged docs without editing each document in the remote DB?.
I need to be able of removing all documents locally, and to restore them inside the local DB automagically from the remote DB.

#4

you could just delete the entire database Manager.SharedInstance.GetDatabase(“db”).Delete and
then pull all documents again. That would re-sync all documents from the remote endpoint.

#5

Hi Skipidar,
Did my suggestion help you or do we need to find another solution for you issue?

#6

Unfortunately it did not work.
I have the following situation:
I have the following document synced into the bucket:

{
  "_id": "cdb36a22-7703-44c2-bf53-2deb63908e56",
  "_rev": "1-4be1696e47e66e1db262c7fd6a6ff531",
  "_sync": {
    "rev": "1-4be1696e47e66e1db262c7fd6a6ff531",
    "sequence": 7,
    "history": {
      "revs": [
        "1-4be1696e47e66e1db262c7fd6a6ff531"
      ],
      "parents": [
        -1
      ],
      "bodies": [
        ""
      ],
      "channels": [
        [
          "channel_skipxxx"
        ]
      ]
    },
    "channels": {
      "*": {
        "seq": 7,
        "rev": "1-4be1696e47e66e1db262c7fd6a6ff531"
      },
      "channel_skipxxx": null
    },
    "access": {
      "skipxxx": {
        "skipxxx": 7
      }
    },
    "time_saved": "2015-06-21T03:49:36.3371586-07:00"
  },
  "checked": false,
  "created_at": "2015-06-21T06:49:32.554Z",
  "creator": "skipxxx",
  "title": "UsersTestDocumenttitle",
  "type": "UsersTestDocument",
  "writers": "[skipxxx]"
}

I am deleting the local database on Android and starting pull-sync again:

de.couchdbexperiment.Context.database.close();
de.couchdbexperiment.Context.database.delete();
de.couchdbexperiment.Context.database = manager.getDatabase(Constants.DATABASE_NAME);

The sync gateway logs the following. It is interesting, that 404 is reported every tim I am starting the pull-sync.
As reported here 404 on _local document is reported when sync is done for the first time,

No documents are pulled to the local DB

04:47:33.299772 HTTP:  #028: GET /gw/_session  (as skipxxx)
04:47:33.300724 HTTP+: #028:     --> 200   (1.9 ms)
04:47:33.341651 HTTP:  #029: GET /gw/_local/69d6429bf49689ec7f5ba96263afab14bd9bf8f8  (as skipxxx)
04:47:33.343555 HTTP: #029:     --> 404 missing  (1.9 ms)
04:47:33.367350 HTTP:  #030: POST /gw/_changes  (as skipxxx)
04:47:33.368302 Changes: MultiChangesFeed({channel_skipxxx}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc0841b05a0}) ...   (to skipxxx)
04:47:33.368302 Changes+: MultiChangesFeed: channels expand to channels.TimedSet{} ...   (to skipxxx)
04:47:33.368302 Changes+: MultiChangesFeed sending &{Seq:4 ID:_user/skipxxx Deleted:false Removed:{} Doc:map[] Changes:[] branched:false}   (to skipxxx)
04:47:33.368302 Changes: MultiChangesFeed done   (to skipxxx)
04:47:33.368302 HTTP+: #030:     --> 200 OK  (0.0 ms)
04:47:50.676191 HTTP:  #031: GET /gw/_session  (as skipxxx)
04:47:50.678494 HTTP+: #031:     --> 200   (2.3 ms)
04:47:50.764872 HTTP:  #032: GET /gw/_local/69d6429bf49689ec7f5ba96263afab14bd9bf8f8  (as skipxxx)
04:47:50.767175 HTTP: #032:     --> 404 missing  (2.3 ms)
04:47:50.815547 HTTP:  #033: POST /gw/_changes  (as skipxxx)
04:47:50.816698 Changes: MultiChangesFeed({channel_skipxxx}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc084366e40}) ...   (to skipxxx)
04:47:50.817850 Changes+: MultiChangesFeed: channels expand to channels.TimedSet{} ...   (to skipxxx)
04:47:50.817850 Changes+: MultiChangesFeed sending &{Seq:4 ID:_user/skipxxx Deleted:false Removed:{} Doc:map[] Changes:[] branched:false}   (to skipxxx)
04:47:50.817850 Changes: MultiChangesFeed done   (to skipxxx)
04:47:50.817850 HTTP+: #033:     --> 200 OK  (0.0 ms)
#7

This seems to be an issue of channels and user rights.
I reduced the sync function to the following, and pulling the documents in db - worked!

     "sync": ` function(doc, oldDoc) { channel("*"); } `,
#8

Hi @skipidar,
What happens if you pull the documents again after the 404 error (in the sync function with the channels)? If you continue to get a 404 I will get support to investigate if this (user rights and channels) is a bug.

Is it a usable solution to delete the database and restore all documents?