firebasefirebase-realtime-databasefirepad

FirePad - delete all revisions which are a number of days older than a certain snapshot


This is continuing from an old thread here https://groups.google.com/forum/#!topic/firepad-io/73dKYaUwTn4)

The aim is to clean the database for documents which have many revisions over a long period

I need help writing a function that issue a FB command to delete all revisions which are 'nd' days older than the 'ns' snapshot.

I am not sure of both the Firebase syntax for this command and how to access the relevant firebase keys properly.

Any help will be greatly appreciated

Thx!


Solution

  • ended up solving this

    PR: https://github.com/firebase/firepad/pull/264

    code:

    FirebaseAdapter.prototype.deleteOldRevisions_ = function(query) {
        var self=this;
        query.once('value', function(s) {
            if (typeof s.val() === 'undefined' || s.val() === null || !s.hasChildren()) return;
            s.forEach(function(rev) { 
                utils.log('removing old revision: '+rev.key);
                rev.ref.remove();
            });
            setTimeout(function() { self.deleteOldRevisions_(query); }, 100); // delete the next one
        });
    }
    
    FirebaseAdapter.prototype.monitorHistory_ = function() {
        var self = this;
        // Get the latest checkpoint as a starting point so we don't have to re-play entire history.
        self.ref_.child('checkpoint').once('value', function(s) {
            //utils.log(new Date().toISOString() + ': got checkpoint');
            if (self.zombie_) { return; } // just in case we were cleaned up before we got the checkpoint data.
            var revisionId = s.child('id').val(),  op = s.child('o').val(), author = s.child('a').val();
            if (op !== null && revisionId !== null && author !== null &&
                op !== undefined && revisionId !== undefined && author !== undefined) {
                self.pendingReceivedRevisions_[revisionId] = { o: op, a: author };
                self.checkpointRevision_ = revisionFromId(revisionId);
                self.monitorHistoryStartingAt_(self.checkpointRevision_ + 1);
            } else {
                self.checkpointRevision_ = 0;
                self.monitorHistoryStartingAt_(self.checkpointRevision_);
            }
    
            // delete revisions older than one week before last checkpoint
            if (revisionId) {
                var historyRef=self.ref_.child('history');
                historyRef.child(revisionId+'/t').once('value', function(s) {
                    if (typeof s.val() !== 'undefined' && s.val() !== null) {
                        var weekBefore=s.val()-(24*60*60*1000*7);
                        //utils.log('checkpoint revision: '+self.checkpointRevision_);
                        //utils.log('checkpoint time: ' + new Date(s.val()));
                        //utils.log('remove before: ' + new Date(weekBefore));
                        self.deleteOldRevisions_(historyRef.orderByChild('t').endAt(weekBefore));
                    }
                });
            }
        });
    };