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", "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": { "describe": {
"columns": [ "columns": [
{ {
@ -14,7 +14,7 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "author_date", "name": "author_date: time::OffsetDateTime",
"ordinal": 2, "ordinal": 2,
"type_info": "Text" "type_info": "Text"
}, },
@ -24,7 +24,7 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "committer_date", "name": "committer_date: time::OffsetDateTime",
"ordinal": 4, "ordinal": 4,
"type_info": "Text" "type_info": "Text"
}, },
@ -37,11 +37,6 @@
"name": "reachable", "name": "reachable",
"ordinal": 6, "ordinal": 6,
"type_info": "Int64" "type_info": "Int64"
},
{
"name": "new",
"ordinal": 7,
"type_info": "Int64"
} }
], ],
"parameters": { "parameters": {
@ -54,9 +49,8 @@
false, false,
false, false,
false, false,
false,
false false
] ]
}, },
"hash": "61bcc32d29fb7b162f3a51b5b463bc917ddce4a5fc292fb19036a88f697f9056" "hash": "a42862017ade20eb742a9761c1b581d4c902dfe12e36a504cc111a9d38407196"
} }

5
Cargo.lock generated
View file

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

View file

@ -16,7 +16,7 @@ humantime-serde = "1.1.1"
mime_guess = "2.0.4" mime_guess = "2.0.4"
rust-embed = "6.8.1" rust-embed = "6.8.1"
serde = { version = "1.0.181", features = ["derive"] } 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"] } time = { version = "0.3.25", features = ["formatting", "macros", "parsing"] }
tokio = { version = "1.29.1", features = ["full"] } tokio = { version = "1.29.1", features = ["full"] }
toml = "0.7.6" toml = "0.7.6"

View file

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

View file

@ -1,12 +1,17 @@
use std::time::Duration; 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; 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 now = OffsetDateTime::now_utc();
let time = OffsetDateTime::parse(time, &Rfc3339)?;
let delta = time - now; let delta = time - now;
let formatted_time = time.format(format_description!( 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 // Take everything up to the first double newline
let title = message let title = message
.split_once("\n\n") .split_once("\n\n")
@ -34,6 +39,6 @@ pub fn summary(message: &str) -> String {
pub fn format_commit_short(hash: &str, message: &str) -> String { pub fn format_commit_short(hash: &str, message: &str) -> String {
let short_hash = hash.chars().take(8).collect::<String>(); let short_hash = hash.chars().take(8).collect::<String>();
let summary = summary(message); let summary = format_commit_summary(message);
format!("{short_hash} ({summary})") format!("{short_hash} ({summary})")
} }

View file

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

View file

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