Skip to content

Conversation

@Adityakk9031
Copy link
Contributor

Closes: #4521
🐛 Issue Summary

In certain cases, a migration that should fail inside a transaction (e.g., due to violating a newly-added constraint) is still being:

Applied to the database, and

Recorded inside schema_migrations,

While the Supabase dashboard reports it as “Failed”.

This results in partial / inconsistent application of the migration file and breaks Supabase’s migration safety guarantees.

The issue appears to stem from the migration executor not correctly propagating SQL transactional errors in some edge cases involving ALTER TABLE ... ADD CONSTRAINT, generated columns, or multi-statement migration files.

✅ What This PR Changes

  1. Fixes error detection + rollback behavior in migration executor

Ensures that any SQL error inside a migration file is:

Correctly captured

Causes a full transaction rollback

Prevents the migration from being marked as applied

  1. Improves error propagation to CLI + Dashboard

Errors now bubble up reliably from the Postgres wire protocol layer.

CLI and API will no longer falsely report:

“Migration failed”

While still committing parts of the migration.

  1. Adds safety guards to avoid partial migrations

Ensures the schema_migrations insert only happens after a successful transaction commit.

Ensures executor failure paths never call the migration-completed hook.

  1. Adds regression tests

Test cases that verify:

Constraint violation → rollback → file NOT applied

Multi-statement migration rollback behavior

Generated column + check constraint edge case

🧪 How the Bug Was Reproduced (Based on User Report)

A migration containing:

new table creation

generated columns

multiple ALTER TABLE operations

new CHECK constraints

…would partially apply, even though a constraint violation should have caused a rollback.

This PR addresses exactly this scenario.

🛠️ Implementation Notes

Updated migration execution wrapper to treat all SQLSTATE codes ≥ 40000 as rollback-worthy.

Removed early return conditions that ran before transaction abort detection.

Ensured error logs do not trigger a false “migration applied” completion flow.

📎 Linked Issue

https: //github.com/supabase/supabase/issues/40761

@Adityakk9031 Adityakk9031 requested a review from a team as a code owner November 25, 2025 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed migration was still applied

1 participant