Trying to restore deleted documents, but not getting the revision data

16 views
Skip to first unread message

Brendan Duddridge

unread,
Sep 14, 2016, 1:30:47 AM9/14/16
to Couchbase Mobile
Hi,

I've written a small bit of code to try and restore deleted documents, but I'm coming up a bit empty.

Here's what I've got so far:

- (IBAction)recoverDeletedDocs:(id)sender {

       CBLQuery *allDocsQuery = [self.currentDatabase createAllDocumentsQuery];

       allDocsQuery.allDocsMode = kCBLIncludeDeleted;


        CBLQueryEnumerator *enumerator = [allDocsQuery run:nil];


        for (CBLQueryRow *row in enumerator) {

              if (row.document.isDeleted) {


                        TFFLog(@"revisions: %@", [row.document getRevisionHistory:nil]);

                       TFFLog(@"row.key: %@, value: %@", row.key, row.value);


                        CBLRevision *revision = row.document.currentRevision;


                        if (revision) {


                                CBLSavedRevision *parent = revision.parentRevision;


                                 if (parent) {


                                        TFFLog(@"properties: %@", parent.properties);


                                        CBLUnsavedRevision *unsaved = [parent createRevision];

                                       unsaved.userProperties = parent.userProperties;

                                       [unsaved save:nil];


                                }


                               


                        }


                }


        }




}




The problem is that the revision on the document is nil. The document properties are also empty.

Given a deleted document, how do I get the previous revision so that I can create a new revision and recover the deleted document?

Thanks

Brendan

Brendan Duddridge

unread,
Oct 18, 2017, 3:02:56 PM10/18/17
to Couchbase Mobile
Hi Jens,

I was just working on tackling this issue again as I never got it going. I was hoping you could shed some light into how I could fetch a deleted document and recover its data. I see in the raw db.sqlite file that the JSON data is there for the document, but it's marked as deleted. If I ask the document for its current revision, it comes back nil.  I thought I might be able to get the current revision and then set the isDeleted flag to false or create a new revision that's not deleted and give it the properties from the deleted revision.

Is this possible?

Thanks,

Brendan

Brendan Duddridge

unread,
Oct 18, 2017, 3:51:50 PM10/18/17
to Couchbase Mobile
Ok, I figured it out.

I need to call getLeafRevisions on my deleted CBLDocument. That actually gives me the revisions and then I can ask for the parentRevison in order to get the properties.

Then I need to take the original deleted revision and create a new revision from it, set the isDeleted flag to NO and then save it. I get my document back when I do that.

Thanks!

Brendan

Brendan Duddridge

unread,
Oct 18, 2017, 4:04:54 PM10/18/17
to Couchbase Mobile
And it turns out that I don't need to get the parent's revision. In fact, that may be nil. So instead I can just get the properties from the current revision from the leaf node and that works a charm.
Reply all
Reply to author
Forward
0 new messages