mirror of
https://github.com/YFGaia/dify-plus.git
synced 2026-06-04 10:14:00 +08:00
feat: - 非权限用户不显示密钥按钮
- 用户id没有自增解决方案
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type GVA_MODEL struct {
|
||||
ID uint `gorm:"primarykey" json:"ID"` // 主键ID
|
||||
ID uint `gorm:"primarykey;autoIncrement" json:"ID"` // 主键ID
|
||||
CreatedAt time.Time // 创建时间
|
||||
UpdatedAt time.Time // 更新时间
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 删除时间
|
||||
|
||||
@@ -35,6 +35,7 @@ func (e *ensureTables) MigrateTable(ctx context.Context) (context.Context, error
|
||||
if !ok {
|
||||
return ctx, system.ErrMissingDBContext
|
||||
}
|
||||
|
||||
tables := []interface{}{
|
||||
sysModel.SysApi{},
|
||||
sysModel.SysUser{},
|
||||
|
||||
@@ -82,6 +82,11 @@ func RegisterTables() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// 如果是PostgreSQL数据库,创建必要的序列
|
||||
if global.GVA_CONFIG.System.DbType == "pgsql" {
|
||||
createPostgreSQLSequences(db)
|
||||
}
|
||||
|
||||
err = bizModel()
|
||||
|
||||
if err != nil {
|
||||
@@ -90,3 +95,74 @@ func RegisterTables() {
|
||||
}
|
||||
global.GVA_LOG.Info("register table success")
|
||||
}
|
||||
|
||||
// createPostgreSQLSequences 为PostgreSQL数据库创建必要的序列
|
||||
func createPostgreSQLSequences(db *gorm.DB) {
|
||||
// 需要创建序列的表列表
|
||||
tables := []string{
|
||||
"sys_users",
|
||||
"sys_apis",
|
||||
"sys_base_menus",
|
||||
"sys_authorities",
|
||||
"sys_dictionaries",
|
||||
"sys_operation_records",
|
||||
"sys_auto_code_histories",
|
||||
"sys_dictionary_details",
|
||||
"sys_base_menu_parameters",
|
||||
"sys_base_menu_btns",
|
||||
"sys_authority_btns",
|
||||
"sys_auto_code_packages",
|
||||
"sys_export_templates",
|
||||
"conditions",
|
||||
"join_templates",
|
||||
"sys_params",
|
||||
"exa_files",
|
||||
"exa_customers",
|
||||
"exa_file_chunks",
|
||||
"exa_file_upload_and_downloads",
|
||||
"account_ding_talk_extends",
|
||||
"app_request_test_batches",
|
||||
"app_request_tests",
|
||||
"system_integrations",
|
||||
"forwarding_extends",
|
||||
"batch_workflows",
|
||||
"batch_workflow_tasks",
|
||||
"sys_user_global_codes",
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
sequenceName := fmt.Sprintf("%s_id_seq", table)
|
||||
|
||||
// 检查序列是否已存在
|
||||
var exists bool
|
||||
checkSQL := "SELECT EXISTS (SELECT 1 FROM pg_sequences WHERE sequencename = ?)"
|
||||
if err := db.Raw(checkSQL, sequenceName).Scan(&exists).Error; err != nil {
|
||||
log.Printf("检查序列 %s 是否存在时出错: %v", sequenceName, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !exists {
|
||||
// 创建序列
|
||||
createSQL := fmt.Sprintf("CREATE SEQUENCE IF NOT EXISTS %s START 1 INCREMENT 1", sequenceName)
|
||||
if err := db.Exec(createSQL).Error; err != nil {
|
||||
log.Printf("创建序列 %s 时出错: %v", sequenceName, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 将序列设置为表的默认值
|
||||
alterSQL := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN id SET DEFAULT nextval('%s')", table, sequenceName)
|
||||
if err := db.Exec(alterSQL).Error; err != nil {
|
||||
log.Printf("设置表 %s 的ID默认值时出错: %v", table, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 更新序列的当前值(如果表中已有数据)
|
||||
updateSQL := fmt.Sprintf("SELECT setval('%s', COALESCE((SELECT MAX(id) FROM %s), 1), true)", sequenceName, table)
|
||||
if err := db.Exec(updateSQL).Error; err != nil {
|
||||
log.Printf("更新序列 %s 的当前值时出错: %v", sequenceName, err)
|
||||
}
|
||||
|
||||
log.Printf("成功为表 %s 创建序列 %s", table, sequenceName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ func GormPgSql() *gorm.DB {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(p.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(p.MaxOpenConns)
|
||||
|
||||
// 为PostgreSQL创建必要的序列
|
||||
createPostgreSQLSequences(db)
|
||||
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user