Skip to content

Commit

Permalink
Merge pull request #2394 from fermyon/update-libsql
Browse files Browse the repository at this point in the history
Update libsql to latest version
  • Loading branch information
itowlson authored Mar 26, 2024
2 parents a400e3f + 99ef32f commit 620fe34
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 65 deletions.
71 changes: 49 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/sqlite-libsql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ async-trait = "0.1.68"
anyhow = "1.0"
# We don't actually use rusqlite itself, but we'd like the same bundled
# libsqlite3-sys as used by spin-sqlite-inproc.
rusqlite = { version = "0.29.0", features = [ "bundled" ] }
rusqlite = { version = "0.29.0", features = ["bundled"] }
spin-sqlite = { path = "../sqlite" }
spin-world = { path = "../world" }
sqlparser = "0.34"
libsql = { version = "0.2.0-alpha.1", features = ["remote"], default-features = false }
libsql = { version = "0.3.2", features = ["remote"], default-features = false }
tokio = { version = "1", features = ["full"] }
12 changes: 7 additions & 5 deletions crates/sqlite-libsql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct LibsqlClient {
}

impl LibsqlClient {
pub fn create(url: &str, token: String) -> anyhow::Result<Self> {
let db = libsql::Database::open_remote(url, token)?;
pub async fn create(url: String, token: String) -> anyhow::Result<Self> {
let db = libsql::Builder::new_remote(url, token).build().await?;
let inner = db.connect()?;
Ok(Self { inner })
}
Expand All @@ -28,7 +28,9 @@ impl spin_sqlite::Connection for LibsqlClient {

Ok(sqlite::QueryResult {
columns: columns(&result),
rows: convert_rows(result).map_err(|e| sqlite::Error::Io(e.to_string()))?,
rows: convert_rows(result)
.await
.map_err(|e| sqlite::Error::Io(e.to_string()))?,
})
}

Expand All @@ -45,12 +47,12 @@ fn columns(rows: &libsql::Rows) -> Vec<String> {
.collect()
}

fn convert_rows(mut rows: libsql::Rows) -> anyhow::Result<Vec<RowResult>> {
async fn convert_rows(mut rows: libsql::Rows) -> anyhow::Result<Vec<RowResult>> {
let mut result_rows = vec![];

let column_count = rows.column_count();

while let Some(row) = rows.next()? {
while let Some(row) = rows.next().await? {
result_rows.push(convert_row(row, column_count));
}

Expand Down
8 changes: 5 additions & 3 deletions crates/trigger/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,24 @@ impl RuntimeConfig {
}

/// Return an iterator of named configured [`SqliteDatabase`]s.
pub fn sqlite_databases(
pub async fn sqlite_databases(
&self,
) -> Result<impl IntoIterator<Item = (String, Arc<dyn Connection>)>> {
let mut databases = HashMap::new();
// Insert explicitly-configured databases
for opts in self.opts_layers() {
for (name, database) in &opts.sqlite_databases {
if !databases.contains_key(name) {
let store = database.build(opts)?;
let store = database.build(opts).await?;
databases.insert(name.to_owned(), store);
}
}
}
// Upsert default store
if !databases.contains_key("default") {
let store = SqliteDatabaseOpts::default(self).build(&RuntimeConfigOpts::default())?;
let store = SqliteDatabaseOpts::default(self)
.build(&RuntimeConfigOpts::default())
.await?;
databases.insert("default".into(), store);
}
Ok(databases.into_iter())
Expand Down
25 changes: 16 additions & 9 deletions crates/trigger/src/runtime_config/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) async fn build_component(
) -> anyhow::Result<SqliteComponent> {
let databases: HashMap<_, _> = runtime_config
.sqlite_databases()
.await
.context("Failed to build sqlite component")?
.into_iter()
.collect();
Expand Down Expand Up @@ -88,10 +89,13 @@ impl SqliteDatabaseOpts {
Self::Spin(SpinSqliteDatabaseOpts::default(runtime_config))
}

pub fn build(&self, config_opts: &RuntimeConfigOpts) -> anyhow::Result<Arc<dyn Connection>> {
pub async fn build(
&self,
config_opts: &RuntimeConfigOpts,
) -> anyhow::Result<Arc<dyn Connection>> {
match self {
Self::Spin(opts) => opts.build(config_opts),
Self::Libsql(opts) => opts.build(),
Self::Libsql(opts) => opts.build().await,
}
}
}
Expand Down Expand Up @@ -135,14 +139,17 @@ pub struct LibsqlOpts {
}

impl LibsqlOpts {
fn build(&self) -> anyhow::Result<Arc<dyn Connection>> {
let url = &check_url(&self.url).with_context(|| {
format!(
"unexpected libSQL URL '{}' in runtime config file ",
self.url
)
})?;
async fn build(&self) -> anyhow::Result<Arc<dyn Connection>> {
let url = check_url(&self.url)
.with_context(|| {
format!(
"unexpected libSQL URL '{}' in runtime config file ",
self.url
)
})?
.to_owned();
let client = spin_sqlite_libsql::LibsqlClient::create(url, self.token.clone())
.await
.context("failed to create SQLite client")?;
Ok(Arc::new(client))
}
Expand Down
Loading

0 comments on commit 620fe34

Please sign in to comment.