feat: event ids, outbox identity migration
This commit is contained in:
@@ -10,6 +10,28 @@ use domain::{
|
||||
},
|
||||
};
|
||||
|
||||
/// THE outbox write path: inserts one `event_queue` row per event, each with a
|
||||
/// fresh `EventId`, inside the caller's transaction so a change and the events
|
||||
/// announcing it commit or roll back together.
|
||||
pub(crate) async fn enqueue_events(
|
||||
tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
|
||||
events: &[domain::events::DomainEvent],
|
||||
) -> Result<(), DomainError> {
|
||||
for event in events {
|
||||
let payload = event_payload::EventPayload::from(event);
|
||||
let payload_json = serde_json::to_string(&payload)
|
||||
.map_err(|e| DomainError::InfrastructureError(format!("serialize: {e}")))?;
|
||||
sqlx::query("INSERT INTO event_queue (event_type, payload, event_id) VALUES (?, ?, ?)")
|
||||
.bind(payload.event_type())
|
||||
.bind(payload_json)
|
||||
.bind(domain::events::EventId::new().value().to_string())
|
||||
.execute(&mut **tx)
|
||||
.await
|
||||
.map_err(|e| DomainError::InfrastructureError(format!("insert event: {e}")))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn map_sqlx_error(e: sqlx::Error) -> DomainError {
|
||||
tracing::error!("Database error: {:?}", e);
|
||||
DomainError::InfrastructureError("Database operation failed".into())
|
||||
|
||||
Reference in New Issue
Block a user