Private
Public Access
1
0

kordophone-db: switch to diesel for more features

This commit is contained in:
2024-12-14 19:03:27 -08:00
parent 86601b027a
commit f79cbbbc85
12 changed files with 432 additions and 249 deletions

View File

@@ -4,8 +4,10 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.94"
chrono = "0.4.38"
microrm = "0.4.4"
diesel = { version = "2.2.6", features = ["chrono", "sqlite", "time"] }
diesel_migrations = { version = "2.2.0", features = ["sqlite"] }
serde = { version = "1.0.215", features = ["derive"] }
time = "0.3.37"
uuid = { version = "1.11.0", features = ["v4"] }

View File

@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId"]
[migrations_directory]
dir = "migrations"

View File

View File

@@ -0,0 +1,4 @@
-- This file should undo anything in `up.sql`
DROP TABLE IF EXISTS `participants`;
DROP TABLE IF EXISTS `conversation_participants`;
DROP TABLE IF EXISTS `conversations`;

View File

@@ -0,0 +1,20 @@
-- Your SQL goes here
CREATE TABLE `participants`(
`id` INTEGER NOT NULL PRIMARY KEY,
`display_name` TEXT NOT NULL
);
CREATE TABLE `conversation_participants`(
`conversation_id` TEXT NOT NULL,
`participant_id` INTEGER NOT NULL,
PRIMARY KEY(`conversation_id`, `participant_id`)
);
CREATE TABLE `conversations`(
`id` TEXT NOT NULL PRIMARY KEY,
`unread_count` BIGINT NOT NULL,
`display_name` TEXT,
`last_message_preview` TEXT,
`date` TIMESTAMP NOT NULL
);

View File

@@ -1,113 +1,100 @@
use std::error::Error;
use microrm::prelude::*;
use microrm::Stored;
use anyhow::Result;
use diesel::prelude::*;
use diesel::query_dsl::BelongingToDsl;
use crate::models::participant::ParticipantID;
use crate::models::{
participant::Participant,
use crate::{models::{
conversation::{
self, Conversation, ConversationID, PendingConversation
}
};
self, Conversation, DbConversation
}, participant::{ConversationParticipant, DbParticipant, Participant}
}, schema};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
pub struct ChatDatabase {
db: DB,
}
#[derive(Database)]
struct DB {
conversations: microrm::IDMap<Conversation>,
participants: microrm::IDMap<Participant>,
db: SqliteConnection,
}
impl ChatDatabase {
pub fn new_in_memory() -> Result<Self, Box<dyn Error + Send + Sync>> {
let db = DB::open_path(":memory:")?;
pub fn new_in_memory() -> Result<Self> {
let mut db = SqliteConnection::establish(":memory:")?;
db.run_pending_migrations(MIGRATIONS)
.map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?;
return Ok(Self {
db: db,
})
}
pub fn insert_conversation(&self, conversation: PendingConversation) -> Result<ConversationID, microrm::Error> {
// First see if conversation guid already exists, update it if so
let guid = conversation.guid();
let mut existing = self.stored_conversation_by_guid(guid)?;
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
use crate::schema::conversations::dsl::*;
use crate::schema::participants::dsl::*;
use crate::schema::conversation_participants::dsl::*;
if let Some(existing) = existing.as_mut() {
conversation.update(existing);
existing.sync();
return Ok(existing.id());
} else {
// Otherwise, insert.
let inserted = self.db.conversations.insert_and_return(conversation.get_conversation())?;
let (db_conversation, db_participants) = conversation.into();
// Insert participants
let participants = conversation.get_participants();
let inserted_participants = participants.iter()
.map(|p| self.db.participants.insert(p.clone()).unwrap())
.collect::<Vec<_>>();
inserted.connect_participants(inserted_participants);
return Ok(inserted.id());
diesel::replace_into(conversations)
.values(&db_conversation)
.execute(&mut self.db)?;
diesel::replace_into(participants)
.values(&db_participants)
.execute(&mut self.db)?;
// Sqlite backend doesn't support batch insert, so we have to do this manually
for participant in db_participants {
let pid = participants
.select(schema::participants::id)
.filter(schema::participants::display_name.eq(&participant.display_name))
.first::<i32>(&mut self.db)?;
diesel::replace_into(conversation_participants)
.values((
conversation_id.eq(&db_conversation.id),
participant_id.eq(pid),
))
.execute(&mut self.db)?;
}
Ok(())
}
pub fn get_conversation_by_id(&self, id: ConversationID) -> Result<Option<Conversation>, microrm::Error> {
self.db.conversations
.by_id(id)
.map(|stored_conversation| stored_conversation
.map(|stored| stored.wrapped())
)
pub fn get_conversation_by_guid(&mut self, match_guid: &str) -> Result<Option<Conversation>> {
use crate::schema::conversations::dsl::*;
use crate::schema::participants::dsl::*;
let result = conversations
.find(match_guid)
.first::<DbConversation>(&mut self.db)
.optional()?;
if let Some(conversation) = result {
let dbParticipants = ConversationParticipant::belonging_to(&conversation)
.inner_join(participants)
.select(DbParticipant::as_select())
.load::<DbParticipant>(&mut self.db)?;
let mut modelConversation: Conversation = conversation.into();
modelConversation.participants = dbParticipants.into_iter().map(|p| p.into()).collect();
return Ok(Some(modelConversation));
}
Ok(None)
}
pub fn get_conversation_by_guid(&self, guid: &str) -> Result<Option<Conversation>, microrm::Error> {
self.db.conversations
.with(Conversation::Guid, guid)
.get()
.and_then(|v| Ok(v
.into_iter()
.map(|c| c.wrapped())
.last()
))
}
pub fn all_conversations(&mut self) -> Result<Vec<Conversation>> {
use crate::schema::conversations::dsl::*;
pub fn all_conversations(&self) -> Result<Vec<Conversation>, microrm::Error> {
self.db.conversations
.get()
.map(|v| v
.into_iter()
.map(|c| c.wrapped())
.collect()
)
}
let result = conversations
.load::<DbConversation>(&mut self.db)?
.into_iter()
.map(|c| c.into())
.collect();
fn upsert_participants(&self, participants: Vec<Participant>) -> Vec<ParticipantID> {
// Filter existing participants and add to result
let existing_participants = participants.iter()
.filter_map(|p| self.db.participants
.with(Participant::DisplayName, &p.display_name)
.get()
.ok()
.and_then(|v| v
.into_iter()
.last()
.map(|p| p.id())
)
)
.collect::<Vec<_>>();
// TODO: Need to resolve participants here also somehow...
participants.iter()
.map(|p| self.db.participants.insert(p.clone()).unwrap())
.collect()
}
fn stored_conversation_by_guid(&self, guid: &str) -> Result<Option<Stored<Conversation>>, microrm::Error> {
self.db.conversations
.with(Conversation::Guid, guid)
.get()
.map(|v| v
.into_iter()
.last()
)
Ok(result)
}
}

View File

@@ -1,5 +1,6 @@
pub mod models;
pub mod chat_database;
pub mod schema;
#[cfg(test)]
mod tests {
@@ -18,18 +19,19 @@ mod tests {
#[test]
fn test_add_conversation() {
let db = ChatDatabase::new_in_memory().unwrap();
let mut db = ChatDatabase::new_in_memory().unwrap();
let guid = "test";
let test_conversation = Conversation::builder()
.guid("test")
.guid(guid)
.unread_count(2)
.display_name("Test Conversation")
.build();
let id = db.insert_conversation(test_conversation.clone()).unwrap();
db.insert_conversation(test_conversation.clone()).unwrap();
// Try to fetch with id now
let conversation = db.get_conversation_by_id(id).unwrap().unwrap();
let conversation = db.get_conversation_by_guid(guid).unwrap().unwrap();
assert_eq!(conversation.guid, "test");
// Modify the conversation and update it
@@ -44,38 +46,40 @@ mod tests {
assert_eq!(all_conversations.len(), 1);
// And make sure the display name was updated
let conversation = db.get_conversation_by_id(id).unwrap().unwrap();
let conversation = db.get_conversation_by_guid(guid).unwrap().unwrap();
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
}
#[test]
fn test_conversation_participants() {
let db = ChatDatabase::new_in_memory().unwrap();
let mut db = ChatDatabase::new_in_memory().unwrap();
let participants: Vec<Participant> = vec!["one".into(), "two".into()];
let guid = uuid::Uuid::new_v4().to_string();
let conversation = ConversationBuilder::new()
.guid(&guid)
.display_name("Test")
.participant_display_names(participants.clone())
.participants(participants.clone())
.build();
let id = db.insert_conversation(conversation).unwrap();
db.insert_conversation(conversation).unwrap();
let read_conversation = db.get_conversation_by_id(id).unwrap().unwrap();
let read_participants: Vec<Participant> = read_conversation.get_participant_display_names();
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
let read_participants = read_conversation.participants;
assert_eq!(participants, read_participants);
// Try making another conversation with the same participants
let conversation = ConversationBuilder::new()
.display_name("A Different Test")
.participant_display_names(participants.clone())
.participants(participants.clone())
.build();
let id = db.insert_conversation(conversation).unwrap();
db.insert_conversation(conversation).unwrap();
let read_conversation = db.get_conversation_by_id(id).unwrap().unwrap();
let read_participants: Vec<Participant> = read_conversation.get_participant_display_names();
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
let read_participants: Vec<Participant> = read_conversation.participants;
assert_eq!(participants, read_participants);
}

View File

@@ -1,26 +1,58 @@
use microrm::prelude::*;
use microrm::Stored;
use time::OffsetDateTime;
use diesel::prelude::*;
use chrono::NaiveDateTime;
use uuid::Uuid;
use crate::models::{
date::Date,
participant::Participant,
};
use super::participant::ParticipantID;
#[derive(Entity, Clone)]
pub struct Conversation {
#[unique]
pub guid: String,
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, Identifiable)]
#[diesel(table_name = crate::schema::conversations)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct DbConversation {
pub id: String,
pub unread_count: i64,
pub display_name: Option<String>,
pub last_message_preview: Option<String>,
pub date: OffsetDateTime,
participant_display_names: microrm::RelationMap<Participant>,
pub date: NaiveDateTime,
}
impl From<Conversation> for DbConversation {
fn from(conversation: Conversation) -> Self {
Self {
id: conversation.guid,
unread_count: conversation.unread_count as i64,
display_name: conversation.display_name,
last_message_preview: conversation.last_message_preview,
date: conversation.date,
}
}
}
impl From<Conversation> for (DbConversation, Vec<Participant>) {
fn from(conversation: Conversation) -> Self {
(
DbConversation {
id: conversation.guid,
unread_count: conversation.unread_count as i64,
display_name: conversation.display_name,
last_message_preview: conversation.last_message_preview,
date: conversation.date,
},
conversation.participants
)
}
}
#[derive(Clone, Debug)]
pub struct Conversation {
pub guid: String,
pub unread_count: u16,
pub display_name: Option<String>,
pub last_message_preview: Option<String>,
pub date: NaiveDateTime,
pub participants: Vec<Participant>,
}
impl Conversation {
@@ -31,76 +63,35 @@ impl Conversation {
pub fn into_builder(&self) -> ConversationBuilder {
ConversationBuilder {
guid: Some(self.guid.clone()),
date: Date::new(self.date),
participant_display_names: None,
date: self.date,
participants: None,
unread_count: Some(self.unread_count),
last_message_preview: self.last_message_preview.clone(),
display_name: self.display_name.clone(),
}
}
pub fn get_participant_display_names(&self) -> Vec<Participant> {
self.participant_display_names
.get()
.unwrap()
.into_iter()
.map(|p| p.wrapped())
.collect()
}
pub fn update(&self, stored_conversation: &mut Stored<Conversation>) {
*stored_conversation.as_mut() = self.clone();
}
pub fn connect_participants(&self, participant_ids: Vec<ParticipantID>) {
participant_ids.iter().for_each(|id| {
self.participant_display_names.connect_to(*id).unwrap();
});
}
}
#[derive(Clone)]
pub struct PendingConversation {
conversation: Conversation,
participants: Vec<Participant>,
}
impl PendingConversation {
pub fn guid(&self) -> &String {
&self.conversation.guid
}
pub fn into_builder(self) -> ConversationBuilder {
ConversationBuilder {
guid: Some(self.conversation.guid),
date: Date::new(self.conversation.date),
participant_display_names: Some(self.participants),
unread_count: Some(self.conversation.unread_count),
last_message_preview: self.conversation.last_message_preview,
display_name: self.conversation.display_name,
impl From<DbConversation> for Conversation {
fn from(db_conversation: DbConversation) -> Self {
Self {
guid: db_conversation.id,
unread_count: db_conversation.unread_count as u16,
display_name: db_conversation.display_name,
last_message_preview: db_conversation.last_message_preview,
date: db_conversation.date,
participants: vec![],
}
}
pub fn update(&self, stored_conversation: &mut microrm::Stored<Conversation>) {
self.conversation.update(stored_conversation);
}
pub fn get_participants(&self) -> &Vec<Participant> {
&self.participants
}
pub fn get_conversation(&self) -> Conversation {
self.conversation.clone()
}
}
#[derive(Default)]
pub struct ConversationBuilder {
guid: Option<String>,
date: Date,
unread_count: Option<i64>,
date: NaiveDateTime,
unread_count: Option<u16>,
last_message_preview: Option<String>,
participant_display_names: Option<Vec<Participant>>,
participants: Option<Vec<Participant>>,
display_name: Option<String>,
}
@@ -114,12 +105,12 @@ impl ConversationBuilder {
self
}
pub fn date(mut self, date: Date) -> Self {
pub fn date(mut self, date: NaiveDateTime) -> Self {
self.date = date;
self
}
pub fn unread_count(mut self, unread_count: i64) -> Self {
pub fn unread_count(mut self, unread_count: u16) -> Self {
self.unread_count = Some(unread_count);
self
}
@@ -129,8 +120,8 @@ impl ConversationBuilder {
self
}
pub fn participant_display_names(mut self, participant_display_names: Vec<Participant>) -> Self {
self.participant_display_names = Some(participant_display_names);
pub fn participants(mut self, participants: Vec<Participant>) -> Self {
self.participants = Some(participants);
self
}
@@ -139,21 +130,14 @@ impl ConversationBuilder {
self
}
fn build_conversation(&self) -> Conversation {
pub fn build(&self) -> Conversation {
Conversation {
guid: self.guid.clone().unwrap_or(Uuid::new_v4().to_string()),
unread_count: self.unread_count.unwrap_or(0),
last_message_preview: self.last_message_preview.clone(),
display_name: self.display_name.clone(),
date: self.date.dt,
participant_display_names: Default::default(),
}
}
pub fn build(self) -> PendingConversation {
PendingConversation {
conversation: self.build_conversation(),
participants: self.participant_display_names.unwrap_or_default(),
date: self.date,
participants: self.participants.clone().unwrap_or_default(),
}
}
}

View File

@@ -1,4 +1,3 @@
use chrono::{DateTime, Local, Utc};
use time::OffsetDateTime;
pub struct Date {

View File

@@ -1,11 +1,35 @@
use microrm::prelude::*;
use diesel::prelude::*;
use crate::{models::conversation::DbConversation, schema::conversation_participants};
#[derive(Entity, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Insertable)]
#[diesel(table_name = crate::schema::participants)]
pub struct Participant {
#[unique]
pub display_name: String,
}
impl From<DbParticipant> for Participant {
fn from(participant: DbParticipant) -> Self {
Participant { display_name: participant.display_name }
}
}
#[derive(Queryable, Selectable, Insertable, AsChangeset, Clone, PartialEq, Debug, Identifiable)]
#[diesel(table_name = crate::schema::participants)]
pub struct DbParticipant {
pub id: i32,
pub display_name: String
}
#[derive(Identifiable, Selectable, Queryable, Associations, Debug)]
#[diesel(belongs_to(DbConversation, foreign_key = conversation_id))]
#[diesel(belongs_to(DbParticipant, foreign_key = participant_id))]
#[diesel(table_name = conversation_participants)]
#[diesel(primary_key(conversation_id, participant_id))]
pub struct ConversationParticipant {
pub conversation_id: String,
pub participant_id: i32,
}
impl Into<Participant> for String {
fn into(self) -> Participant {
Participant { display_name: self }

View File

@@ -0,0 +1,27 @@
diesel::table! {
conversations (id) {
id -> Text,
unread_count -> BigInt,
display_name -> Nullable<Text>,
last_message_preview -> Nullable<Text>,
date -> Timestamp,
}
}
diesel::table! {
participants (id) {
id -> Integer,
display_name -> Text,
}
}
diesel::table! {
conversation_participants (conversation_id, participant_id) {
conversation_id -> Text,
participant_id -> Integer,
}
}
diesel::joinable!(conversation_participants -> conversations (conversation_id));
diesel::joinable!(conversation_participants -> participants (participant_id));
diesel::allow_tables_to_appear_in_same_query!(conversations, participants, conversation_participants);