hey Prisma team. Can someone please answer a coupl...
# orm-help
n
hey Prisma team. Can someone please answer a couple questions about Prisma Migrate? 1. How does it handle transactions within migration files? Eg. I have a migration file (generated by prisma migrate) with 10 sql queries. 3 queries are wrapped in a transaction (
BEGIN
...
COMMIT
). What will happen if one of the queries within that transaction fail? 2. What will happen to the queries executed before a failure during the migration. From what I've observed, the queries executed before the failed query aren't rolled back. But just wanted to check what would be the best practices around making sure that each migration is a transaction? 3. When is
applied_steps_count
incremented in
_prisma_migrations
table?
t
Hi Nishant! Quick reply: 1. Migrate does not start transactions on its own when applying migration. The right approach is indeed to insert
BEGIN
and
COMMIT
statements yourself where you want them. Migrate works that way to be less surprising, and at scale, transactions can be impractical. If you have a transaction block in the middle of a migration, and one statement fails, then: a. the whole migration will stop there, b. the previous statements inside the transaction block will be rolled back, but not the statements before the
BEGIN
.
(I am assuming you are on postgres, btw)
✅ 1
2. Yes, there is no automatic rollback. If you are fine with this, inserting a
BEGIN
at the start of each migration and a
COMMIT
at the end will give you real transactions. We might want to add optional automatic transactions in the future, but I haven't seen many people asking for it, so if you are interested, a GitHub issue stating your use case would be the next step.
3. We increment it when we apply the migration. Currently, the whole migration is sent as a single block to the database, so that counts as only one step. On new connectors, or in the future in the current ones, we hope we can split the migration files to run in multiple steps, for more granularity and better errors.
I hope that helps 🙂 happy to answer if you have more questions.
n
Thanks for the quick response Tom. This is super helpful 🙂