-
Notifications
You must be signed in to change notification settings - Fork 77
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
Use a transaction when applying migrations #1822
Conversation
I'm not quite following the exact issue this is trying to resolve. If a migration fails, then it should exit without updating the counter. What was the behavior with #1781 with multiple statements? Did it successfully execute the first statement, and then fail? I'm curious if you considered wrapping the entire I'd also like to note that the migration code doesn't look like it handles concurrent processes trying to do migrations. Probably not an issue for triagebot, but I wanted to just note it. |
We don't really know. Currently, based on the state of the DB, it looks like it executed the second statement, and then it failed.. The goal is to make the execution of the migration and the increment of the counter atomic. It is true that if the migration fails, then the counter should not be updated (even without a transaction). However, adding a transaction fixes the following two problems:
I did consider that. I don't think that performance is really important here, usually you just execute 1, maybe 2 migrations, the already applied migrations are not being executed anyway, so there is no permanently increasing performance cost. So I wouldn't personally do it because of performance reasons. My reasoning was that if you have a good migration and then a bad migration, you might want to at least go as far as you can (hence separate migrations). On the other hand, if you apply multiple migrations together, you probably added them together in the same PR, and therefore they are probably related to one another, and therefore if one fails, you might later want to modify all of the newly added migrations in another PR with a fix. So perhaps wrapping everything in one larger transaction makes more sense.
You're right. I'm not sure if we deal with this in any of our tools, this is non-trivial to resolve, AFAIK. |
What information do you have that makes it seem that way? From what I understand:
Nowhere along this does it seem like the lack of transactions is the problem. |
Thank you for the detailed analysis! ❤️ The counter was indeed 15, Mark now modified it to 14, and the last migration is in effect (the index exists), but the penultimate one isn't (I asked Mark to enable the intarray extension manually though). In these very specific circumstances, it probably wouldn't have helped to have transactions, but if the multistatement migration was added as a new migration, instead of being modified (and this can happen again in the future), transactions would prevent the error. But having transactions for migrations is a good idea nevertheless, regardless if it would prevent issues in this specific case. |
Unless I'm misunderstanding, I don't think it would. If you try to run a migration with multiple statements, it would error with |
I see. I thought it would execute the first statement only. Ok, so it wouldn't help in this case. It's still a good idea to do it though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go ahead and merge, even though I don't think this will fix any issues we've had. It is appropriate to do changes like this in a transaction, though it will only help if the triagebot executable is killed in-between the two statements, and I think the likelihood of that is near zero.
I did a little bit of scanning to see which kinds of statements postgresql supports in transactions. There are a few that aren't supported (around 11 I could find), but none jump out to me to be ones we would likely ever use.
Thanks! I assume you meant "doesn't support in transactions"? |
Correct, I updated the comment. There are around 11 that aren't supported ("create db", "vacuum", etc.). |
Recently, migrations in triagebot have been causing some issues. When I checked the code I noticed that transactions weren't used, which caused two issues:
execute
). This would be rolled back if it was wrapped in a transaction.This PR wraps each migration in a separate transaction.
CC @apiraino