From c531e0047447de0af74bee5a302fd2506ea71262 Mon Sep 17 00:00:00 2001 From: OldCat <924417424@qq.com> Date: Mon, 9 Sep 2024 11:48:42 +0800 Subject: [PATCH 1/2] fix: The sqlite database file is not updated because the database connection is not closed when the program exits. --- cmd/serve/serve.go | 2 ++ internal/conf/db_gorm.go | 8 ++++++++ internal/dao/dao.go | 13 +++++++++++++ internal/dao/jinzhu/jinzhu.go | 4 ++++ internal/dao/sakila/sakila.go | 4 ++++ internal/dao/slonik/slonik.go | 4 ++++ 6 files changed, 35 insertions(+) diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index b7dcbff12..3b7916451 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -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" @@ -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) diff --git a/internal/conf/db_gorm.go b/internal/conf/db_gorm.go index 66095f226..dfa381cfd 100644 --- a/internal/conf/db_gorm.go +++ b/internal/conf/db_gorm.go @@ -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(日志输出的目标,前缀和日志包含的内容) diff --git a/internal/dao/dao.go b/internal/dao/dao.go index 8b355e364..9cbb7051c 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -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") { diff --git a/internal/dao/jinzhu/jinzhu.go b/internal/dao/jinzhu/jinzhu.go index 16b6d64a7..ddc9cd2f2 100644 --- a/internal/dao/jinzhu/jinzhu.go +++ b/internal/dao/jinzhu/jinzhu.go @@ -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() diff --git a/internal/dao/sakila/sakila.go b/internal/dao/sakila/sakila.go index 0a605bf8c..69029e318 100644 --- a/internal/dao/sakila/sakila.go +++ b/internal/dao/sakila/sakila.go @@ -26,3 +26,7 @@ func NewAuthorizationManageService() core.AuthorizationManageService { logrus.Fatal("not support now") return nil } + +func CloseDbObject() { + logrus.Fatal("not support now") +} diff --git a/internal/dao/slonik/slonik.go b/internal/dao/slonik/slonik.go index 705c5bbf9..34fd0ae0d 100644 --- a/internal/dao/slonik/slonik.go +++ b/internal/dao/slonik/slonik.go @@ -26,3 +26,7 @@ func NewAuthorizationManageService() core.AuthorizationManageService { logrus.Fatal("not support now") return nil } + +func CloseDbObject() { + logrus.Fatal("not support now") +} From 0a8bb0512a852a047e623c9200d8b60e274e335c Mon Sep 17 00:00:00 2001 From: OldCat <924417424@qq.com> Date: Mon, 9 Sep 2024 13:46:36 +0800 Subject: [PATCH 2/2] fix:The first post shows bugs with no data yet --- web/src/views/Post.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/src/views/Post.vue b/web/src/views/Post.vue index 2170f7baa..1811a4fce 100644 --- a/web/src/views/Post.vue +++ b/web/src/views/Post.vue @@ -5,7 +5,7 @@ -
+