Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sqlite memory connection issues #2104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/atuin-client/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,21 @@ impl Sqlite {
.with_regexp()
.create_if_missing(true);

let pool = SqlitePoolOptions::new()
.acquire_timeout(Duration::from_secs_f64(timeout))
.connect_with(opts)
.await?;
let mut pool_opts = SqlitePoolOptions::new()
.acquire_timeout(Duration::from_secs_f64(timeout));

// Workaround for sqlx-sqlite in-memory failure.
// Make sure a single connection is created to avoid overwriting the database.
// Safe to use in this case since memory databases are only used for testing.
// https://github.com/launchbadge/sqlx/issues/2510
if path.to_str().unwrap().ends_with(":memory:"){
LecrisUT marked this conversation as resolved.
Show resolved Hide resolved
pool_opts = pool_opts
.max_connections(1)
.idle_timeout(None)
.max_lifetime(None);
}

let pool = pool_opts.connect_with(opts).await?;

Self::setup_db(&pool).await?;

Expand Down
43 changes: 28 additions & 15 deletions crates/atuin-client/src/record/sqlite_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,21 @@ impl SqliteStore {
.foreign_keys(true)
.create_if_missing(true);

let pool = SqlitePoolOptions::new()
.acquire_timeout(Duration::from_secs_f64(timeout))
let mut pool_opts = SqlitePoolOptions::new()
.acquire_timeout(Duration::from_secs_f64(timeout));

// Workaround for sqlx-sqlite in-memory failure.
// Make sure a single connection is created to avoid overwriting the database.
// Safe to use in this case since memory databases are only used for testing.
// https://github.com/launchbadge/sqlx/issues/2510
if path.to_str().unwrap().ends_with(":memory:"){
pool_opts = pool_opts
.max_connections(1)
.idle_timeout(None)
.max_lifetime(None);
}

let pool = pool_opts
.connect_with(opts)
.await?;

Expand All @@ -72,16 +85,16 @@ impl SqliteStore {
"insert or ignore into store(id, idx, host, tag, timestamp, version, data, cek)
values(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
)
.bind(r.id.0.as_hyphenated().to_string())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting change here too

.bind(r.idx as i64)
.bind(r.host.id.0.as_hyphenated().to_string())
.bind(r.tag.as_str())
.bind(r.timestamp as i64)
.bind(r.version.as_str())
.bind(r.data.data.as_str())
.bind(r.data.content_encryption_key.as_str())
.execute(&mut **tx)
.await?;
.bind(r.id.0.as_hyphenated().to_string())
.bind(r.idx as i64)
.bind(r.host.id.0.as_hyphenated().to_string())
.bind(r.tag.as_str())
.bind(r.timestamp as i64)
.bind(r.version.as_str())
.bind(r.data.data.as_str())
.bind(r.data.content_encryption_key.as_str())
.execute(&mut **tx)
.await?;

Ok(())
}
Expand Down Expand Up @@ -122,7 +135,7 @@ impl SqliteStore {
impl Store for SqliteStore {
async fn push_batch(
&self,
records: impl Iterator<Item = &Record<EncryptedData>> + Send + Sync,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the formatting change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my IDE being overzealous, I will fix them shortly

records: impl Iterator<Item=&Record<EncryptedData>> + Send + Sync,
) -> Result<()> {
let mut tx = self.pool.begin().await?;

Expand Down Expand Up @@ -181,7 +194,7 @@ impl Store for SqliteStore {
}

async fn len_all(&self) -> Result<u64> {
let res: Result<(i64,), sqlx::Error> = sqlx::query_as("select count(*) from store")
let res: Result<(i64, ), sqlx::Error> = sqlx::query_as("select count(*) from store")
.fetch_one(&self.pool)
.await;
match res {
Expand All @@ -191,7 +204,7 @@ impl Store for SqliteStore {
}

async fn len_tag(&self, tag: &str) -> Result<u64> {
let res: Result<(i64,), sqlx::Error> =
let res: Result<(i64, ), sqlx::Error> =
sqlx::query_as("select count(*) from store where tag=?1")
.bind(tag)
.fetch_one(&self.pool)
Expand Down
Loading