Skip to content

Commit c4f101a

Browse files
committed
chore: consolidate to just one start-client for postgres tooling
1 parent 884e1c4 commit c4f101a

File tree

3 files changed

+32
-71
lines changed

3 files changed

+32
-71
lines changed

flake.nix

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -334,35 +334,25 @@
334334
chmod +x $out/bin/start-postgres-server
335335
'';
336336

337-
# Start a version of the client.
338-
start-client = pkgs.runCommand "start-postgres-client" { } ''
339-
mkdir -p $out/bin
340-
substitute ${./nix/tools/run-client.sh.in} $out/bin/start-postgres-client \
341-
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
342-
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
343-
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
344-
chmod +x $out/bin/start-postgres-client
345-
'';
346-
347337
# Start a version of the client and runs migrations script on server.
348-
start-client-and-migrate =
338+
start-client =
349339
let
350340
migrationsDir = ./migrations/db;
351341
postgresqlSchemaSql = ./nix/tools/postgresql_schema.sql;
352342
pgbouncerAuthSchemaSql = ./ansible/files/pgbouncer_config/pgbouncer_auth_schema.sql;
353343
statExtensionSql = ./ansible/files/stat_extension.sql;
354344
in
355-
pkgs.runCommand "start-postgres-client-migrate" { } ''
345+
pkgs.runCommand "start-postgres-client" { } ''
356346
mkdir -p $out/bin
357-
substitute ${./nix/tools/run-client-migrate.sh.in} $out/bin/start-postgres-client-migrate \
347+
substitute ${./nix/tools/run-client.sh.in} $out/bin/start-postgres-client \
358348
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
359349
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
360350
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
361351
--subst-var-by 'MIGRATIONS_DIR' '${migrationsDir}' \
362352
--subst-var-by 'POSTGRESQL_SCHEMA_SQL' '${postgresqlSchemaSql}' \
363353
--subst-var-by 'PGBOUNCER_AUTH_SCHEMA_SQL' '${pgbouncerAuthSchemaSql}' \
364354
--subst-var-by 'STAT_EXTENSION_SQL' '${statExtensionSql}'
365-
chmod +x $out/bin/start-postgres-client-migrate
355+
chmod +x $out/bin/start-postgres-client
366356
'';
367357

368358
# Migrate between two data directories.
@@ -517,7 +507,6 @@
517507
{
518508
start-server = mkApp "start-server" "start-postgres-server";
519509
start-client = mkApp "start-client" "start-postgres-client";
520-
start-client-and-migrate = mkApp "start-client-and-migrate" "start-postgres-client-migrate";
521510
start-replica = mkApp "start-replica" "start-postgres-replica";
522511
migration-test = mkApp "migrate-tool" "migrate-postgres";
523512
sync-exts-versions = mkApp "sync-exts-versions" "sync-exts-versions";

nix/tools/run-client-migrate.sh.in

Lines changed: 0 additions & 54 deletions
This file was deleted.

nix/tools/run-client.sh.in

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,35 @@ else
2020
echo "Please provide a valid Postgres version (15, 16, or orioledb-16)"
2121
exit 1
2222
fi
23-
23+
#vars for migration.sh
2424
export PATH=$BINDIR/bin:$PATH
25-
25+
export POSTGRES_DB=postgres
26+
export POSTGRES_HOST=localhost
27+
export POSTGRES_PORT=@PGSQL_DEFAULT_PORT@
2628
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
29+
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
30+
MIGRATIONS_DIR=@MIGRATIONS_DIR@
31+
POSTGRESQL_SCHEMA_SQL=@POSTGRESQL_SCHEMA_SQL@
32+
PGBOUNCER_AUTH_SCHEMA_SQL=@PGBOUNCER_AUTH_SCHEMA_SQL@
33+
STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
34+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U "$PGSQL_SUPERUSER" -p "$PORTNO" -h localhost -d postgres <<-EOSQL
35+
create role postgres superuser login password '$PGPASSWORD';
36+
alter database postgres owner to postgres;
37+
EOSQL
38+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$PGBOUNCER_AUTH_SCHEMA_SQL"
39+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -d postgres -f "$STAT_EXTENSION_SQL"
40+
for sql in "$MIGRATIONS_DIR"/init-scripts/*.sql; do
41+
echo "$0: running $sql"
42+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -f "$sql" postgres
43+
done
44+
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -p "$PORTNO" -h localhost -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
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
2753

2854
exec psql -U postgres -p "$PORTNO" -h localhost postgres

0 commit comments

Comments
 (0)