-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log dropped events in database (#4984)
Co-authored-by: Simon Dumas <[email protected]>
- Loading branch information
Showing
7 changed files
with
126 additions
and
24 deletions.
There are no files selected for viewing
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
...cing-psql/src/main/resources/scripts/postgres/init/V1_10_M11_001__ship_dropped_events.ddl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE IF NOT EXISTS public.ship_dropped_events( | ||
ordering bigint NOT NULL, | ||
type text NOT NULL, | ||
org text NOT NULL, | ||
project text NOT NULL, | ||
id text NOT NULL, | ||
rev integer NOT NULL, | ||
value JSONB NOT NULL, | ||
instant timestamptz NOT NULL, | ||
PRIMARY KEY(org, project, id, rev) | ||
); | ||
|
41 changes: 41 additions & 0 deletions
41
ship/src/main/scala/ch/epfl/bluebrain/nexus/ship/DroppedEventStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ch.epfl.bluebrain.nexus.ship | ||
|
||
import cats.effect.IO | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.exporter.RowEvent | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.implicits._ | ||
import doobie.postgres.implicits._ | ||
import doobie.implicits._ | ||
|
||
final class DroppedEventStore(xas: Transactors) { | ||
|
||
def count: IO[Long] = | ||
sql"""SELECT COUNT(*) FROM ship_dropped_events""".stripMargin.query[Long].unique.transact(xas.read) | ||
|
||
def truncate = | ||
sql"""TRUNCATE ship_dropped_events""".update.run.transact(xas.write).void | ||
|
||
def save(rowEvent: RowEvent) = | ||
sql""" | ||
| INSERT INTO ship_dropped_events ( | ||
| ordering, | ||
| type, | ||
| org, | ||
| project, | ||
| id, | ||
| rev, | ||
| value, | ||
| instant | ||
| ) | ||
| VALUES ( | ||
| ${rowEvent.ordering}, | ||
| ${rowEvent.`type`}, | ||
| ${rowEvent.org}, | ||
| ${rowEvent.project}, | ||
| ${rowEvent.id}, | ||
| ${rowEvent.rev}, | ||
| ${rowEvent.value}, | ||
| ${rowEvent.instant} | ||
| )""".stripMargin.update.run.transact(xas.write).void | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
ship/src/test/scala/ch/epfl/bluebrain/nexus/ship/DroppedEventStoreSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ch.epfl.bluebrain.nexus.ship | ||
|
||
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.nxv | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.exporter.RowEvent | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{EntityType, Label} | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.offset.Offset | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.postgres.Doobie | ||
import ch.epfl.bluebrain.nexus.testkit.mu.NexusSuite | ||
import io.circe.Json | ||
import io.circe.syntax.KeyOps | ||
import munit.AnyFixture | ||
|
||
import java.time.Instant | ||
|
||
class DroppedEventStoreSuite extends NexusSuite with Doobie.Fixture { | ||
|
||
override def munitFixtures: Seq[AnyFixture[_]] = List(doobie) | ||
|
||
private lazy val xas = doobie() | ||
private lazy val store = new DroppedEventStore(xas) | ||
|
||
test("Insert an event and truncate it") { | ||
val rowEvent = RowEvent( | ||
Offset.At(5L), | ||
EntityType("entity"), | ||
Label.unsafe("org"), | ||
Label.unsafe("project"), | ||
nxv + "id", | ||
3, | ||
Json.obj("field" := "value"), | ||
Instant.EPOCH | ||
) | ||
for { | ||
_ <- store.save(rowEvent) | ||
_ <- store.count.assertEquals(1L) | ||
_ <- store.truncate | ||
_ <- store.count.assertEquals(0L) | ||
} yield () | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters