From 855261b61baa6be55ee39bc32191e25dbbd608fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trygve=20Laugst=C3=B8l?= Date: Tue, 31 Dec 2024 12:34:35 +0100 Subject: [PATCH] Less smart database setup, single user for the app Makes this run on Scaleway where you only get a single database user. Can probably be improved later. --- .gitignore | 1 + .idea/sqldialects.xml | 4 ---- .idea/vcs.xml | 9 ++++++++ .settings.sh | 2 +- backend/src/main.rs | 32 +++++++++++++++++++++++++- init.sql => migrations/00-create.sql | 17 +++----------- migrations/00-drop.sql | 11 +++++++++ migrations/20241225094146_baseline.sql | 2 +- migrations/20241225161533_baseline.sql | 4 ++-- migrations/20241226063214_baseline.sql | 4 ++-- 10 files changed, 61 insertions(+), 25 deletions(-) rename init.sql => migrations/00-create.sql (56%) create mode 100644 migrations/00-drop.sql diff --git a/.gitignore b/.gitignore index f25fce1..eabd01e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.tmp.* tmp *.tmp +*.env diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 95736e8..6df4889 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -1,10 +1,6 @@ - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..7741923 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,5 +1,14 @@ + + + + + + + + diff --git a/.settings.sh b/.settings.sh index 4447e6b..336a7cc 100644 --- a/.settings.sh +++ b/.settings.sh @@ -10,4 +10,4 @@ export PGPORT=5555 export PGUSER=postgres export PGPASSWORD=postgres -export DATABASE_URL="postgres://skjera-owner:skjera-owner@localhost/skjera" +export DATABASE_URL="postgres://localhost/skjera" diff --git a/backend/src/main.rs b/backend/src/main.rs index 1bf4e07..e71c992 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -31,7 +31,7 @@ use std::process::exit; use tokio::net::TcpListener; use tokio::signal; use tower_http::{services::ServeDir, trace::TraceLayer}; -use tracing::{debug, info, span, Level}; +use tracing::{debug, info, span, warn, Level}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; static COOKIE_NAME: &str = "SESSION"; @@ -46,6 +46,8 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); + warn!("skjera starting"); + // We don't care if there is a problem here let env = dotenv::dotenv(); let is_local = env.is_ok(); @@ -63,6 +65,11 @@ async fn main() { info!("DATABASE_URL: {:?}", &options); + // match run_migrations(options.clone()).await { + // Ok(_) => info!("migrations applies"), + // Err(err) => warn!("could not apply migrations: {}", err), + // } + let pool = sqlx::postgres::PgPool::connect_lazy_with(options); let assets_path = if is_local { "backend/assets" } else { "assets" }.to_string(); let ctx = ReqwestClient::new(); @@ -91,6 +98,28 @@ async fn main() { start_server(server_impl, "0.0.0.0:8080").await } +// async fn run_migrations(pg_options: PgConnectOptions) -> Result<(), anyhow::Error> { +// let postgres_pool = PgPoolOptions::new() +// .max_connections(1) +// .after_connect(|conn, _meta| { +// Box::pin(async move { +// conn.execute("SET search_path = 'public';").await?; +// Ok(()) +// }) +// }) +// .connect_with(pg_options) +// .await +// .map_err(|e| anyhow::anyhow!(e))?; +// +// let migrator = Migrator::new(Path::new("./migrations")) +// .await +// .map_err(|e| anyhow::anyhow!(e))?; +// migrator +// .run(&postgres_pool) +// .await +// .map_err(|e| anyhow::anyhow!(e)) +// } + #[derive(Clone, Debug)] struct ServerImpl { pool: sqlx::PgPool, @@ -154,6 +183,7 @@ async fn start_server(server_impl: ServerImpl, addr: &str) { // Run the server with graceful shutdown let listener = TcpListener::bind(addr).await.unwrap(); + info!("skjera is listening on {}", addr); axum::serve(listener, app) .with_graceful_shutdown(shutdown_signal()) .await diff --git a/init.sql b/migrations/00-create.sql similarity index 56% rename from init.sql rename to migrations/00-create.sql index 5d16662..9e79836 100644 --- a/init.sql +++ b/migrations/00-create.sql @@ -1,24 +1,13 @@ -\c postgres - -SELECT PG_TERMINATE_BACKEND(pid) -FROM pg_stat_activity -WHERE datname = 'skjera'; - -DROP DATABASE IF EXISTS skjera; -DROP ROLE IF EXISTS "skjera-backend"; -DROP ROLE IF EXISTS "skjera-owner"; - ---- +-- This is a psql script, not a generic sql script CREATE DATABASE skjera; +\c skjera + CREATE ROLE "skjera-backend" WITH LOGIN ENCRYPTED PASSWORD 'skjera-backend'; --- GRANT ALL ON DATABASE skjera to "skjera-backend"; CREATE ROLE "skjera-owner" WITH LOGIN ENCRYPTED PASSWORD 'skjera-owner'; GRANT ALL ON DATABASE skjera TO "skjera-owner"; -\c skjera - GRANT ALL ON SCHEMA public TO "skjera-owner"; GRANT USAGE ON SCHEMA public TO "skjera-backend"; diff --git a/migrations/00-drop.sql b/migrations/00-drop.sql new file mode 100644 index 0000000..3425887 --- /dev/null +++ b/migrations/00-drop.sql @@ -0,0 +1,11 @@ +-- This is a psql script, not a generic sql script + +\c postgres + +SELECT PG_TERMINATE_BACKEND(pid) +FROM pg_stat_activity +WHERE datname = 'skjera'; + +DROP DATABASE IF EXISTS skjera; +DROP ROLE IF EXISTS "skjera-backend"; +DROP ROLE IF EXISTS "skjera-owner"; diff --git a/migrations/20241225094146_baseline.sql b/migrations/20241225094146_baseline.sql index 026d758..cae7891 100644 --- a/migrations/20241225094146_baseline.sql +++ b/migrations/20241225094146_baseline.sql @@ -1,3 +1,3 @@ CREATE SCHEMA skjera; -GRANT ALL ON SCHEMA skjera TO "skjera-backend"; +-- GRANT ALL ON SCHEMA skjera TO "skjera-backend"; diff --git a/migrations/20241225161533_baseline.sql b/migrations/20241225161533_baseline.sql index 8a4351d..cb7ca49 100644 --- a/migrations/20241225161533_baseline.sql +++ b/migrations/20241225161533_baseline.sql @@ -9,7 +9,7 @@ CREATE TABLE skjera.employee PRIMARY KEY (id) ); -GRANT ALL ON skjera.employee TO "skjera-backend"; +-- GRANT ALL ON skjera.employee TO "skjera-backend"; CREATE TABLE skjera.some_account ( @@ -22,7 +22,7 @@ CREATE TABLE skjera.some_account PRIMARY KEY (id) ); -GRANT ALL ON skjera.some_account TO "skjera-backend"; +-- GRANT ALL ON skjera.some_account TO "skjera-backend"; INSERT INTO skjera.employee(name) VALUES ('Trygve Laugstøl'), diff --git a/migrations/20241226063214_baseline.sql b/migrations/20241226063214_baseline.sql index a4e9804..afa15c4 100644 --- a/migrations/20241226063214_baseline.sql +++ b/migrations/20241226063214_baseline.sql @@ -10,7 +10,7 @@ CREATE TABLE skjera.employee PRIMARY KEY (id) ); -GRANT ALL ON skjera.employee TO "skjera-backend"; +-- GRANT ALL ON skjera.employee TO "skjera-backend"; CREATE TABLE skjera.some_account ( @@ -23,7 +23,7 @@ CREATE TABLE skjera.some_account PRIMARY KEY (id) ); -GRANT ALL ON skjera.some_account TO "skjera-backend"; +-- GRANT ALL ON skjera.some_account TO "skjera-backend"; INSERT INTO skjera.employee(email, name) VALUES ('trygvis@scienta.no', 'Trygve Laugstøl'),