Skip to content

Commit

Permalink
snapshots!
Browse files Browse the repository at this point in the history
  • Loading branch information
renatillas committed Jan 22, 2025
1 parent 2ecc30b commit c064dfc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 14 deletions.
12 changes: 12 additions & 0 deletions birdie_snapshots/postgres_snapshot.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
version: 1.2.5
title: postgres snapshot
file: ./test/eventsourcing_postgres_test.gleam
test_name: postgres_store_store_snapshots_test
---
Ok(Some(Snapshot(
"92085b42-032c-4d7a-84de-a86d67123858",
BankAccount(True, 4.01),
3,
1737553670,
)))
10 changes: 10 additions & 0 deletions birdie_snapshots/postgres_snapshot.new
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
version: 1.2.5
title: postgres snapshot
---
Ok(Some(Snapshot(
"92085b42-032c-4d7a-84de-a86d67123858",
BankAccount(True, 4.01),
3,
1737553717,
)))
3 changes: 2 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "eventsourcing_postgres"
version = "5.0.0"
version = "5.0.1"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand All @@ -21,3 +21,4 @@ gleam_json = ">= 2.3.0 and < 3.0.0"
gleeunit = ">= 1.2.0 and < 2.0.0"
birdie = ">= 1.2.5 and < 2.0.0"
pprint = ">= 1.0.4 and < 2.0.0"
exception = ">= 2.0.0 and < 3.0.0"
2 changes: 2 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ packages = [
{ name = "birl", version = "1.8.0", build_tools = ["gleam"], requirements = ["gleam_regexp", "gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "2AC7BA26F998E3DFADDB657148BD5DDFE966958AD4D6D6957DD0D22E5B56C400" },
{ name = "edit_distance", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "edit_distance", source = "hex", outer_checksum = "A1E485C69A70210223E46E63985FA1008B8B2DDA9848B7897469171B29020C05" },
{ name = "eventsourcing", version = "6.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_stdlib"], otp_app = "eventsourcing", source = "hex", outer_checksum = "E72DBA6CB88745EC87EAFCBAC1083CF0F1FDDD96CC108276AA867147B313714A" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
{ name = "glance", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "784CE3B5658CF589B2E811031992FDADDFA9C7FD2A51F1140EE019F121D6D0EB" },
Expand Down Expand Up @@ -35,6 +36,7 @@ packages = [
[requirements]
birdie = { version = ">= 1.2.5 and < 2.0.0" }
eventsourcing = { version = ">= 6.0.0 and < 7.0.0" }
exception = { version = ">= 2.0.0 and < 3.0.0" }
gleam_json = { version = ">= 2.3.0 and < 3.0.0" }
gleam_stdlib = { version = ">= 0.52.0 and < 2.0.0" }
gleeunit = { version = ">= 1.2.0 and < 2.0.0" }
Expand Down
43 changes: 42 additions & 1 deletion src/eventsourcing_postgres.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ const save_snapshot_query = "
entity = EXCLUDED.entity
"

const select_snapshot_query = "
SELECT aggregate_type, aggregate_id, sequence, entity, timestamp
FROM snapshot
WHERE aggregate_type = $1
AND aggregate_id = $2
"

// TYPES ----

type Metadata =
Expand Down Expand Up @@ -128,7 +135,41 @@ fn load_snapshot(
Option(eventsourcing.Snapshot(entity)),
eventsourcing.EventSourcingError(error),
) {
Ok(None)
let row_decoder = {
use aggregate_id <- decode.field(1, decode.string)
use sequence <- decode.field(2, decode.int)
use entity <- decode.field(3, {
use entity_string <- decode.then(decode.string)
let assert Ok(entity) =
json.parse(entity_string, postgres_store.entity_decoder)
decode.success(entity)
})
use timestamp <- decode.field(4, decode.int)

decode.success(eventsourcing.Snapshot(
aggregate_id: aggregate_id,
entity: entity,
sequence: sequence,
timestamp: timestamp,
))
}

pog.query(select_snapshot_query)
|> pog.parameter(pog.text(postgres_store.aggregate_type))
|> pog.parameter(pog.text(aggregate_id))
|> pog.returning(row_decoder)
|> pog.execute(postgres_store.db)
|> result.map(fn(response) {
case response.rows {
[] -> None
[snapshot, ..] -> option.Some(snapshot)
}
})
|> result.map_error(fn(error) {
eventsourcing.EventStoreError(
"Failed to load snapshot: " <> pprint.format(error),
)
})
}

fn save_snapshot(
Expand Down
31 changes: 19 additions & 12 deletions test/eventsourcing_postgres_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import birdie
import eventsourcing
import eventsourcing_postgres
import example_bank_account
import exception
import gleam/option
import gleeunit
import gleeunit/should
Expand Down Expand Up @@ -43,6 +44,10 @@ fn drop_snapshot_table() {
}

pub fn postgres_store_test() {
use <- exception.defer(fn() {
drop_event_table()
|> should.be_ok
})
let postgres_store = postgres_store()
let query = fn(_, _) { Nil }

Expand Down Expand Up @@ -88,11 +93,13 @@ pub fn postgres_store_test() {
)
|> pprint.format
|> birdie.snap(title: "postgres store")
drop_event_table()
|> should.be_ok
}

pub fn postgres_store_load_events_test() {
use <- exception.defer(fn() {
drop_event_table()
|> should.be_ok
})
let postgres_store = postgres_store()
let query = fn(_, _) { Nil }

Expand Down Expand Up @@ -140,12 +147,16 @@ pub fn postgres_store_load_events_test() {
)
|> pprint.format
|> birdie.snap(title: "postgres store load events")

drop_event_table()
|> should.be_ok
}

pub fn postgres_store_store_snapshots_test() {
use <- exception.defer(fn() {
drop_event_table()
|> should.be_ok
drop_snapshot_table()
|> should.be_ok
})

let postgres_store = postgres_store()
let query = fn(_, _) { Nil }

Expand All @@ -162,7 +173,7 @@ pub fn postgres_store_store_snapshots_test() {
example_bank_account.apply,
example_bank_account.BankAccount(opened: False, balance: 0.0),
)
|> eventsourcing.with_snapshots(eventsourcing.SnapshotConfig(2))
|> eventsourcing.with_snapshots(eventsourcing.SnapshotConfig(1))

eventsourcing.execute(
event_sourcing,
Expand All @@ -188,14 +199,10 @@ pub fn postgres_store_store_snapshots_test() {
|> should.be_ok
|> should.equal(Nil)

eventsourcing.load_aggregate(
eventsourcing.get_latest_snapshot(
event_sourcing,
"92085b42-032c-4d7a-84de-a86d67123858",
)
|> pprint.format
|> birdie.snap(title: "postgres store")
drop_event_table()
|> should.be_ok
drop_snapshot_table()
|> should.be_ok
|> birdie.snap(title: "postgres snapshot")
}

0 comments on commit c064dfc

Please sign in to comment.