Rollback ("down") Migrations #11263
Replies: 27 comments 10 replies
-
Any update here? |
Beta Was this translation helpful? Give feedback.
-
this is pretty important, how do others handle wrong migrations? |
Beta Was this translation helpful? Give feedback.
-
Agreed. Would also like this. |
Beta Was this translation helpful? Give feedback.
-
This would be nice |
Beta Was this translation helpful? Give feedback.
-
I would also love to have a nice solution to this. Currently, we do something like this, which is not ideal and sometimes breaks:
|
Beta Was this translation helpful? Give feedback.
-
Bumping this -- automatic down migrations would be incredibly helpful within many developer workflows. |
Beta Was this translation helpful? Give feedback.
-
bumping this ^ |
Beta Was this translation helpful? Give feedback.
-
bumping this ^ |
Beta Was this translation helpful? Give feedback.
-
Hi, I'd create a simple shell script to handle it for me. Adding new migration
Applying migration
Rollback migration
CodeThe script will tries to look into you
#!/bin/bash
set -a # automatically export all variables
. <(cat .env supabase/.env)
set +a
# Function to display help information
help() {
echo "Usage: $0 [command] [options]"
echo
echo "Commands:"
echo " new NAME Create a new migration item"
echo " up Push the current state"
echo " rollback Rollback to the previous state"
echo " help Display this help message"
echo
echo "Options for 'new' command:"
echo " -n NAME Specify the name of the new migration"
}
new() {
local migration_name=$1
shift
local migrations_folder="./supabase/migrations"
local migration_timestamp="$(date +%Y%m%d%H%M%S)"
mkdir -p $migrations_folder
echo "-- $migration_name | Up commands" >"$migrations_folder/${migration_timestamp}_$migration_name.sql"
echo "-- $migration_name | Drop commands" >"$migrations_folder/${migration_timestamp}|_$migration_name.drop.sql"
echo "Created $migration_name migration files"
}
rollback() {
read -p "Attention the Supabase DB will be rollback to the previous migration. Are you sure? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Operation aborted"
exit 0
fi
local rollback_limit=1
read -red '' -a versions <<<$(psql $PG_CONN_URL -t -A -c \
"select version from supabase_migrations.schema_migrations order by version DESC LIMIT $rollback_limit;")
for version in "${versions[@]}"; do
local drop_file=$(fzf --exit-0 --select-1 -q "$version|_.drop.sql")
if [[ -z $drop_file ]]; then
echo "No drop script found for $version migration."
exit 1
fi
echo "Rollback migration: $version with '$drop_file'"
local drop_result=$(
psql $PG_CONN_URL -t -A --single-transaction -f \
<(cat <(echo "delete from supabase_migrations.schema_migrations where version='$version';") $drop_file)
)
if [[ $drop_result == *"ERROR"* ]]; then
echo "==="
echo "Error while executing script: '$drop_file'"
exit 1
fi
echo "Rollback migration: Successfully executed '$drop_file'"
done
}
main() {
local command=$1
shift
case $command in
new) new "$@" ;;
up) ./supabase-cli migration up --self-hosted $@ ;; # Here you can put the default supabase-cli
rollback) rollback ;;
-h | --help) help ;;
*)
echo "Invalid option: '$command'"
help
exit 1
;;
esac
}
main "$@" There is another alias script #! /bin/bash
set -a # automatically export all variables
. <(cat .env supabase/.env)
set +a
ARGS=$*
# '--self-host' : Use Self host
if [[ $* == *"--self-host"* ]]; then
ARGS="$@ --db-url $PG_CONN_URL"
fi
# echo $ARGS
pnpm dlx supabase $(echo ${ARGS/--self-host/}) |
Beta Was this translation helpful? Give feedback.
-
I was quite surprised when I found this discussion! Should be one of the essentials to also be able to undo migrations. |
Beta Was this translation helpful? Give feedback.
-
How is this not a thing? Please add rollback for last migration. |
Beta Was this translation helpful? Give feedback.
-
+1 for needing down migrations! |
Beta Was this translation helpful? Give feedback.
-
Agreed. This is a crucial feature. We need a way to handle wrong migrations |
Beta Was this translation helpful? Give feedback.
-
Please refrain from commenting "+1", "I want this too" etc. It has no known impact on prioritization and will send a notification email to up to 22 participants. |
Beta Was this translation helpful? Give feedback.
-
any update on this? Has anyone from Supabase even seen this issue yet? Rolling back migrations is a basic feature in many other tools, it's quite surprising how this is not in Supabase yet |
Beta Was this translation helpful? Give feedback.
-
One of the several things I'd like to see from Supabase, but I see no response to are:
|
Beta Was this translation helpful? Give feedback.
-
Yes, we need this! |
Beta Was this translation helpful? Give feedback.
-
The question for Supabase developers team. |
Beta Was this translation helpful? Give feedback.
-
Regarding not having down migrations for a production deploy, after reading the docs, I believe the general recommendation is do enough testing you are sure you will not have to roll back production after pushing the migrations, or, as an alternative, having a migration written ahead of time that is essentially a down migration that you can quickly push up if necessary. Not the best solution; it would be much better to have formal down migrations so you don't mess up the migration history will rollbacks, and can write them and not worry about accidentally pushing the wrong migration. In development, my team and I use |
Beta Was this translation helpful? Give feedback.
-
Long story short: you can use
if you just want to remove a migration entry from the database. (Or probably best: Apply a new migration that un-does a previous migration, then remove both those entries with the Details: I recently started using AI with MCP, and in my last session I asked it to add a new RLS Policy. That was not what I wanted, but well, I did actually need the RLS, and there was no error in it or anything, so I thought let's just go with it. Stupid thing is that the naming scheme the AI applied to the migration was also different than mine; which breaks the ordering. In this case, I HAVE TO revert, there's almost no way around (I could have from now on adapted a new naming scheme, but I don't want to!) Took me a while to find out that I'm surprised though that this command is only mentioned once as part of another script... And I would say that a situation like this is a good and valid example of why sometimes rollback is really helpful (yes, of course we should always check what AI does, and we should always test things before it goes to prod etc...) But sometimes sh*t like this happens and you just want to revert! |
Beta Was this translation helpful? Give feedback.
-
We eventually decided to use node-pg-migrate for migrations, it supports
migrate down as well
https://salsita.github.io/node-pg-migrate/
We can use it with Supabase
…On Wed, Apr 23, 2025, 12:26 Jonas ***@***.***> wrote:
you can use
supabase migration repair --status reverted [your migration identifier]
to remove migrations from the migration table.
With that, you can: apply a new migration that un-does a previous
migration, then remove both those entries with the repair --status reverted
command from the migration table.
—
Reply to this email directly, view it on GitHub
<#11263 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACYJBQ4SB6OHBS3XUWRTVQL224QBRAVCNFSM6AAAAAATJG6YDKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEOJRHAZDAOI>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi all! I've been using Hasura for my company's codebase but am interested in learning more about how Supabase works.
I'm trying to set up migrations, and after following the guide included in the Supabase CLI blog post, I was able to get a forward migration working. But I'm curious whether there is a way perform a migration "rollback"? Use case: developer A and developer B are working on two sibling branches, so when merging their branches it might be helpful to roll back applied migrations to resolve potential conflicts.
Hasura generates an
up.sql
anddown.sql
for each migration, and you can runhasura migrate apply --down 2
orhasura migrate apply --up 1
to move your database schema forward or backward in time. Is there a similar implementation that I'm missing for Supabase?Hasura doc for rolling back migrations: https://hasura.io/docs/latest/migrations-metadata-seeds/manage-migrations/#roll-back-migrations
Beta Was this translation helpful? Give feedback.
All reactions