Skip to content

Commit

Permalink
feat(#40): implement inmem-db and historization (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumpy-Squirrel authored Oct 26, 2023
1 parent 66a0131 commit 8d967ea
Show file tree
Hide file tree
Showing 13 changed files with 892 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ reg-room-service
!cmd/**
target
**/*.swp
config.yaml
config*.yaml
*.http
**/*.jar
api-generator
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.21
require (
github.com/StephanHCB/go-autumn-logging v0.3.0
github.com/StephanHCB/go-autumn-logging-zerolog v0.5.0
github.com/d4l3k/messagediff v1.2.1
github.com/go-chi/chi/v5 v5.0.10
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.3.1
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/StephanHCB/go-autumn-logging-zerolog v0.5.0/go.mod h1:Hspu94dHAKtgjMA
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/d4l3k/messagediff v1.2.1 h1:ZcAIMYsUg0EAp9X+tt8/enBE/Q8Yd5kzPynLyKptt9U=
github.com/d4l3k/messagediff v1.2.1/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
Expand All @@ -14,6 +16,8 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down
8 changes: 6 additions & 2 deletions internal/entity/base.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package entity

import "time"
import (
"time"

"gorm.io/gorm"
)

type Base struct {
ID string `gorm:"primaryKey; type:varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
2 changes: 2 additions & 0 deletions internal/entity/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Group struct {
type GroupMember struct {
Member

// TODO references to get integrity check!

// GroupID references the group to which the member belongs (or has been invited)
GroupID string `gorm:"type:varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;index:room_group_member_grpid"`

Expand Down
3 changes: 2 additions & 1 deletion internal/entity/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import "gorm.io/gorm"
type History struct {
gorm.Model
Entity string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;index:att_histories_entity_idx"` // the name (type) of the entity
EntityId uint `gorm:"NOT NULL;index:att_histories_entity_idx"` // the pk of the entity
EntityId string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;index:att_histories_entity_idx"` // the pk of the entity
Operation string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL"` // update / delete / undelete
RequestId string `gorm:"type:varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"` // optional request id that triggered the change
Identity string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;index:att_histories_identity_idx"` // the subject that triggered the change
Diff string `gorm:"type:text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"`
Expand Down
14 changes: 10 additions & 4 deletions internal/entity/member.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package entity

import "gorm.io/gorm"
import (
"time"
)

type Member struct {
// This contains ID = the badge number of the attendee (an attendee can only either be in a
// group or invited, and can only ever be in one room at the same time.
gorm.Model
// ID contains the badge number of the attendee (an attendee can only either be in a
// group or invited, and can only ever be in one room at the same time).
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time

// intentionally not supplying DeletedAt -- don't want soft delete

// Nickname caches the nickname of the attendee
Nickname string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL"`
Expand Down
2 changes: 2 additions & 0 deletions internal/entity/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Room struct {
type RoomMember struct {
Member

// TODO references to get integrity check!

// RoomID references the room to which the attendee belongs
RoomID string `gorm:"type:varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;index:room_room_member_roomid"`
}
56 changes: 56 additions & 0 deletions internal/repository/database/dbrepo/constructors.go
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
package dbrepo

import (
"context"
"strings"

aulogging "github.com/StephanHCB/go-autumn-logging"

"github.com/eurofurence/reg-room-service/internal/repository/database"
"github.com/eurofurence/reg-room-service/internal/repository/database/historizeddb"
"github.com/eurofurence/reg-room-service/internal/repository/database/inmemorydb"
"github.com/eurofurence/reg-room-service/internal/repository/database/mysqldb"
)

var activeRepository database.Repository

func SetRepository(repository database.Repository) {
activeRepository = repository
}

func GetRepository() database.Repository {
if activeRepository == nil {
aulogging.Logger.NoCtx().Error().Print("You must Open() the database before using it. This is an error in your implementation.")
panic("You must Open() the database before using it. This is an error in your implementation.")
}
return activeRepository
}

func Open(ctx context.Context, variant string, mysqlConnectString string) error {
var r database.Repository
if variant == "mysql" {
aulogging.Info(ctx, "Opening mysql database...")
r = historizeddb.New(mysqldb.New(mysqlConnectString))
} else {
aulogging.Warn(ctx, "Opening inmemory database (not useful for production!)...")
r = historizeddb.New(inmemorydb.New())
}
err := r.Open(ctx)
SetRepository(r)
return err
}

func Close(ctx context.Context) {
aulogging.Info(ctx, "Closing database...")
GetRepository().Close(ctx)
SetRepository(nil)
}

func Migrate(ctx context.Context) error {
aulogging.Info(ctx, "Migrating database...")
return GetRepository().Migrate(ctx)
}

func MysqlConnectString(username string, password string, databaseName string, parameters []string) string {
return username + ":" + password + "@" +
databaseName + "?" + strings.Join(parameters, "&")
}
Loading

0 comments on commit 8d967ea

Please sign in to comment.