cqrs-postgres 0.3.3

An implementation of cqrs for a PostgreSQL backend.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! > _Note: Experimental!_
//!
//! Types for reacting to raw event data in PostgreSQL event store.

use crate::db_wrapper::{DbConnection, DbPool, ReactorError};
use cqrs_core::{
    reactor::{AggregatePredicate, EventTypesPredicate, Reaction, ReactionPredicate},
    CqrsError, RawEvent,
};
use postgres::{rows::Rows, types::ToSql, Connection};
use r2d2::Pool;
use r2d2_postgres::PostgresConnectionManager;
use std::{
    fmt::Write,
    sync::atomic::{AtomicBool, Ordering},
    time::Duration,
};

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
pub struct NullReaction;

impl Reaction for NullReaction {
    type Error = String;

    fn reaction_name() -> &'static str {
        "Null"
    }

    fn react(&mut self, _event: RawEvent) -> Result<(), Self::Error> {
        Ok(())
    }

    fn predicate(&self) -> ReactionPredicate {
        ReactionPredicate::default()
    }

    fn interval() -> Duration {
        Duration::from_secs(1)
    }
}

#[derive(Debug)]
pub struct PostgresReactor<P = Pool<PostgresConnectionManager>> {
    pool: P,
    run: AtomicBool,
}

impl<P> PostgresReactor<P>
where
    P: for<'conn> DbPool<'conn>,
{
    pub fn new(pool: P) -> Self {
        Self {
            pool,
            run: AtomicBool::new(true),
        }
    }

    pub fn stop_reaction(&self) {
        self.run.store(false, Ordering::Relaxed);
    }

    pub fn start_reaction<R: Reaction>(
        &self,
        mut reaction: R,
    ) -> Result<usize, ReactorError<R, impl CqrsError, impl CqrsError>> {
        let mut event_count = usize::default();

        while self.run.load(Ordering::Relaxed) {
            let conn = self.pool.get().map_err(ReactorError::pool)?;
            let since = conn
                .load_since(R::reaction_name())
                .map_err(ReactorError::postgres)?;
            let mut params: Vec<Box<dyn ToSql>> = Vec::default();
            let query_with_args =
                self.generate_query_with_args::<R>(reaction.predicate(), &mut params, 100);

            let raw_events = conn
                .read_all_events(&query_with_args, since, params.as_slice())
                .map_err(ReactorError::postgres)?;

            for event in raw_events {
                let event_id = event.event_id;
                reaction.react(event).map_err(ReactorError::react)?;

                conn.save_since(R::reaction_name(), event_id)
                    .map_err(ReactorError::postgres)?;

                event_count += 1;
            }

            drop(conn);

            ::std::thread::sleep(R::interval());
        }

        Ok(event_count)
    }

    fn generate_query_with_args<R: Reaction>(
        &self,
        predicate: ReactionPredicate,
        params: &mut Vec<Box<dyn ToSql>>,
        max_count: u64,
    ) -> String {
        let max_count = Box::new(max_count.min(i64::max_value() as u64) as i64);

        match predicate.aggregate_predicate {
            AggregatePredicate::AllAggregates(EventTypesPredicate::AllEventTypes) => {
                params.push(max_count);

                String::from(
                    "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                     FROM events \
                     WHERE event_id > $1 \
                     ORDER BY event_id ASC \
                     LIMIT $2",
                )
            }
            AggregatePredicate::AllAggregates(EventTypesPredicate::SpecificEventTypes(
                event_types,
            )) => {
                params.push(Box::new(event_types));
                params.push(max_count);

                String::from(
                    "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                     FROM events \
                     WHERE event_id > $1 \
                     AND event_type = ANY ($2) \
                     ORDER BY event_id ASC \
                     LIMIT $3",
                )
            }
            AggregatePredicate::SpecificAggregates(aggregate_predicates) => {
                let mut query = String::from(
                    "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                     FROM events \
                     WHERE event_id > $1 AND (FALSE",
                );

                let mut param_count = 1;

                for predicate in aggregate_predicates {
                    match &predicate.event_types {
                        EventTypesPredicate::SpecificEventTypes(event_types) => {
                            write!(
                                query,
                                " OR (aggregate_type = ${} AND event_type = ANY (${}))",
                                param_count + 1,
                                param_count + 2
                            )
                            .expect("Formatting integers into a string never fails");

                            params.push(Box::new(predicate.aggregate_type));
                            params.push(Box::new(event_types));
                            param_count += 2;
                        }
                        EventTypesPredicate::AllEventTypes => {
                            write!(query, " OR (aggregate_type = ${})", param_count + 1)
                                .expect("Formatting integers into a string never fails");

                            params.push(Box::new(predicate.aggregate_type));
                            param_count += 1;
                        }
                    }
                }

                write!(query, ") ORDER BY event_id ASC LIMIT ${}", param_count + 1)
                    .expect("Formatting integers into a string never fails");

                params.push(max_count);
                query
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        db_wrapper::{DbConnection, DbPool, ReactorError},
        reactor::{self, NullReaction, PostgresReactor},
    };
    use cqrs_core::{
        reactor::{
            AggregatePredicate, EventTypesPredicate, Reaction, ReactionPredicate,
            SpecificAggregatePredicate,
        },
        CqrsError, EventNumber, RawEvent, Since,
    };
    use lazy_static::lazy_static;
    use parking_lot::Mutex;
    use postgres::{error, types::ToSql, Connection};
    use r2d2_postgres::{r2d2::Pool, PostgresConnectionManager, TlsMode};
    use std::{
        io::{self, Error},
        sync::Arc,
        thread,
        time::Duration,
    };

    lazy_static! {
        static ref PREDICATE: Mutex<ReactionPredicate> = Mutex::new(ReactionPredicate::default());
        static ref RAW_EVENT: RawEvent = RawEvent {
            event_id: EventNumber::new(1).unwrap(),
            aggregate_type: String::from(""),
            entity_id: String::from(""),
            sequence: EventNumber::new(1).unwrap(),
            event_type: String::from(""),
            payload: Vec::from("{}"),
        };
        static ref RAW_EVENTS: Vec<RawEvent> = vec![RAW_EVENT.clone(), RAW_EVENT.clone(),];
    }

    #[derive(Debug)]
    pub struct MockPool {
        get_result: Result<MockConnection, String>,
    }

    impl<'conn> DbPool<'conn> for MockPool {
        type Connection = MockConnection;
        type Error = String;

        fn get(&self) -> Result<Self::Connection, Self::Error> {
            self.get_result.clone()
        }
    }

    #[derive(Debug, Clone)]
    pub struct LoadSince {
        expected_reaction_name: String,
        result: Result<Since, String>,
    }

    impl Default for LoadSince {
        fn default() -> Self {
            LoadSince {
                expected_reaction_name: String::from("Mock"),
                result: Ok(Since::BeginningOfStream),
            }
        }
    }

    #[derive(Debug, Clone)]
    pub struct SaveSince {
        expected_reaction_name: String,
        expected_event_id: EventNumber,
        result: Result<(), String>,
    }

    impl Default for SaveSince {
        fn default() -> Self {
            SaveSince {
                expected_reaction_name: String::from("Mock"),
                expected_event_id: EventNumber::MIN_VALUE,
                result: Ok(()),
            }
        }
    }

    #[derive(Debug, Clone)]
    pub struct ReadAllEvents {
        expected_query: Option<String>,
        expected_since: Since,
        expected_params: Option<String>,
        result: Result<Vec<RawEvent>, String>,
    }

    impl Default for ReadAllEvents {
        fn default() -> Self {
            ReadAllEvents {
                expected_query: None,
                expected_since: Since::BeginningOfStream,
                expected_params: None,
                result: Ok(vec![]),
            }
        }
    }

    #[derive(Debug, Clone)]
    pub struct MockConnection {
        load_since_data: LoadSince,
        save_since_data: SaveSince,
        read_all_events_data: ReadAllEvents,
    }

    impl Default for MockConnection {
        fn default() -> Self {
            MockConnection {
                load_since_data: LoadSince::default(),
                save_since_data: SaveSince::default(),
                read_all_events_data: ReadAllEvents::default(),
            }
        }
    }

    impl<'conn> DbConnection<'conn> for MockConnection {
        type Error = String;

        fn load_since(&self, reaction_name: &str) -> Result<Since, Self::Error> {
            assert_eq!(reaction_name, self.load_since_data.expected_reaction_name);
            self.load_since_data.result.clone()
        }

        fn save_since(
            &self,
            reaction_name: &str,
            event_id: EventNumber,
        ) -> Result<(), Self::Error> {
            assert_eq!(reaction_name, self.save_since_data.expected_reaction_name);
            assert_eq!(event_id, self.save_since_data.expected_event_id);
            self.save_since_data.result.clone()
        }

        fn read_all_events(
            &self,
            query: &str,
            since: Since,
            params: &[Box<dyn ToSql>],
        ) -> Result<Vec<RawEvent>, Self::Error> {
            assert_eq!(since, self.read_all_events_data.expected_since);

            if let Some(expected_query) = &self.read_all_events_data.expected_query {
                assert_eq!(query, expected_query);
            }

            if let Some(expected_params) = &self.read_all_events_data.expected_params {
                assert_eq!(&format!("{:?}", params), expected_params);
            }

            self.read_all_events_data.result.clone()
        }
    }

    #[derive(Clone, Debug)]
    pub struct MockReaction {
        events: Vec<RawEvent>,
        predicate: ReactionPredicate,
        react_result: Result<(), String>,
    }

    impl MockReaction {
        fn event_count(&self) -> usize {
            self.events.len()
        }
    }

    impl Default for MockReaction {
        fn default() -> Self {
            MockReaction {
                predicate: ReactionPredicate::default(),
                events: vec![],
                react_result: Ok(()),
            }
        }
    }

    impl Reaction for MockReaction {
        type Error = String;

        fn reaction_name() -> &'static str {
            "Mock"
        }

        fn react(&mut self, event: RawEvent) -> Result<(), Self::Error> {
            self.events.push(event);
            self.react_result.clone()
        }

        fn predicate(&self) -> ReactionPredicate {
            self.predicate
        }

        fn interval() -> Duration {
            Duration::from_millis(100)
        }
    }

    #[test]
    fn can_read_all_aggregates_and_all_events() {
        let pool = ok_pool(
            String::from(
                "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                 FROM events \
                 WHERE event_id > $1 \
                 ORDER BY event_id ASC \
                 LIMIT $2",
            ),
            String::from("[100]"),
        );

        let reaction = MockReaction::default();

        assert_eq!(2, test_reaction(pool, reaction).unwrap());
    }

    #[test]
    fn can_read_specific_aggregates_and_all_events() {
        let pool = ok_pool(
            String::from(
                "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                 FROM events \
                 WHERE event_id > $1 \
                 AND (FALSE OR (aggregate_type = $2)) \
                 ORDER BY event_id ASC \
                 LIMIT $3",
            ),
            String::from("[\"material_location_availability\", 100]"),
        );

        let reaction = MockReaction {
            predicate: ReactionPredicate {
                aggregate_predicate: AggregatePredicate::SpecificAggregates(&[
                    SpecificAggregatePredicate {
                        aggregate_type: "material_location_availability",
                        event_types: EventTypesPredicate::AllEventTypes,
                    },
                ]),
            },
            ..MockReaction::default()
        };

        assert_eq!(2, test_reaction(pool, reaction).unwrap());
    }

    #[test]
    fn can_read_all_aggregates_and_specific_events() {
        let pool = ok_pool(
            String::from(
                "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                 FROM events \
                 WHERE event_id > $1 \
                 AND event_type = ANY ($2) \
                 ORDER BY event_id ASC \
                 LIMIT $3",
            ),
            String::from("[[\"sources_updated\"], 100]"),
        );

        let reaction = MockReaction {
            predicate: ReactionPredicate {
                aggregate_predicate: AggregatePredicate::AllAggregates(
                    EventTypesPredicate::SpecificEventTypes(&["sources_updated"]),
                ),
            },
            ..MockReaction::default()
        };

        assert_eq!(2, test_reaction(pool, reaction).unwrap());
    }

    #[test]
    fn can_read_specific_aggregates_and_specific_events() {
        let pool = ok_pool(
            String::from(
                "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                 FROM events \
                 WHERE event_id > $1 \
                 AND (FALSE OR (aggregate_type = $2 AND event_type = ANY ($3))) \
                 ORDER BY event_id ASC \
                 LIMIT $4",
            ),
            String::from("[\"material_location_availability\", [\"sources_updated\", \"end_of_life_updated\"], 100]"),
        );

        let reaction = MockReaction {
            predicate: ReactionPredicate {
                aggregate_predicate: AggregatePredicate::SpecificAggregates(&[
                    SpecificAggregatePredicate {
                        aggregate_type: "material_location_availability",
                        event_types: EventTypesPredicate::SpecificEventTypes(&[
                            "sources_updated",
                            "end_of_life_updated",
                        ]),
                    },
                ]),
            },
            ..MockReaction::default()
        };

        assert_eq!(2, test_reaction(pool, reaction).unwrap());
    }

    #[test]
    fn get_connection_error() {
        let error_message = "connection pool error";
        let test_error = Err(String::from(error_message));

        let pool = MockPool {
            get_result: test_error,
        };

        let reaction = MockReaction {
            ..MockReaction::default()
        };

        let result = test_reaction(pool, reaction);

        assert!(result.is_err());
        assert_eq!(
            "Pool error during reaction: connection pool error",
            result.unwrap_err().to_string()
        );
    }

    #[test]
    fn load_since_error() {
        let error_message = "load_since error";
        let test_error = Err(String::from(error_message));

        let connection = MockConnection {
            load_since_data: LoadSince {
                result: test_error,
                ..LoadSince::default()
            },
            ..MockConnection::default()
        };

        let pool = MockPool {
            get_result: Ok(connection),
        };

        let reaction = MockReaction {
            ..MockReaction::default()
        };

        let result = test_reaction(pool, reaction);

        assert!(result.is_err());
        assert_eq!(
            "Postgres error during reaction: load_since error",
            result.err().unwrap().to_string()
        );
    }

    #[test]
    fn read_all_events_error() {
        let error_message = "read_all_events error";
        let test_error = Err(String::from(error_message));

        let connection = MockConnection {
            read_all_events_data: ReadAllEvents {
                result: test_error,
                ..ReadAllEvents::default()
            },
            ..MockConnection::default()
        };

        let pool = MockPool {
            get_result: Ok(connection),
        };

        let reaction = MockReaction {
            ..MockReaction::default()
        };

        let result = test_reaction(pool, reaction);

        assert!(result.is_err());
        assert_eq!(
            "Postgres error during reaction: read_all_events error",
            result.err().unwrap().to_string()
        );
    }

    #[test]
    fn react_error() {
        let error_message = "react error";
        let test_error = Err(String::from(error_message));

        let connection = MockConnection {
            read_all_events_data: ReadAllEvents {
                result: Ok(RAW_EVENTS.to_vec()),
                ..ReadAllEvents::default()
            },
            ..MockConnection::default()
        };

        let pool = MockPool {
            get_result: Ok(connection),
        };

        let reaction = MockReaction {
            react_result: test_error,
            ..MockReaction::default()
        };

        let result = test_reaction(pool, reaction);

        assert!(result.is_err());
        assert_eq!(
            "React error during reaction: react error",
            result.err().unwrap().to_string()
        );
    }

    #[test]
    fn save_since_error() {
        let error_message = "save_since error";
        let test_error = Err(String::from(error_message));

        let connection = MockConnection {
            read_all_events_data: ReadAllEvents {
                result: Ok(RAW_EVENTS.to_vec()),
                ..ReadAllEvents::default()
            },
            save_since_data: SaveSince {
                result: test_error,
                ..SaveSince::default()
            },
            ..MockConnection::default()
        };

        let pool = MockPool {
            get_result: Ok(connection),
        };

        let reaction = MockReaction {
            ..MockReaction::default()
        };

        let result = test_reaction(pool, reaction);

        assert!(result.is_err());
        assert_eq!(
            "Postgres error during reaction: save_since error",
            result.err().unwrap().to_string()
        );
    }

    fn ok_pool(expected_query: String, expected_params: String) -> MockPool {
        let connection = MockConnection {
            read_all_events_data: ReadAllEvents {
                expected_query: Some(expected_query),
                expected_params: Some(expected_params),
                result: Ok(RAW_EVENTS.to_vec()),
                ..ReadAllEvents::default()
            },
            ..MockConnection::default()
        };

        MockPool {
            get_result: Ok(connection),
        }
    }

    fn test_reaction(
        pool: MockPool,
        reaction: MockReaction,
    ) -> Result<usize, ReactorError<MockReaction, impl CqrsError, impl CqrsError>> {
        let local_reactor = Arc::new(PostgresReactor::new(pool));
        let thread_reactor = Arc::clone(&local_reactor);

        let handle = thread::spawn(move || thread_reactor.start_reaction(reaction));

        ::std::thread::sleep(Duration::from_millis(50));
        local_reactor.stop_reaction();

        handle.join().unwrap()
    }
}