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

feat: opendal on postgres output type, bump opendal #1928

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion resources/opendal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["shuttle-service", "opendal"]

[dependencies]
async-trait = "0.1.56"
opendal = "0.45"
opendal = "0.50"
serde = { version = "1", features = ["derive"] }
shuttle-service = { path = "../../service", version = "0.49.0" }

Expand Down
2 changes: 1 addition & 1 deletion resources/opendal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl IntoResource<Operator> for OpendalOutput {
async fn into_resource(self) -> Result<Operator, shuttle_service::Error> {
let scheme = Scheme::from_str(&self.scheme).map_err(Error)?;

Ok(Operator::via_map(scheme, self.cfg).map_err(Error)?)
Ok(Operator::via_iter(scheme, self.cfg).map_err(Error)?)
}
}

Expand Down
4 changes: 4 additions & 0 deletions resources/shared-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keywords = ["shuttle-service", "database"]
async-trait = "0.1.56"
diesel-async = { version = "0.4.1", optional = true }
mongodb = { version = "2.3.0", optional = true }
opendal = { version = "0.50", optional = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
shuttle-service = { path = "../../service", version = "0.49.0" }
Expand All @@ -31,3 +32,6 @@ diesel-async-deadpool = ["diesel-async", "diesel-async/deadpool"]
# Postgres with an sqlx PgPool
sqlx = ["dep:sqlx", "sqlx/runtime-tokio", "sqlx/tls-rustls"]
sqlx-native-tls = ["dep:sqlx", "sqlx/runtime-tokio", "sqlx/tls-native-tls"]

# OpenDAL backed by Postgres
opendal = ["dep:opendal", "opendal/services-postgresql", "postgres", "sqlx"]
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 36 additions & 1 deletion resources/shared-db/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl IntoResource<diesel_async::AsyncPgConnection> for OutputWrapper {
use diesel_async::{AsyncConnection, AsyncPgConnection};

let connection_string: String = self.into_resource().await.unwrap();
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

Ok(AsyncPgConnection::establish(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
Expand Down Expand Up @@ -141,7 +142,7 @@ impl IntoResource<diesel_deadpool::Pool<diesel_async::AsyncPgConnection>> for Ou
#[async_trait]
impl IntoResource<sqlx::PgPool> for OutputWrapper {
async fn into_resource(self) -> Result<sqlx::PgPool, Error> {
let connection_string: String = self.into_resource().await.unwrap();
let connection_string: String = self.into_resource().await?;

Ok(sqlx::postgres::PgPoolOptions::new()
.min_connections(MIN_CONNECTIONS)
Expand All @@ -151,3 +152,37 @@ impl IntoResource<sqlx::PgPool> for OutputWrapper {
.map_err(shuttle_service::error::CustomError::new)?)
}
}

#[cfg(feature = "opendal")]
#[async_trait]
impl IntoResource<opendal::Operator> for OutputWrapper {
async fn into_resource(self) -> Result<opendal::Operator, Error> {
let connection_string: String = self.into_resource().await?;
let pool = sqlx::postgres::PgPoolOptions::new()
.min_connections(MIN_CONNECTIONS)
.max_connections(MAX_CONNECTIONS)
.connect(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS opendal (key TEXT PRIMARY KEY, value BYTEA NOT NULL)",
)
.execute(&pool)
.await
.map_err(shuttle_service::error::CustomError::new)?;

let config = opendal::services::Postgresql::default()
.root("/")
.connection_string(&connection_string)
.table("opendal")
// key field type in the table should be compatible with Rust's &str like text
.key_field("key")
// value field type in the table should be compatible with Rust's Vec<u8> like bytea
.value_field("value");
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
let op = opendal::Operator::new(config)
.map_err(shuttle_service::error::CustomError::new)?
.finish();

Ok(op)
}
}