Skip to content

Commit

Permalink
fix(shuttle-turso): use open_remote when using local_addr (#1701)
Browse files Browse the repository at this point in the history
* fix(shuttle-turso): use open_remote when using local_addr

* bump version
  • Loading branch information
jonaro00 authored Mar 25, 2024
1 parent 1b7a8a1 commit c437091
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion resources/turso/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shuttle-turso"
version = "0.42.0"
version = "0.42.1"
edition = "2021"
license = "Apache-2.0"
description = "Plugin to obtain a client connected to a Turso database"
Expand Down
25 changes: 13 additions & 12 deletions resources/turso/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Turso {
pub struct TursoOutput {
conn_url: Url,
token: Option<String>,
environment: Environment,
remote: bool,
}

impl Turso {
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Turso {
async fn output_from_addr(
&self,
addr: &str,
environment: Environment,
remote: bool,
) -> Result<TursoOutput, shuttle_service::Error> {
Ok(TursoOutput {
conn_url: Url::parse(addr).map_err(Error::UrlParseError)?,
Expand All @@ -67,7 +67,7 @@ impl Turso {
} else {
Some(self.token.clone())
},
environment,
remote,
})
}
}
Expand All @@ -89,12 +89,12 @@ impl ResourceInputBuilder for Turso {
"addr must start with either libsql:// or https://",
)));
}
self.output_from_addr(&self.addr, md.env).await
self.output_from_addr(&self.addr, true).await
}
}
Environment::Local => {
match self.local_addr {
Some(ref local_addr) => self.output_from_addr(local_addr, md.env).await,
Some(ref local_addr) => self.output_from_addr(local_addr, true).await,
None => {
// Default to a local db of the name of the service.
let db_file = std::env::current_dir() // Should be root of the project's workspace
Expand All @@ -110,7 +110,7 @@ impl ResourceInputBuilder for Turso {
conn_url: Url::parse(&conn_url).map_err(Error::UrlParseError)?,
// Nullify the token since we're using a file as database.
token: None,
environment: md.env,
remote: false,
})
}
}
Expand All @@ -122,16 +122,17 @@ impl ResourceInputBuilder for Turso {
#[async_trait]
impl IntoResource<Connection> for TursoOutput {
async fn into_resource(self) -> Result<Connection, shuttle_service::Error> {
let database = match self.environment {
Environment::Deployment => Database::open_remote(
let database = if self.remote {
Database::open_remote(
self.conn_url.to_string(),
self.token
.clone()
.ok_or(ShuttleError::Custom(CustomError::msg(
"missing token for remote database",
)))?,
),
Environment::Local => Database::open(self.conn_url.to_string()),
)
} else {
Database::open(self.conn_url.to_string())
};
database
.map_err(|err| ShuttleError::Custom(err.into()))?
Expand Down Expand Up @@ -159,7 +160,7 @@ mod test {
TursoOutput {
conn_url: Url::parse(local_addr).unwrap(),
token: None,
environment: Environment::Local,
remote: true,
}
)
}
Expand Down Expand Up @@ -196,7 +197,7 @@ mod test {
TursoOutput {
conn_url: Url::parse(&addr).unwrap(),
token: Some("token".to_string()),
environment: Environment::Deployment,
remote: true,
}
)
}
Expand Down

0 comments on commit c437091

Please sign in to comment.