Skip to content

Commit

Permalink
[db] Access DB via connections, not via pool (#4140)
Browse files Browse the repository at this point in the history
- Accesses the DB via connections explicitly, rather than using a pool
- This change reduces generics slightly, and simplifies error handling.
Callers can deal with "pool errors" when checking out the connection,
and can deal with "query errors" separately when issuing the queries
themselves.

Depends on oxidecomputer/async-bb8-diesel#52
Fixes #4132
  • Loading branch information
smklein authored Sep 29, 2023
1 parent b03dd6b commit 2a6ef48
Show file tree
Hide file tree
Showing 67 changed files with 1,474 additions and 1,535 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ api_identity = { path = "api_identity" }
approx = "0.5.1"
assert_matches = "1.5.0"
assert_cmd = "2.0.12"
async-bb8-diesel = { git = "https://github.com/oxidecomputer/async-bb8-diesel", rev = "be3d9bce50051d8c0e0c06078e8066cc27db3001" }
async-bb8-diesel = { git = "https://github.com/oxidecomputer/async-bb8-diesel", rev = "da04c087f835a51e0441addb19c5ef4986e1fcf2" }
async-trait = "0.1.73"
authz-macros = { path = "nexus/authz-macros" }
backoff = { version = "0.4.0", features = [ "tokio" ] }
Expand Down
26 changes: 14 additions & 12 deletions dev-tools/omdb/src/bin/omdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ async fn cmd_db_disk_list(
.filter(dsl::time_deleted.is_null())
.limit(i64::from(u32::from(limit)))
.select(Disk::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading disks")?;

Expand Down Expand Up @@ -421,11 +421,13 @@ async fn cmd_db_disk_info(

use db::schema::disk::dsl as disk_dsl;

let conn = datastore.pool_connection_for_tests().await?;

let disk = disk_dsl::disk
.filter(disk_dsl::id.eq(args.uuid))
.limit(1)
.select(Disk::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*conn)
.await
.context("loading requested disk")?;

Expand All @@ -445,7 +447,7 @@ async fn cmd_db_disk_info(
.filter(instance_dsl::id.eq(instance_uuid))
.limit(1)
.select(Instance::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*conn)
.await
.context("loading requested instance")?;

Expand Down Expand Up @@ -540,7 +542,7 @@ async fn cmd_db_disk_physical(
.filter(zpool_dsl::time_deleted.is_null())
.filter(zpool_dsl::physical_disk_id.eq(args.uuid))
.select(Zpool::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading zpool from pysical disk id")?;

Expand All @@ -560,7 +562,7 @@ async fn cmd_db_disk_physical(
.filter(dataset_dsl::time_deleted.is_null())
.filter(dataset_dsl::pool_id.eq(zp.id()))
.select(Dataset::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading dataset")?;

Expand Down Expand Up @@ -595,7 +597,7 @@ async fn cmd_db_disk_physical(
let regions = region_dsl::region
.filter(region_dsl::dataset_id.eq(did))
.select(Region::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading region")?;

Expand All @@ -614,7 +616,7 @@ async fn cmd_db_disk_physical(
.filter(dsl::volume_id.eq_any(volume_ids))
.limit(i64::from(u32::from(limit)))
.select(Disk::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading disks")?;

Expand Down Expand Up @@ -642,7 +644,7 @@ async fn cmd_db_disk_physical(
.filter(instance_dsl::id.eq(instance_uuid))
.limit(1)
.select(Instance::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading requested instance")?;

Expand Down Expand Up @@ -877,7 +879,7 @@ async fn cmd_db_instances(
let instances = dsl::instance
.limit(i64::from(u32::from(limit)))
.select(Instance::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading instances")?;

Expand Down Expand Up @@ -971,7 +973,7 @@ async fn load_zones_version(
.filter(dsl::version.eq(nexus_db_model::Generation::from(version)))
.limit(1)
.select(DnsVersion::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading requested version")?;

Expand Down Expand Up @@ -1013,7 +1015,7 @@ async fn cmd_db_dns_diff(
.filter(dsl::version_added.eq(version.version))
.limit(i64::from(u32::from(limit)))
.select(DnsName::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading added names")?;
check_limit(&added, limit, || "loading added names");
Expand All @@ -1023,7 +1025,7 @@ async fn cmd_db_dns_diff(
.filter(dsl::version_removed.eq(version.version))
.limit(i64::from(u32::from(limit)))
.select(DnsName::as_select())
.load_async(datastore.pool_for_tests().await?)
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("loading added names")?;
check_limit(&added, limit, || "loading removed names");
Expand Down
8 changes: 4 additions & 4 deletions nexus/db-macros/src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,11 +806,11 @@ fn generate_database_functions(config: &Config) -> TokenStream {
#lookup_filter
.select(nexus_db_model::#resource_name::as_select())
.get_result_async(
datastore.pool_authorized(opctx).await?
&*datastore.pool_connection_authorized(opctx).await?
)
.await
.map_err(|e| {
public_error_from_diesel_pool(
public_error_from_diesel(
e,
ErrorHandler::NotFoundByLookup(
ResourceType::#resource_name,
Expand Down Expand Up @@ -891,10 +891,10 @@ fn generate_database_functions(config: &Config) -> TokenStream {
#soft_delete_filter
#(.filter(dsl::#pkey_column_names.eq(#pkey_names.clone())))*
.select(nexus_db_model::#resource_name::as_select())
.get_result_async(datastore.pool_authorized(opctx).await?)
.get_result_async(&*datastore.pool_connection_authorized(opctx).await?)
.await
.map_err(|e| {
public_error_from_diesel_pool(
public_error_from_diesel(
e,
ErrorHandler::NotFoundByLookup(
ResourceType::#resource_name,
Expand Down
Loading

0 comments on commit 2a6ef48

Please sign in to comment.