Compare commits

..

No commits in common. "73988d6264bd626c40f164c63f770bce0e7c24f6" and "5f06e0847fc61a0c4bb33ac23aa4a0c5230a42fe" have entirely different histories.

7 changed files with 6 additions and 94 deletions

View File

@ -1 +1 @@
DATABASE_URL=otter.sqlite3 DATABASE_URL=data/otter.sqlite3

View File

@ -1 +0,0 @@
otter.sqlite3

View File

@ -2,7 +2,7 @@
# see https://diesel.rs/guides/configuring-diesel-cli # see https://diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/schema.rs" file = "src/db/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
sqlite_integer_primary_key_is_bigint = true sqlite_integer_primary_key_is_bigint = true

View File

@ -98,10 +98,6 @@ pub fn initialize_db(path: impl AsRef<Path>, run_migrations: bool) -> Result<DbP
pub fn initialize_db_in_memory(run_migrations: bool) -> Result<DbPool, DbError> { pub fn initialize_db_in_memory(run_migrations: bool) -> Result<DbPool, DbError> {
let manager = ConnectionManager::<SqliteConnection>::new(":memory:"); let manager = ConnectionManager::<SqliteConnection>::new(":memory:");
let pool = Pool::builder() let pool = Pool::builder()
// An in-memory database is recreated for each opened connection, so we try to force only a
// single connection to stay alive
.min_idle(Some(1))
.max_size(1)
.connection_customizer(Box::new(AddQueryDebugLogs)) .connection_customizer(Box::new(AddQueryDebugLogs))
.build(manager)?; .build(manager)?;

View File

@ -1,7 +1,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use diesel::{alias, dsl::not, prelude::*, sqlite::Sqlite}; use diesel::{alias, dsl::not, prelude::*};
use gpodder::AuthErr; use gpodder::AuthErr;
use super::SqliteRepository; use super::SqliteRepository;
@ -70,8 +70,6 @@ impl gpodder::DeviceRepository for SqliteRepository {
patch: gpodder::DevicePatch, patch: gpodder::DevicePatch,
) -> Result<(), gpodder::AuthErr> { ) -> Result<(), gpodder::AuthErr> {
(|| { (|| {
let conn = &mut self.pool.get()?;
if let Some(mut device) = devices::table if let Some(mut device) = devices::table
.select(Device::as_select()) .select(Device::as_select())
.filter( .filter(
@ -79,7 +77,7 @@ impl gpodder::DeviceRepository for SqliteRepository {
.eq(user.id) .eq(user.id)
.and(devices::device_id.eq(device_id)), .and(devices::device_id.eq(device_id)),
) )
.get_result(conn) .get_result(&mut self.pool.get()?)
.optional()? .optional()?
{ {
if let Some(caption) = patch.caption { if let Some(caption) = patch.caption {
@ -95,7 +93,7 @@ impl gpodder::DeviceRepository for SqliteRepository {
devices::caption.eq(&device.caption), devices::caption.eq(&device.caption),
devices::type_.eq(&device.type_), devices::type_.eq(&device.type_),
)) ))
.execute(conn)?; .execute(&mut self.pool.get()?)?;
} else { } else {
let device = NewDevice { let device = NewDevice {
device_id: device_id.to_string(), device_id: device_id.to_string(),
@ -106,7 +104,7 @@ impl gpodder::DeviceRepository for SqliteRepository {
diesel::insert_into(devices::table) diesel::insert_into(devices::table)
.values(device) .values(device)
.execute(conn)?; .execute(&mut self.pool.get()?)?;
} }
Ok::<_, DbError>(()) Ok::<_, DbError>(())

View File

@ -102,43 +102,3 @@ fn test_refresh_session() {
Some(new_session) Some(new_session)
); );
} }
#[test]
fn test_remove_old_sessions() {
let (store, users) = common::setup();
let now = chrono::Utc::now().trunc_subsecs(0);
let timestamps = [
now,
now - TimeDelta::seconds(1),
now - TimeDelta::seconds(2),
];
for (i, ts) in timestamps.into_iter().enumerate() {
store
.insert_session(&Session {
id: i as i64,
last_seen: ts,
user: users[0].clone(),
})
.expect("insert shouldn't fail");
}
assert_eq!(
store
.remove_old_sessions(now)
.expect("remove old sessions shouldn't fail"),
2
);
assert!(matches!(store.get_session(1), Ok(None)));
assert!(matches!(store.get_session(2), Ok(None)));
assert_eq!(
store.get_session(0).expect("get session shouldn't fail"),
Some(Session {
id: 0,
last_seen: now,
user: users[0].clone(),
})
);
}

View File

@ -1,41 +0,0 @@
mod common;
use gpodder::{DevicePatch, DeviceRepository, DeviceType};
#[test]
fn test_insert_devices() {
let (store, users) = common::setup();
store.devices_for_user(&users[0]).unwrap();
store
.update_device_info(
&users[0],
"device1",
DevicePatch {
caption: Some("caption1".to_string()),
r#type: Some(DeviceType::Other),
},
)
.expect("update info shouldn't fail");
store
.update_device_info(
&users[1],
"device2",
DevicePatch {
caption: Some("caption2".to_string()),
r#type: Some(DeviceType::Laptop),
},
)
.expect("update info shouldn't fail");
let devices = store.devices_for_user(&users[0]);
assert!(matches!(devices, Ok(_)));
let devices = devices.unwrap();
assert_eq!(devices.len(), 1);
assert_eq!(devices[0].caption, "caption1");
assert_eq!(devices[0].r#type, DeviceType::Other);
}