Skip to content

Discussion: Revert on update #456

@rootkea

Description

@rootkea

Hello all!

This is not really a bug report but a discussion post. I would like to know how are you reverting the update op.

For update operation a proper revert will restore the initial value and not just empty it. For e.g. a = 20 and now after update a = 100. To revert means a should be set to 20 only (the previous value) and not 0 or "".

I looked at the README and the example specified there is not really reverting but just overwriting the value:

module.exports = {
  up(db) {
    return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
  },

  down(db) {
    return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
  }
};

In down we are just setting blacklisted to false blindly. What if the original value before the update was true for some records? This is not a true revert.

So, how do you revert the update op?

One approach I can think of is:
In up, read the db first before updating and save the data read in a new .json file as a backup. Then in down, just read that newly created backup json file and update the db.

Is it okay if we create the file on-the-fly after reading the db? Is this approach correct?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions