Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
scroll top