Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Revert on update #456

Open
rootkea opened this issue Jan 15, 2025 · 0 comments
Open

Discussion: Revert on update #456

rootkea opened this issue Jan 15, 2025 · 0 comments

Comments

@rootkea
Copy link

rootkea commented Jan 15, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant