Skip to content

Commit

Permalink
db: replace upperdb with gorm (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 authored Dec 3, 2022
1 parent f46b213 commit 99214e8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 88 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2.4.0
uses: golangci/golangci-lint-action@v3
with:
version: v1.37.0
args: --timeout=30m
version: v1.49.0
args: --timeout=10m
18 changes: 0 additions & 18 deletions .github/workflows/lsif.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
.idea/
config.json
data/

Houki
houki.db
17 changes: 8 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ require (
github.com/flamego/flamego v0.0.0-20210821060740-3c58a415258e
github.com/flamego/session v0.0.0-20210607182212-8d30fdff82f2
github.com/flamego/template v0.0.0-20210515150544-049eb6703243
github.com/flamego/validator v0.0.0-20210821065223-7cb80dd2ce7a
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/glebarez/sqlite v1.5.0
github.com/golang-migrate/migrate/v4 v4.14.1
github.com/google/cel-go v0.7.2
github.com/json-iterator/go v1.1.9
github.com/lib/pq v1.8.0
github.com/mattn/go-sqlite3 v1.14.8 // indirect
github.com/lib/pq v1.10.4
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.3.0
github.com/wuhan005/gadget v0.0.0-20210723152124-0fc7e8810653
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gorm.io/gorm v1.24.2
unknwon.dev/clog/v2 v2.2.0
upper.io/db.v3 v3.8.0+incompatible
)

require (
golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70 // indirect
golang.org/x/text v0.3.7 // indirect
)
93 changes: 71 additions & 22 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package context

import (
"encoding/json"
"fmt"
"net/http"

"github.com/flamego/flamego"
jsoniter "github.com/json-iterator/go"
log "unknwon.dev/clog/v2"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ func (c *Context) Success(data ...interface{}) {
d = ""
}

err := jsoniter.NewEncoder(c.ResponseWriter()).Encode(
err := json.NewEncoder(c.ResponseWriter()).Encode(
map[string]interface{}{
"error": 0,
"data": d,
Expand All @@ -65,7 +65,7 @@ func (c *Context) Error(errorCode uint, message string, v ...interface{}) {
message = fmt.Sprintf(message, v...)
}

err := jsoniter.NewEncoder(c.ResponseWriter()).Encode(
err := json.NewEncoder(c.ResponseWriter()).Encode(
map[string]interface{}{
"error": errorCode,
"msg": message,
Expand Down
19 changes: 10 additions & 9 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
package db

import (
"database/sql"

"github.com/glebarez/sqlite"
"github.com/pkg/errors"
"upper.io/db.v3/lib/sqlbuilder"
"upper.io/db.v3/sqlite"
"gorm.io/gorm"

"github.com/wuhan005/Houki/assets/migrations"
"github.com/wuhan005/Houki/internal/dbutil"
)

var Modules ModulesStore

func New() (sqlbuilder.Database, error) {
db, err := sqlite.Open(sqlite.ConnectionURL{
Database: "houki.db",
})
func New() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("houki.db"))
if err != nil {
return nil, errors.Wrap(err, "open database")
}

_, err = dbutil.Migrate(db.Driver().(*sql.DB), migrations.Migrations)
sqlDatabase, err := db.DB()
if err != nil {
return nil, errors.Wrap(err, "get sql database")
}

_, err = dbutil.Migrate(sqlDatabase, migrations.Migrations)
if err != nil {
return nil, errors.Wrap(err, "migrate")
}
Expand Down
38 changes: 14 additions & 24 deletions internal/db/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"time"

"github.com/pkg/errors"
dbv3 "upper.io/db.v3"
"upper.io/db.v3/lib/sqlbuilder"
"gorm.io/gorm"

"github.com/wuhan005/Houki/internal/dbutil"
"github.com/wuhan005/Houki/internal/module"
Expand All @@ -27,7 +26,7 @@ type ModulesStore interface {
Delete(ctx context.Context, id string) error
}

func NewModulesStore(db sqlbuilder.Database) ModulesStore {
func NewModulesStore(db *gorm.DB) ModulesStore {
return &modules{db}
}

Expand All @@ -44,7 +43,7 @@ func (m *Module) IsEnabled() bool {
}

type modules struct {
sqlbuilder.Database
*gorm.DB
}

type GetModuleOptions struct {
Expand All @@ -53,20 +52,20 @@ type GetModuleOptions struct {

func (db *modules) List(ctx context.Context, opts GetModuleOptions) ([]*Module, error) {
var modules []*Module
q := db.WithContext(ctx).SelectFrom("modules")
q := db.WithContext(ctx).Model(&Module{})
if opts.EnabledOnly {
q = q.Where("enabled = TRUE")
}
return modules, q.All(&modules)
return modules, q.Find(&modules).Error
}

var ErrModuleNotFound = errors.New("module does not found")

func (db *modules) Get(ctx context.Context, id string) (*Module, error) {
var module Module
err := db.WithContext(ctx).SelectFrom("modules").Where("id = ?", id).One(&module)
err := db.WithContext(ctx).Model(&Module{}).Where("id = ?", id).First(&module).Error
if err != nil {
if err == dbv3.ErrNoMoreRows {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrModuleNotFound
}
return nil, err
Expand All @@ -82,10 +81,10 @@ type CreateModuleOptions struct {
var ErrModuleExists = errors.New("module has already been created")

func (db *modules) Create(ctx context.Context, opts CreateModuleOptions) error {
_, err := db.WithContext(ctx).InsertInto("modules").
Columns("id", "body").
Values(opts.ID, opts.Body).
Exec()
err := db.WithContext(ctx).Model(&Module{}).Create(&Module{
ID: opts.ID,
Body: opts.Body,
}).Error
if dbutil.IsUniqueViolation(err, "idx_module_id") {
return ErrModuleExists
}
Expand All @@ -102,11 +101,7 @@ func (db *modules) Update(ctx context.Context, id string, opts UpdateModuleOptio
return err
}

_, err = db.WithContext(ctx).
Update("modules").
Set("body", opts.Body).
Where("id = ?", id).Exec()
return err
return db.WithContext(ctx).Model(&Module{}).Where("id = ?", id).Update("body", opts.Body).Error
}

func (db *modules) SetStatus(ctx context.Context, id string, enabled bool) error {
Expand All @@ -115,11 +110,7 @@ func (db *modules) SetStatus(ctx context.Context, id string, enabled bool) error
return err
}

_, err = db.WithContext(ctx).
Update("modules").
Set("enabled", enabled).
Where("id = ?", id).Exec()
return err
return db.WithContext(ctx).Model(&Module{}).Where("id = ?", id).Update("enabled", enabled).Error
}

func (db *modules) Delete(ctx context.Context, id string) error {
Expand All @@ -128,6 +119,5 @@ func (db *modules) Delete(ctx context.Context, id string) error {
return err
}

_, err = db.WithContext(ctx).DeleteFrom("modules").Where("id = ?", id).Exec()
return err
return db.WithContext(ctx).Model(&Module{}).Delete("id = ?", id).Error
}

0 comments on commit 99214e8

Please sign in to comment.