3
3
4
4
[ ! -z " $DEBUG " ] && set -x
5
5
6
- # first argument should be '15' or '16' for the version
7
- if [ " $1 " == " 15" ]; then
6
+ # Default values
7
+ PSQL_VERSION=" 15"
8
+ MIGRATION_FILE=" "
9
+ PORTNO=" @PGSQL_DEFAULT_PORT@"
10
+
11
+ # Function to display help
12
+ print_help () {
13
+ echo " Usage: nix run .#start-client -- [options]"
14
+ echo
15
+ echo " Options:"
16
+ echo " -v, --version [15|16|orioledb-16] Specify the PostgreSQL version to use (required)"
17
+ echo " -f, --file FILE Provide a custom migration script"
18
+ echo " -h, --help Show this help message"
19
+ echo
20
+ echo " Description:"
21
+ echo " Starts an interactive 'psql' session connecting to a Postgres database started with the"
22
+ echo " 'nix run .#start-server' command. If a migration file is not provided, the client"
23
+ echo " initializes the database with the default migrations for a new Supabase project."
24
+ echo " If a migrations file is provided, default migrations are skipped"
25
+ echo " If no migration file is provided, it runs the default Supabase migrations."
26
+ echo
27
+ echo " Examples:"
28
+ echo " nix run .#start-client"
29
+ echo " nix run .#start-client -- --version 15"
30
+ echo " nix run .#start-client -- --version 16 --file custom_migration.sql"
31
+ echo " nix run .#start-client -- --version 16 --port 5433"
32
+ }
33
+
34
+ # Parse arguments
35
+ while [[ " $# " -gt 0 ]]; do
36
+ case " $1 " in
37
+ -v|--version)
38
+ if [[ -n " $2 " && ! " $2 " =~ ^- ]]; then
39
+ PSQL_VERSION=" $2 "
40
+ shift 2
41
+ else
42
+ echo " Error: --version requires an argument (15, 16, or orioledb-16)"
43
+ exit 1
44
+ fi
45
+ ;;
46
+ -f|--file)
47
+ if [[ -n " $2 " && ! " $2 " =~ ^- ]]; then
48
+ MIGRATION_FILE=" $2 "
49
+ shift 2
50
+ else
51
+ echo " Error: --file requires a filename"
52
+ exit 1
53
+ fi
54
+ ;;
55
+ -h|--help)
56
+ print_help
57
+ exit 0
58
+ ;;
59
+ * )
60
+ echo " Unknown option: $1 "
61
+ print_help
62
+ exit 1
63
+ ;;
64
+ esac
65
+ done
66
+
67
+ # Check if version is provided
68
+ if [[ -z " $PSQL_VERSION " ]]; then
69
+ echo " Error: PostgreSQL version is required."
70
+ print_help
71
+ exit 1
72
+ fi
73
+
74
+ # Determine PostgreSQL version
75
+ if [ " $PSQL_VERSION " == " 15" ]; then
8
76
echo " Starting client for PSQL 15"
9
77
PSQL15=@PSQL15_BINDIR@
10
78
BINDIR=" $PSQL15 "
11
- elif [ " $1 " == " 16" ]; then
79
+ elif [ " $PSQL_VERSION " == " 16" ]; then
12
80
echo " Starting client for PSQL 16"
13
81
PSQL16=@PSQL16_BINDIR@
14
82
BINDIR=" $PSQL16 "
15
- elif [ " $1 " == " orioledb-16" ]; then
83
+ elif [ " $PSQL_VERSION " == " orioledb-16" ]; then
16
84
echo " Starting client for PSQL ORIOLEDB 16"
17
85
PSQLORIOLEDB16=@PSQLORIOLEDB16_BINDIR@
18
86
BINDIR=" $PSQLORIOLEDB16 "
19
87
else
20
88
echo " Please provide a valid Postgres version (15, 16, or orioledb-16)"
21
89
exit 1
22
90
fi
91
+
23
92
# vars for migration.sh
24
93
export PATH=$BINDIR /bin:$PATH
25
94
export POSTGRES_DB=postgres
26
95
export POSTGRES_HOST=localhost
27
- export POSTGRES_PORT=@PGSQL_DEFAULT_PORT@
96
+
28
97
PORTNO=" ${2:-@ PGSQL_DEFAULT_PORT@ } "
29
98
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
30
99
MIGRATIONS_DIR=@MIGRATIONS_DIR@
@@ -35,20 +104,37 @@ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$POR
35
104
create role postgres superuser login password '$PGPASSWORD ';
36
105
alter database postgres owner to postgres;
37
106
EOSQL
38
- for sql in " $MIGRATIONS_DIR " /init-scripts/* .sql; do
39
- echo " $0 : running $sql "
40
- psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -f " $sql " postgres
41
- done
42
- psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -c " ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD '"
43
- psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $PGBOUNCER_AUTH_SCHEMA_SQL "
44
- psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $STAT_EXTENSION_SQL "
45
- # run migrations as super user - postgres user demoted in post-setup
46
- for sql in " $MIGRATIONS_DIR " /migrations/* .sql; do
47
- echo " $0 : running $sql "
48
- psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p " $PORTNO " -h localhost -f " $sql " postgres
49
- done
50
- psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p " $PORTNO " -h localhost -f " $POSTGRESQL_SCHEMA_SQL " postgres
51
- # TODO Do we need to reset stats when running migrations locally?
52
- # psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -c 'SELECT extensions.pg_stat_statements_reset(); SELECT pg_stat_reset();' postgres || true
53
107
108
+ # Use custom migration script if provided
109
+ if [ -n " $MIGRATION_FILE " ]; then
110
+ echo " $0 : running user-provided migration file $MIGRATION_FILE "
111
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U " $PGSQL_SUPERUSER " -p " $PORTNO " -h localhost -f " $MIGRATION_FILE " postgres
112
+ else
113
+ # Run default init scripts
114
+ for sql in " $MIGRATIONS_DIR " /init-scripts/* .sql; do
115
+ echo " $0 : running $sql "
116
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -f " $sql " postgres
117
+ done
118
+
119
+ # Alter user password
120
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -c " ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD '"
121
+
122
+ # Run additional schema files
123
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $PGBOUNCER_AUTH_SCHEMA_SQL "
124
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p " $PORTNO " -h localhost -d postgres -f " $STAT_EXTENSION_SQL "
125
+
126
+ # Run migrations as superuser
127
+ for sql in " $MIGRATIONS_DIR " /migrations/* .sql; do
128
+ echo " $0 : running $sql "
129
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p " $PORTNO " -h localhost -f " $sql " postgres
130
+ done
131
+
132
+ # Run PostgreSQL schema
133
+ psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p " $PORTNO " -h localhost -f " $POSTGRESQL_SCHEMA_SQL " postgres
134
+ fi
135
+
136
+ # Optional: Reset stats if needed
137
+ # psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U supabase_admin -p "$PORTNO" -h localhost -c 'SELECT extensions.pg_stat_statements_reset(); SELECT pg_stat_reset();' postgres || true
138
+
139
+ # Start interactive psql session
54
140
exec psql -U postgres -p " $PORTNO " -h localhost postgres
0 commit comments