-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#40): add database skeleton and entities
- Loading branch information
1 parent
0752723
commit 7cda85c
Showing
12 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
openapi: 3.1.0 | ||
openapi: 3.0.4 | ||
info: | ||
title: Room Service | ||
description: |- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package entity | ||
|
||
import "time" | ||
|
||
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package entity | ||
|
||
// Group is a group of attendees that wish to be assigned to a Room together. | ||
type Group struct { | ||
Base | ||
|
||
// Name is the name of the group | ||
Name string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;uniqueIndex:room_group_name_uidx"` | ||
|
||
// Flags is a comma-separated list of flags, with both leading and trailing comma. The allowed flags are configuration dependent | ||
Flags string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"` | ||
|
||
// Comments are optional, not processed in any way | ||
Comments string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" testdiff:"ignore"` | ||
|
||
// MaximumSize defaults to a value from service configuration, but we store it here so admins can increase it manually for some groups | ||
MaximumSize uint | ||
|
||
// Owner is the badge number (attendee ID) of the attendee owning the group. Ownership can be passed to another attendee. | ||
Owner uint | ||
} | ||
|
||
// GroupMember associates attendees to a group, either as a member or as an invited member. | ||
type GroupMember struct { | ||
Member | ||
|
||
// 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"` | ||
|
||
// IsInvite is true if the member has been invited, or false if the member has already joined | ||
IsInvite bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package entity | ||
|
||
import "gorm.io/gorm" | ||
|
||
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 | ||
|
||
// Nickname caches the nickname of the attendee | ||
Nickname string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL"` | ||
|
||
// AvatarURL caches the url to obtain the avatar for this attendee, points to an image such as a png or jpg | ||
AvatarURL string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"` | ||
|
||
// Flags is a comma-separated list of flags such as "has_key", with a leading and trailing comma | ||
Flags string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package entity | ||
|
||
type Room struct { | ||
Base | ||
|
||
// Name is the name of the room | ||
Name string `gorm:"type:varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL;uniqueIndex:room_room_name_uidx"` | ||
|
||
// Flags is a comma-separated list of flags, with both leading and trailing comma. The allowed flags are configuration dependent | ||
Flags string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"` | ||
|
||
// Comments are optional, not processed in any way | ||
Comments string `gorm:"type:varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" testdiff:"ignore"` | ||
|
||
// Size is the size of the room | ||
Size uint | ||
} | ||
|
||
type RoomMember struct { | ||
Member | ||
|
||
// 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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package dbrepo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package database | ||
|
||
import "context" | ||
|
||
type Repository interface { | ||
Open(ctx context.Context) error | ||
Close(ctx context.Context) | ||
Migrate(ctx context.Context) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package mysqldb | ||
|
||
import ( | ||
"context" | ||
aulogging "github.com/StephanHCB/go-autumn-logging" | ||
"github.com/eurofurence/reg-room-service/internal/entity" | ||
"github.com/eurofurence/reg-room-service/internal/repository/database" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
"time" | ||
) | ||
|
||
type MysqlRepository struct { | ||
db *gorm.DB | ||
connectString string | ||
Now func() time.Time | ||
} | ||
|
||
func Create(connectString string) database.Repository { | ||
return &MysqlRepository{ | ||
Now: time.Now, | ||
connectString: connectString, | ||
} | ||
} | ||
|
||
func (r *MysqlRepository) Open(ctx context.Context) error { | ||
gormConfig := gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: "room_", | ||
}, | ||
Logger: logger.Default.LogMode(logger.Silent), | ||
} | ||
|
||
db, err := gorm.Open(mysql.Open(r.connectString), &gormConfig) | ||
if err != nil { | ||
aulogging.ErrorErrf(ctx, err, "failed to open mysql connection: %s", err.Error()) | ||
return err | ||
} | ||
|
||
sqlDb, err := db.DB() | ||
if err != nil { | ||
aulogging.ErrorErrf(ctx, err, "failed to configure mysql connection: %s", err.Error()) | ||
return err | ||
} | ||
|
||
// see https://making.pusher.com/production-ready-connection-pooling-in-go/ | ||
sqlDb.SetMaxOpenConns(100) | ||
sqlDb.SetMaxIdleConns(50) | ||
sqlDb.SetConnMaxLifetime(time.Minute * 10) | ||
|
||
r.db = db | ||
return nil | ||
} | ||
|
||
func (r *MysqlRepository) Close(_ context.Context) { | ||
// no more db close in gorm v2 | ||
} | ||
|
||
func (r *MysqlRepository) Migrate(ctx context.Context) error { | ||
err := r.db.AutoMigrate( | ||
&entity.Member{}, | ||
&entity.Group{}, | ||
&entity.Room{}, | ||
) | ||
if err != nil { | ||
aulogging.Logger.NoCtx().Error().WithErr(err).Printf("failed to migrate mysql db: %s", err.Error()) | ||
return err | ||
} | ||
return nil | ||
} |