chore: added sqlx_cli dependency

- Formatted init_db.sh script
This commit is contained in:
minhtrannhat 2024-05-02 18:38:51 -04:00
parent 3c09eccfef
commit 53f20f16f4
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
3 changed files with 888 additions and 25 deletions

854
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -19,3 +19,15 @@ actix-web = "4.5.1"
reqwest = "0.12.2" reqwest = "0.12.2"
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }
tokio = { version = "1.36.0", features = ["full"] } tokio = { version = "1.36.0", features = ["full"] }
[dependencies.sqlx]
version = "0.7"
default-features = false
features = [
"runtime-tokio-rustls",
"macros",
"postgres",
"uuid",
"chrono",
"migrate"
]

View File

@ -1,9 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -x set -x
set -eo pipefail set -eo pipefail
# check for sqlx and psql first
if ! [ -x "$(command -v psql)" ]; then if ! [ -x "$(command -v psql)" ]; then
echo >&2 "Error: psql is not installed." echo >&2 "Error: psql is not installed."
exit 1 exit 1
@ -12,56 +10,55 @@ fi
if ! [ -x "$(command -v sqlx)" ]; then if ! [ -x "$(command -v sqlx)" ]; then
echo >&2 "Error: sqlx is not installed." echo >&2 "Error: sqlx is not installed."
echo >&2 "Use:" echo >&2 "Use:"
echo >&2 " cargo install --version='~0.7' sqlx-cli \ echo >&2 " cargo install --version='~0.7' sqlx-cli --no-default-features --features rustls,postgres"
--no-default-features --features rustls,postgres"
echo >&2 "to install it." echo >&2 "to install it."
exit 1 exit 1
fi fi
# Check if a custom user has been set, otherwise default to 'postgres' # Check if a custom user has been set, otherwise default to 'postgres'
DB_USER="${POSTGRES_USER:=postgres}" DB_USER="${POSTGRES_USER:=postgres}"
# Check if a custom password has been set, otherwise default to 'password' # Check if a custom password has been set, otherwise default to 'password'
DB_PASSWORD="${POSTGRES_PASSWORD:=password}" DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
# Check if a custom database name has been set, otherwise default to 'newsletter' # Check if a custom database name has been set, otherwise default to 'newsletter'
DB_NAME="${POSTGRES_DB:=newsletter}" DB_NAME="${POSTGRES_DB:=newsletter}"
# Check if a custom port has been set, otherwise default to '5432' # Check if a custom port has been set, otherwise default to '5432'
DB_PORT="${POSTGRES_PORT:=5432}" DB_PORT="${POSTGRES_PORT:=5432}"
# Check if a custom host has been set, otherwise default to 'localhost' # Check if a custom host has been set, otherwise default to 'localhost'
DB_HOST="${POSTGRES_HOST:=localhost}" DB_HOST="${POSTGRES_HOST:=localhost}"
# Launch postgres using Docker
# Allow to skip Docker if a dockerized Postgres database is already running # Allow to skip Docker if a dockerized Postgres database is already running
if [[ -z "${SKIP_DOCKER}" ]] if [[ -z "${SKIP_DOCKER}" ]]
then then
# if a postgres container is running, print instructions to kill it and exit
RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
echo >&2 "there is a postgres container already running, kill it with"
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
exit 1
fi
# Launch postgres using Docker
docker run \ docker run \
-e POSTGRES_USER=${DB_USER} \ -e POSTGRES_USER=${DB_USER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \
-e POSTGRES_DB=${DB_NAME} \ -e POSTGRES_DB=${DB_NAME} \
-p "${DB_PORT}":5432 \ -p "${DB_PORT}":5432 \
-d postgres \ -d \
postgres -N 1000 --name "postgres_$(date '+%s')" \
# ^ Increased maximum number of connections for testing purposes postgres -N 1000
# ^ Increased maximum number of connections for testing purposes
fi fi
# Keep pinging Postgres until it's ready to accept commands # Keep pinging Postgres until it's ready to accept commands
export PGPASSWORD="${DB_PASSWORD}" until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
until psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
>&2 echo "Postgres is still unavailable - sleeping" >&2 echo "Postgres is still unavailable - sleeping"
sleep 1 sleep 1
done done
>&2 echo "Postgres is up and running on port ${DB_PORT}!" >&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
export DATABASE_URL
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
>&2 echo "DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
sqlx database create sqlx database create
sqlx migrate run sqlx migrate run
>&2 echo "Postgres has been migrated, ready to go!" >&2 echo "Postgres has been migrated, ready to go!"