Rollback ("down") Migrations #11263
Replies: 10 comments 5 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.
-
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