Skip to content

Commit

Permalink
Skip over missing version numbers safely
Browse files Browse the repository at this point in the history
Refs #1973
  • Loading branch information
thewilkybarkid committed Oct 1, 2024
1 parent b40fdb3 commit 9b7d44f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/LibsqlEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
WHERE
resource_type = ${encoded.resource_type}
AND resource_id = ${encoded.resource_id}
AND resource_version = ${encoded.resource_version}
AND resource_version >= ${encoded.resource_version}
)
`.raw,
Effect.andThen(Schema.decodeUnknown(LibsqlResults)),
Expand Down
56 changes: 33 additions & 23 deletions test/LibsqlEventStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,39 @@ describe('when the last known version is up to date', () => {
})

describe('when the last known version is out of date', () => {
it.prop([fc.uuid(), fc.feedbackEvent(), fc.feedbackEvent()])(
'does not replace an event',
(resourceId, event1, event2) =>
Effect.gen(function* () {
const eventStore = yield* _.make

yield* eventStore.commitEvent(resourceId, 0)(event1)

const error = yield* Effect.flip(eventStore.commitEvent(resourceId, 0)(event2))

expect(error).toBeInstanceOf(EventStore.ResourceHasChanged)

const actual = yield* eventStore.getEvents(resourceId)
const all = yield* eventStore.getAllEvents

expect(actual).toStrictEqual({ events: [event1], latestVersion: 1 })
expect(all).toStrictEqual([{ event: event1, resourceId, version: 1 }])
}).pipe(
Effect.provideServiceEffect(Uuid.GenerateUuid, Uuid.make),
Effect.provide(LibsqlClient.layer({ url: Config.succeed(':memory:') })),
Effect.provide(TestContext.TestContext),
Effect.runPromise,
),
it.prop([
fc.uuid(),
fc
.integer({ min: 2 })
.chain(latestVersion => fc.tuple(fc.constant(latestVersion), fc.integer({ max: latestVersion - 1 }))),
fc.feedbackEvent(),
fc.feedbackEvent(),
fc.feedbackEvent(),
])('does not replace an event', (resourceId, [event2Version, lastKnownVersion], event1, event2, event3) =>
Effect.gen(function* () {
const eventStore = yield* _.make

yield* eventStore.commitEvent(resourceId, 0)(event1)
yield* eventStore.commitEvent(resourceId, event2Version - 1)(event2)

const error = yield* Effect.flip(eventStore.commitEvent(resourceId, lastKnownVersion)(event3))

expect(error).toBeInstanceOf(EventStore.ResourceHasChanged)

const actual = yield* eventStore.getEvents(resourceId)
const all = yield* eventStore.getAllEvents

expect(actual).toStrictEqual({ events: [event1, event2], latestVersion: event2Version })
expect(all).toStrictEqual([
{ event: event1, resourceId, version: 1 },
{ event: event2, resourceId, version: event2Version },
])
}).pipe(
Effect.provideServiceEffect(Uuid.GenerateUuid, Uuid.make),
Effect.provide(LibsqlClient.layer({ url: Config.succeed(':memory:') })),
Effect.provide(TestContext.TestContext),
Effect.runPromise,
),
)
})

Expand Down

0 comments on commit 9b7d44f

Please sign in to comment.