Fix 'without db' confusion

This commit is contained in:
Luca Palmieri 2024-08-31 17:09:08 +02:00
parent 06b5915560
commit c69a0cbbda
5 changed files with 64 additions and 28 deletions

View file

@ -16,6 +16,9 @@ env:
CARGO_TERM_COLOR: always CARGO_TERM_COLOR: always
SQLX_VERSION: 0.8.0 SQLX_VERSION: 0.8.0
SQLX_FEATURES: "rustls,postgres" SQLX_FEATURES: "rustls,postgres"
APP_USER: app
APP_USER_PWD: secret
APP_DB_NAME: newsletter
jobs: jobs:
test: test:
@ -56,6 +59,18 @@ jobs:
# This may be useful for ensuring reproducible builds, to use the exact same set of dependencies that were available when the package was published. # This may be useful for ensuring reproducible builds, to use the exact same set of dependencies that were available when the package was published.
# It may also be useful if a newer version of a dependency is published that no longer builds on your system, or has other problems # It may also be useful if a newer version of a dependency is published that no longer builds on your system, or has other problems
- name: Create app user in Postgres
run: |
sudo apt-get install postgresql-client
# Create the application user
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${CREATE_QUERY}"
# Grant create db privileges to the app user
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${GRANT_QUERY}"
- name: Migrate database - name: Migrate database
run: | run: |
SKIP_DOCKER=true ./scripts/init_db.sh SKIP_DOCKER=true ./scripts/init_db.sh
@ -117,6 +132,17 @@ jobs:
--features ${{ env.SQLX_FEATURES }} --features ${{ env.SQLX_FEATURES }}
--no-default-features --no-default-features
--locked --locked
- name: Create app user in Postgres
run: |
sudo apt-get install postgresql-client
# Create the application user
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${CREATE_QUERY}"
# Grant create db privileges to the app user
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${GRANT_QUERY}"
- name: Migrate database - name: Migrate database
run: SKIP_DOCKER=true ./scripts/init_db.sh run: SKIP_DOCKER=true ./scripts/init_db.sh
- name: Install cargo-llvm-cov - name: Install cargo-llvm-cov

View file

@ -10,16 +10,13 @@ if ! [ -x "$(command -v sqlx)" ]; then
exit 1 exit 1
fi fi
# Check if a custom user has been set, otherwise default to 'postgres' # Check if a custom parameter has been set, otherwise use default values
DB_USER="${POSTGRES_USER:=postgres}" DB_PORT="${DB_PORT:=5432}"
# Check if a custom password has been set, otherwise default to 'password' SUPERUSER="${SUPERUSER:=postgres}"
DB_PASSWORD="${POSTGRES_PASSWORD:=password}" SUPERUSER_PWD="${SUPERUSER_PWD:=password}"
# Check if a custom database name has been set, otherwise default to 'newsletter' APP_USER="${APP_USER:=app}"
DB_NAME="${POSTGRES_DB:=newsletter}" APP_USER_PWD="${APP_USER_PWD:=secret}"
# Check if a custom port has been set, otherwise default to '5432' APP_DB_NAME="${APP_DB_NAME:=newsletter}"
DB_PORT="${POSTGRES_PORT:=5432}"
# Check if a custom host has been set, otherwise default to 'localhost'
DB_HOST="${POSTGRES_HOST:=localhost}"
# 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}" ]]
@ -34,15 +31,14 @@ then
CONTAINER_NAME="postgres_$(date '+%s')" CONTAINER_NAME="postgres_$(date '+%s')"
# Launch postgres using Docker # Launch postgres using Docker
docker run \ docker run \
-e POSTGRES_USER=${DB_USER} \ --env POSTGRES_USER=${SUPERUSER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \ --env POSTGRES_PASSWORD=${SUPERUSER_PWD} \
-e POSTGRES_DB=${DB_NAME} \ --health-cmd="pg_isready -U ${SUPERUSER} || exit 1" \
--health-cmd="pg_isready -U ${DB_USER} || exit 1" \
--health-interval=1s \ --health-interval=1s \
--health-timeout=5s \ --health-timeout=5s \
--health-retries=5 \ --health-retries=5 \
-p "${DB_PORT}":5432 \ --publish "${DB_PORT}":5432 \
-d \ --detach \
--name "${CONTAINER_NAME}" \ --name "${CONTAINER_NAME}" \
postgres -N 1000 postgres -N 1000
# ^ Increased maximum number of connections for testing purposes # ^ Increased maximum number of connections for testing purposes
@ -54,11 +50,21 @@ then
>&2 echo "Postgres is still unavailable - sleeping" >&2 echo "Postgres is still unavailable - sleeping"
sleep 1 sleep 1
done done
# Create the application user
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';"
docker exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER}" -c "${CREATE_QUERY}"
# Grant create db privileges to the app user
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;"
docker exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER}" -c "${GRANT_QUERY}"
fi fi
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" >&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME} # Create the application database
DATABASE_URL=postgres://${APP_USER}:${APP_USER_PWD}@localhost:${DB_PORT}/${APP_DB_NAME}
export DATABASE_URL
sqlx database create sqlx database create
sqlx migrate run sqlx migrate run

View file

@ -18,7 +18,7 @@ pub struct ApplicationSettings {
pub host: String, pub host: String,
} }
#[derive(serde::Deserialize)] #[derive(serde::Deserialize, Clone)]
pub struct DatabaseSettings { pub struct DatabaseSettings {
pub username: String, pub username: String,
pub password: Secret<String>, pub password: Secret<String>,
@ -30,7 +30,7 @@ pub struct DatabaseSettings {
} }
impl DatabaseSettings { impl DatabaseSettings {
pub fn without_db(&self) -> PgConnectOptions { pub fn connect_options(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl { let ssl_mode = if self.require_ssl {
PgSslMode::Require PgSslMode::Require
} else { } else {
@ -42,10 +42,7 @@ impl DatabaseSettings {
.password(self.password.expose_secret()) .password(self.password.expose_secret())
.port(self.port) .port(self.port)
.ssl_mode(ssl_mode) .ssl_mode(ssl_mode)
} .database(&self.database_name)
pub fn with_db(&self) -> PgConnectOptions {
self.without_db().database(&self.database_name)
} }
} }

View file

@ -11,7 +11,8 @@ async fn main() -> std::io::Result<()> {
init_subscriber(subscriber); init_subscriber(subscriber);
let configuration = get_configuration().expect("Failed to read configuration."); let configuration = get_configuration().expect("Failed to read configuration.");
let connection_pool = PgPoolOptions::new().connect_lazy_with(configuration.database.with_db()); let connection_pool =
PgPoolOptions::new().connect_lazy_with(configuration.database.connect_options());
let sender_email = configuration let sender_email = configuration
.email_client .email_client

View file

@ -1,3 +1,4 @@
use secrecy::Secret;
use sqlx::{Connection, Executor, PgConnection, PgPool}; use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener; use std::net::TcpListener;
use std::sync::LazyLock; use std::sync::LazyLock;
@ -62,23 +63,28 @@ async fn spawn_app() -> TestApp {
pub async fn configure_database(config: &DatabaseSettings) -> PgPool { pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
// Create database // Create database
let mut connection = PgConnection::connect_with(&config.without_db()) let maintenance_settings = DatabaseSettings {
database_name: "postgres".to_string(),
username: "postgres".to_string(),
password: Secret::new("password".to_string()),
..config.clone()
};
let mut connection = PgConnection::connect_with(&maintenance_settings.connect_options())
.await .await
.expect("Failed to connect to Postgres"); .expect("Failed to connect to Postgres");
connection connection
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name)) .execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str())
.await .await
.expect("Failed to create database."); .expect("Failed to create database.");
// Migrate database // Migrate database
let connection_pool = PgPool::connect_with(config.with_db()) let connection_pool = PgPool::connect_with(config.connect_options())
.await .await
.expect("Failed to connect to Postgres."); .expect("Failed to connect to Postgres.");
sqlx::migrate!("./migrations") sqlx::migrate!("./migrations")
.run(&connection_pool) .run(&connection_pool)
.await .await
.expect("Failed to migrate the database"); .expect("Failed to migrate the database");
connection_pool connection_pool
} }