kpcli: adds 'db' subcommand for interacting with the database
This commit is contained in:
@@ -8,6 +8,7 @@ anyhow = "1.0.94"
|
||||
chrono = "0.4.38"
|
||||
diesel = { version = "2.2.6", features = ["chrono", "sqlite", "time"] }
|
||||
diesel_migrations = { version = "2.2.0", features = ["sqlite"] }
|
||||
kordophone = { path = "../kordophone" }
|
||||
serde = { version = "1.0.215", features = ["derive"] }
|
||||
time = "0.3.37"
|
||||
uuid = { version = "1.11.0", features = ["v4"] }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use diesel::prelude::*;
|
||||
use diesel::{prelude::*, sqlite::Sqlite};
|
||||
use diesel::query_dsl::BelongingToDsl;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::{
|
||||
models::{
|
||||
@@ -20,7 +21,11 @@ pub struct ChatDatabase {
|
||||
|
||||
impl ChatDatabase {
|
||||
pub fn new_in_memory() -> Result<Self> {
|
||||
let mut db = SqliteConnection::establish(":memory:")?;
|
||||
Self::new(":memory:")
|
||||
}
|
||||
|
||||
pub fn new(db_path: &str) -> Result<Self> {
|
||||
let mut db = SqliteConnection::establish(db_path)?;
|
||||
db.run_pending_migrations(MIGRATIONS)
|
||||
.map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?;
|
||||
|
||||
|
||||
@@ -4,3 +4,5 @@ pub mod schema;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use chat_database::ChatDatabase;
|
||||
@@ -1,4 +1,4 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use chrono::{DateTime, NaiveDateTime};
|
||||
use uuid::Uuid;
|
||||
use crate::models::participant::Participant;
|
||||
|
||||
@@ -29,6 +29,29 @@ impl Conversation {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<kordophone::model::Conversation> for Conversation {
|
||||
fn from(value: kordophone::model::Conversation) -> Self {
|
||||
Self {
|
||||
guid: value.guid,
|
||||
unread_count: u16::try_from(value.unread_count).unwrap(),
|
||||
display_name: value.display_name,
|
||||
last_message_preview: value.last_message_preview,
|
||||
date: DateTime::from_timestamp(
|
||||
value.date.unix_timestamp(),
|
||||
value.date.unix_timestamp_nanos()
|
||||
.try_into()
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.unwrap()
|
||||
.naive_local(),
|
||||
participants: value.participant_display_names
|
||||
.into_iter()
|
||||
.map(|p| p.into())
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ConversationBuilder {
|
||||
guid: Option<String>,
|
||||
|
||||
Reference in New Issue
Block a user