Skip to content

Commit

Permalink
Less smart database setup, single user for the app
Browse files Browse the repository at this point in the history
Makes this run on Scaleway where you only get a single database user.
Can probably be improved later.
  • Loading branch information
trygvis committed Dec 31, 2024
1 parent 93e971a commit 855261b
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.tmp.*
tmp
*.tmp
*.env
4 changes: 0 additions & 4 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .settings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
32 changes: 31 additions & 1 deletion backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 3 additions & 14 deletions init.sql → migrations/00-create.sql
Original file line number Diff line number Diff line change
@@ -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";
11 changes: 11 additions & 0 deletions migrations/00-drop.sql
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion migrations/20241225094146_baseline.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CREATE SCHEMA skjera;

GRANT ALL ON SCHEMA skjera TO "skjera-backend";
-- GRANT ALL ON SCHEMA skjera TO "skjera-backend";
4 changes: 2 additions & 2 deletions migrations/20241225161533_baseline.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand All @@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions migrations/20241226063214_baseline.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand All @@ -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 ('[email protected]', 'Trygve Laugstøl'),
Expand Down

0 comments on commit 855261b

Please sign in to comment.