Skip to content

Commit

Permalink
gorm (#380)
Browse files Browse the repository at this point in the history
* gorm

* types.Mod

* wip

* log entity

* chatlog

* metric

* oauth entity

* mesecons entity

* cleanup

* go mod tidy

---------

Co-authored-by: BuckarooBanzay <[email protected]>
  • Loading branch information
BuckarooBanzay and BuckarooBanzay authored Oct 15, 2024
1 parent 15b0a63 commit 7f53146
Show file tree
Hide file tree
Showing 36 changed files with 440 additions and 749 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/server"
"github.com/minetest-go/mtdb"
"gorm.io/gorm"
)

var Version string

type App struct {
DBContext *mtdb.Context
DB *sql.DB
G *gorm.DB
WorldDir string
Repos *db.Repositories
ModManager *modmanager.ModManager
Expand Down
5 changes: 3 additions & 2 deletions app/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ func (a *App) AttachDatabase() error {
}
a.DBContext = dbctx

db_, err := db.Init(a.WorldDir)
db_, g, err := db.Init(a.WorldDir)
if err != nil {
return err
}
a.DB = db_
a.G = g

err = db.Migrate(db_)
if err != nil {
return err
}

a.Repos = db.NewRepositories(db_)
a.Repos = db.NewRepositories(g)
a.Mail = mail.New(dbctx)
a.ModManager = modmanager.New(a.WorldDir, a.Repos.ModRepo)

Expand Down
2 changes: 1 addition & 1 deletion db/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestBackupSqlite3(t *testing.T) {
tmpdir, err := os.MkdirTemp(os.TempDir(), "mtui")
assert.NoError(t, err)

db_, err := db.Init(tmpdir)
db_, _, err := db.Init(tmpdir)
assert.NoError(t, err)
assert.NotNil(t, db_)

Expand Down
16 changes: 10 additions & 6 deletions db/chat_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"time"

"github.com/google/uuid"
"github.com/minetest-go/dbutil"
"gorm.io/gorm"
)

type ChatLogRepository struct {
dbu *dbutil.DBUtil[*types.ChatLog]
g *gorm.DB
}

func (r *ChatLogRepository) Insert(l *types.ChatLog) error {
Expand All @@ -21,17 +21,21 @@ func (r *ChatLogRepository) Insert(l *types.ChatLog) error {
l.Timestamp = time.Now().UnixMilli()
}

return r.dbu.Insert(l)
return r.g.Create(l).Error
}

func (r *ChatLogRepository) Search(channel string, from, to int64) ([]*types.ChatLog, error) {
return r.dbu.SelectMulti("where channel = %s and timestamp > %s and timestamp < %s order by timestamp asc limit 1000", channel, from, to)
var list []*types.ChatLog
err := r.g.Where("timestamp > ?", from).Where("timestamp < ?", to).Where(types.ChatLog{Channel: channel}).Find(&list).Error
return list, err
}

func (r *ChatLogRepository) GetLatest(channel string, limit int) ([]*types.ChatLog, error) {
return r.dbu.SelectMulti("where channel = %s order by timestamp asc limit %s", channel, limit)
var list []*types.ChatLog
err := r.g.Where(types.ChatLog{Channel: channel}).Order("timestamp ASC").Limit(limit).Find(&list).Error
return list, err
}

func (r *ChatLogRepository) DeleteBefore(timestamp int64) error {
return r.dbu.Delete("where timestamp < %s", timestamp)
return r.g.Where("timestamp < ?", timestamp).Delete(types.ChatLog{}).Error
}
4 changes: 2 additions & 2 deletions db/chat_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestChatLogRepo(t *testing.T) {
_db := setupDB(t)
repo := db.NewRepositories(_db).ChatLogRepo
_, g := setupDB(t)
repo := db.NewRepositories(g).ChatLogRepo

assert.NoError(t, repo.Insert(&types.ChatLog{Timestamp: 100, Channel: "main", Name: "player1", Message: "msg1"}))
assert.NoError(t, repo.Insert(&types.ChatLog{Timestamp: 200, Channel: "main", Name: "player2", Message: "msg2"}))
Expand Down
24 changes: 14 additions & 10 deletions db/config.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package db

import (
"database/sql"
"mtui/types"

"github.com/minetest-go/dbutil"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type ConfigRepository struct {
dbu *dbutil.DBUtil[*types.ConfigEntry]
g *gorm.DB
}

func (r *ConfigRepository) GetByKey(key types.ConfigKey) (*types.ConfigEntry, error) {
c, err := r.dbu.Select("where key = %s", key)
if err == sql.ErrNoRows {
return nil, nil
var list []*types.ConfigEntry
err := r.g.Where(types.ConfigEntry{Key: key}).Limit(1).Find(&list).Error
if len(list) == 0 {
return nil, err
}
return c, err
return list[0], err
}

func (r *ConfigRepository) Set(c *types.ConfigEntry) error {
return r.dbu.InsertOrReplace(c)
return r.g.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "key"}},
UpdateAll: true,
}).Create(c).Error
}

func (r *ConfigRepository) Delete(key string) error {
return r.dbu.Delete("where key = %s", key)
func (r *ConfigRepository) Delete(key types.ConfigKey) error {
return r.g.Delete(types.ConfigEntry{Key: key}).Error
}
4 changes: 2 additions & 2 deletions db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestConfig(t *testing.T) {
DB := setupDB(t)
repo := db.NewRepositories(DB).ConfigRepo
_, g := setupDB(t)
repo := db.NewRepositories(g).ConfigRepo

// create
assert.NoError(t, repo.Set(&types.ConfigEntry{Key: "x", Value: "y"}))
Expand Down
25 changes: 15 additions & 10 deletions db/feature.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package db

import (
"database/sql"
"mtui/types"

"github.com/minetest-go/dbutil"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type FeatureRepository struct {
dbu *dbutil.DBUtil[*types.Feature]
g *gorm.DB
}

func (r *FeatureRepository) Set(m *types.Feature) error {
return r.dbu.InsertOrReplace(m)
return r.g.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
UpdateAll: true,
}).Create(m).Error
}

func (r *FeatureRepository) GetAll() ([]*types.Feature, error) {
return r.dbu.SelectMulti("")
var list []*types.Feature
err := r.g.Find(&list).Error
return list, err
}

func (r *FeatureRepository) GetByName(name string) (*types.Feature, error) {
f, err := r.dbu.Select("where name = %s", name)
if err == sql.ErrNoRows {
return nil, nil
} else {
return f, err
var list []*types.Feature
err := r.g.Where(types.Feature{Name: name}).Limit(1).Find(&list).Error
if len(list) == 0 {
return nil, err
}
return list[0], err
}
5 changes: 3 additions & 2 deletions db/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

func TestFeatureRepo(t *testing.T) {
_db := setupDB(t)
repo := db.NewRepositories(_db).FeatureRepository
_, g := setupDB(t)
repo := db.NewRepositories(g).FeatureRepository

// create
assert.NoError(t, repo.Set(&types.Feature{Name: "f1", Enabled: true}))
assert.NoError(t, repo.Set(&types.Feature{Name: "f2", Enabled: false}))
Expand Down
20 changes: 15 additions & 5 deletions db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ package db

import (
"database/sql"
"fmt"
"path"

_ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func Init(world_dir string) (*sql.DB, error) {
func Init(world_dir string) (*sql.DB, *gorm.DB, error) {

connStr := "mtui.sqlite?_timeout=5000&_journal=WAL&_cache=shared"
var err error
db, err := sql.Open("sqlite3", path.Join(world_dir, "mtui.sqlite?_timeout=5000&_journal=WAL&_cache=shared"))
db, err := sql.Open("sqlite3", path.Join(world_dir, connStr))
if err != nil {
return nil, err
return nil, nil, fmt.Errorf("open error: %v", err)
}

err = db.Ping()
if err != nil {
return nil, err
return nil, nil, fmt.Errorf("ping error: %v", err)
}

g, err := gorm.Open(sqlite.Open(path.Join(world_dir, connStr)))
if err != nil {
return nil, nil, fmt.Errorf("gorm.Open error: %v", err)
}

return db, nil
return db, g, nil
}
Loading

0 comments on commit 7f53146

Please sign in to comment.