Skip to content

Commit 723b40d

Browse files
refactor(db): move workflow run and approval ownership
Signed-off-by: tornquist <[email protected]>
1 parent d8ac1da commit 723b40d

3 files changed

Lines changed: 195 additions & 167 deletions

File tree

‎crates/buzz-db/src/lib.rs‎

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,169 +1308,6 @@ impl Db {
13081308
}
13091309
}
13101310

1311-
/// Create a new workflow run.
1312-
#[datastore_span(name = "create_workflow_run", system = "postgresql")]
1313-
pub async fn create_workflow_run(
1314-
&self,
1315-
community_id: CommunityId,
1316-
workflow_id: Uuid,
1317-
trigger_event_id: Option<&[u8]>,
1318-
trigger_context: Option<&serde_json::Value>,
1319-
) -> Result<Uuid> {
1320-
workflow::create_workflow_run(
1321-
&self.pool,
1322-
community_id,
1323-
workflow_id,
1324-
trigger_event_id,
1325-
trigger_context,
1326-
)
1327-
.await
1328-
}
1329-
1330-
/// Fetch a single workflow run, scoped to its community.
1331-
#[datastore_span(name = "get_workflow_run", system = "postgresql")]
1332-
pub async fn get_workflow_run(
1333-
&self,
1334-
community_id: CommunityId,
1335-
id: Uuid,
1336-
) -> Result<workflow::WorkflowRunRecord> {
1337-
workflow::get_workflow_run(&self.pool, community_id, id).await
1338-
}
1339-
1340-
/// List runs for a workflow.
1341-
#[datastore_span(name = "list_workflow_runs", system = "postgresql")]
1342-
pub async fn list_workflow_runs(
1343-
&self,
1344-
community_id: CommunityId,
1345-
workflow_id: Uuid,
1346-
limit: i64,
1347-
) -> Result<Vec<workflow::WorkflowRunRecord>> {
1348-
workflow::list_workflow_runs(&self.pool, community_id, workflow_id, limit).await
1349-
}
1350-
1351-
/// List one keyset-paginated page of workflow runs.
1352-
#[datastore_span(name = "list_workflow_runs_page", system = "postgresql")]
1353-
pub async fn list_workflow_runs_page(
1354-
&self,
1355-
community_id: CommunityId,
1356-
workflow_id: Uuid,
1357-
before: Option<chrono::DateTime<chrono::Utc>>,
1358-
before_id: Option<Uuid>,
1359-
limit: i64,
1360-
) -> Result<Vec<workflow::WorkflowRunRecord>> {
1361-
workflow::list_workflow_runs_page(
1362-
&self.pool,
1363-
community_id,
1364-
workflow_id,
1365-
before,
1366-
before_id,
1367-
limit,
1368-
)
1369-
.await
1370-
}
1371-
1372-
/// Update a workflow run's status.
1373-
#[datastore_span(name = "update_workflow_run", system = "postgresql")]
1374-
pub async fn update_workflow_run(
1375-
&self,
1376-
community_id: CommunityId,
1377-
id: Uuid,
1378-
status: workflow::RunStatus,
1379-
current_step: i32,
1380-
trace: &serde_json::Value,
1381-
failure: Option<workflow::WorkflowRunFailure<'_>>,
1382-
) -> Result<()> {
1383-
workflow::update_workflow_run(
1384-
&self.pool,
1385-
community_id,
1386-
id,
1387-
status,
1388-
current_step,
1389-
trace,
1390-
failure,
1391-
)
1392-
.await
1393-
}
1394-
1395-
/// Create an approval request.
1396-
#[datastore_span(name = "create_approval", system = "postgresql")]
1397-
pub async fn create_approval(&self, params: workflow::CreateApprovalParams<'_>) -> Result<()> {
1398-
workflow::create_approval(&self.pool, params).await
1399-
}
1400-
1401-
/// Fetch an approval by raw token.
1402-
#[datastore_span(name = "get_approval", system = "postgresql")]
1403-
pub async fn get_approval(
1404-
&self,
1405-
community_id: CommunityId,
1406-
token: &str,
1407-
) -> Result<workflow::ApprovalRecord> {
1408-
workflow::get_approval(&self.pool, community_id, token).await
1409-
}
1410-
1411-
/// Fetch an approval by its already-hashed token (no re-hashing).
1412-
#[datastore_span(name = "get_approval_by_stored_hash", system = "postgresql")]
1413-
pub async fn get_approval_by_stored_hash(
1414-
&self,
1415-
community_id: CommunityId,
1416-
token_hash: &[u8],
1417-
) -> Result<workflow::ApprovalRecord> {
1418-
workflow::get_approval_by_stored_hash(&self.pool, community_id, token_hash).await
1419-
}
1420-
1421-
/// Fetch all approvals for a workflow run.
1422-
#[datastore_span(name = "get_run_approvals", system = "postgresql")]
1423-
pub async fn get_run_approvals(
1424-
&self,
1425-
community_id: CommunityId,
1426-
workflow_id: uuid::Uuid,
1427-
run_id: uuid::Uuid,
1428-
) -> Result<Vec<workflow::ApprovalRecord>> {
1429-
workflow::get_run_approvals(&self.pool, community_id, workflow_id, run_id).await
1430-
}
1431-
1432-
/// Update an approval's status.
1433-
#[datastore_span(name = "update_approval", system = "postgresql")]
1434-
pub async fn update_approval(
1435-
&self,
1436-
community_id: CommunityId,
1437-
token: &str,
1438-
status: workflow::ApprovalStatus,
1439-
approver_pubkey: Option<&[u8]>,
1440-
note: Option<&str>,
1441-
) -> Result<bool> {
1442-
workflow::update_approval(
1443-
&self.pool,
1444-
community_id,
1445-
token,
1446-
status,
1447-
approver_pubkey,
1448-
note,
1449-
)
1450-
.await
1451-
}
1452-
1453-
/// Update an approval by its already-hashed token (no re-hashing).
1454-
#[datastore_span(name = "update_approval_by_stored_hash", system = "postgresql")]
1455-
pub async fn update_approval_by_stored_hash(
1456-
&self,
1457-
community_id: CommunityId,
1458-
token_hash: &[u8],
1459-
status: workflow::ApprovalStatus,
1460-
approver_pubkey: Option<&[u8]>,
1461-
note: Option<&str>,
1462-
) -> Result<bool> {
1463-
workflow::update_approval_by_stored_hash(
1464-
&self.pool,
1465-
community_id,
1466-
token_hash,
1467-
status,
1468-
approver_pubkey,
1469-
note,
1470-
)
1471-
.await
1472-
}
1473-
14741311
/// Ensures monthly partitions exist for the next N months.
14751312
#[datastore_span(name = "ensure_future_partitions", system = "postgresql")]
14761313
pub async fn ensure_future_partitions(&self, months_ahead: u32) -> Result<()> {

‎crates/buzz-db/src/workflow.rs‎

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,177 @@ pub async fn find_by_owner_and_name(
12681268
}
12691269
}
12701270

1271-
// -- Db API -------------------------------------------------------------------
1271+
// -- Run and approval Db API --------------------------------------------------
1272+
1273+
impl Db {
1274+
/// Create a new workflow run.
1275+
#[datastore_span(name = "create_workflow_run", system = "postgresql")]
1276+
pub async fn create_workflow_run(
1277+
&self,
1278+
community_id: CommunityId,
1279+
workflow_id: Uuid,
1280+
trigger_event_id: Option<&[u8]>,
1281+
trigger_context: Option<&serde_json::Value>,
1282+
) -> Result<Uuid> {
1283+
crate::workflow::create_workflow_run(
1284+
&self.pool,
1285+
community_id,
1286+
workflow_id,
1287+
trigger_event_id,
1288+
trigger_context,
1289+
)
1290+
.await
1291+
}
1292+
1293+
/// Fetch a single workflow run, scoped to its community.
1294+
#[datastore_span(name = "get_workflow_run", system = "postgresql")]
1295+
pub async fn get_workflow_run(
1296+
&self,
1297+
community_id: CommunityId,
1298+
id: Uuid,
1299+
) -> Result<crate::workflow::WorkflowRunRecord> {
1300+
crate::workflow::get_workflow_run(&self.pool, community_id, id).await
1301+
}
1302+
1303+
/// List runs for a workflow.
1304+
#[datastore_span(name = "list_workflow_runs", system = "postgresql")]
1305+
pub async fn list_workflow_runs(
1306+
&self,
1307+
community_id: CommunityId,
1308+
workflow_id: Uuid,
1309+
limit: i64,
1310+
) -> Result<Vec<crate::workflow::WorkflowRunRecord>> {
1311+
crate::workflow::list_workflow_runs(&self.pool, community_id, workflow_id, limit).await
1312+
}
1313+
1314+
/// List one keyset-paginated page of workflow runs.
1315+
#[datastore_span(name = "list_workflow_runs_page", system = "postgresql")]
1316+
pub async fn list_workflow_runs_page(
1317+
&self,
1318+
community_id: CommunityId,
1319+
workflow_id: Uuid,
1320+
before: Option<chrono::DateTime<chrono::Utc>>,
1321+
before_id: Option<Uuid>,
1322+
limit: i64,
1323+
) -> Result<Vec<crate::workflow::WorkflowRunRecord>> {
1324+
crate::workflow::list_workflow_runs_page(
1325+
&self.pool,
1326+
community_id,
1327+
workflow_id,
1328+
before,
1329+
before_id,
1330+
limit,
1331+
)
1332+
.await
1333+
}
1334+
1335+
/// Update a workflow run's status.
1336+
#[datastore_span(name = "update_workflow_run", system = "postgresql")]
1337+
pub async fn update_workflow_run(
1338+
&self,
1339+
community_id: CommunityId,
1340+
id: Uuid,
1341+
status: crate::workflow::RunStatus,
1342+
current_step: i32,
1343+
trace: &serde_json::Value,
1344+
failure: Option<crate::workflow::WorkflowRunFailure<'_>>,
1345+
) -> Result<()> {
1346+
crate::workflow::update_workflow_run(
1347+
&self.pool,
1348+
community_id,
1349+
id,
1350+
status,
1351+
current_step,
1352+
trace,
1353+
failure,
1354+
)
1355+
.await
1356+
}
1357+
1358+
/// Create an approval request.
1359+
#[datastore_span(name = "create_approval", system = "postgresql")]
1360+
pub async fn create_approval(
1361+
&self,
1362+
params: crate::workflow::CreateApprovalParams<'_>,
1363+
) -> Result<()> {
1364+
crate::workflow::create_approval(&self.pool, params).await
1365+
}
1366+
1367+
/// Fetch an approval by raw token.
1368+
#[datastore_span(name = "get_approval", system = "postgresql")]
1369+
pub async fn get_approval(
1370+
&self,
1371+
community_id: CommunityId,
1372+
token: &str,
1373+
) -> Result<crate::workflow::ApprovalRecord> {
1374+
crate::workflow::get_approval(&self.pool, community_id, token).await
1375+
}
1376+
1377+
/// Fetch an approval by its already-hashed token (no re-hashing).
1378+
#[datastore_span(name = "get_approval_by_stored_hash", system = "postgresql")]
1379+
pub async fn get_approval_by_stored_hash(
1380+
&self,
1381+
community_id: CommunityId,
1382+
token_hash: &[u8],
1383+
) -> Result<crate::workflow::ApprovalRecord> {
1384+
crate::workflow::get_approval_by_stored_hash(&self.pool, community_id, token_hash).await
1385+
}
1386+
1387+
/// Fetch all approvals for a workflow run.
1388+
#[datastore_span(name = "get_run_approvals", system = "postgresql")]
1389+
pub async fn get_run_approvals(
1390+
&self,
1391+
community_id: CommunityId,
1392+
workflow_id: uuid::Uuid,
1393+
run_id: uuid::Uuid,
1394+
) -> Result<Vec<crate::workflow::ApprovalRecord>> {
1395+
crate::workflow::get_run_approvals(&self.pool, community_id, workflow_id, run_id).await
1396+
}
1397+
1398+
/// Update an approval's status.
1399+
#[datastore_span(name = "update_approval", system = "postgresql")]
1400+
pub async fn update_approval(
1401+
&self,
1402+
community_id: CommunityId,
1403+
token: &str,
1404+
status: crate::workflow::ApprovalStatus,
1405+
approver_pubkey: Option<&[u8]>,
1406+
note: Option<&str>,
1407+
) -> Result<bool> {
1408+
crate::workflow::update_approval(
1409+
&self.pool,
1410+
community_id,
1411+
token,
1412+
status,
1413+
approver_pubkey,
1414+
note,
1415+
)
1416+
.await
1417+
}
1418+
1419+
/// Update an approval by its already-hashed token (no re-hashing).
1420+
#[datastore_span(name = "update_approval_by_stored_hash", system = "postgresql")]
1421+
pub async fn update_approval_by_stored_hash(
1422+
&self,
1423+
community_id: CommunityId,
1424+
token_hash: &[u8],
1425+
status: crate::workflow::ApprovalStatus,
1426+
approver_pubkey: Option<&[u8]>,
1427+
note: Option<&str>,
1428+
) -> Result<bool> {
1429+
crate::workflow::update_approval_by_stored_hash(
1430+
&self.pool,
1431+
community_id,
1432+
token_hash,
1433+
status,
1434+
approver_pubkey,
1435+
note,
1436+
)
1437+
.await
1438+
}
1439+
}
1440+
1441+
// -- Workflow lifecycle Db API ------------------------------------------------
12721442

12731443
impl Db {
12741444
/// Create a new workflow.

‎crates/buzz-db/tests/store_ownership.rs‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ fn workflow_lifecycle_store_has_single_ownership() {
805805
assert_eq!(count(root, "enum WorkflowStatus"), 0);
806806
assert_eq!(count(store, "enum WorkflowStatus"), 1);
807807

808-
for deferred in [
808+
for method in [
809809
"create_workflow_run",
810810
"get_workflow_run",
811811
"list_workflow_runs",
@@ -818,13 +818,34 @@ fn workflow_lifecycle_store_has_single_ownership() {
818818
"update_approval",
819819
"update_approval_by_stored_hash",
820820
] {
821+
let signature = format!("pub async fn {method}(");
822+
assert_eq!(count(root, &signature), 0, "{method} remains in lib.rs");
821823
assert_eq!(
822-
count(root, &format!("pub async fn {deferred}(")),
824+
count(store, &signature),
825+
2,
826+
"{method} must have one Db wrapper and one workflow SQL function"
827+
);
828+
assert_eq!(
829+
count(store, &format!("name = \"{method}\"")),
823830
1,
824-
"run/approval wrapper moved into the lifecycle slice"
831+
"{method} span is not unique"
825832
);
826833
}
827834

835+
for ty in [
836+
"WorkflowRunRecord",
837+
"WorkflowRunFailure",
838+
"ApprovalRecord",
839+
"CreateApprovalParams",
840+
] {
841+
assert_eq!(count(root, &format!("struct {ty}")), 0);
842+
assert_eq!(count(store, &format!("struct {ty}")), 1);
843+
}
844+
for ty in ["RunStatus", "ApprovalStatus"] {
845+
assert_eq!(count(root, &format!("enum {ty}")), 0);
846+
assert_eq!(count(store, &format!("enum {ty}")), 1);
847+
}
848+
828849
assert_eq!(count(root, "pub async fn usage_workflow_counts("), 1);
829850
assert_eq!(count(store, "pub async fn usage_workflow_counts("), 0);
830851
}

0 commit comments

Comments
 (0)