Skip to content

Commit

Permalink
Merge pull request #16 from superfly/tweak-conn-init
Browse files Browse the repository at this point in the history
Fix CrConn's Drop
  • Loading branch information
jeromegn authored Aug 9, 2023
2 parents e3ac00b + e5cda76 commit d69104a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
9 changes: 3 additions & 6 deletions crates/corro-agent/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use corro_types::{
config::{Config, DEFAULT_GOSSIP_PORT},
members::{MemberEvent, Members},
schema::init_schema,
sqlite::{init_cr_conn, CrConn, Migration, SqlitePoolError},
sqlite::{CrConn, Migration, SqlitePoolError},
sync::{generate_sync, SyncMessageDecodeError, SyncMessageEncodeError},
};

Expand Down Expand Up @@ -91,8 +91,7 @@ pub async fn setup(conf: Config, tripwire: Tripwire) -> eyre::Result<(Agent, Age
}

let actor_id = {
let mut conn = CrConn(Connection::open(&conf.db_path)?);
init_cr_conn(&mut conn)?;
let conn = CrConn::init(Connection::open(&conf.db_path)?)?;
conn.query_row("SELECT crsql_site_id();", [], |row| {
row.get::<_, ActorId>(0)
})?
Expand Down Expand Up @@ -2320,9 +2319,7 @@ pub mod tests {

#[test]
fn test_in_memory_versions_compaction() -> eyre::Result<()> {
let mut conn = rusqlite::Connection::open_in_memory()?;

init_cr_conn(&mut conn)?;
let conn = CrConn::init(rusqlite::Connection::open_in_memory()?)?;

conn.execute_batch(
"CREATE TABLE foo (a INTEGER PRIMARY KEY, b INTEGER); SELECT crsql_as_crr('foo');",
Expand Down
21 changes: 10 additions & 11 deletions crates/corro-types/src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ mod tests {
use crate::{
change::{row_to_change, SqliteValue},
schema::{make_schema_inner, parse_sql},
sqlite::{init_cr_conn, setup_conn},
sqlite::{setup_conn, CrConn},
};

use super::*;
Expand All @@ -1185,9 +1185,7 @@ mod tests {
let tmpdir = tempfile::tempdir()?;
let db_path = tmpdir.path().join("test.db");

let mut conn = rusqlite::Connection::open(&db_path).expect("could not open conn");

init_cr_conn(&mut conn)?;
let mut conn = CrConn::init(rusqlite::Connection::open(&db_path)?)?;

setup_conn(
&mut conn,
Expand Down Expand Up @@ -1314,9 +1312,9 @@ mod tests {
let tmpdir = tempfile::tempdir().unwrap();
let db_path = tmpdir.path().join("test.db");

let mut conn = rusqlite::Connection::open(&db_path).expect("could not open conn");

init_cr_conn(&mut conn).unwrap();
let mut conn =
CrConn::init(rusqlite::Connection::open(&db_path).expect("could not open conn"))
.expect("could not init crsql");

setup_conn(
&mut conn,
Expand Down Expand Up @@ -1363,10 +1361,11 @@ mod tests {
}

{
let mut conn2 = rusqlite::Connection::open(tmpdir.path().join("test2.db"))
.expect("could not open conn");

init_cr_conn(&mut conn2).unwrap();
let mut conn2 = CrConn::init(
rusqlite::Connection::open(tmpdir.path().join("test2.db"))
.expect("could not open conn"),
)
.expect("could not init crsql");

setup_conn(
&mut conn2,
Expand Down
9 changes: 7 additions & 2 deletions crates/corro-types/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,14 @@ impl ManageConnection for CrConnManager {
}

#[derive(Debug)]
pub struct CrConn(pub Connection);
pub struct CrConn(Connection);

impl CrConn {
pub fn init(mut conn: Connection) -> Result<Self, rusqlite::Error> {
init_cr_conn(&mut conn)?;
Ok(Self(conn))
}

pub fn transaction(&mut self) -> rusqlite::Result<Transaction> {
self.0
.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)
Expand Down Expand Up @@ -168,7 +173,7 @@ impl Drop for CrConn {
}
}

pub fn init_cr_conn(conn: &mut Connection) -> Result<(), rusqlite::Error> {
fn init_cr_conn(conn: &mut Connection) -> Result<(), rusqlite::Error> {
let ext_dir = &CRSQL_EXT_DIR;
trace!(
"loading crsqlite extension from path: {}",
Expand Down

0 comments on commit d69104a

Please sign in to comment.