aboutsummaryrefslogtreecommitdiffstats
path: root/atuin-server/src/database.rs
diff options
context:
space:
mode:
authorEllie Huxtable <ellie@elliehuxtable.com>2022-06-10 10:00:59 +0100
committerGitHub <noreply@github.com>2022-06-10 10:00:59 +0100
commit23b9d34e162a9cf27b519c1fe075785be25c46e5 (patch)
tree48eab6e38d829b5f4147eb36d7e45d436d85f1b6 /atuin-server/src/database.rs
parentEnhancements to bash init script (#445) (diff)
downloadatuin-23b9d34e162a9cf27b519c1fe075785be25c46e5.zip
Add configurable history length (#447)
* Add configurable history length This allows servers to decide the max length of each history item they want to store! Some users might have much larger history lines than others. This setting can be set to 0 to allow for unlimited history length. This is not recommended for a public server install, but for a private one it can work nicely. * Format lol
Diffstat (limited to 'atuin-server/src/database.rs')
-rw-r--r--atuin-server/src/database.rs25
1 files changed, 21 insertions, 4 deletions
diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs
index d2246918..ef6c6d85 100644
--- a/atuin-server/src/database.rs
+++ b/atuin-server/src/database.rs
@@ -4,12 +4,13 @@ use async_trait::async_trait;
use chrono::{Datelike, TimeZone};
use chronoutil::RelativeDuration;
use sqlx::{postgres::PgPoolOptions, Result};
-use tracing::{debug, instrument};
+use tracing::{debug, instrument, warn};
use super::{
calendar::{TimePeriod, TimePeriodInfo},
models::{History, NewHistory, NewSession, NewUser, Session, User},
};
+use crate::settings::Settings;
use crate::settings::HISTORY_PAGE_SIZE;
use atuin_common::utils::get_days_from_month;
@@ -61,18 +62,19 @@ pub trait Database {
#[derive(Clone)]
pub struct Postgres {
pool: sqlx::Pool<sqlx::postgres::Postgres>,
+ settings: Settings,
}
impl Postgres {
- pub async fn new(uri: &str) -> Result<Self> {
+ pub async fn new(settings: Settings) -> Result<Self> {
let pool = PgPoolOptions::new()
.max_connections(100)
- .connect(uri)
+ .connect(settings.db_uri.as_str())
.await?;
sqlx::migrate!("./migrations").run(&pool).await?;
- Ok(Self { pool })
+ Ok(Self { pool, settings })
}
}
@@ -252,6 +254,21 @@ impl Database for Postgres {
let hostname: &str = &i.hostname;
let data: &str = &i.data;
+ if data.len() > self.settings.max_history_length
+ && self.settings.max_history_length != 0
+ {
+ // Don't return an error here. We want to insert as much of the
+ // history list as we can, so log the error and continue going.
+
+ warn!(
+ "history too long, got length {}, max {}",
+ data.len(),
+ self.settings.max_history_length
+ );
+
+ continue;
+ }
+
sqlx::query(
"insert into history
(client_id, user_id, hostname, timestamp, data)