├── .gitignore ├── README.md ├── go.mod ├── json_encoder.go ├── LICENSE ├── eventstore_test.go ├── go.sum └── eventstore.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | .vscode -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EventHorizon with PostgreSQL 2 | 3 | ## Features 4 | 5 | - EventStore 6 | 7 | ```golang 8 | db := pg.Connect(&pg.Options{ 9 | Addr: os.Getenv("POSTGRES_ADDR"), 10 | Database: os.Getenv("POSTGRES_DB"), 11 | User: os.Getenv("POSTGRES_USER"), 12 | Password: os.Getenv("POSTGRES_PASSWORD"), 13 | }) 14 | defer db.Close() 15 | 16 | store, err := ehpg.NewEventStore(db) 17 | ``` -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/giautm/eh-pg 2 | 3 | require ( 4 | github.com/go-pg/pg v7.1.7+incompatible 5 | github.com/google/uuid v1.1.0 6 | github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect 7 | github.com/looplab/eventhorizon v0.5.0 8 | github.com/onsi/ginkgo v1.7.0 // indirect 9 | github.com/onsi/gomega v1.4.3 // indirect 10 | mellium.im/sasl v0.2.1 // indirect 11 | ) 12 | -------------------------------------------------------------------------------- /json_encoder.go: -------------------------------------------------------------------------------- 1 | package ehpg 2 | 3 | import ( 4 | "encoding/json" 5 | 6 | eh "github.com/looplab/eventhorizon" 7 | ) 8 | 9 | type Encoder interface { 10 | Marshal(eh.EventData) ([]byte, error) 11 | Unmarshal(eh.EventType, []byte) (eh.EventData, error) 12 | String() string 13 | } 14 | 15 | type jsonEncoder struct{} 16 | 17 | func (jsonEncoder) Marshal(data eh.EventData) ([]byte, error) { 18 | if data != nil { 19 | return json.Marshal(data) 20 | } 21 | return nil, nil 22 | } 23 | 24 | func (jsonEncoder) Unmarshal(eventType eh.EventType, raw []byte) (data eh.EventData, err error) { 25 | if len(raw) == 0 { 26 | return nil, nil 27 | } 28 | if data, err = eh.CreateEventData(eventType); err == nil { 29 | if err = json.Unmarshal(raw, data); err == nil { 30 | return data, nil 31 | } 32 | } 33 | return nil, err 34 | } 35 | 36 | func (jsonEncoder) String() string { 37 | return "json" 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Giau Tran Minh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /eventstore_test.go: -------------------------------------------------------------------------------- 1 | package ehpg_test 2 | 3 | import ( 4 | "context" 5 | "os" 6 | "testing" 7 | 8 | "github.com/giautm/eh-pg" 9 | "github.com/go-pg/pg" 10 | eh "github.com/looplab/eventhorizon" 11 | testutil "github.com/looplab/eventhorizon/eventstore" 12 | ) 13 | 14 | func TestEventStore(t *testing.T) { 15 | db := pg.Connect(&pg.Options{ 16 | Addr: os.Getenv("POSTGRES_ADDR"), 17 | Database: os.Getenv("POSTGRES_DB"), 18 | User: os.Getenv("POSTGRES_USER"), 19 | Password: os.Getenv("POSTGRES_PASSWORD"), 20 | }) 21 | defer db.Close() 22 | 23 | store, err := ehpg.NewEventStore(db) 24 | if err != nil { 25 | t.Fatal("there should be no error:", err) 26 | } 27 | if store == nil { 28 | t.Fatal("there should be a store") 29 | } 30 | 31 | ctx := eh.NewContextWithNamespace(context.Background(), "ns") 32 | defer func() { 33 | t.Log("clearing db") 34 | if err = store.Clear(context.Background()); err != nil { 35 | t.Fatal("there should be no error:", err) 36 | } 37 | if err = store.Clear(ctx); err != nil { 38 | t.Fatal("there should be no error:", err) 39 | } 40 | }() 41 | 42 | // Run the actual test suite. 43 | t.Log("event store with default namespace") 44 | testutil.AcceptanceTest(t, context.Background(), store) 45 | 46 | t.Log("event store with other namespace") 47 | testutil.AcceptanceTest(t, ctx, store) 48 | 49 | t.Log("event store maintainer") 50 | testutil.MaintainerAcceptanceTest(t, context.Background(), store) 51 | } 52 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | contrib.go.opencensus.io/exporter/stackdriver v0.6.0/go.mod h1:QeFzMJDAw8TXt5+aRaSuE8l5BwaMIOIlaVkBOPRuMuw= 3 | github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= 4 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 5 | github.com/globalsign/mgo v0.0.0-20180828104044-6f9f54af1356/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= 6 | github.com/go-pg/pg v7.1.7+incompatible h1:MXeUtzJtt9hie8LSANh0FvaS7ANQ535qD3Q5dtXy8q8= 7 | github.com/go-pg/pg v7.1.7+incompatible/go.mod h1:a2oXow+aFOrvwcKs3eIA0lNFmMilrxK2sOkB5NWe0vA= 8 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 9 | github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= 10 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 11 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 12 | github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s= 13 | github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 14 | github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= 15 | github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA= 16 | github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= 17 | github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= 18 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 19 | github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k= 20 | github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 21 | github.com/jpillora/backoff v0.0.0-20170918002102-8eab2debe79d/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0= 22 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 23 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 24 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 25 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 26 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 27 | github.com/looplab/eventhorizon v0.5.0 h1:NPpieD3lNSlfE2daP3xOFMFWJmfuLE6mcXCnaR6Lv74= 28 | github.com/looplab/eventhorizon v0.5.0/go.mod h1:I4DbJuQ20hHfNXgedANnpn6T53RpPCRhsrjxqAlXzUI= 29 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 30 | github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= 31 | github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 32 | github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= 33 | github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= 34 | github.com/tikivn/ops-delivery v0.0.0-20181018124425-46da8013a0e5 h1:MFo3z4zSQoZYTtkuhX2UvTLwzo+5QrQXcgSSxdPnUko= 35 | go.opencensus.io v0.15.0/go.mod h1:UffZAU+4sDEINUGP/B7UfBBkq4fqLu9zXAX7ke6CHW0= 36 | golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ= 37 | golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 38 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 39 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= 40 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 41 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 42 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= 43 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 44 | golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 45 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= 46 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 47 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 48 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 49 | google.golang.org/api v0.0.0-20180904000447-0ad5a633fea1/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= 50 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 51 | google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 52 | google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= 53 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 54 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= 55 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 56 | gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= 57 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 58 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= 59 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 60 | gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= 61 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 62 | mellium.im/sasl v0.2.1 h1:nspKSRg7/SyO0cRGY71OkfHab8tf9kCts6a6oTDut0w= 63 | mellium.im/sasl v0.2.1/go.mod h1:ROaEDLQNuf9vjKqE1SrAfnsobm2YKXT1gnN1uDp1PjQ= 64 | -------------------------------------------------------------------------------- /eventstore.go: -------------------------------------------------------------------------------- 1 | package ehpg 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "errors" 7 | "fmt" 8 | "time" 9 | 10 | "github.com/go-pg/pg" 11 | "github.com/go-pg/pg/orm" 12 | "github.com/google/uuid" 13 | eh "github.com/looplab/eventhorizon" 14 | ) 15 | 16 | // ErrCouldNotClearDB is when the database could not be cleared. 17 | var ErrCouldNotClearDB = errors.New("could not clear database") 18 | 19 | // ErrConflictVersion is when a version conflict occurs when saving an aggregate. 20 | var ErrVersionConflict = errors.New("Can not create/update aggregate") 21 | 22 | // ErrCouldNotMarshalEvent is when an event could not be marshaled into JSON. 23 | var ErrCouldNotMarshalEvent = errors.New("could not marshal event") 24 | 25 | // ErrCouldNotUnmarshalEvent is when an event could not be unmarshaled into a concrete type. 26 | var ErrCouldNotUnmarshalEvent = errors.New("could not unmarshal event") 27 | 28 | // ErrCouldNotSaveAggregate is when an aggregate could not be saved. 29 | var ErrCouldNotSaveAggregate = errors.New("could not save aggregate") 30 | 31 | // EventStore implements an eh.EventStore for PostgreSQL. 32 | type EventStore struct { 33 | db *pg.DB 34 | encoder Encoder 35 | } 36 | 37 | var _ = eh.EventStore(&EventStore{}) 38 | 39 | type AggregateRecord struct { 40 | tableName struct{} `sql:"aggregates"` 41 | 42 | Namespace string `sql:"namespace,type:varchar(250),pk"` 43 | AggregateID uuid.UUID `sql:"aggregate_id,type:uuid,pk"` 44 | Version int `sql:"version"` 45 | } 46 | 47 | type AggregateEvent struct { 48 | tableName struct{} `sql:"event_store"` 49 | 50 | EventID uuid.UUID `sql:"event_id,type:uuid,pk"` 51 | Namespace string `sql:"namespace,type:varchar(250)"` 52 | AggregateID uuid.UUID `sql:"aggregate_id,type:uuid"` 53 | AggregateType eh.AggregateType `sql:"aggregate_type,type:varchar(250)"` 54 | EventType eh.EventType `sql:"event_type,type:varchar(250)"` 55 | RawData json.RawMessage `sql:"data,type:jsonb"` 56 | Timestamp time.Time `sql:"timestamp"` 57 | Version int `sql:"version"` 58 | Context map[string]interface{} `sql:"context"` 59 | data eh.EventData `sql:"-"` 60 | } 61 | 62 | // NewUUID for mocking in tests 63 | var NewUUID = uuid.New 64 | 65 | // newDBEvent returns a new dbEvent for an event. 66 | func (s *EventStore) newDBEvent(ctx context.Context, event eh.Event) (*AggregateEvent, error) { 67 | ns := eh.NamespaceFromContext(ctx) 68 | 69 | // Marshal event data if there is any. 70 | raw, err := s.encoder.Marshal(event.Data()) 71 | if err != nil { 72 | return nil, eh.EventStoreError{ 73 | BaseErr: err, 74 | Err: ErrCouldNotMarshalEvent, 75 | Namespace: ns, 76 | } 77 | } 78 | 79 | return &AggregateEvent{ 80 | EventID: NewUUID(), 81 | AggregateID: event.AggregateID(), 82 | AggregateType: event.AggregateType(), 83 | EventType: event.EventType(), 84 | RawData: raw, 85 | Timestamp: event.Timestamp(), 86 | Version: event.Version(), 87 | Context: eh.MarshalContext(ctx), 88 | Namespace: ns, 89 | }, nil 90 | } 91 | 92 | // NewEventStore creates a new EventStore. 93 | func NewEventStore(db *pg.DB) (*EventStore, error) { 94 | s := &EventStore{ 95 | db: db, 96 | encoder: &jsonEncoder{}, 97 | } 98 | err := s.CreateTables(&orm.CreateTableOptions{ 99 | IfNotExists: true, 100 | }) 101 | if err != nil { 102 | return nil, err 103 | } 104 | return s, nil 105 | } 106 | 107 | var tables = []interface{}{ 108 | (*AggregateEvent)(nil), 109 | (*AggregateRecord)(nil), 110 | } 111 | 112 | func (s *EventStore) CreateTables(opts *orm.CreateTableOptions) error { 113 | return s.db.RunInTransaction(func(tx *pg.Tx) error { 114 | for _, table := range tables { 115 | if err := s.db.CreateTable(table, opts); err != nil { 116 | return err 117 | } 118 | } 119 | 120 | return nil 121 | }) 122 | } 123 | 124 | // Save implements the Save method of the eventhorizon.EventStore interface. 125 | func (s *EventStore) Save(ctx context.Context, events []eh.Event, originalVersion int) error { 126 | ns := eh.NamespaceFromContext(ctx) 127 | 128 | if len(events) == 0 { 129 | return eh.EventStoreError{ 130 | Err: eh.ErrNoEventsToAppend, 131 | Namespace: ns, 132 | } 133 | } 134 | 135 | // Build all event records, with incrementing versions starting from the 136 | // original aggregate version. 137 | dbEvents := make([]AggregateEvent, len(events)) 138 | aggregateID := events[0].AggregateID() 139 | version := originalVersion 140 | for i, event := range events { 141 | // Only accept events belonging to the same aggregate. 142 | if event.AggregateID() != aggregateID { 143 | return eh.EventStoreError{ 144 | Err: eh.ErrInvalidEvent, 145 | Namespace: ns, 146 | } 147 | } 148 | 149 | // Only accept events that apply to the correct aggregate version. 150 | if event.Version() != version+1 { 151 | return eh.EventStoreError{ 152 | Err: eh.ErrIncorrectEventVersion, 153 | Namespace: ns, 154 | } 155 | } 156 | 157 | // Create the event record for the DB. 158 | e, err := s.newDBEvent(ctx, event) 159 | if err != nil { 160 | return err 161 | } 162 | dbEvents[i] = *e 163 | version++ 164 | } 165 | 166 | // Either insert a new aggregate or append to an existing. 167 | err := s.db.WithParam("namespace", ns). 168 | WithContext(ctx). 169 | RunInTransaction(func(tx *pg.Tx) (err error) { 170 | var result orm.Result 171 | aggregate := AggregateRecord{ 172 | Namespace: ns, 173 | AggregateID: aggregateID, 174 | } 175 | if originalVersion == 0 { 176 | aggregate.Version = len(dbEvents) 177 | result, err = tx.Model(&aggregate). 178 | Insert() 179 | } else { 180 | result, err = tx.Model(&aggregate). 181 | Where("namespace = ?namespace AND version = ? AND aggregate_id = ?", originalVersion, aggregateID). 182 | Set("version = version + ?", len(dbEvents)). 183 | Update() 184 | } 185 | if err != nil { 186 | return err 187 | } 188 | if result.RowsAffected() != 1 { 189 | return ErrVersionConflict 190 | } 191 | if err = tx.Insert(&dbEvents); err != nil { 192 | return err 193 | } 194 | return nil 195 | }) 196 | if err != nil { 197 | return eh.EventStoreError{ 198 | BaseErr: err, 199 | Err: ErrCouldNotSaveAggregate, 200 | Namespace: ns, 201 | } 202 | } 203 | 204 | return nil 205 | } 206 | 207 | // Load implements the Load method of the eventhorizon.EventStore interface. 208 | func (s *EventStore) Load(ctx context.Context, id uuid.UUID) ([]eh.Event, error) { 209 | ns := eh.NamespaceFromContext(ctx) 210 | 211 | var events []eh.Event 212 | err := s.db.WithParam("namespace", ns). 213 | WithContext(ctx). 214 | Model((*AggregateEvent)(nil)). 215 | Where("namespace = ?namespace AND aggregate_id = ?", id). 216 | Order("version ASC"). 217 | ForEach(func(e *AggregateEvent) (err error) { 218 | if e.data, err = s.encoder.Unmarshal(e.EventType, e.RawData); err != nil { 219 | return eh.EventStoreError{ 220 | BaseErr: err, 221 | Err: ErrCouldNotUnmarshalEvent, 222 | Namespace: ns, 223 | } 224 | } 225 | e.RawData = nil 226 | events = append(events, event{ 227 | AggregateEvent: *e, 228 | }) 229 | return nil 230 | }) 231 | if err != nil { 232 | return nil, eh.EventStoreError{ 233 | BaseErr: err, 234 | Err: err, 235 | Namespace: ns, 236 | } 237 | } 238 | 239 | return events, nil 240 | } 241 | 242 | // Replace an event, the version must match. Useful for maintenance actions. 243 | // Returns ErrAggregateNotFound if there is no aggregate. 244 | func (s *EventStore) Replace(ctx context.Context, event eh.Event) error { 245 | ns := eh.NamespaceFromContext(ctx) 246 | 247 | exist, err := s.db.WithParam("namespace", ns). 248 | WithContext(ctx). 249 | Model((*AggregateRecord)(nil)). 250 | Where("namespace = ?namespace"). 251 | Where("aggregate_id = ?", event.AggregateID()). 252 | Exists() 253 | if err != nil { 254 | return eh.EventStoreError{ 255 | BaseErr: err, 256 | Err: err, 257 | Namespace: ns, 258 | } 259 | } else if !exist { 260 | return eh.ErrAggregateNotFound 261 | } 262 | 263 | var eventID uuid.UUID 264 | err = s.db.WithParam("namespace", ns). 265 | WithContext(ctx). 266 | Model((*AggregateEvent)(nil)). 267 | Where("namespace = ?namespace"). 268 | Where("aggregate_id = ? AND version = ?", event.AggregateID(), event.Version()). 269 | Column("event_id"). 270 | Select(&eventID) 271 | if err == nil { 272 | // Create the event record for the DB. 273 | e, err := s.newDBEvent(ctx, event) 274 | if err != nil { 275 | return eh.EventStoreError{ 276 | BaseErr: err, 277 | Err: err, 278 | Namespace: ns, 279 | } 280 | } 281 | e.EventID = eventID 282 | err = s.db.Update(e) 283 | } 284 | if err == pg.ErrNoRows { 285 | return eh.ErrInvalidEvent 286 | } else if err != nil { 287 | return eh.EventStoreError{ 288 | BaseErr: err, 289 | Err: err, 290 | Namespace: ns, 291 | } 292 | } 293 | 294 | return nil 295 | } 296 | 297 | // RenameEvent renames all instances of the event type. 298 | func (s *EventStore) RenameEvent(ctx context.Context, from, to eh.EventType) error { 299 | ns := eh.NamespaceFromContext(ctx) 300 | 301 | _, err := s.db.WithParam("namespace", ns). 302 | WithContext(ctx). 303 | Model((*AggregateEvent)(nil)). 304 | Where("namespace = ?namespace AND event_type = ?", from). 305 | Set("event_type = ?", to). 306 | Update() 307 | if err != nil { 308 | return eh.EventStoreError{ 309 | BaseErr: err, 310 | Err: ErrCouldNotSaveAggregate, 311 | Namespace: ns, 312 | } 313 | } 314 | 315 | return nil 316 | } 317 | 318 | // func (s *EventStore) Close() error { 319 | // return s.db.Close() 320 | // } 321 | 322 | // Clear clears the event storage. 323 | func (s *EventStore) Clear(ctx context.Context) error { 324 | ns := eh.NamespaceFromContext(ctx) 325 | 326 | err := s.db.WithParam("namespace", ns). 327 | WithContext(ctx). 328 | RunInTransaction(func(tx *pg.Tx) (err error) { 329 | _, err = tx.Model((*AggregateRecord)(nil)). 330 | Where("namespace = ?namespace"). 331 | Delete() 332 | if err == nil { 333 | _, err = tx.Model((*AggregateEvent)(nil)). 334 | Where("namespace = ?namespace"). 335 | Delete() 336 | } 337 | return err 338 | }) 339 | if err != nil { 340 | return eh.EventStoreError{ 341 | BaseErr: err, 342 | Err: ErrCouldNotClearDB, 343 | Namespace: ns, 344 | } 345 | } 346 | return nil 347 | } 348 | 349 | // event is the private implementation of the eventhorizon.Event interface 350 | // for a MongoDB event store. 351 | type event struct { 352 | AggregateEvent 353 | } 354 | 355 | // AggrgateID implements the AggrgateID method of the eventhorizon.Event interface. 356 | func (e event) AggregateID() uuid.UUID { 357 | return e.AggregateEvent.AggregateID 358 | } 359 | 360 | // AggregateType implements the AggregateType method of the eventhorizon.Event interface. 361 | func (e event) AggregateType() eh.AggregateType { 362 | return e.AggregateEvent.AggregateType 363 | } 364 | 365 | // EventType implements the EventType method of the eventhorizon.Event interface. 366 | func (e event) EventType() eh.EventType { 367 | return e.AggregateEvent.EventType 368 | } 369 | 370 | // Data implements the Data method of the eventhorizon.Event interface. 371 | func (e event) Data() eh.EventData { 372 | return e.AggregateEvent.data 373 | } 374 | 375 | // Version implements the Version method of the eventhorizon.Event interface. 376 | func (e event) Version() int { 377 | return e.AggregateEvent.Version 378 | } 379 | 380 | // Timestamp implements the Timestamp method of the eventhorizon.Event interface. 381 | func (e event) Timestamp() time.Time { 382 | return e.AggregateEvent.Timestamp 383 | } 384 | 385 | // String implements the String method of the eventhorizon.Event interface. 386 | func (e event) String() string { 387 | return fmt.Sprintf("%s@%d", e.AggregateEvent.EventType, e.AggregateEvent.Version) 388 | } 389 | --------------------------------------------------------------------------------