feat: event ids, outbox identity migration
This commit is contained in:
@@ -154,6 +154,16 @@ pub enum EventPayload {
|
||||
},
|
||||
}
|
||||
|
||||
/// Wire form of an event: the tagged payload plus its optional identity.
|
||||
/// Messages without `event_id` (queued before identities existed) still decode.
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
pub struct EventMessage {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub event_id: Option<String>,
|
||||
#[serde(flatten)]
|
||||
pub payload: EventPayload,
|
||||
}
|
||||
|
||||
impl EventPayload {
|
||||
pub fn event_type(&self) -> &'static str {
|
||||
match self {
|
||||
|
||||
@@ -105,3 +105,29 @@ fn image_stored_event_type() {
|
||||
});
|
||||
assert_eq!(payload.event_type(), "ImageStored");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_message_round_trips_its_event_id_and_decodes_without_one() {
|
||||
use event_payload::EventMessage;
|
||||
|
||||
let with_id = EventMessage {
|
||||
event_id: Some(Uuid::new_v4().to_string()),
|
||||
payload: EventPayload::from(&review_logged()),
|
||||
};
|
||||
let json = serde_json::to_string(&with_id).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<EventMessage>(&json).unwrap(),
|
||||
with_id
|
||||
);
|
||||
|
||||
let legacy = serde_json::to_string(&EventPayload::from(&review_logged())).unwrap();
|
||||
let decoded: EventMessage = serde_json::from_str(&legacy).unwrap();
|
||||
assert_eq!(decoded.event_id, None);
|
||||
|
||||
let unit = EventMessage {
|
||||
event_id: Some("x".into()),
|
||||
payload: EventPayload::SearchReindexRequested,
|
||||
};
|
||||
let json = serde_json::to_string(&unit).unwrap();
|
||||
assert_eq!(serde_json::from_str::<EventMessage>(&json).unwrap(), unit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user