Rename db to util and use OffsetDateTime with sqlx

This commit is contained in:
Joscha 2023-08-06 17:38:49 +02:00
parent 6fcd073738
commit 553a56bb12
7 changed files with 46 additions and 28 deletions

View file

@ -1,6 +1,6 @@
{
"db_name": "SQLite",
"query": "SELECT * FROM commits WHERE hash = ?",
"query": "SELECT hash, author, author_date AS \"author_date: time::OffsetDateTime\", committer, committer_date AS \"committer_date: time::OffsetDateTime\", message, reachable FROM commits WHERE hash = ? ",
"describe": {
"columns": [
{
@ -14,7 +14,7 @@
"type_info": "Text"
},
{
"name": "author_date",
"name": "author_date: time::OffsetDateTime",
"ordinal": 2,
"type_info": "Text"
},
@ -24,7 +24,7 @@
"type_info": "Text"
},
{
"name": "committer_date",
"name": "committer_date: time::OffsetDateTime",
"ordinal": 4,
"type_info": "Text"
},
@ -37,11 +37,6 @@
"name": "reachable",
"ordinal": 6,
"type_info": "Int64"
},
{
"name": "new",
"ordinal": 7,
"type_info": "Int64"
}
],
"parameters": {
@ -54,9 +49,8 @@
false,
false,
false,
false,
false
]
},
"hash": "61bcc32d29fb7b162f3a51b5b463bc917ddce4a5fc292fb19036a88f697f9056"
"hash": "a42862017ade20eb742a9761c1b581d4c902dfe12e36a504cc111a9d38407196"
}

5
Cargo.lock generated
View file

@ -2546,6 +2546,7 @@ dependencies = [
"smallvec",
"sqlformat",
"thiserror",
"time",
"tokio",
"tokio-stream",
"tracing",
@ -2583,6 +2584,7 @@ dependencies = [
"sha2",
"sqlx-core",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
"syn 1.0.109",
"tempfile",
@ -2628,6 +2630,7 @@ dependencies = [
"sqlx-core",
"stringprep",
"thiserror",
"time",
"tracing",
"whoami",
]
@ -2667,6 +2670,7 @@ dependencies = [
"sqlx-core",
"stringprep",
"thiserror",
"time",
"tracing",
"whoami",
]
@ -2689,6 +2693,7 @@ dependencies = [
"percent-encoding",
"serde",
"sqlx-core",
"time",
"tracing",
"url",
]

View file

@ -16,7 +16,7 @@ humantime-serde = "1.1.1"
mime_guess = "2.0.4"
rust-embed = "6.8.1"
serde = { version = "1.0.181", features = ["derive"] }
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] }
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite", "time"] }
time = { version = "0.3.25", features = ["formatting", "macros", "parsing"] }
tokio = { version = "1.29.1", features = ["full"] }
toml = "0.7.6"

View file

@ -1,8 +1,8 @@
mod config;
mod db;
mod recurring;
mod somehow;
mod state;
mod util;
mod web;
use std::{io, path::PathBuf, process};

View file

@ -1,12 +1,17 @@
use std::time::Duration;
use time::{format_description::well_known::Rfc3339, macros::format_description, OffsetDateTime};
use gix::date::Time;
use time::{macros::format_description, OffsetDateTime, UtcOffset};
use crate::somehow;
pub fn format_time(time: &str) -> somehow::Result<String> {
pub fn time_to_offset_datetime(time: Time) -> somehow::Result<OffsetDateTime> {
Ok(OffsetDateTime::from_unix_timestamp(time.seconds)?
.to_offset(UtcOffset::from_whole_seconds(time.offset)?))
}
pub fn format_time(time: OffsetDateTime) -> somehow::Result<String> {
let now = OffsetDateTime::now_utc();
let time = OffsetDateTime::parse(time, &Rfc3339)?;
let delta = time - now;
let formatted_time = time.format(format_description!(
@ -21,7 +26,7 @@ pub fn format_time(time: &str) -> somehow::Result<String> {
})
}
pub fn summary(message: &str) -> String {
pub fn format_commit_summary(message: &str) -> String {
// Take everything up to the first double newline
let title = message
.split_once("\n\n")
@ -34,6 +39,6 @@ pub fn summary(message: &str) -> String {
pub fn format_commit_short(hash: &str, message: &str) -> String {
let short_hash = hash.chars().take(8).collect::<String>();
let summary = summary(message);
let summary = format_commit_summary(message);
format!("{short_hash} ({summary})")
}

View file

@ -7,7 +7,7 @@ use axum::{
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, db, somehow};
use crate::{config::Config, somehow, util};
struct Commit {
hash: String,
@ -18,7 +18,7 @@ struct Commit {
impl Commit {
fn new(hash: String, message: &str, reachable: i64) -> Self {
Self {
short: db::format_commit_short(&hash, message),
short: util::format_commit_short(&hash, message),
hash,
reachable,
}
@ -48,9 +48,23 @@ pub async fn get(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<Response> {
let Some(commit) = sqlx::query!("SELECT * FROM commits WHERE hash = ?", hash)
.fetch_optional(&db)
.await?
let Some(commit) = sqlx::query!(
"\
SELECT \
hash, \
author, \
author_date AS \"author_date: time::OffsetDateTime\", \
committer, \
committer_date AS \"committer_date: time::OffsetDateTime\", \
message, \
reachable \
FROM commits \
WHERE hash = ? \
",
hash
)
.fetch_optional(&db)
.await?
else {
return Ok(StatusCode::NOT_FOUND.into_response());
};
@ -89,12 +103,12 @@ pub async fn get(
current: "commit".to_string(),
hash: commit.hash,
author: commit.author,
author_date: db::format_time(&commit.author_date)?,
author_date: util::format_time(commit.author_date)?,
commit: commit.committer,
commit_date: db::format_time(&commit.committer_date)?,
commit_date: util::format_time(commit.committer_date)?,
parents,
children,
summary: db::summary(&commit.message),
summary: util::format_commit_summary(&commit.message),
message: commit.message.trim_end().to_string(),
reachable: commit.reachable,
}

View file

@ -3,7 +3,7 @@ use axum::{extract::State, response::IntoResponse};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, db, somehow};
use crate::{config::Config, somehow, util};
struct Ref {
name: String,
@ -36,7 +36,7 @@ pub async fn get(
.fetch(&db)
.map_ok(|r| Ref {
name: r.name,
short: db::format_commit_short(&r.hash, &r.message),
short: util::format_commit_short(&r.hash, &r.message),
hash: r.hash,
tracked: r.tracked != 0,
})