Skip to content

Commit 4ab3640

Browse files
authored
feat: pg-restore utility (#1243)
Co-authored-by: Sam Rose <samuel@supabase.io>
1 parent f7bb3fe commit 4ab3640

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

flake.nix

+8
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@
459459
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
460460
chmod +x $out/bin/start-postgres-replica
461461
'';
462+
pg-restore =
463+
pkgs.runCommand "run-pg-restore" { } ''
464+
mkdir -p $out/bin
465+
substitute ${./nix/tools/run-restore.sh.in} $out/bin/pg-restore \
466+
--subst-var-by PSQL15_BINDIR '${basePackages.psql_15.bin}'
467+
chmod +x $out/bin/pg-restore
468+
'';
462469
sync-exts-versions = pkgs.runCommand "sync-exts-versions" { } ''
463470
mkdir -p $out/bin
464471
substitute ${./nix/tools/sync-exts-versions.sh.in} $out/bin/sync-exts-versions \
@@ -589,6 +596,7 @@
589596
start-replica = mkApp "start-replica" "start-postgres-replica";
590597
migration-test = mkApp "migrate-tool" "migrate-postgres";
591598
sync-exts-versions = mkApp "sync-exts-versions" "sync-exts-versions";
599+
pg-restore = mkApp "pg-restore" "pg-restore";
592600
};
593601

594602
# 'devShells.default' lists the set of packages that are included in the

nix/tools/run-restore.sh.in

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env bash
2+
# shellcheck shell=bash
3+
4+
set -euo pipefail
5+
6+
# Function to display help message
7+
show_help() {
8+
echo "Usage: nix run .#pg-restore -- [OPTIONS]"
9+
echo
10+
echo "Run pg_restore with the specified parameters."
11+
echo
12+
echo "Options:"
13+
echo " --version PostgreSQL version (currently only 15 is supported)"
14+
echo " --dbname Name of the database to restore to"
15+
echo " --host Host of the database server"
16+
echo " --user Database user to connect as"
17+
echo " --file Path to the file to restore from (absolute or relative to current directory)"
18+
echo " --port Port number (default: 5432)"
19+
echo " -h, --help Show this help message and exit"
20+
echo "Example:"
21+
echo "nix run .#pg-restore -- --version 15 --dbname postgres --host localhost --user postgres --port 5435 --file my.dump"
22+
}
23+
24+
# Initialize variables
25+
PG_VERSION=""
26+
DBNAME=""
27+
DBHOST=""
28+
DBUSER=""
29+
RESTORE_FILE=""
30+
PORT="5432"
31+
32+
# Parse command line arguments
33+
while [[ $# -gt 0 ]]; do
34+
case $1 in
35+
--version)
36+
PG_VERSION="$2"
37+
shift 2
38+
;;
39+
--dbname)
40+
DBNAME="$2"
41+
shift 2
42+
;;
43+
--host)
44+
DBHOST="$2"
45+
shift 2
46+
;;
47+
--user)
48+
DBUSER="$2"
49+
shift 2
50+
;;
51+
--file)
52+
RESTORE_FILE="$2"
53+
shift 2
54+
;;
55+
--port)
56+
PORT="$2"
57+
shift 2
58+
;;
59+
-h|--help)
60+
show_help
61+
exit 0
62+
;;
63+
*)
64+
echo "Unknown option: $1"
65+
show_help
66+
exit 1
67+
;;
68+
esac
69+
done
70+
71+
# Check if all required arguments are provided
72+
if [ -z "$PG_VERSION" ] || [ -z "$DBNAME" ] || [ -z "$DBHOST" ] || [ -z "$DBUSER" ] || [ -z "$RESTORE_FILE" ]; then
73+
echo "Error: Missing required arguments."
74+
show_help
75+
exit 1
76+
fi
77+
78+
if [ "$PG_VERSION" == "15" ]; then
79+
echo "Starting restore for PSQL 15"
80+
PSQL15=@PSQL15_BINDIR@
81+
PSQL_BINDIR="$PSQL15"
82+
else
83+
echo "Error: Please provide a valid Postgres version (currently only 15 is supported)"
84+
show_help
85+
exit 1
86+
fi
87+
88+
# Convert RESTORE_FILE to an absolute path if it's relative
89+
if [[ "$RESTORE_FILE" != /* ]]; then
90+
RESTORE_FILE="$(pwd)/$RESTORE_FILE"
91+
fi
92+
93+
# Check if the file exists
94+
if [ ! -f "$RESTORE_FILE" ]; then
95+
echo "Error: Restore file '$RESTORE_FILE' does not exist."
96+
exit 1
97+
fi
98+
99+
echo "Using restore file: $RESTORE_FILE"
100+
101+
# Run pg_restore and capture its exit status
102+
"$PSQL_BINDIR/bin/pg_restore" \
103+
-h "$DBHOST" \
104+
-p "$PORT" \
105+
-U "$DBUSER" \
106+
-d "$DBNAME" \
107+
-v \
108+
--no-owner \
109+
--no-acl \
110+
"$RESTORE_FILE"
111+
112+
RESTORE_STATUS=$?
113+
114+
# Check the exit status of pg_restore
115+
if [ $RESTORE_STATUS -eq 0 ]; then
116+
echo "Restore completed successfully."
117+
exit 0
118+
else
119+
echo "Restore failed with exit code $RESTORE_STATUS."
120+
exit $RESTORE_STATUS
121+
fi

0 commit comments

Comments
 (0)