mirror of
https://github.com/YFGaia/dify-plus.git
synced 2026-06-04 10:14:00 +08:00
fix: admin初始化有误修复
This commit is contained in:
@@ -5,9 +5,8 @@ import (
|
|||||||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
|
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
|
||||||
"go.uber.org/zap"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DBApi struct{}
|
type DBApi struct{}
|
||||||
@@ -20,7 +19,8 @@ type DBApi struct{}
|
|||||||
// @Success 200 {object} response.Response{data=string} "初始化用户数据库"
|
// @Success 200 {object} response.Response{data=string} "初始化用户数据库"
|
||||||
// @Router /init/initdb [post]
|
// @Router /init/initdb [post]
|
||||||
func (i *DBApi) InitDB(c *gin.Context) {
|
func (i *DBApi) InitDB(c *gin.Context) {
|
||||||
if global.GVA_DB != nil {
|
|
||||||
|
if !initDBService.IfInit() {
|
||||||
global.GVA_LOG.Error("已存在数据库配置!")
|
global.GVA_LOG.Error("已存在数据库配置!")
|
||||||
response.FailWithMessage("已存在数据库配置", c)
|
response.FailWithMessage("已存在数据库配置", c)
|
||||||
return
|
return
|
||||||
@@ -51,12 +51,11 @@ func (i *DBApi) InitDB(c *gin.Context) {
|
|||||||
// @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "初始化用户数据库"
|
// @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "初始化用户数据库"
|
||||||
// @Router /init/checkdb [post]
|
// @Router /init/checkdb [post]
|
||||||
func (i *DBApi) CheckDB(c *gin.Context) {
|
func (i *DBApi) CheckDB(c *gin.Context) {
|
||||||
var (
|
// init
|
||||||
message = "前往初始化数据库"
|
var needInit = true
|
||||||
needInit = true
|
var message = "前往初始化数据库"
|
||||||
)
|
|
||||||
|
|
||||||
if global.GVA_DB != nil {
|
if initDBService.IfInit() {
|
||||||
message = "数据库无需初始化"
|
message = "数据库无需初始化"
|
||||||
needInit = false
|
needInit = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ pgsql:
|
|||||||
db-name: dify
|
db-name: dify
|
||||||
username: postgres
|
username: postgres
|
||||||
password: difyai123456
|
password: difyai123456
|
||||||
path: 127.0.0.1
|
path: 127.0.0.01
|
||||||
engine: ""
|
engine: ""
|
||||||
log-mode: error
|
log-mode: error
|
||||||
max-idle-conns: 10
|
max-idle-conns: 10
|
||||||
|
|||||||
@@ -81,8 +81,58 @@ func Gorm() *gorm.DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RegisterTables(db *gorm.DB) {
|
func RegisterTables(db *gorm.DB) {
|
||||||
|
var err error
|
||||||
|
var count int64
|
||||||
|
var menu system.SysBaseMenuBtn
|
||||||
|
var authority system.SysAuthority
|
||||||
|
if err = global.GVA_DB.Model(&menu).Count(&count).Error; count == 0 {
|
||||||
|
if err = global.GVA_DB.Model(&authority).Count(&count).Error; count == 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// auto
|
||||||
|
err = db.AutoMigrate(
|
||||||
|
system.SysApi{},
|
||||||
|
system.SysIgnoreApi{},
|
||||||
|
system.SysUser{},
|
||||||
|
system.SysBaseMenu{},
|
||||||
|
system.JwtBlacklist{},
|
||||||
|
system.SysAuthority{},
|
||||||
|
system.SysDictionary{},
|
||||||
|
system.SysOperationRecord{},
|
||||||
|
system.SysAutoCodeHistory{},
|
||||||
|
system.SysDictionaryDetail{},
|
||||||
|
system.SysBaseMenuParameter{},
|
||||||
|
system.SysBaseMenuBtn{},
|
||||||
|
system.SysAuthorityBtn{},
|
||||||
|
system.SysAutoCodePackage{},
|
||||||
|
system.SysExportTemplate{},
|
||||||
|
system.Condition{},
|
||||||
|
system.JoinTemplate{},
|
||||||
|
system.SysParams{},
|
||||||
|
|
||||||
err := db.AutoMigrate(tables)
|
example.ExaFile{},
|
||||||
|
example.ExaCustomer{},
|
||||||
|
example.ExaFileChunk{},
|
||||||
|
example.ExaFileUploadAndDownload{},
|
||||||
|
|
||||||
|
adapter.CasbinRule{},
|
||||||
|
|
||||||
|
// Extend gaia model
|
||||||
|
gaia.AccountDingTalkExtend{},
|
||||||
|
gaia.AppRequestTestBatch{},
|
||||||
|
gaia.AppRequestTest{},
|
||||||
|
gaia.SystemIntegration{}, // Extend System Integration
|
||||||
|
gaia.ForwardingExtend{}, // Extend Forwarding Extend
|
||||||
|
gaia.BatchWorkflow{}, // Extend Batch Workflow
|
||||||
|
gaia.BatchWorkflowTask{}, // Extend Batch Workflow Task
|
||||||
|
gaia.AppVersionConfig{}, // 应用版本全局配置(Token)
|
||||||
|
gaia.AppVersionRelease{}, // 应用版本发布
|
||||||
|
gaia.AppVersionDownload{}, // 应用版本各平台安装包
|
||||||
|
gaia.ModelProviderConfig{}, // 模型提供商配置
|
||||||
|
gaia.ModelProxyLog{}, // 模型中转请求日志
|
||||||
|
system.SysUserGlobalCode{}, // Extend Global Code
|
||||||
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("register table failed", zap.Error(err))
|
global.GVA_LOG.Error("register table failed", zap.Error(err))
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||||
|
modelSystem "github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -86,6 +87,22 @@ func RegisterInit(order int, i SubInitializer) {
|
|||||||
|
|
||||||
type InitDBService struct{}
|
type InitDBService struct{}
|
||||||
|
|
||||||
|
// IfInit 判断是否数据库初始化了
|
||||||
|
func (initDBService *InitDBService) IfInit() (init bool) {
|
||||||
|
var menuCount, authorityCount int64
|
||||||
|
if global.GVA_DB != nil {
|
||||||
|
init = true
|
||||||
|
var menu modelSystem.SysBaseMenuBtn
|
||||||
|
var authority modelSystem.SysAuthority
|
||||||
|
global.GVA_DB.Model(&menu).Count(&menuCount)
|
||||||
|
global.GVA_DB.Model(&authority).Count(&authorityCount)
|
||||||
|
if menuCount == 0 && authorityCount == 1 {
|
||||||
|
init = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return init
|
||||||
|
}
|
||||||
|
|
||||||
// InitDB 创建数据库并初始化 总入口
|
// InitDB 创建数据库并初始化 总入口
|
||||||
func (initDBService *InitDBService) InitDB(conf request.InitDB) (err error) {
|
func (initDBService *InitDBService) InitDB(conf request.InitDB) (err error) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
@@ -122,7 +139,8 @@ func (initDBService *InitDBService) InitDB(conf request.InitDB) (err error) {
|
|||||||
|
|
||||||
db := ctx.Value("db").(*gorm.DB)
|
db := ctx.Value("db").(*gorm.DB)
|
||||||
global.GVA_DB = db
|
global.GVA_DB = db
|
||||||
|
db.Exec("DELETE FROM sys_base_menus")
|
||||||
|
db.Exec("DELETE FROM sys_authorities")
|
||||||
if err = initHandler.InitTables(ctx, initializers); err != nil {
|
if err = initHandler.InitTables(ctx, initializers); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ from models.account import (
|
|||||||
)
|
)
|
||||||
from models.account_money_extend import AccountMoneyExtend
|
from models.account_money_extend import AccountMoneyExtend
|
||||||
from models.model import DifySetup
|
from models.model import DifySetup
|
||||||
|
from services.account_service_extend import TenantExtendService
|
||||||
from services.billing_service import BillingService
|
from services.billing_service import BillingService
|
||||||
from services.errors.account import (
|
from services.errors.account import (
|
||||||
AccountAlreadyInTenantError,
|
AccountAlreadyInTenantError,
|
||||||
@@ -1313,6 +1314,19 @@ class RegisterService:
|
|||||||
|
|
||||||
TenantService.create_owner_tenant_if_not_exist(account=account, is_setup=True)
|
TenantService.create_owner_tenant_if_not_exist(account=account, is_setup=True)
|
||||||
|
|
||||||
|
# extend begin: admin 初始化不同步问题 - 在 dify 初始化完成后,自动调用 admin 初始化
|
||||||
|
# 将 setup 用户加入到第一个工作区(admin 租户),确保后续用户信息同步时权限正确
|
||||||
|
tenant_extend_service = TenantExtendService
|
||||||
|
super_admin_id = tenant_extend_service.get_super_admin_id().id
|
||||||
|
super_admin_tenant_id = tenant_extend_service.get_super_admin_tenant_id().id
|
||||||
|
if super_admin_id and super_admin_tenant_id:
|
||||||
|
is_create = TenantExtendService.create_default_tenant_member_if_not_exist(
|
||||||
|
super_admin_tenant_id, account.id
|
||||||
|
)
|
||||||
|
if is_create:
|
||||||
|
TenantService.switch_tenant(account, super_admin_tenant_id)
|
||||||
|
# extend end: admin 初始化不同步问题
|
||||||
|
|
||||||
dify_setup = DifySetup(version=dify_config.project.version)
|
dify_setup = DifySetup(version=dify_config.project.version)
|
||||||
db.session.add(dify_setup)
|
db.session.add(dify_setup)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|||||||
@@ -1679,7 +1679,7 @@ services:
|
|||||||
|
|
||||||
# Extend - admin-server
|
# Extend - admin-server
|
||||||
admin-server:
|
admin-server:
|
||||||
image: ccr.ccs.tencentyun.com/yfgaia/dify-plus-admin-server:1.12.1
|
image: ccr.ccs.tencentyun.com/yfgaia/dify-plus-admin-server:1.12.1.fex.1
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
# JWT signing key must match API's SECRET_KEY for token compatibility
|
# JWT signing key must match API's SECRET_KEY for token compatibility
|
||||||
|
|||||||
Reference in New Issue
Block a user