-1

I have a script that migrates data in the database.

It copies property X to property Y.
If I want the script to be idempotent, what should it do on subsequent call if X changed?

For example:

X is 'a'   
==> I run the script, then Y is 'a'.    
now X is 'b'   
  • What would subsequent call to the script do? set Y to be 'b' or leave it untouched?

For those who wanted me to clarify my question - I am trying to clarify the meaning of "idempotent" when a resource it references changes.

I was asked to write an idempotent script, and one of the comments I got on it were that I did not handle the scenario where Y changes. I had to know if I misunderstood something or if it was not specified in the requirement.

Please do not close this answer as I think future readers may benefit it.

  • 1
    If you set X to 'b', then run a script that copies X to Y, then obviously Y is now 'b'. – immibis Jan 17 at 22:23
  • 6
    A function is idempotent if it results in the same output given the same input. Trying to determine if a function is idempotent by giving it two different inputs is not a meaningful exercise. – Robert Harvey Jan 17 at 23:12
  • @RobertHarvey I thought that the meaning was - no matter how many times I run it, the end result will always be that Y = X. So the end result will always be the same. Do those kind of scripts have a different name? – guy mograbi Jan 18 at 22:57
1

You already have the correct answer to your question: idempotence is a meaningless concept in a situation where the inputs change.

But för a more helpful answer, imagine the following scenario: your colleague says, "I locked the database to give you and me exclusive access to write to it. I then may or may not have run your supposedly idempotent script on it and I won't tell you which. I also set up a hook in the database such that if I ran the script before, it will explode if it changes anything now, and if I didn't run it before, the end result should still be the expected after you run it or it will explode."

It's a contrived scenario, but it leaves only one valid behavior for your script.

  • So how do you call a script that the end result is always the same. in my example - every time I run it, X will become Y. is there a name for this? – guy mograbi Jan 18 at 22:59
  • also - if my script logs output. does it affect its idempotency? logs are side effects. – guy mograbi Jan 18 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.