Skip to content

Commit

Permalink
Add startup script to start the postgres instance on a selected port (#…
Browse files Browse the repository at this point in the history
…1901)

Just easier to manage multiple postgres instances this way.

This script will: 
1. set the port number you passed to it in the passport server .env
2. start the postgres instance with the selected port.
3. verify that you didnt try to use a already taken post, so you dont
have to guess.


Works only on unix based machines tho (and, i think, WSL. didnt check
tho)

---------

Co-authored-by: Richard Liu <[email protected]>
  • Loading branch information
ororsatti and rrrliu authored Jan 13, 2025
1 parent 38578c1 commit 46a942c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion apps/passport-server/src/database/postgresConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNaN, parseInt } from "lodash";
import { ClientConfig } from "pg";
import { PoolOptionsExplicit, SslSettings } from "postgres-pool";

Expand All @@ -24,6 +25,12 @@ export function getDatabaseConfiguration(
if (process.env.DATABASE_DB_NAME === undefined) {
throw new Error("Missing environment variable: DATABASE_DB_NAME");
}
if (
process.env.DATABASE_PORT !== undefined &&
isNaN(process.env.DATABASE_PORT)
) {
throw new Error("Invalid environment variable: DATABASE_PORT");
}
if (!["true", "false"].includes(process.env.DATABASE_SSL || "")) {
throw new Error("Missing or incorrect env variable: DATABASE_SSL");
}
Expand All @@ -49,7 +56,7 @@ export function getDatabaseConfiguration(
password: process.env.DATABASE_PASSWORD,
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_DB_NAME,
port: 5432,
port: parseInt(process.env.DATABASE_PORT ?? "5432"),
ssl:
process.env.DATABASE_SSL === "true"
? { rejectUnauthorized: false }
Expand Down
1 change: 1 addition & 0 deletions apps/passport-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export interface APIs {
export interface EnvironmentVariables {
MAILGUN_API_KEY?: string;
DATABASE_USERNAME?: string;
DATABASE_PORT?: string;
DATABASE_PASSWORD?: string;
DATABASE_HOST?: string;
DATABASE_DB_NAME?: string;
Expand Down
42 changes: 42 additions & 0 deletions scripts/db-up-zupass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
PORT=${1:-5432}
if ! [[ $PORT =~ ^[0-9]+$ && $PORT -gt 0 ]]; then
echo "Error: $PORT is not a port number." >&2
exit 1
fi


# Get a list of all listening port numbers
ports=$(sudo lsof -i -P -n | grep LISTEN | awk '{print $(NF - 1)}' | awk -F':' '{print $NF}')

# Convert the list into an array
port_array=($ports)

# Print the list of ports
for port in "${port_array[@]}"; do
if [[ $PORT -eq $port ]]; then
echo "Error: $PORT is taken." >&2
exit 1
fi
done

ENV_FILE="./apps/passport-server/.env"

# Check if .env file exists
if [[ ! -f $ENV_FILE ]]; then
echo "Error: $ENV_FILE does not exist." >&2
exit 1
fi

# Update DATABASE_PORT in the .env file or add it if it doesn't exist
if grep -q "^DATABASE_PORT=" "$ENV_FILE"; then
# Update the existing DATABASE_PORT line
sed -i '' "s/^DATABASE_PORT=.*/DATABASE_PORT=$PORT/" "$ENV_FILE"
echo "Updated DATABASE_PORT to $PORT in $ENV_FILE."
else
# Add DATABASE_PORT to the .env file
echo "DATABASE_PORT=$PORT" >> "$ENV_FILE"
echo "Added DATABASE_PORT=$PORT to $ENV_FILE."
fi

pg_ctl -D apps/passport-server/local-db-data -l apps/passport-server/local-db-log -o "-p $PORT" start
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"EMBEDDED_ZAPPS",
"DEVCON_TICKET_QUERY_ORIGINS",
"GENERATED_CHUNKS",
"DATABASE_PORT",
"TELEGRAM_CHAT_TO_PRODUCT_IDS",
"IGNORE_NON_PRIORITY_FEEDS",
"PRIORITY_FEED_PROVIDER_URLS",
Expand Down

0 comments on commit 46a942c

Please sign in to comment.