I wouldn’t claim it works the same way for everyone, but the difference between coffee being a treatment for migraines and migraines being a symptom of coffee withdrawal may be indistinguishable in immediate term.
I quit caffeine for about 2 years and the headaches never subsided (except the caffeine withdrawal ones of course).
They are just a fact of my life I’m afraid.
As much as I’d like to get off of caffeine I am very grateful to have is as a remedy.