Migrations wrapped in the db transaction?

I am using branching and db migrations and I wonder if the migration file is automatically wrapped in a db transaction so that nothing from the file is applied if something fails? I don't see this documented anywhere.
9 Replies
josip
josipOP5d ago
That is what I expected too but I just had a case where migration has failed but it was partially applied. So it must be some bug
garyaustin
garyaustin5d ago
Maybe give some info on what was partially applied. If true (migration file in transaction) it would be a postgres thing and not likely bugged.
josip
josipOP5d ago
I have a migration file that looks something like this:
create table wallet.cashu_proofs (
"id" uuid primary key default gen_random_uuid(),
"user_id" uuid not null references wallet.users (id) on delete cascade,
...
);

...

alter table wallet.cashu_send_swaps add column requires_input_proofs_swap boolean generated always as (amount_to_send != input_amount) stored;

alter table wallet.cashu_send_swaps add constraint cashu_send_swaps_keyset_required_check
check (
(requires_input_proofs_swap = false) or
(requires_input_proofs_swap = true and keyset_id is not null and keyset_counter is not null)
);

...
create table wallet.cashu_proofs (
"id" uuid primary key default gen_random_uuid(),
"user_id" uuid not null references wallet.users (id) on delete cascade,
...
);

...

alter table wallet.cashu_send_swaps add column requires_input_proofs_swap boolean generated always as (amount_to_send != input_amount) stored;

alter table wallet.cashu_send_swaps add constraint cashu_send_swaps_keyset_required_check
check (
(requires_input_proofs_swap = false) or
(requires_input_proofs_swap = true and keyset_id is not null and keyset_counter is not null)
);

...
and the dashboard says that the migration failed with this error:
2025/11/24 21:26:50 ERROR: check constraint "cashu_send_swaps_keyset_required_check" of relation "cashu_send_swaps" is violated by some row (SQLSTATE 23514)
At statement: 99
-- Add constraint: keyset_id and keyset_counter are required when requires_input_proofs_swap is true
alter table wallet.cashu_send_swaps add constraint cashu_send_swaps_keyset_required_check
check (
(requires_input_proofs_swap = false) or
(requires_input_proofs_swap = true and keyset_id is not null and keyset_counter is not null)
)
2025/11/24 21:26:50 ERROR: check constraint "cashu_send_swaps_keyset_required_check" of relation "cashu_send_swaps" is violated by some row (SQLSTATE 23514)
At statement: 99
-- Add constraint: keyset_id and keyset_counter are required when requires_input_proofs_swap is true
alter table wallet.cashu_send_swaps add constraint cashu_send_swaps_keyset_required_check
check (
(requires_input_proofs_swap = false) or
(requires_input_proofs_swap = true and keyset_id is not null and keyset_counter is not null)
)
but I see cashu_proofs table created. Also in schema_migrations table I see that migration as executed. Full migration file can be seen here. It's pretty long so in the message above I left only what should be important for the issue.
josip
josipOP5d ago
No description
josip
josipOP5d ago
No description
garyaustin
garyaustin5d ago
If @vick comes around maybe he can throw in some ideas.
But other than him, you likely won't get help in the detail level here assuming no one else has input on transaction being involved with the migration file. Another mod thinks so too... https://discord.com/channels/839993398554656828/1429254115464052907/1429259581069983764
josip
josipOP5d ago
I mean migration was supposed to fail because there was some existing data that didn't respect the constraint but I expected it to be rolled back ok. I will report the bug then. thx for the help
silentworks
silentworks5d ago
I know for the CLI migrations run in transactions, but I don't use branching so not sure how those work.

Did you find this page helpful?