Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add type_name filter for event log filters #38

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sql/create_event_log_uuid.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ CREATE TABLE
type_name text NOT NULL,
event jsonb NOT NULL,
metadata jsonb NOT NULL,
UNIQUE (entity_id, version)
);
UNIQUE (entity_id, type_name, version)
);
27 changes: 21 additions & 6 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ where
sqlx::query(
"SELECT version, event
FROM event
WHERE entity_id = $1
WHERE entity_id = $1 AND type_name = $2
ORDER BY seq_no ASC",
)
.bind(id)
.bind(E::TYPE_NAME)
.fetch(&**pool)
.map_err(|error| Error::Sqlx("cannot get next event".to_string(), error))
.map(|row| {
Expand Down Expand Up @@ -306,9 +307,10 @@ where
let version = sqlx::query(
"SELECT MAX(version)
FROM event
WHERE entity_id = $1",
WHERE entity_id = $1 AND type_name = $2",
)
.bind(id)
.bind(E::TYPE_NAME)
.fetch_one(&mut *tx)
.await
.map_err(|error| Error::Sqlx("cannot select max version".to_string(), error))
Expand Down Expand Up @@ -367,7 +369,7 @@ mod tests {
};
use error_ext::BoxError;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::{json, Value};
use sqlx::{postgres::PgSslMode, Executor, Row, Transaction};
use std::error::Error as StdError;
use testcontainers::{runners::AsyncRunner, ContainerRequest, ImageExt};
Expand Down Expand Up @@ -568,7 +570,7 @@ mod tests {
)
.bind(&id)
.bind(1_i64)
.bind("test")
.bind("counter")
.bind(serde_json::to_value(&Event::Increased { id, inc: 40 })?)
.bind(Value::Null)
.execute(&*pool)
Expand All @@ -579,7 +581,7 @@ mod tests {
)
.bind(&id)
.bind(2_i64)
.bind("test")
.bind("counter")
.bind(serde_json::to_value(&Event::Decreased { id, dec: 20 })?)
.bind(Value::Null)
.execute(&*pool)
Expand All @@ -590,7 +592,7 @@ mod tests {
)
.bind(&id)
.bind(3_i64)
.bind("test")
.bind("counter")
.bind(serde_json::to_value(&Event::Increased { id, inc: 22 })?)
.bind(Value::Null)
.execute(&*pool)
Expand Down Expand Up @@ -625,6 +627,19 @@ mod tests {

let id = Uuid::from_u128(0);

// insert misleading event into table that should be ignored for the counter entity
sqlx::query(
"INSERT INTO event (entity_id, version, type_name, event, metadata)
VALUES ($1, $2, $3, $4, $5)",
)
.bind(&id)
.bind(1_i64)
.bind("faker")
.bind(json!({ "name": "Meier", "address": "Musterstraße 42" }))
.bind(Value::Null)
.execute(&*pool)
.await?;

let mut counter = Counter::default().entity().build(id, pool.clone()).await?;
assert_eq!(counter.entity, Counter(0));

Expand Down