Added center group deserialization

This commit is contained in:
Mikayla Maki 2022-11-18 14:20:52 -08:00
parent 75d3d46b1b
commit 6530658c3e
14 changed files with 264 additions and 149 deletions

View file

@ -42,11 +42,16 @@ impl Connection {
/// Attempts to open the database at uri. If it fails, a shared memory db will be opened
/// instead.
pub fn open_file(uri: &str) -> Self {
Self::open(uri, true).unwrap_or_else(|_| Self::open_memory(uri))
Self::open(uri, true).unwrap_or_else(|_| Self::open_memory(Some(uri)))
}
pub fn open_memory(uri: &str) -> Self {
let in_memory_path = format!("file:{}?mode=memory&cache=shared", uri);
pub fn open_memory(uri: Option<&str>) -> Self {
let in_memory_path = if let Some(uri) = uri {
format!("file:{}?mode=memory&cache=shared", uri)
} else {
":memory:".to_string()
};
Self::open(&in_memory_path, false).expect("Could not create fallback in memory db")
}
@ -110,7 +115,7 @@ mod test {
#[test]
fn string_round_trips() -> Result<()> {
let connection = Connection::open_memory("string_round_trips");
let connection = Connection::open_memory(Some("string_round_trips"));
connection
.exec(indoc! {"
CREATE TABLE text (
@ -136,7 +141,7 @@ mod test {
#[test]
fn tuple_round_trips() {
let connection = Connection::open_memory("tuple_round_trips");
let connection = Connection::open_memory(Some("tuple_round_trips"));
connection
.exec(indoc! {"
CREATE TABLE test (
@ -170,7 +175,7 @@ mod test {
#[test]
fn bool_round_trips() {
let connection = Connection::open_memory("bool_round_trips");
let connection = Connection::open_memory(Some("bool_round_trips"));
connection
.exec(indoc! {"
CREATE TABLE bools (
@ -196,7 +201,7 @@ mod test {
#[test]
fn backup_works() {
let connection1 = Connection::open_memory("backup_works");
let connection1 = Connection::open_memory(Some("backup_works"));
connection1
.exec(indoc! {"
CREATE TABLE blobs (
@ -211,7 +216,7 @@ mod test {
.unwrap();
// Backup connection1 to connection2
let connection2 = Connection::open_memory("backup_works_other");
let connection2 = Connection::open_memory(Some("backup_works_other"));
connection1.backup_main(&connection2).unwrap();
// Delete the added blob and verify its deleted on the other side
@ -224,7 +229,7 @@ mod test {
#[test]
fn multi_step_statement_works() {
let connection = Connection::open_memory("multi_step_statement_works");
let connection = Connection::open_memory(Some("multi_step_statement_works"));
connection
.exec(indoc! {"

View file

@ -62,7 +62,7 @@ mod test {
#[test]
fn test_migrations_are_added_to_table() {
let connection = Connection::open_memory("migrations_are_added_to_table");
let connection = Connection::open_memory(Some("migrations_are_added_to_table"));
// Create first migration with a single step and run it
connection
@ -131,7 +131,7 @@ mod test {
#[test]
fn test_migration_setup_works() {
let connection = Connection::open_memory("migration_setup_works");
let connection = Connection::open_memory(Some("migration_setup_works"));
connection
.exec(indoc! {"
@ -163,7 +163,7 @@ mod test {
#[test]
fn migrations_dont_rerun() {
let connection = Connection::open_memory("migrations_dont_rerun");
let connection = Connection::open_memory(Some("migrations_dont_rerun"));
// Create migration which clears a tabl
@ -222,7 +222,7 @@ mod test {
#[test]
fn changed_migration_fails() {
let connection = Connection::open_memory("changed_migration_fails");
let connection = Connection::open_memory(Some("changed_migration_fails"));
// Create a migration with two steps and run it
connection

View file

@ -59,7 +59,7 @@ mod tests {
#[test]
fn test_nested_savepoints() -> Result<()> {
let connection = Connection::open_memory("nested_savepoints");
let connection = Connection::open_memory(Some("nested_savepoints"));
connection
.exec(indoc! {"

View file

@ -352,7 +352,7 @@ mod test {
#[test]
fn blob_round_trips() {
let connection1 = Connection::open_memory("blob_round_trips");
let connection1 = Connection::open_memory(Some("blob_round_trips"));
connection1
.exec(indoc! {"
CREATE TABLE blobs (
@ -369,7 +369,7 @@ mod test {
assert_eq!(write.step().unwrap(), StepResult::Done);
// Read the blob from the
let connection2 = Connection::open_memory("blob_round_trips");
let connection2 = Connection::open_memory(Some("blob_round_trips"));
let mut read = Statement::prepare(&connection2, "SELECT * FROM blobs").unwrap();
assert_eq!(read.step().unwrap(), StepResult::Row);
assert_eq!(read.column_blob(0).unwrap(), blob);
@ -383,7 +383,7 @@ mod test {
#[test]
pub fn maybe_returns_options() {
let connection = Connection::open_memory("maybe_returns_options");
let connection = Connection::open_memory(Some("maybe_returns_options"));
connection
.exec(indoc! {"
CREATE TABLE texts (

View file

@ -9,7 +9,7 @@ use crate::{
};
pub struct ThreadSafeConnection<M: Migrator> {
uri: Arc<str>,
uri: Option<Arc<str>>,
persistent: bool,
initialize_query: Option<&'static str>,
connection: Arc<ThreadLocal<Connection>>,
@ -20,9 +20,13 @@ unsafe impl<T: Migrator> Send for ThreadSafeConnection<T> {}
unsafe impl<T: Migrator> Sync for ThreadSafeConnection<T> {}
impl<M: Migrator> ThreadSafeConnection<M> {
pub fn new(uri: &str, persistent: bool) -> Self {
pub fn new(uri: Option<&str>, persistent: bool) -> Self {
if persistent == true && uri == None {
// This panic is securing the unwrap in open_file(), don't remove it!
panic!("Cannot create a persistent connection without a URI")
}
Self {
uri: Arc::from(uri),
uri: uri.map(|str| Arc::from(str)),
persistent,
initialize_query: None,
connection: Default::default(),
@ -41,13 +45,14 @@ impl<M: Migrator> ThreadSafeConnection<M> {
/// called from the deref function.
/// If opening fails, the connection falls back to a shared memory connection
fn open_file(&self) -> Connection {
Connection::open_file(self.uri.as_ref())
// This unwrap is secured by a panic in the constructor. Be careful if you remove it!
Connection::open_file(self.uri.as_ref().unwrap())
}
/// Opens a shared memory connection using the file path as the identifier. This unwraps
/// as we expect it always to succeed
fn open_shared_memory(&self) -> Connection {
Connection::open_memory(self.uri.as_ref())
Connection::open_memory(self.uri.as_ref().map(|str| str.deref()))
}
// Open a new connection for the given domain, leaving this