From 543603da026e88f8455f17567e0e6db9040d50b0 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Thu, 18 Nov 2021 23:13:17 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20-=20pgsql=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84bug=20-=20pgsql=20=E8=8E=B7=E5=8F=96=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E6=97=B6=20=E6=8A=A5=E9=94=99=20-=20SysOpera?= =?UTF-8?q?tionRecord=20longtext=20=E6=94=B9=E4=B8=BA=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/model/system/sys_operation_record.go | 4 +-- server/service/system/sys_authority.go | 4 +-- server/service/system/sys_initdb.go | 26 +++++++++++++- server/service/system/sys_initdb_mysql.go | 24 +------------ server/service/system/sys_initdb_pgsql.go | 26 +++----------- .../source/example/{file.go => file_mysql.go} | 10 +++--- server/source/example/file_pgsql.go | 34 +++++++++++++++++++ .../system/view_authority_menu_postgres.go | 6 ++-- 8 files changed, 76 insertions(+), 58 deletions(-) rename server/source/example/{file.go => file_mysql.go} (82%) create mode 100644 server/source/example/file_pgsql.go diff --git a/server/model/system/sys_operation_record.go b/server/model/system/sys_operation_record.go index 43bf7569..bd316bb4 100644 --- a/server/model/system/sys_operation_record.go +++ b/server/model/system/sys_operation_record.go @@ -17,8 +17,8 @@ type SysOperationRecord struct { Latency time.Duration `json:"latency" form:"latency" gorm:"column:latency;comment:延迟" swaggertype:"string"` // 延迟 Agent string `json:"agent" form:"agent" gorm:"column:agent;comment:代理"` // 代理 ErrorMessage string `json:"error_message" form:"error_message" gorm:"column:error_message;comment:错误信息"` // 错误信息 - Body string `json:"body" form:"body" gorm:"type:longtext;column:body;comment:请求Body"` // 请求Body - Resp string `json:"resp" form:"resp" gorm:"type:longtext;column:resp;comment:响应Body"` // 响应Body + Body string `json:"body" form:"body" gorm:"type:text;column:body;comment:请求Body"` // 请求Body + Resp string `json:"resp" form:"resp" gorm:"type:text;column:resp;comment:响应Body"` // 响应Body UserID int `json:"user_id" form:"user_id" gorm:"column:user_id;comment:用户id"` // 用户id User SysUser `json:"user"` } diff --git a/server/service/system/sys_authority.go b/server/service/system/sys_authority.go index d9925c7d..a55070d7 100644 --- a/server/service/system/sys_authority.go +++ b/server/service/system/sys_authority.go @@ -122,9 +122,9 @@ func (authorityService *AuthorityService) GetAuthorityInfoList(info request.Page limit := info.PageSize offset := info.PageSize * (info.Page - 1) db := global.GVA_DB.Model(&system.SysAuthority{}) - err = db.Where("parent_id = 0").Count(&total).Error + err = db.Where("parent_id = ?", "0").Count(&total).Error var authority []system.SysAuthority - err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error + err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = ?", "0").Find(&authority).Error if len(authority) > 0 { for k := range authority { err = authorityService.findChildrenAuthority(&authority[k]) diff --git a/server/service/system/sys_initdb.go b/server/service/system/sys_initdb.go index 23bcf249..37d0f239 100644 --- a/server/service/system/sys_initdb.go +++ b/server/service/system/sys_initdb.go @@ -1,6 +1,8 @@ package system import ( + "database/sql" + "fmt" adapter "github.com/casbin/gorm-adapter/v3" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/example" @@ -26,7 +28,7 @@ func (initDBService *InitDBService) InitDB(conf request.InitDB) error { } // initTables 初始化表 -// Author SliverHorn +// Author [SliverHorn](https://github.com/SliverHorn) func (initDBService *InitDBService) initTables() error { return global.GVA_DB.AutoMigrate( system.SysApi{}, @@ -49,3 +51,25 @@ func (initDBService *InitDBService) initTables() error { ) } + +// createDatabase 创建数据库(mysql) +// Author [SliverHorn](https://github.com/SliverHorn) +// Author: [songzhibin97](https://github.com/songzhibin97) + +func (initDBService *InitDBService) createDatabase(dsn string, driver string, createSql string) error { + db, err := sql.Open(driver, dsn) + if err != nil { + return err + } + defer func(db *sql.DB) { + err = db.Close() + if err != nil { + fmt.Println(err) + } + }(db) + if err = db.Ping(); err != nil { + return err + } + _, err = db.Exec(createSql) + return err +} diff --git a/server/service/system/sys_initdb_mysql.go b/server/service/system/sys_initdb_mysql.go index a3a4b4a9..b55eb859 100644 --- a/server/service/system/sys_initdb_mysql.go +++ b/server/service/system/sys_initdb_mysql.go @@ -1,7 +1,6 @@ package system import ( - "database/sql" "fmt" "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" @@ -29,27 +28,6 @@ func (initDBService *InitDBService) writeMysqlConfig(mysql config.Mysql) error { return global.GVA_VP.WriteConfig() } -// createDatabase 创建数据库(mysql) -// Author [SliverHorn](https://github.com/SliverHorn) -// Author: [songzhibin97](https://github.com/songzhibin97) -func (initDBService *InitDBService) createDatabase(dsn string, driver string, createSql string) error { - db, err := sql.Open(driver, dsn) - if err != nil { - return err - } - defer func(db *sql.DB) { - err = db.Close() - if err != nil { - fmt.Println(err) - } - }(db) - if err = db.Ping(); err != nil { - return err - } - _, err = db.Exec(createSql) - return err -} - // initMsqlDB 创建数据库并初始化 mysql // Author [piexlmax](https://github.com/piexlmax) // Author [SliverHorn](https://github.com/SliverHorn) @@ -110,6 +88,6 @@ func (initDBService *InitDBService) initMysqlData() error { system.AuthoritiesMenus, system.DictionaryDetail, system.ViewAuthorityMenuMysql, - example.File, + example.FileMysql, ) } diff --git a/server/service/system/sys_initdb_pgsql.go b/server/service/system/sys_initdb_pgsql.go index 8f2d4bc9..605f362e 100644 --- a/server/service/system/sys_initdb_pgsql.go +++ b/server/service/system/sys_initdb_pgsql.go @@ -1,7 +1,6 @@ package system import ( - "context" "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" model "github.com/flipped-aurora/gin-vue-admin/server/model/system" @@ -9,7 +8,6 @@ import ( "github.com/flipped-aurora/gin-vue-admin/server/source/example" "github.com/flipped-aurora/gin-vue-admin/server/source/system" "github.com/flipped-aurora/gin-vue-admin/server/utils" - "github.com/jackc/pgx/v4" uuid "github.com/satori/go.uuid" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -19,6 +17,7 @@ import ( // writePgsqlConfig pgsql 回写配置 // Author [SliverHorn](https://github.com/SliverHorn) func (initDBService *InitDBService) writePgsqlConfig(pgsql config.Pgsql) error { + global.GVA_CONFIG.System.DbType = "pgsql" global.GVA_CONFIG.Pgsql = pgsql cs := utils.StructToMap(global.GVA_CONFIG) for k, v := range cs { @@ -28,27 +27,10 @@ func (initDBService *InitDBService) writePgsqlConfig(pgsql config.Pgsql) error { return global.GVA_VP.WriteConfig() } -// createPgsqlDatabase 根据页面传递的数据库名 创建数据库 -// Author [SliverHorn](https://github.com/SliverHorn) -func (initDBService *InitDBService) createPgsqlDatabase(dsn string, dbName string) error { - ctx := context.Background() - _sql := "CREATE DATABASE " + dbName - db, err := pgx.Connect(ctx, dsn) - if err != nil { - return err - } - defer func() { - _ = db.Close(ctx) - }() - if err = db.Ping(ctx); err != nil { - return err - } - _, err = db.Exec(ctx, _sql) - return err -} func (initDBService *InitDBService) initPgsqlDB(conf request.InitDB) error { dsn := conf.PgsqlEmptyDsn() - if err := initDBService.createPgsqlDatabase(dsn, conf.DBName); err != nil { + createSql := "CREATE DATABASE " + conf.DBName + if err := initDBService.createDatabase(dsn, "pgx", createSql); err != nil { return err } // 创建数据库 @@ -100,6 +82,6 @@ func (initDBService *InitDBService) initPgsqlData() error { system.DictionaryDetail, system.ViewAuthorityMenuPostgres, - example.File, + example.FilePgsql, ) } diff --git a/server/source/example/file.go b/server/source/example/file_mysql.go similarity index 82% rename from server/source/example/file.go rename to server/source/example/file_mysql.go index cb1a3c60..a4ab4284 100644 --- a/server/source/example/file.go +++ b/server/source/example/file_mysql.go @@ -7,15 +7,15 @@ import ( "gorm.io/gorm" ) -var File = new(file) +var FileMysql = new(fileMysql) -type file struct{} +type fileMysql struct{} -func (f *file) TableName() string { +func (f *fileMysql) TableName() string { return "exa_file_upload_and_downloads" } -func (f *file) Initialize() error { +func (f *fileMysql) Initialize() error { entities := []example.ExaFileUploadAndDownload{ {Name: "10.png", Url: "https://qmplusimg.henrongyi.top/gvalogo.png", Tag: "png", Key: "158787308910.png"}, {Name: "logo.png", Url: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"}, @@ -26,7 +26,7 @@ func (f *file) Initialize() error { return nil } -func (f *file) CheckDataExist() bool { +func (f *fileMysql) CheckDataExist() bool { if errors.Is(global.GVA_DB.Where("`name` = ? AND `key` = ?", "logo.png", "1587973709logo.png").First(&example.ExaFileUploadAndDownload{}).Error, gorm.ErrRecordNotFound) { return false } diff --git a/server/source/example/file_pgsql.go b/server/source/example/file_pgsql.go new file mode 100644 index 00000000..cfe1b87c --- /dev/null +++ b/server/source/example/file_pgsql.go @@ -0,0 +1,34 @@ +package example + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/example" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var FilePgsql = new(fileMysql) + +type filePgsql struct{} + +func (f *filePgsql) TableName() string { + return "exa_file_upload_and_downloads" +} + +func (f *filePgsql) Initialize() error { + entities := []example.ExaFileUploadAndDownload{ + {Name: "10.png", Url: "https://qmplusimg.henrongyi.top/gvalogo.png", Tag: "png", Key: "158787308910.png"}, + {Name: "logo.png", Url: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, f.TableName()+"表数据初始化失败!") + } + return nil +} + +func (f *filePgsql) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("name = ? AND key = ?", "logo.png", "1587973709logo.png").First(&example.ExaFileUploadAndDownload{}).Error, gorm.ErrRecordNotFound) { + return false + } + return true +} diff --git a/server/source/system/view_authority_menu_postgres.go b/server/source/system/view_authority_menu_postgres.go index 43b47ca3..4776a4d8 100644 --- a/server/source/system/view_authority_menu_postgres.go +++ b/server/source/system/view_authority_menu_postgres.go @@ -36,9 +36,9 @@ func (a *viewAuthorityMenuPostgres) Initialize() error { @menus.deleted_at as deleted_at, @menus.menu_level as menu_level, @menus.default_menu as default_menu, - @authorities_menus.menu_id as menu_id, - @authorities_menus.authority_id as authority_id - from (@authorities_menus join @menus on ((@authorities_menus.menu_id = @menus.id)));` + @authorities_menus.sys_base_menu_id as menu_id, + @authorities_menus.sys_authority_authority_id as authority_id + from (@authorities_menus join @menus on ((@authorities_menus.sys_base_menu_id = @menus.id)));` sql = strings.ReplaceAll(sql, "@table_name", a.TableName()) sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus") sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName())