Add basic db schema

This commit is contained in:
lemonsh 2022-04-16 12:55:32 +02:00
parent 368492e943
commit 665b9bda99
4 changed files with 41 additions and 13 deletions

View file

@ -17,6 +17,7 @@
*/
use sqlx::postgres::{PgConnectOptions, PgConnectionInfo, PgPoolOptions};
use sqlx::Executor;
use sqlx::{ConnectOptions, PgPool};
use tracing::info;
use tracing::log::LevelFilter;
@ -29,15 +30,16 @@ impl Database {
connect_options.log_statements(LevelFilter::Debug);
info!("Connecting to the database");
let pool = PgPoolOptions::new().connect_with(connect_options).await?;
let pgver = pool
.acquire()
.await?
let mut conn = pool.acquire().await?;
let pgver = conn
.server_version_num()
.map(|v| v.to_string());
info!(
"Database connected, PostgreSQL version {}",
"Database connected, PostgreSQL version '{}', migrating schema",
pgver.as_deref().unwrap_or("unknown")
);
conn.execute(include_str!("sql/schema.sql")).await?;
Ok(Self(pool))
}
@ -48,4 +50,6 @@ impl Database {
pub async fn close(&self) {
self.0.close().await;
}
}

20
src/sql/schema.sql Normal file
View file

@ -0,0 +1,20 @@
create table if not exists users(
id serial primary key,
username varchar(16) not null unique,
hash varchar(128) not null
);
create table if not exists categories(
id serial primary key,
name varchar(32) not null unique
);
create table if not exists tasks(
id serial primary key,
name varchar(128) not null,
description varchar(32768) not null,
author integer references users(id) not null,
category integer references categories(id) not null,
created timestamp not null,
deadline timestamp
)

View file

@ -24,7 +24,7 @@ use sqlx::types::chrono::Utc;
use tera::{Context, Tera};
use tokio::sync::broadcast;
use tracing::info;
use warp::Filter;
use warp::{Filter, Reply};
use warp_sessions::{CookieOptions, SameSiteCookieOption, SessionWithStore};
use std::time::SystemTime;
@ -35,10 +35,11 @@ macro_rules! warp_try {
match $expr {
Ok(o) => o,
Err(e) => {
warn!("Error in a request handler: {}", e);
return reply::with_status(
use warp::Reply;
tracing::warn!("Error in a request handler: {}", e);
return warp::reply::with_status(
format!("Error: {}", e),
StatusCode::INTERNAL_SERVER_ERROR,
warp::http::StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response();
}
@ -49,10 +50,14 @@ macro_rules! warp_try {
match $expr {
Ok(o) => o,
Err(e) => {
warn!("Error in a request handler: {}", e);
use warp::Reply;
tracing::warn!("Error in a request handler: {}", e);
return (
reply::with_status(format!("Error: {}", e), StatusCode::INTERNAL_SERVER_ERROR)
.into_response(),
warp::reply::with_status(
format!("Error: {}", e),
warp::http::StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response(),
$store,
);
}
@ -113,7 +118,7 @@ impl App {
let route = warp_session!(
warp::path::end(),
self.session_store,
move |mut s: SessionWithStore<PostgresSessionStore>| async move {
move |mut s: SessionWithStore<_>| async move {
s.session.insert_raw("test", "something".into());
(
warp::reply::html(format!("session id: {}", s.session.id())),

View file

@ -1 +0,0 @@
Hallo