Skip to content

Commit

Permalink
Merge pull request #572 from t924417424/main
Browse files Browse the repository at this point in the history
fix: The sqlite database file is not updated because the database con…
  • Loading branch information
alimy committed Sep 10, 2024
2 parents a4348f2 + 0a8bb05 commit de7cf35
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/rocboss/paopao-ce/cmd"
"github.com/rocboss/paopao-ce/internal"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/dao"
"github.com/rocboss/paopao-ce/internal/service"
"github.com/rocboss/paopao-ce/pkg/debug"
"github.com/rocboss/paopao-ce/pkg/utils"
Expand Down Expand Up @@ -47,6 +48,7 @@ func init() {
}

func deferFn() {
dao.CloseDsx()
if cfg.If("Sentry") {
// Flush buffered events before the program terminates.
sentry.Flush(2 * time.Second)
Expand Down
8 changes: 8 additions & 0 deletions internal/conf/db_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func MustGormDB() *gorm.DB {
return _gormdb
}

func CloseGormDB() {
db, err := _gormdb.DB()
if err != nil {
log.Fatalf("close gorm db failed: %s", err)
}
_ = db.Close()
}

func newGormDB() (db *gorm.DB, err error) {
newLogger := logger.New(
logrus.StandardLogger(), // io writer(日志输出的目标,前缀和日志包含的内容)
Expand Down
13 changes: 13 additions & 0 deletions internal/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ func initDsX() {
logrus.Infof("use %s as core.ServantA with version %s", dsaVer.Name(), dsaVer.Version())
}

func CloseDsx() {
if cfg.If("Gorm") {
jinzhu.CloseDbObject()
} else if cfg.If("Sqlx") {
sakila.CloseDbObject()
} else if cfg.If("Sqlc") && cfg.Any("Postgres", "PostgreSQL") {
slonik.CloseDbObject()
} else {
// default use gorm as orm for sql database
jinzhu.CloseDbObject()
}
}

func initOSS() {
var v core.VersionInfo
if cfg.If("AliOSS") {
Expand Down
4 changes: 4 additions & 0 deletions internal/dao/jinzhu/jinzhu.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func NewDataService() (core.DataService, core.VersionInfo) {
return cache.NewCacheDataService(ds), ds
}

func CloseDbObject() {
conf.CloseGormDB()
}

func NewWebDataServantA() (core.WebDataServantA, core.VersionInfo) {
lazyInitial()
db := conf.MustGormDB()
Expand Down
4 changes: 4 additions & 0 deletions internal/dao/sakila/sakila.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func NewAuthorizationManageService() core.AuthorizationManageService {
logrus.Fatal("not support now")
return nil
}

func CloseDbObject() {
logrus.Fatal("not support now")
}
4 changes: 4 additions & 0 deletions internal/dao/slonik/slonik.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func NewAuthorizationManageService() core.AuthorizationManageService {
logrus.Fatal("not support now")
return nil
}

func CloseDbObject() {
logrus.Fatal("not support now")
}
2 changes: 1 addition & 1 deletion web/src/views/Post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<n-list class="main-content-wrap" bordered>
<n-list-item>
<n-spin :show="loading">
<div class="detail-wrap" v-if="post.id > 1">
<div class="detail-wrap" v-if="post.id > 0">
<post-detail :post="post" @reload="reloadPost" />
</div>
<div class="empty-wrap" v-else>
Expand Down

0 comments on commit de7cf35

Please sign in to comment.