reorg: split repo / database so settings can use db connection as well
This commit is contained in:
34
kordophone-db/src/database.rs
Normal file
34
kordophone-db/src/database.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use anyhow::Result;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::repository::Repository;
|
||||
use crate::settings::Settings;
|
||||
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
pub struct Database {
|
||||
pub connection: SqliteConnection,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn new(path: &str) -> Result<Self> {
|
||||
let mut connection = SqliteConnection::establish(path)?;
|
||||
connection.run_pending_migrations(MIGRATIONS)
|
||||
.map_err(|e| anyhow::anyhow!("Error running migrations: {}", e))?;
|
||||
|
||||
Ok(Self { connection })
|
||||
}
|
||||
|
||||
pub fn new_in_memory() -> Result<Self> {
|
||||
Self::new(":memory:")
|
||||
}
|
||||
|
||||
pub fn get_repository(&mut self) -> Repository {
|
||||
Repository::new(self)
|
||||
}
|
||||
|
||||
pub fn get_settings(&mut self) -> Settings {
|
||||
Settings::new(self)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
pub mod database;
|
||||
pub mod models;
|
||||
pub mod chat_database;
|
||||
pub mod repository;
|
||||
pub mod schema;
|
||||
pub mod settings;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use chat_database::ChatDatabase;
|
||||
pub use repository::Repository;
|
||||
@@ -2,11 +2,14 @@ use anyhow::Result;
|
||||
use diesel::prelude::*;
|
||||
use diesel::query_dsl::BelongingToDsl;
|
||||
|
||||
use crate::models::Participant;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
database::Database,
|
||||
models::{
|
||||
Conversation,
|
||||
Message,
|
||||
Participant,
|
||||
db::conversation::Record as ConversationRecord,
|
||||
db::participant::{
|
||||
ConversationParticipant,
|
||||
@@ -18,32 +21,13 @@ use crate::{
|
||||
schema,
|
||||
};
|
||||
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
pub struct ChatDatabase {
|
||||
db: SqliteConnection,
|
||||
pub struct Repository<'a> {
|
||||
db: &'a mut Database,
|
||||
}
|
||||
|
||||
impl ChatDatabase {
|
||||
pub fn new_in_memory() -> Result<Self> {
|
||||
Self::new(":memory:")
|
||||
}
|
||||
|
||||
// Helper function to get the last inserted row ID
|
||||
// This is a workaround since the Sqlite backend doesn't support `RETURNING`
|
||||
// Huge caveat with this is that it depends on whatever the last insert was, prevents concurrent inserts.
|
||||
fn last_insert_id(&mut self) -> Result<i32> {
|
||||
Ok(diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("last_insert_rowid()"))
|
||||
.get_result(&mut self.db)?)
|
||||
}
|
||||
|
||||
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))?;
|
||||
|
||||
Ok(Self { db })
|
||||
impl<'a> Repository<'a> {
|
||||
pub fn new(db: &'a mut Database) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
pub fn insert_conversation(&mut self, conversation: Conversation) -> Result<()> {
|
||||
@@ -55,25 +39,25 @@ impl ChatDatabase {
|
||||
|
||||
diesel::replace_into(conversations)
|
||||
.values(&db_conversation)
|
||||
.execute(&mut self.db)?;
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
diesel::replace_into(participants)
|
||||
.values(&db_participants)
|
||||
.execute(&mut self.db)?;
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
// 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)?;
|
||||
.first::<i32>(&mut self.db.connection)?;
|
||||
|
||||
diesel::replace_into(conversation_participants)
|
||||
.values((
|
||||
conversation_id.eq(&db_conversation.id),
|
||||
participant_id.eq(pid),
|
||||
))
|
||||
.execute(&mut self.db)?;
|
||||
.execute(&mut self.db.connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -85,14 +69,14 @@ impl ChatDatabase {
|
||||
|
||||
let result = conversations
|
||||
.find(match_guid)
|
||||
.first::<ConversationRecord>(&mut self.db)
|
||||
.first::<ConversationRecord>(&mut self.db.connection)
|
||||
.optional()?;
|
||||
|
||||
if let Some(conversation) = result {
|
||||
let db_participants = ConversationParticipant::belonging_to(&conversation)
|
||||
.inner_join(participants)
|
||||
.select(ParticipantRecord::as_select())
|
||||
.load::<ParticipantRecord>(&mut self.db)?;
|
||||
.load::<ParticipantRecord>(&mut self.db.connection)?;
|
||||
|
||||
let mut model_conversation: Conversation = conversation.into();
|
||||
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||
@@ -108,14 +92,14 @@ impl ChatDatabase {
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
let db_conversations = conversations
|
||||
.load::<ConversationRecord>(&mut self.db)?;
|
||||
.load::<ConversationRecord>(&mut self.db.connection)?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for db_conversation in db_conversations {
|
||||
let db_participants = ConversationParticipant::belonging_to(&db_conversation)
|
||||
.inner_join(participants)
|
||||
.select(ParticipantRecord::as_select())
|
||||
.load::<ParticipantRecord>(&mut self.db)?;
|
||||
.load::<ParticipantRecord>(&mut self.db.connection)?;
|
||||
|
||||
let mut model_conversation: Conversation = db_conversation.into();
|
||||
model_conversation.participants = db_participants.into_iter().map(|p| p.into()).collect();
|
||||
@@ -137,14 +121,14 @@ impl ChatDatabase {
|
||||
|
||||
diesel::replace_into(messages)
|
||||
.values(&db_message)
|
||||
.execute(&mut self.db)?;
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
diesel::replace_into(conversation_messages)
|
||||
.values((
|
||||
conversation_id.eq(conversation_guid),
|
||||
message_id.eq(&db_message.id),
|
||||
))
|
||||
.execute(&mut self.db)?;
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -159,7 +143,7 @@ impl ChatDatabase {
|
||||
.inner_join(messages)
|
||||
.select(MessageRecord::as_select())
|
||||
.order_by(schema::messages::date.asc())
|
||||
.load::<MessageRecord>(&mut self.db)?;
|
||||
.load::<MessageRecord>(&mut self.db.connection)?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for message_record in message_records {
|
||||
@@ -169,7 +153,7 @@ impl ChatDatabase {
|
||||
if let Some(pid) = message_record.sender_participant_id {
|
||||
let participant = participants
|
||||
.find(pid)
|
||||
.first::<ParticipantRecord>(&mut self.db)?;
|
||||
.first::<ParticipantRecord>(&mut self.db.connection)?;
|
||||
message.sender = participant.into();
|
||||
}
|
||||
|
||||
@@ -179,6 +163,14 @@ impl ChatDatabase {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// Helper function to get the last inserted row ID
|
||||
// This is a workaround since the Sqlite backend doesn't support `RETURNING`
|
||||
// Huge caveat with this is that it depends on whatever the last insert was, prevents concurrent inserts.
|
||||
fn last_insert_id(&mut self) -> Result<i32> {
|
||||
Ok(diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("last_insert_rowid()"))
|
||||
.get_result(&mut self.db.connection)?)
|
||||
}
|
||||
|
||||
fn get_or_create_participant(&mut self, participant: &Participant) -> Option<i32> {
|
||||
match participant {
|
||||
Participant::Me => None,
|
||||
@@ -187,7 +179,7 @@ impl ChatDatabase {
|
||||
|
||||
let existing_participant = participants
|
||||
.filter(display_name.eq(p_name))
|
||||
.first::<ParticipantRecord>(&mut self.db)
|
||||
.first::<ParticipantRecord>(&mut self.db.connection)
|
||||
.optional()
|
||||
.unwrap();
|
||||
|
||||
@@ -202,7 +194,7 @@ impl ChatDatabase {
|
||||
|
||||
diesel::insert_into(participants)
|
||||
.values(&participant_record)
|
||||
.execute(&mut self.db)
|
||||
.execute(&mut self.db.connection)
|
||||
.unwrap();
|
||||
|
||||
self.last_insert_id().ok()
|
||||
@@ -42,6 +42,13 @@ diesel::table! {
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
settings (key) {
|
||||
key -> Text,
|
||||
value -> Binary,
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
71
kordophone-db/src/settings.rs
Normal file
71
kordophone-db/src/settings.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use diesel::*;
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use anyhow::Result;
|
||||
use crate::database::Database;
|
||||
#[derive(Insertable, Queryable, AsChangeset)]
|
||||
#[diesel(table_name = crate::schema::settings)]
|
||||
struct SettingsRow<'a> {
|
||||
key: &'a str,
|
||||
value: &'a [u8],
|
||||
}
|
||||
|
||||
pub struct Settings<'a> {
|
||||
db: &'a mut Database,
|
||||
}
|
||||
|
||||
impl<'a> Settings<'a> {
|
||||
pub fn new(db: &'a mut Database) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
pub fn put<T: Serialize>(
|
||||
&mut self,
|
||||
k: &str,
|
||||
v: &T,
|
||||
) -> Result<()> {
|
||||
use crate::schema::settings::dsl::*;
|
||||
let bytes = bincode::serialize(v)?;
|
||||
|
||||
diesel::insert_into(settings)
|
||||
.values(SettingsRow { key: k, value: &bytes })
|
||||
.on_conflict(key)
|
||||
.do_update()
|
||||
.set((value.eq(&bytes)))
|
||||
.execute(&mut self.db.connection)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get<T: DeserializeOwned>(
|
||||
&mut self,
|
||||
k: &str,
|
||||
) -> Result<Option<T>> {
|
||||
use crate::schema::settings::dsl::*;
|
||||
let blob: Option<Vec<u8>> = settings
|
||||
.select(value)
|
||||
.filter(key.eq(k))
|
||||
.first(&mut self.db.connection)
|
||||
.optional()?;
|
||||
|
||||
Ok(match blob {
|
||||
Some(b) => Some(bincode::deserialize(&b)?),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn del(&mut self, k: &str) -> Result<usize> {
|
||||
use crate::schema::settings::dsl::*;
|
||||
Ok(diesel::delete(settings.filter(key.eq(k))).execute(&mut self.db.connection)?)
|
||||
}
|
||||
|
||||
pub fn list_keys(&mut self) -> Result<Vec<String>> {
|
||||
use crate::schema::settings::dsl::*;
|
||||
let keys: Vec<String> = settings
|
||||
.select(key)
|
||||
.load(&mut self.db.connection)?;
|
||||
|
||||
Ok(keys)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use crate::{
|
||||
chat_database::ChatDatabase,
|
||||
database::Database,
|
||||
repository::Repository,
|
||||
models::{
|
||||
conversation::{Conversation, ConversationBuilder},
|
||||
participant::Participant,
|
||||
message::Message,
|
||||
}
|
||||
},
|
||||
settings::Settings,
|
||||
};
|
||||
|
||||
// Helper function to compare participants ignoring database IDs
|
||||
@@ -26,12 +28,14 @@ fn participants_vec_equal_ignoring_id(a: &[Participant], b: &[Participant]) -> b
|
||||
|
||||
#[test]
|
||||
fn test_database_init() {
|
||||
let _ = ChatDatabase::new_in_memory().unwrap();
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let _ = Repository::new(&mut db);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_conversation() {
|
||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let mut repository = db.get_repository();
|
||||
|
||||
let guid = "test";
|
||||
let test_conversation = Conversation::builder()
|
||||
@@ -40,10 +44,10 @@ fn test_add_conversation() {
|
||||
.display_name("Test Conversation")
|
||||
.build();
|
||||
|
||||
db.insert_conversation(test_conversation.clone()).unwrap();
|
||||
repository.insert_conversation(test_conversation.clone()).unwrap();
|
||||
|
||||
// Try to fetch with id now
|
||||
let conversation = db.get_conversation_by_guid(guid).unwrap().unwrap();
|
||||
let conversation = repository.get_conversation_by_guid(guid).unwrap().unwrap();
|
||||
assert_eq!(conversation.guid, "test");
|
||||
|
||||
// Modify the conversation and update it
|
||||
@@ -51,20 +55,21 @@ fn test_add_conversation() {
|
||||
.display_name("Modified Conversation")
|
||||
.build();
|
||||
|
||||
db.insert_conversation(modified_conversation.clone()).unwrap();
|
||||
repository.insert_conversation(modified_conversation.clone()).unwrap();
|
||||
|
||||
// Make sure we still only have one conversation.
|
||||
let all_conversations = db.all_conversations().unwrap();
|
||||
let all_conversations = repository.all_conversations().unwrap();
|
||||
assert_eq!(all_conversations.len(), 1);
|
||||
|
||||
// And make sure the display name was updated
|
||||
let conversation = db.get_conversation_by_guid(guid).unwrap().unwrap();
|
||||
let conversation = repository.get_conversation_by_guid(guid).unwrap().unwrap();
|
||||
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_conversation_participants() {
|
||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let mut repository = db.get_repository();
|
||||
|
||||
let participants: Vec<Participant> = vec!["one".into(), "two".into()];
|
||||
|
||||
@@ -75,9 +80,9 @@ fn test_conversation_participants() {
|
||||
.participants(participants.clone())
|
||||
.build();
|
||||
|
||||
db.insert_conversation(conversation).unwrap();
|
||||
repository.insert_conversation(conversation).unwrap();
|
||||
|
||||
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
||||
let read_conversation = repository.get_conversation_by_guid(&guid).unwrap().unwrap();
|
||||
let read_participants = read_conversation.participants;
|
||||
|
||||
assert!(participants_vec_equal_ignoring_id(&participants, &read_participants));
|
||||
@@ -88,9 +93,9 @@ fn test_conversation_participants() {
|
||||
.participants(participants.clone())
|
||||
.build();
|
||||
|
||||
db.insert_conversation(conversation).unwrap();
|
||||
repository.insert_conversation(conversation).unwrap();
|
||||
|
||||
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
||||
let read_conversation = repository.get_conversation_by_guid(&guid).unwrap().unwrap();
|
||||
let read_participants: Vec<Participant> = read_conversation.participants;
|
||||
|
||||
assert!(participants_vec_equal_ignoring_id(&participants, &read_participants));
|
||||
@@ -98,7 +103,8 @@ fn test_conversation_participants() {
|
||||
|
||||
#[test]
|
||||
fn test_all_conversations_with_participants() {
|
||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let mut repository = db.get_repository();
|
||||
|
||||
// Create two conversations with different participants
|
||||
let participants1: Vec<Participant> = vec!["one".into(), "two".into()];
|
||||
@@ -119,11 +125,11 @@ fn test_all_conversations_with_participants() {
|
||||
.build();
|
||||
|
||||
// Insert both conversations
|
||||
db.insert_conversation(conversation1).unwrap();
|
||||
db.insert_conversation(conversation2).unwrap();
|
||||
repository.insert_conversation(conversation1).unwrap();
|
||||
repository.insert_conversation(conversation2).unwrap();
|
||||
|
||||
// Get all conversations and verify the results
|
||||
let all_conversations = db.all_conversations().unwrap();
|
||||
let all_conversations = repository.all_conversations().unwrap();
|
||||
assert_eq!(all_conversations.len(), 2);
|
||||
|
||||
// Find and verify each conversation's participants
|
||||
@@ -136,7 +142,8 @@ fn test_all_conversations_with_participants() {
|
||||
|
||||
#[test]
|
||||
fn test_messages() {
|
||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let mut repository = db.get_repository();
|
||||
|
||||
// First create a conversation with participants
|
||||
let participants = vec!["Alice".into(), "Bob".into()];
|
||||
@@ -146,7 +153,7 @@ fn test_messages() {
|
||||
.build();
|
||||
let conversation_id = conversation.guid.clone();
|
||||
|
||||
db.insert_conversation(conversation).unwrap();
|
||||
repository.insert_conversation(conversation).unwrap();
|
||||
|
||||
// Create and insert a message from Me
|
||||
let message1 = Message::builder()
|
||||
@@ -160,11 +167,11 @@ fn test_messages() {
|
||||
.build();
|
||||
|
||||
// Insert both messages
|
||||
db.insert_message(&conversation_id, message1.clone()).unwrap();
|
||||
db.insert_message(&conversation_id, message2.clone()).unwrap();
|
||||
repository.insert_message(&conversation_id, message1.clone()).unwrap();
|
||||
repository.insert_message(&conversation_id, message2.clone()).unwrap();
|
||||
|
||||
// Retrieve messages
|
||||
let messages = db.get_messages_for_conversation(&conversation_id).unwrap();
|
||||
let messages = repository.get_messages_for_conversation(&conversation_id).unwrap();
|
||||
assert_eq!(messages.len(), 2);
|
||||
|
||||
// Verify first message (from Me)
|
||||
@@ -184,14 +191,15 @@ fn test_messages() {
|
||||
|
||||
#[test]
|
||||
fn test_message_ordering() {
|
||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let mut repository = db.get_repository();
|
||||
|
||||
// Create a conversation
|
||||
let conversation = ConversationBuilder::new()
|
||||
.display_name("Test Chat")
|
||||
.build();
|
||||
let conversation_id = conversation.guid.clone();
|
||||
db.insert_conversation(conversation).unwrap();
|
||||
repository.insert_conversation(conversation).unwrap();
|
||||
|
||||
// Create messages with specific timestamps
|
||||
let now = chrono::Utc::now().naive_utc();
|
||||
@@ -211,16 +219,31 @@ fn test_message_ordering() {
|
||||
.build();
|
||||
|
||||
// Insert messages
|
||||
db.insert_message(&conversation_id, message1).unwrap();
|
||||
db.insert_message(&conversation_id, message2).unwrap();
|
||||
db.insert_message(&conversation_id, message3).unwrap();
|
||||
repository.insert_message(&conversation_id, message1).unwrap();
|
||||
repository.insert_message(&conversation_id, message2).unwrap();
|
||||
repository.insert_message(&conversation_id, message3).unwrap();
|
||||
|
||||
// Retrieve messages and verify order
|
||||
let messages = db.get_messages_for_conversation(&conversation_id).unwrap();
|
||||
let messages = repository.get_messages_for_conversation(&conversation_id).unwrap();
|
||||
assert_eq!(messages.len(), 3);
|
||||
|
||||
// Messages should be ordered by date
|
||||
for i in 1..messages.len() {
|
||||
assert!(messages[i].date > messages[i-1].date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_settings() {
|
||||
let mut db = Database::new_in_memory().unwrap();
|
||||
let mut settings = db.get_settings();
|
||||
|
||||
settings.put("test", &"test".to_string()).unwrap();
|
||||
assert_eq!(settings.get::<String>("test").unwrap().unwrap(), "test");
|
||||
|
||||
settings.del("test").unwrap();
|
||||
assert!(settings.get::<String>("test").unwrap().is_none());
|
||||
|
||||
let keys = settings.list_keys().unwrap();
|
||||
assert_eq!(keys.len(), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user