Skip to content

Commit

Permalink
feat(mongo): enhance EventStore with transaction hooks and detailed c…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
pawel authored and bounoable committed Sep 14, 2023
1 parent a6002ff commit 5c7a424
Show file tree
Hide file tree
Showing 9 changed files with 872 additions and 142 deletions.
1 change: 1 addition & 0 deletions .docker/mongo-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ services:
dockerfile: .docker/tag-test.Dockerfile
args:
TAGS: mongo
TEST_PATH: ./backend/mongo/...
environment:
- MONGOSTORE_URL=mongodb://mongostore:27017
- MONGOREPLSTORE_URL=mongodb://mongostore_replicaset:27017
Expand Down
4 changes: 3 additions & 1 deletion .docker/tag-test.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM golang:1.18
ARG TAGS
ARG TEST_PATH=./...
ENV TAGS $TAGS
ENV TEST_PATH $TEST_PATH
WORKDIR /test
COPY go.mod go.sum /test/
RUN go mod download
COPY . .
CMD go test -v -tags=$TAGS ./...
CMD go test -v -tags=$TAGS $TEST_PATH
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
test:
runs-on: self-hosted
runs-on: ubuntu-latest
timeout-minutes: 10
if: |
!startsWith(github.event.head_commit.message, 'docs') &&
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.PHONY: docs
docs:
@./scripts/clear.sh
@./scripts/docs.sh

.PHONY: generate
Expand Down
17 changes: 12 additions & 5 deletions backend/mongo/mongotest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ import (
// Option that ensures a unique database name for every call to NewEventStore
// during the current process.
func NewEventStore(enc codec.Encoding, opts ...mongo.EventStoreOption) *mongo.EventStore {
return mongo.NewEventStore(enc, append(
[]mongo.EventStoreOption{mongo.Database(UniqueName("event_"))},
opts...,
)...)
}

// UniqueName generates a unique string by appending a random hexadecimal
// string to the provided prefix. The generated string is intended for use as a
// unique identifier in database names. If there's an error in reading from the
// random source, the function will panic.
func UniqueName(prefix string) string {
b := make([]byte, 8)
if _, err := rand.Read(b); err != nil {
panic(err)
}
id := hex.EncodeToString(b)

return mongo.NewEventStore(enc, append(
[]mongo.EventStoreOption{mongo.Database(fmt.Sprintf("event_%s", id))},
opts...,
)...)
return fmt.Sprintf("%s%s", prefix, id)
}
Loading

0 comments on commit 5c7a424

Please sign in to comment.