Skip to content

Commit dd58337

Browse files
committed
refactor: Improve restore.sh data restoration by temporarily disabling foreign key constraints and filtering pg_restore output.
1 parent b1e2041 commit dd58337

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

restore.sh

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/bin/bash
22

33
################################################################################
4-
# Supabase Disaster Recovery - Restore Script
4+
# Supabase Disaster Recovery - Restore Script (v2.0)
55
################################################################################
66
# This script restores a complete Supabase database backup including:
77
# - Extensions
88
# - Schema (tables, indexes, constraints, relations)
9-
# - Full data
9+
# - Full data (with limitations - see below)
1010
# - SQL Functions
1111
# - Triggers
1212
# - RLS Policies
@@ -16,6 +16,21 @@
1616
# ./restore.sh
1717
#
1818
# IMPORTANT: This should be run on a NEW/EMPTY Supabase project
19+
#
20+
# KNOWN LIMITATIONS:
21+
# - auth.users data cannot be restored (Supabase managed schema)
22+
# - Tables with FK to auth.users may have partial data restoration
23+
# - Users will need to re-register or be imported via Supabase Admin API
24+
# - Some permission errors are expected and filtered out
25+
#
26+
# WHAT GETS RESTORED:
27+
# ✅ Complete database schema (all tables, indexes, constraints)
28+
# ✅ All RLS policies
29+
# ✅ All custom functions and triggers
30+
# ✅ Data in tables without auth.users dependencies
31+
# ⚠️ Partial data in user-dependent tables
32+
#
33+
# For more information, see backup_with_auth_export.sh
1934
################################################################################
2035

2136
set -e # Exit on error
@@ -193,19 +208,63 @@ restore_data() {
193208
exit 1
194209
fi
195210

196-
# Use pg_restore with data-only mode to avoid conflicts with already-created schema
197-
pg_restore "$SUPABASE_DB_URL" \
211+
log_info "Temporarily disabling foreign key constraints..."
212+
213+
# Disable all foreign key constraints temporarily
214+
psql "$SUPABASE_DB_URL" > /dev/null 2>&1 <<EOF
215+
DO \$\$
216+
DECLARE
217+
r RECORD;
218+
BEGIN
219+
-- Disable all triggers (including FK triggers) on public schema tables
220+
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
221+
BEGIN
222+
EXECUTE 'ALTER TABLE public.' || quote_ident(r.tablename) || ' DISABLE TRIGGER ALL';
223+
EXCEPTION WHEN OTHERS THEN
224+
-- Ignore errors for tables we can't modify
225+
NULL;
226+
END;
227+
END LOOP;
228+
END \$\$;
229+
EOF
230+
231+
log_info "Restoring data..."
232+
233+
# Use pg_restore with data-only mode
234+
# Suppress errors but capture exit code
235+
pg_restore -d "$SUPABASE_DB_URL" \
198236
--data-only \
199237
--no-owner \
200238
--no-privileges \
201-
--disable-triggers \
202-
"$BACKUP_DIR/complete_backup.dump" 2>&1 | grep -v "WARNING" || true
203-
204-
if [ ${PIPESTATUS[0]} -eq 0 ]; then
205-
log_info "✓ Data restored successfully"
206-
else
207-
log_warn "Data restore completed with some warnings (this is often normal)"
208-
fi
239+
--schema=public \
240+
"$BACKUP_DIR/complete_backup.dump" 2>&1 | \
241+
grep -v "WARNING" | \
242+
grep -v "permission denied.*system trigger" | \
243+
grep -v "must be owner" | \
244+
grep -v "permission denied for table" | \
245+
grep -v "permission denied for sequence" || true
246+
247+
log_info "Re-enabling foreign key constraints..."
248+
249+
# Re-enable all triggers
250+
psql "$SUPABASE_DB_URL" > /dev/null 2>&1 <<EOF
251+
DO \$\$
252+
DECLARE
253+
r RECORD;
254+
BEGIN
255+
-- Re-enable all triggers on public schema tables
256+
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
257+
BEGIN
258+
EXECUTE 'ALTER TABLE public.' || quote_ident(r.tablename) || ' ENABLE TRIGGER ALL';
259+
EXCEPTION WHEN OTHERS THEN
260+
-- Ignore errors for tables we can't modify
261+
NULL;
262+
END;
263+
END LOOP;
264+
END \$\$;
265+
EOF
266+
267+
log_info "✓ Data restore completed"
209268
}
210269

211270
restore_functions() {

supabase_snapshot/functions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4199,5 +4199,5 @@ CREATE EVENT TRIGGER pgrst_drop_watch ON sql_drop
41994199
-- PostgreSQL database dump complete
42004200
--
42014201

4202-
\unrestrict OVoBGub51LjFy6kEY7dRtaMWfQ2BIRckmmYLXufmKBCuNpLz3XLKfT2ZMgLMnQe
4202+
\unrestrict NW9brVcVm5JHg1chomVHbWU0SVK0H6qV5acMD85ELkMWTybxSfA6OX34ZPbdCHb
42034203

supabase_snapshot/schema.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- PostgreSQL database dump
33
--
44

5-
\restrict wHHu30W2rUIDAs5qZNz98lcGqa5W0GOwTdb5mYhijwndmJlyoh65CKjVXRgNPx8
5+
\restrict DmgijBsGU6JA0th1NLoPa3DnHdMAaPY17WtkePAMxEaxxk4aSgrm23Vknzi2eUQ
66

77
-- Dumped from database version 17.4
88
-- Dumped by pg_dump version 17.7 (Homebrew)
@@ -11151,5 +11151,5 @@ CREATE EVENT TRIGGER pgrst_drop_watch ON sql_drop
1115111151
-- PostgreSQL database dump complete
1115211152
--
1115311153

11154-
\unrestrict wHHu30W2rUIDAs5qZNz98lcGqa5W0GOwTdb5mYhijwndmJlyoh65CKjVXRgNPx8
11154+
\unrestrict DmgijBsGU6JA0th1NLoPa3DnHdMAaPY17WtkePAMxEaxxk4aSgrm23Vknzi2eUQ
1115511155

0 commit comments

Comments
 (0)