mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-01-01 12:16:37 +08:00
refactor: oauth2登录重构
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
entity2 "mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
entity3 "mayfly-go/internal/mongo/domain/entity"
|
||||
@@ -10,6 +8,9 @@ import (
|
||||
entity4 "mayfly-go/internal/redis/domain/entity"
|
||||
entity5 "mayfly-go/internal/sys/domain/entity"
|
||||
entity7 "mayfly-go/internal/tag/domain/entity"
|
||||
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// T2022 TODO 在此之前的数据库表结构初始化, 目前先使用mayfly-go.sql文件初始化数据库结构
|
||||
|
||||
@@ -1,77 +1,18 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
authentity "mayfly-go/internal/auth/domain/entity"
|
||||
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
"mayfly-go/internal/sys/api"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
// T20230720 三方登录表
|
||||
func T20230720() *gormigrate.Migration {
|
||||
return &gormigrate.Migration{
|
||||
ID: "20230319",
|
||||
ID: "20230720",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
// 添加路由权限
|
||||
res := &entity.Resource{
|
||||
Model: model.Model{
|
||||
DeletedModel: model.DeletedModel{Id: 133},
|
||||
},
|
||||
Pid: 4,
|
||||
UiPath: "sys/auth",
|
||||
Type: 1,
|
||||
Status: 1,
|
||||
Code: "system:auth",
|
||||
Name: "登录认证",
|
||||
Weight: 10000001,
|
||||
Meta: "{\"component\":\"system/auth/AuthInfo\"," +
|
||||
"\"icon\":\"User\",\"isKeepAlive\":true," +
|
||||
"\"routeName\":\"AuthInfo\"}",
|
||||
}
|
||||
if err := insertResource(tx, res); err != nil {
|
||||
return err
|
||||
}
|
||||
res = &entity.Resource{
|
||||
Model: model.Model{
|
||||
DeletedModel: model.DeletedModel{Id: 134},
|
||||
},
|
||||
Pid: 133,
|
||||
UiPath: "sys/auth/base",
|
||||
Type: 2,
|
||||
Status: 1,
|
||||
Code: "system:auth:base",
|
||||
Name: "基本权限",
|
||||
Weight: 10000000,
|
||||
Meta: "null",
|
||||
}
|
||||
if err := insertResource(tx, res); err != nil {
|
||||
return err
|
||||
}
|
||||
// 加大params字段长度
|
||||
now := time.Now()
|
||||
if err := tx.AutoMigrate(&entity.Config{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(&entity.Config{
|
||||
Model: model.Model{
|
||||
CreateTime: &now,
|
||||
CreatorId: 1,
|
||||
Creator: "admin",
|
||||
UpdateTime: &now,
|
||||
ModifierId: 1,
|
||||
Modifier: "admin",
|
||||
},
|
||||
Name: api.AuthOAuth2Name,
|
||||
Key: api.AuthOAuth2Key,
|
||||
Params: api.AuthOAuth2Param,
|
||||
Value: "{}",
|
||||
Remark: api.AuthOAuth2Remark,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.AutoMigrate(&entity.OAuthAccount{})
|
||||
return tx.AutoMigrate(&authentity.Oauth2Account{})
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return nil
|
||||
|
||||
@@ -1,29 +1,33 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/config"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/rediscli"
|
||||
"time"
|
||||
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RunMigrations 数据库迁移操作
|
||||
func RunMigrations(db *gorm.DB) error {
|
||||
// 添加分布式锁, 防止多个服务同时执行迁移
|
||||
if rediscli.GetCli() != nil {
|
||||
if ok, err := rediscli.GetCli().
|
||||
SetNX(context.Background(), "migrations", "lock", time.Minute).Result(); err != nil {
|
||||
return err
|
||||
} else if !ok {
|
||||
lock := rediscli.NewLock("mayfly:db:migrations", 1*time.Minute)
|
||||
if lock != nil {
|
||||
if !lock.Lock() {
|
||||
return nil
|
||||
}
|
||||
defer rediscli.Del("migrations")
|
||||
defer lock.UnLock()
|
||||
}
|
||||
|
||||
if !config.Conf.Mysql.AutoMigration {
|
||||
return nil
|
||||
}
|
||||
|
||||
return run(db,
|
||||
T2022,
|
||||
// T2022,
|
||||
T20230720,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user