Persist links to a database

This commit is contained in:
Marc Plano-Lesay 2025-04-30 21:45:08 +10:00
parent 17fb0c1856
commit b157985bf3
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
10 changed files with 451 additions and 44 deletions

123
src/db.rs Normal file
View file

@ -0,0 +1,123 @@
use crate::models::{Magnet, NewMagnet};
use crate::schema::magnets;
use crate::PostInfo;
use color_eyre::eyre::{eyre, Result, WrapErr};
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use std::fs::create_dir_all;
use std::path::Path;
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
/// Database for storing magnet links and associated information
pub struct Database {
conn: SqliteConnection,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::PostInfo;
use chrono::Utc;
use tempfile::tempdir;
#[test]
fn test_database_initialization() {
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("test.db");
let db = Database::new(&db_path);
assert!(db.is_ok());
assert!(db_path.exists());
}
#[test]
fn test_store_and_retrieve_magnet_links() {
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("test.db");
let mut db = Database::new(&db_path).unwrap();
let post_info = PostInfo {
title: "Test Title".to_string(),
submitter: "test_user".to_string(),
subreddit: "test_subreddit".to_string(),
magnet_links: vec![
"magnet:?xt=urn:btih:test1".to_string(),
"magnet:?xt=urn:btih:test2".to_string(),
],
timestamp: Utc::now(),
};
let expected_timestamp = post_info.timestamp.naive_utc();
let result = db.store_magnets(&post_info);
assert!(result.is_ok());
let magnets = db.get_all_magnets().unwrap();
assert_eq!(magnets.len(), 2);
for magnet in magnets {
assert!(
magnet.link == "magnet:?xt=urn:btih:test1"
|| magnet.link == "magnet:?xt=urn:btih:test2"
);
assert_eq!(magnet.title, "Test Title");
assert_eq!(magnet.submitter, "test_user");
assert_eq!(magnet.subreddit, "test_subreddit");
assert_eq!(magnet.published_at, expected_timestamp);
}
}
}
impl Database {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
let database_url = path
.as_ref()
.to_str()
.ok_or_else(|| eyre!("Database path is not valid UTF-8"))?;
if let Some(parent) = path.as_ref().parent() {
create_dir_all(parent)
.wrap_err_with(|| format!("Failed to create directory: {:?}", parent))?;
}
let mut conn = SqliteConnection::establish(database_url)
.wrap_err("Failed to open database connection")?;
conn.run_pending_migrations(MIGRATIONS)
.expect("Failed to apply database migrations");
Ok(Database { conn })
}
pub fn get_all_magnets(&mut self) -> Result<Vec<Magnet>> {
let results = magnets::table
.select(Magnet::as_select())
.load(&mut self.conn)
.wrap_err("Failed to load magnets from database")?;
Ok(results)
}
pub fn store_magnets(&mut self, post: &PostInfo) -> Result<usize> {
let published_at = post.timestamp.naive_utc();
let links = post
.magnet_links
.iter()
.map(|m| NewMagnet {
title: post.title.as_str(),
submitter: post.submitter.as_str(),
subreddit: post.subreddit.as_str(),
link: m,
published_at: &published_at,
})
.collect::<Vec<NewMagnet>>();
diesel::insert_into(magnets::table)
.values(&links)
.execute(&mut self.conn)
.wrap_err("Failed to save new magnet")
}
}