From 62bc9247f121c9ac2ece17de6c57a7df2bc33522 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Mon, 22 Apr 2024 17:11:42 +0800 Subject: [PATCH 01/15] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/options/options.go | 45 +++--------- config.yaml | 39 ++++++----- pkg/controller/cluster/cluster.go | 9 +-- pkg/controller/controller.go | 6 +- pkg/controller/tenant/tenant.go | 6 +- pkg/controller/user/user.go | 6 +- pkg/db/dbconn/conn.go | 5 ++ pkg/db/factory.go | 40 +++++------ pkg/db/iface/cluster.go | 33 +++++++++ pkg/db/iface/factory.go | 7 ++ pkg/db/iface/tenant.go | 32 +++++++++ pkg/db/iface/user.go | 34 +++++++++ pkg/db/{ => mysql}/cluster.go | 47 +++---------- pkg/db/mysql/conn.go | 42 +++++++++++ pkg/db/mysql/dao.go | 27 +++++++ pkg/db/{ => mysql}/migrator.go | 3 +- pkg/db/{ => mysql}/tenant.go | 42 ++--------- pkg/db/{ => mysql}/user.go | 44 ++---------- pkg/db/sqlite/cluster.go | 113 ++++++++++++++++++++++++++++++ pkg/db/sqlite/conn.go | 37 ++++++++++ pkg/db/sqlite/dao.go | 26 +++++++ pkg/db/sqlite/migrator.go | 48 +++++++++++++ pkg/db/sqlite/tenant.go | 93 ++++++++++++++++++++++++ pkg/db/sqlite/user.go | 81 +++++++++++++++++++++ pkg/db/tx_func/tx_func.go | 3 + pkg/types/constant.go | 6 ++ 26 files changed, 672 insertions(+), 202 deletions(-) create mode 100644 pkg/db/dbconn/conn.go create mode 100644 pkg/db/iface/cluster.go create mode 100644 pkg/db/iface/factory.go create mode 100644 pkg/db/iface/tenant.go create mode 100644 pkg/db/iface/user.go rename pkg/db/{ => mysql}/cluster.go (67%) create mode 100644 pkg/db/mysql/conn.go create mode 100644 pkg/db/mysql/dao.go rename pkg/db/{ => mysql}/migrator.go (98%) rename pkg/db/{ => mysql}/tenant.go (66%) rename pkg/db/{ => mysql}/user.go (63%) create mode 100644 pkg/db/sqlite/cluster.go create mode 100644 pkg/db/sqlite/conn.go create mode 100644 pkg/db/sqlite/dao.go create mode 100644 pkg/db/sqlite/migrator.go create mode 100644 pkg/db/sqlite/tenant.go create mode 100644 pkg/db/sqlite/user.go create mode 100644 pkg/db/tx_func/tx_func.go create mode 100644 pkg/types/constant.go diff --git a/cmd/app/options/options.go b/cmd/app/options/options.go index 1172443c..0b5b0895 100644 --- a/cmd/app/options/options.go +++ b/cmd/app/options/options.go @@ -17,27 +17,21 @@ limitations under the License. package options import ( - "fmt" + "github.com/caoyingjunz/pixiu/pkg/db/iface" "os" - pixiuConfig "github.com/caoyingjunz/pixiulib/config" - "github.com/gin-gonic/gin" - "github.com/spf13/cobra" - "gorm.io/driver/mysql" - "gorm.io/gorm" - "gorm.io/gorm/logger" - "github.com/caoyingjunz/pixiu/cmd/app/config" "github.com/caoyingjunz/pixiu/pkg/controller" "github.com/caoyingjunz/pixiu/pkg/db" + pixiuConfig "github.com/caoyingjunz/pixiulib/config" + "github.com/gin-gonic/gin" + "github.com/spf13/cobra" ) const ( - maxIdleConns = 10 - maxOpenConns = 100 - defaultListen = 8080 defaultTokenKey = "pixiu" + //defaultConfigFile = "./config.yaml" defaultConfigFile = "/etc/pixiu/config.yaml" ) @@ -48,7 +42,7 @@ type Options struct { HttpEngine *gin.Engine // 数据库接口 - Factory db.ShareDaoFactory + Factory iface.ShareDaoFactory // 貔貅主控制接口 Controller controller.PixiuInterface @@ -114,32 +108,9 @@ func (o *Options) register() error { } func (o *Options) registerDatabase() error { - sqlConfig := o.ComponentConfig.Mysql - dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", - sqlConfig.User, - sqlConfig.Password, - sqlConfig.Host, - sqlConfig.Port, - sqlConfig.Name) - - opt := &gorm.Config{} - if o.ComponentConfig.Default.Mode == "debug" { - opt.Logger = logger.Default.LogMode(logger.Info) - } - - DB, err := gorm.Open(mysql.Open(dsn), opt) - if err != nil { - return err - } - // 设置数据库连接池 - sqlDB, err := DB.DB() - if err != nil { - return err - } - sqlDB.SetMaxIdleConns(maxIdleConns) - sqlDB.SetMaxOpenConns(maxOpenConns) - o.Factory, err = db.NewDaoFactory(DB, o.ComponentConfig.Default.AutoMigrate) + var err error + o.Factory, err = db.NewDaoFactory(&o.ComponentConfig.Db, o.ComponentConfig.Default.Mode, o.ComponentConfig.Default.AutoMigrate) if err != nil { return err } diff --git a/config.yaml b/config.yaml index 338608f1..802c0cdc 100644 --- a/config.yaml +++ b/config.yaml @@ -1,17 +1,22 @@ -default: - # 运行模式,可选 debug 和 release - mode: debug - # 服务监听端口 - listen: 8090 - # jwt 签名的 key - jwt_key: pixiu - # 自动创建指定模型的数据库表结构,不会更新已存在的数据库表 - auto_migrate: true - -# 数据库地址信息 -mysql: - host: peng - user: root - password: Pixiu868686 - port: 3306 - name: pixiu +default: + # 运行模式,可选 debug 和 release + mode: debug + # 服务监听端口 + listen: 8090 + # jwt 签名的 key + jwt_key: pixiu + # 自动创建指定模型的数据库表结构,不会更新已存在的数据库表 + auto_migrate: true + +db: + type: sqlite + + # 数据库地址信息 +# mysql: +# host: peng +# user: root +# password: Pixiu868686 +# port: 3306 +# name: pixiu + sqlite: + db: pixiu.db \ No newline at end of file diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index bc49157d..fd0fe50a 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -19,6 +19,8 @@ package cluster import ( "context" "fmt" + "github.com/caoyingjunz/pixiu/pkg/db/iface" + "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "net/http" "regexp" "strconv" @@ -41,7 +43,6 @@ import ( "github.com/caoyingjunz/pixiu/api/server/errors" "github.com/caoyingjunz/pixiu/cmd/app/config" "github.com/caoyingjunz/pixiu/pkg/client" - "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/types" "github.com/caoyingjunz/pixiu/pkg/util/uuid" @@ -86,7 +87,7 @@ func init() { type cluster struct { cc config.Config - factory db.ShareDaoFactory + factory iface.ShareDaoFactory } func (c *cluster) preCreate(ctx context.Context, req *types.CreateClusterRequest) error { @@ -107,7 +108,7 @@ func (c *cluster) Create(ctx context.Context, req *types.CreateClusterRequest) e } var cs *client.ClusterSet - var txFunc db.TxFunc = func() (err error) { + var txFunc tx_func.TxFunc = func() (err error) { cs, err = client.NewClusterSet(req.KubeConfig) return err } @@ -634,7 +635,7 @@ func (c *cluster) model2Type(o *model.Cluster) *types.Cluster { return tc } -func NewCluster(cfg config.Config, f db.ShareDaoFactory) *cluster { +func NewCluster(cfg config.Config, f iface.ShareDaoFactory) *cluster { return &cluster{ cc: cfg, factory: f, diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index cf1ddc9b..8598ad37 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -21,7 +21,7 @@ import ( "github.com/caoyingjunz/pixiu/pkg/controller/cluster" "github.com/caoyingjunz/pixiu/pkg/controller/tenant" "github.com/caoyingjunz/pixiu/pkg/controller/user" - "github.com/caoyingjunz/pixiu/pkg/db" + "github.com/caoyingjunz/pixiu/pkg/db/iface" ) type PixiuInterface interface { @@ -32,14 +32,14 @@ type PixiuInterface interface { type pixiu struct { cc config.Config - factory db.ShareDaoFactory + factory iface.ShareDaoFactory } func (p *pixiu) Cluster() cluster.Interface { return cluster.NewCluster(p.cc, p.factory) } func (p *pixiu) Tenant() tenant.Interface { return tenant.NewTenant(p.cc, p.factory) } func (p *pixiu) User() user.Interface { return user.NewUser(p.cc, p.factory) } -func New(cfg config.Config, f db.ShareDaoFactory) PixiuInterface { +func New(cfg config.Config, f iface.ShareDaoFactory) PixiuInterface { return &pixiu{ cc: cfg, factory: f, diff --git a/pkg/controller/tenant/tenant.go b/pkg/controller/tenant/tenant.go index 5c77d91d..7fbb4793 100644 --- a/pkg/controller/tenant/tenant.go +++ b/pkg/controller/tenant/tenant.go @@ -18,12 +18,12 @@ package tenant import ( "context" + "github.com/caoyingjunz/pixiu/pkg/db/iface" "k8s.io/klog/v2" "github.com/caoyingjunz/pixiu/api/server/errors" "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/types" ) @@ -42,7 +42,7 @@ type Interface interface { type tenant struct { cc config.Config - factory db.ShareDaoFactory + factory iface.ShareDaoFactory } func (t *tenant) Create(ctx context.Context, req *types.CreateTenantRequest) error { @@ -147,7 +147,7 @@ func (t *tenant) model2Type(o *model.Tenant) *types.Tenant { } } -func NewTenant(cfg config.Config, f db.ShareDaoFactory) *tenant { +func NewTenant(cfg config.Config, f iface.ShareDaoFactory) *tenant { return &tenant{ cc: cfg, factory: f, diff --git a/pkg/controller/user/user.go b/pkg/controller/user/user.go index e288d9da..d4495797 100644 --- a/pkg/controller/user/user.go +++ b/pkg/controller/user/user.go @@ -19,12 +19,12 @@ package user import ( "context" "fmt" + "github.com/caoyingjunz/pixiu/pkg/db/iface" "k8s.io/klog/v2" "github.com/caoyingjunz/pixiu/api/server/errors" "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/types" "github.com/caoyingjunz/pixiu/pkg/util" @@ -51,7 +51,7 @@ type Interface interface { type user struct { cc config.Config - factory db.ShareDaoFactory + factory iface.ShareDaoFactory } func (u *user) Create(ctx context.Context, req *types.CreateUserRequest) error { @@ -191,7 +191,7 @@ func model2Type(o *model.User) *types.User { } } -func NewUser(cfg config.Config, f db.ShareDaoFactory) *user { +func NewUser(cfg config.Config, f iface.ShareDaoFactory) *user { return &user{ cc: cfg, factory: f, diff --git a/pkg/db/dbconn/conn.go b/pkg/db/dbconn/conn.go new file mode 100644 index 00000000..051713b8 --- /dev/null +++ b/pkg/db/dbconn/conn.go @@ -0,0 +1,5 @@ +package dbconn + +type DbConn struct { + Conn interface{} +} diff --git a/pkg/db/factory.go b/pkg/db/factory.go index cbff0edb..42775a29 100644 --- a/pkg/db/factory.go +++ b/pkg/db/factory.go @@ -17,32 +17,26 @@ limitations under the License. package db import ( + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db/dbconn" + "github.com/caoyingjunz/pixiu/pkg/db/iface" + + "github.com/caoyingjunz/pixiu/pkg/db/mysql" + "github.com/caoyingjunz/pixiu/pkg/db/sqlite" "gorm.io/gorm" ) -type ShareDaoFactory interface { - Cluster() ClusterInterface - Tenant() TenantInterface - User() UserInterface -} - -type shareDaoFactory struct { - db *gorm.DB -} - -func (f *shareDaoFactory) Cluster() ClusterInterface { return newCluster(f.db) } -func (f *shareDaoFactory) Tenant() TenantInterface { return newTenant(f.db) } -func (f *shareDaoFactory) User() UserInterface { return newUser(f.db) } - -func NewDaoFactory(db *gorm.DB, migrate bool) (ShareDaoFactory, error) { - if migrate { - // 自动创建指定模型的数据库表结构 - if err := newMigrator(db).AutoMigrate(); err != nil { - return nil, err - } +func NewDaoFactory(dbConfig *config.DbConfig, mode string, migrate bool) (iface.ShareDaoFactory, error) { + var db *dbconn.DbConn + var err error + switch dbConfig.Type { + case "mysql": + db, err = mysql.NewDb(dbConfig.Mysql, mode, migrate) + return mysql.New(db.Conn.(*gorm.DB)) + case "sqlite": + db, err = sqlite.NewDb(dbConfig.Sqlite, mode, migrate) + return sqlite.New(db.Conn.(*gorm.DB)) } - return &shareDaoFactory{ - db: db, - }, nil + return nil, err } diff --git a/pkg/db/iface/cluster.go b/pkg/db/iface/cluster.go new file mode 100644 index 00000000..ca9c8b67 --- /dev/null +++ b/pkg/db/iface/cluster.go @@ -0,0 +1,33 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package iface + +import ( + "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/db/tx_func" +) + +type ClusterInterface interface { + Create(ctx context.Context, object *model.Cluster, fns ...tx_func.TxFunc) (*model.Cluster, error) + Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error + Delete(ctx context.Context, cid int64) (*model.Cluster, error) + Get(ctx context.Context, cid int64) (*model.Cluster, error) + List(ctx context.Context) ([]model.Cluster, error) + + GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) +} diff --git a/pkg/db/iface/factory.go b/pkg/db/iface/factory.go new file mode 100644 index 00000000..957cf9bf --- /dev/null +++ b/pkg/db/iface/factory.go @@ -0,0 +1,7 @@ +package iface + +type ShareDaoFactory interface { + Cluster() ClusterInterface + Tenant() TenantInterface + User() UserInterface +} diff --git a/pkg/db/iface/tenant.go b/pkg/db/iface/tenant.go new file mode 100644 index 00000000..4928d665 --- /dev/null +++ b/pkg/db/iface/tenant.go @@ -0,0 +1,32 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package iface + +import ( + "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" +) + +type TenantInterface interface { + Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) + Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error + Delete(ctx context.Context, cid int64) (*model.Tenant, error) + Get(ctx context.Context, cid int64) (*model.Tenant, error) + List(ctx context.Context) ([]model.Tenant, error) + + GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) +} diff --git a/pkg/db/iface/user.go b/pkg/db/iface/user.go new file mode 100644 index 00000000..00f575a3 --- /dev/null +++ b/pkg/db/iface/user.go @@ -0,0 +1,34 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package iface + +import ( + "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" +) + +type UserInterface interface { + Create(ctx context.Context, object *model.User) (*model.User, error) + Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error + Delete(ctx context.Context, uid int64) error + Get(ctx context.Context, uid int64) (*model.User, error) + List(ctx context.Context) ([]model.User, error) + + Count(ctx context.Context) (int64, error) + + GetUserByName(ctx context.Context, userName string) (*model.User, error) +} diff --git a/pkg/db/cluster.go b/pkg/db/mysql/cluster.go similarity index 67% rename from pkg/db/cluster.go rename to pkg/db/mysql/cluster.go index 4e34ab53..a8ff158c 100644 --- a/pkg/db/cluster.go +++ b/pkg/db/mysql/cluster.go @@ -1,49 +1,24 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package db +package mysql import ( "context" "fmt" - "time" - - "gorm.io/gorm" - "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "github.com/caoyingjunz/pixiu/pkg/util/errors" + "gorm.io/gorm" + "time" ) -type TxFunc func() error - -type ClusterInterface interface { - Create(ctx context.Context, object *model.Cluster, fns ...TxFunc) (*model.Cluster, error) - Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error - Delete(ctx context.Context, cid int64) (*model.Cluster, error) - Get(ctx context.Context, cid int64) (*model.Cluster, error) - List(ctx context.Context) ([]model.Cluster, error) - - GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) -} - type cluster struct { db *gorm.DB } -func (c *cluster) Create(ctx context.Context, object *model.Cluster, fns ...TxFunc) (*model.Cluster, error) { +func newCluster(db *gorm.DB) *cluster { + return &cluster{db: db} +} + +func (c *cluster) Create(ctx context.Context, object *model.Cluster, fns ...tx_func.TxFunc) (*model.Cluster, error) { now := time.Now() object.GmtCreate = now object.GmtModified = now @@ -136,7 +111,3 @@ func (c *cluster) GetClusterByName(ctx context.Context, name string) (*model.Clu return &object, nil } - -func newCluster(db *gorm.DB) ClusterInterface { - return &cluster{db} -} diff --git a/pkg/db/mysql/conn.go b/pkg/db/mysql/conn.go new file mode 100644 index 00000000..c429c6eb --- /dev/null +++ b/pkg/db/mysql/conn.go @@ -0,0 +1,42 @@ +package mysql + +import ( + "fmt" + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db/dbconn" + "github.com/caoyingjunz/pixiu/pkg/types" + mysqlDriver "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func NewDb(sqlConfig config.MysqlOptions, mode string, migrate bool) (*dbconn.DbConn, error) { + + opt := &gorm.Config{} + if mode == mode { + opt.Logger = logger.Default.LogMode(logger.Info) + } + + dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", + sqlConfig.User, + sqlConfig.Password, + sqlConfig.Host, + sqlConfig.Port, + sqlConfig.Name) + DB, err := gorm.Open(mysqlDriver.Open(dsn), opt) + sqlDB, err := DB.DB() + if err != nil { + return nil, err + } + sqlDB.SetMaxIdleConns(types.MaxIdleConns) + sqlDB.SetMaxOpenConns(types.MaxOpenConns) + if migrate { + if err := newMigrator(DB).AutoMigrate(); err != nil { + return nil, err + } + } + + return &dbconn.DbConn{ + Conn: DB, + }, nil +} diff --git a/pkg/db/mysql/dao.go b/pkg/db/mysql/dao.go new file mode 100644 index 00000000..42de3a9e --- /dev/null +++ b/pkg/db/mysql/dao.go @@ -0,0 +1,27 @@ +package mysql + +import ( + "github.com/caoyingjunz/pixiu/pkg/db/iface" + "gorm.io/gorm" +) + +type mysql struct { + db *gorm.DB +} + +func (m *mysql) Cluster() iface.ClusterInterface { + return newCluster(m.db) +} + +func (m *mysql) Tenant() iface.TenantInterface { + return newTenant(m.db) +} + +func (m *mysql) User() iface.UserInterface { + return newUser(m.db) +} + +func New(db *gorm.DB) (iface.ShareDaoFactory, error) { + + return &mysql{db: db}, nil +} diff --git a/pkg/db/migrator.go b/pkg/db/mysql/migrator.go similarity index 98% rename from pkg/db/migrator.go rename to pkg/db/mysql/migrator.go index a754509a..c1c755fa 100644 --- a/pkg/db/migrator.go +++ b/pkg/db/mysql/migrator.go @@ -14,11 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -package db +package mysql import ( "github.com/caoyingjunz/pixiu/pkg/db/model" - "gorm.io/gorm" ) diff --git a/pkg/db/tenant.go b/pkg/db/mysql/tenant.go similarity index 66% rename from pkg/db/tenant.go rename to pkg/db/mysql/tenant.go index c81cb3cb..9ec9d9d5 100644 --- a/pkg/db/tenant.go +++ b/pkg/db/mysql/tenant.go @@ -1,45 +1,21 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package db +package mysql import ( "context" - "time" - - "gorm.io/gorm" - "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/util/errors" + "gorm.io/gorm" + "time" ) -type TenantInterface interface { - Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) - Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error - Delete(ctx context.Context, cid int64) (*model.Tenant, error) - Get(ctx context.Context, cid int64) (*model.Tenant, error) - List(ctx context.Context) ([]model.Tenant, error) - - GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) -} - type tenant struct { db *gorm.DB } +func newTenant(db *gorm.DB) *tenant { + return &tenant{db: db} +} + func (t *tenant) Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) { now := time.Now() object.GmtCreate = now @@ -115,7 +91,3 @@ func (t *tenant) GetTenantByName(ctx context.Context, name string) (*model.Tenan return &object, nil } - -func newTenant(db *gorm.DB) *tenant { - return &tenant{db} -} diff --git a/pkg/db/user.go b/pkg/db/mysql/user.go similarity index 63% rename from pkg/db/user.go rename to pkg/db/mysql/user.go index 2c504087..07ff58d5 100644 --- a/pkg/db/user.go +++ b/pkg/db/mysql/user.go @@ -1,47 +1,21 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package db +package mysql import ( "context" - "time" - - "gorm.io/gorm" - "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/util/errors" + "gorm.io/gorm" + "time" ) -type UserInterface interface { - Create(ctx context.Context, object *model.User) (*model.User, error) - Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error - Delete(ctx context.Context, uid int64) error - Get(ctx context.Context, uid int64) (*model.User, error) - List(ctx context.Context) ([]model.User, error) - - Count(ctx context.Context) (int64, error) - - GetUserByName(ctx context.Context, userName string) (*model.User, error) -} - type user struct { db *gorm.DB } +func newUser(db *gorm.DB) *user { + return &user{db: db} +} + func (u *user) Create(ctx context.Context, object *model.User) (*model.User, error) { now := time.Now() object.GmtCreate = now @@ -105,7 +79,3 @@ func (u *user) GetUserByName(ctx context.Context, userName string) (*model.User, return &object, nil } - -func newUser(db *gorm.DB) *user { - return &user{db} -} diff --git a/pkg/db/sqlite/cluster.go b/pkg/db/sqlite/cluster.go new file mode 100644 index 00000000..780dbc84 --- /dev/null +++ b/pkg/db/sqlite/cluster.go @@ -0,0 +1,113 @@ +package sqlite + +import ( + "context" + "fmt" + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/db/tx_func" + "github.com/caoyingjunz/pixiu/pkg/util/errors" + "gorm.io/gorm" + "time" +) + +type cluster struct { + db *gorm.DB +} + +func newCluster(db *gorm.DB) *cluster { + return &cluster{db: db} +} + +func (c *cluster) Create(ctx context.Context, object *model.Cluster, fns ...tx_func.TxFunc) (*model.Cluster, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := c.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + if err := tx.Create(object).Error; err != nil { + return err + } + + for _, fn := range fns { + if err := fn(); err != nil { + return err + } + } + return nil + }); err != nil { + return nil, err + } + return object, nil +} + +func (c *cluster) Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error { + // 系统维护字段 + updates["gmt_modified"] = time.Now() + updates["resource_version"] = resourceVersion + 1 + + f := c.db.WithContext(ctx).Model(&model.Cluster{}).Where("id = ? and resource_version = ?", cid, resourceVersion).Updates(updates) + if f.Error != nil { + return f.Error + } + if f.RowsAffected == 0 { + return errors.ErrRecordNotUpdate + } + + return nil +} + +func (c *cluster) Delete(ctx context.Context, cid int64) (*model.Cluster, error) { + // 仅当数据库支持回写功能时才能正常 + //if err := c.db.Clauses(clause.Returning{}).Where("id = ?", cid).Delete(&object).Error; err != nil { + // return nil, err + //} + object, err := c.Get(ctx, cid) + if err != nil { + return nil, err + } + if object == nil { + return nil, nil + } + + if object.Protected { + return nil, fmt.Errorf("集群开启删除保护,不允许被删除") + } + if err = c.db.WithContext(ctx).Where("id = ?", cid).Delete(&model.Cluster{}).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (c *cluster) Get(ctx context.Context, cid int64) (*model.Cluster, error) { + var object model.Cluster + if err := c.db.WithContext(ctx).Where("id = ?", cid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +func (c *cluster) List(ctx context.Context) ([]model.Cluster, error) { + var cs []model.Cluster + if err := c.db.WithContext(ctx).Find(&cs).Error; err != nil { + return nil, err + } + + return cs, nil +} + +func (c *cluster) GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) { + var object model.Cluster + if err := c.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} diff --git a/pkg/db/sqlite/conn.go b/pkg/db/sqlite/conn.go new file mode 100644 index 00000000..7a8f7959 --- /dev/null +++ b/pkg/db/sqlite/conn.go @@ -0,0 +1,37 @@ +package sqlite + +import ( + "fmt" + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db/dbconn" + "github.com/caoyingjunz/pixiu/pkg/types" + sqliteDriver "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func NewDb(sqlConfig config.SqliteOptions, mode string, migrate bool) (*dbconn.DbConn, error) { + + opt := &gorm.Config{} + if mode == mode { + opt.Logger = logger.Default.LogMode(logger.Info) + } + + dsn := fmt.Sprintf("%s?charset=utf8&parseTime=True&loc=Local", sqlConfig.Db) + DB, err := gorm.Open(sqliteDriver.Open(dsn), opt) + sqlDB, err := DB.DB() + if err != nil { + return nil, err + } + sqlDB.SetMaxIdleConns(types.MaxIdleConns) + sqlDB.SetMaxOpenConns(types.MaxOpenConns) + if migrate { + if err := newMigrator(DB).AutoMigrate(); err != nil { + return nil, err + } + } + + return &dbconn.DbConn{ + Conn: DB, + }, nil +} diff --git a/pkg/db/sqlite/dao.go b/pkg/db/sqlite/dao.go new file mode 100644 index 00000000..4c24399d --- /dev/null +++ b/pkg/db/sqlite/dao.go @@ -0,0 +1,26 @@ +package sqlite + +import ( + "github.com/caoyingjunz/pixiu/pkg/db/iface" + "gorm.io/gorm" +) + +type sqlite struct { + db *gorm.DB +} + +func (s *sqlite) Cluster() iface.ClusterInterface { + return newCluster(s.db) +} + +func (s *sqlite) Tenant() iface.TenantInterface { + return newTenant(s.db) +} + +func (s *sqlite) User() iface.UserInterface { + return newUser(s.db) +} + +func New(db *gorm.DB) (iface.ShareDaoFactory, error) { + return &sqlite{db: db}, nil +} diff --git a/pkg/db/sqlite/migrator.go b/pkg/db/sqlite/migrator.go new file mode 100644 index 00000000..8348d9fd --- /dev/null +++ b/pkg/db/sqlite/migrator.go @@ -0,0 +1,48 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package sqlite + +import ( + "github.com/caoyingjunz/pixiu/pkg/db/model" + "gorm.io/gorm" +) + +type migrator struct { + db *gorm.DB +} + +// AutoMigrate 自动创建指定模型的数据库表结构 +func (m *migrator) AutoMigrate() error { + return m.CreateTables(model.GetMigrationModels()...) +} + +func (m *migrator) CreateTables(dst ...interface{}) error { + for _, d := range dst { + if m.db.Migrator().HasTable(d) { + continue + } + if err := m.db.Migrator().CreateTable(d); err != nil { + return err + } + } + + return nil +} + +func newMigrator(db *gorm.DB) *migrator { + return &migrator{db} +} diff --git a/pkg/db/sqlite/tenant.go b/pkg/db/sqlite/tenant.go new file mode 100644 index 00000000..50b52aec --- /dev/null +++ b/pkg/db/sqlite/tenant.go @@ -0,0 +1,93 @@ +package sqlite + +import ( + "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/util/errors" + "gorm.io/gorm" + "time" +) + +type tenant struct { + db *gorm.DB +} + +func newTenant(db *gorm.DB) *tenant { + return &tenant{db: db} +} + +func (t *tenant) Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := t.db.WithContext(ctx).Create(object).Error; err != nil { + return nil, err + } + return object, nil +} + +func (t *tenant) Update(ctx context.Context, tid int64, resourceVersion int64, updates map[string]interface{}) error { + // 系统维护字段 + updates["gmt_modified"] = time.Now() + updates["resource_version"] = resourceVersion + 1 + + f := t.db.WithContext(ctx).Model(&model.Tenant{}).Where("id = ? and resource_version = ?", tid, resourceVersion).Updates(updates) + if f.Error != nil { + return f.Error + } + + if f.RowsAffected == 0 { + return errors.ErrRecordNotFound + } + + return nil +} + +func (t *tenant) Delete(ctx context.Context, tid int64) (*model.Tenant, error) { + object, err := t.Get(ctx, tid) + if err != nil { + return nil, err + } + if object == nil { + return nil, nil + } + if err = t.db.WithContext(ctx).Where("id = ?", tid).Delete(&model.Tenant{}).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (t *tenant) Get(ctx context.Context, tid int64) (*model.Tenant, error) { + var object model.Tenant + if err := t.db.WithContext(ctx).Where("id = ?", tid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +func (t *tenant) List(ctx context.Context) ([]model.Tenant, error) { + var objects []model.Tenant + if err := t.db.WithContext(ctx).Find(&objects).Error; err != nil { + return nil, err + } + + return objects, nil +} + +func (t *tenant) GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) { + var object model.Tenant + if err := t.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} diff --git a/pkg/db/sqlite/user.go b/pkg/db/sqlite/user.go new file mode 100644 index 00000000..fc527bab --- /dev/null +++ b/pkg/db/sqlite/user.go @@ -0,0 +1,81 @@ +package sqlite + +import ( + "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/util/errors" + "gorm.io/gorm" + "time" +) + +type user struct { + db *gorm.DB +} + +func newUser(db *gorm.DB) *user { + return &user{db: db} +} + +func (u *user) Create(ctx context.Context, object *model.User) (*model.User, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := u.db.WithContext(ctx).Create(object).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (u *user) Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error { + return nil +} + +func (u *user) Delete(ctx context.Context, uid int64) error { + return u.db.WithContext(ctx).Where("id = ?", uid).Delete(&model.User{}).Error +} + +func (u *user) Get(ctx context.Context, uid int64) (*model.User, error) { + var object model.User + if err := u.db.WithContext(ctx).Where("id = ?", uid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +// List 获取用户列表 +// TODO: 暂时不做分页考虑 +func (u *user) List(ctx context.Context) ([]model.User, error) { + var objects []model.User + if err := u.db.WithContext(ctx).Find(&objects).Error; err != nil { + return nil, err + } + + return objects, nil +} + +func (u *user) Count(ctx context.Context) (int64, error) { + var total int64 + if err := u.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil { + return 0, err + } + + return total, nil +} + +func (u *user) GetUserByName(ctx context.Context, userName string) (*model.User, error) { + var object model.User + if err := u.db.WithContext(ctx).Where("name = ?", userName).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} diff --git a/pkg/db/tx_func/tx_func.go b/pkg/db/tx_func/tx_func.go new file mode 100644 index 00000000..b010623c --- /dev/null +++ b/pkg/db/tx_func/tx_func.go @@ -0,0 +1,3 @@ +package tx_func + +type TxFunc func() error diff --git a/pkg/types/constant.go b/pkg/types/constant.go new file mode 100644 index 00000000..ecf74223 --- /dev/null +++ b/pkg/types/constant.go @@ -0,0 +1,6 @@ +package types + +const ( + MaxIdleConns = 10 + MaxOpenConns = 100 +) From 0d4ec979a1f623d38e2684b1201d1da4d3339863 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Tue, 23 Apr 2024 11:39:07 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E6=97=A0=E5=85=B3?= =?UTF-8?q?=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/options/options.go | 15 +++++++-------- go.mod | 1 + pkg/controller/cluster/cluster.go | 8 ++++---- pkg/controller/controller.go | 6 +++--- pkg/controller/tenant/tenant.go | 7 +++---- pkg/controller/user/user.go | 6 +++--- pkg/db/factory.go | 13 ++++++++++--- pkg/db/iface/factory.go | 7 ------- pkg/db/mysql/cluster.go | 6 ++++-- pkg/db/mysql/conn.go | 8 +++++--- pkg/db/mysql/dao.go | 4 ++-- pkg/db/mysql/migrator.go | 2 +- pkg/db/mysql/tenant.go | 6 ++++-- pkg/db/mysql/user.go | 6 ++++-- pkg/db/sqlite/cluster.go | 6 ++++-- pkg/db/sqlite/conn.go | 8 +++++--- pkg/db/sqlite/dao.go | 4 ++-- pkg/db/sqlite/migrator.go | 2 +- pkg/db/sqlite/tenant.go | 6 ++++-- 19 files changed, 67 insertions(+), 54 deletions(-) delete mode 100644 pkg/db/iface/factory.go diff --git a/cmd/app/options/options.go b/cmd/app/options/options.go index 0b5b0895..3c7da5b1 100644 --- a/cmd/app/options/options.go +++ b/cmd/app/options/options.go @@ -17,21 +17,20 @@ limitations under the License. package options import ( - "github.com/caoyingjunz/pixiu/pkg/db/iface" "os" - "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/controller" - "github.com/caoyingjunz/pixiu/pkg/db" pixiuConfig "github.com/caoyingjunz/pixiulib/config" "github.com/gin-gonic/gin" "github.com/spf13/cobra" + + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/controller" + "github.com/caoyingjunz/pixiu/pkg/db" ) const ( - defaultListen = 8080 - defaultTokenKey = "pixiu" - //defaultConfigFile = "./config.yaml" + defaultListen = 8080 + defaultTokenKey = "pixiu" defaultConfigFile = "/etc/pixiu/config.yaml" ) @@ -42,7 +41,7 @@ type Options struct { HttpEngine *gin.Engine // 数据库接口 - Factory iface.ShareDaoFactory + Factory db.ShareDaoFactory // 貔貅主控制接口 Controller controller.PixiuInterface diff --git a/go.mod b/go.mod index cf89b701..73d11161 100644 --- a/go.mod +++ b/go.mod @@ -38,6 +38,7 @@ require ( golang.org/x/time v0.1.0 google.golang.org/protobuf v1.28.1 // indirect gorm.io/driver/mysql v1.3.6 + gorm.io/driver/sqlite v1.5.5 gorm.io/gorm v1.23.8 helm.sh/helm/v3 v3.6.3 k8s.io/api v0.22.1 diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index fd0fe50a..828744bd 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -19,8 +19,6 @@ package cluster import ( "context" "fmt" - "github.com/caoyingjunz/pixiu/pkg/db/iface" - "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "net/http" "regexp" "strconv" @@ -46,6 +44,8 @@ import ( "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/types" "github.com/caoyingjunz/pixiu/pkg/util/uuid" + "github.com/caoyingjunz/pixiu/pkg/db" + "github.com/caoyingjunz/pixiu/pkg/db/tx_func" ) type ClusterGetter interface { @@ -87,7 +87,7 @@ func init() { type cluster struct { cc config.Config - factory iface.ShareDaoFactory + factory db.ShareDaoFactory } func (c *cluster) preCreate(ctx context.Context, req *types.CreateClusterRequest) error { @@ -635,7 +635,7 @@ func (c *cluster) model2Type(o *model.Cluster) *types.Cluster { return tc } -func NewCluster(cfg config.Config, f iface.ShareDaoFactory) *cluster { +func NewCluster(cfg config.Config, f db.ShareDaoFactory) *cluster { return &cluster{ cc: cfg, factory: f, diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 8598ad37..cf1ddc9b 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -21,7 +21,7 @@ import ( "github.com/caoyingjunz/pixiu/pkg/controller/cluster" "github.com/caoyingjunz/pixiu/pkg/controller/tenant" "github.com/caoyingjunz/pixiu/pkg/controller/user" - "github.com/caoyingjunz/pixiu/pkg/db/iface" + "github.com/caoyingjunz/pixiu/pkg/db" ) type PixiuInterface interface { @@ -32,14 +32,14 @@ type PixiuInterface interface { type pixiu struct { cc config.Config - factory iface.ShareDaoFactory + factory db.ShareDaoFactory } func (p *pixiu) Cluster() cluster.Interface { return cluster.NewCluster(p.cc, p.factory) } func (p *pixiu) Tenant() tenant.Interface { return tenant.NewTenant(p.cc, p.factory) } func (p *pixiu) User() user.Interface { return user.NewUser(p.cc, p.factory) } -func New(cfg config.Config, f iface.ShareDaoFactory) PixiuInterface { +func New(cfg config.Config, f db.ShareDaoFactory) PixiuInterface { return &pixiu{ cc: cfg, factory: f, diff --git a/pkg/controller/tenant/tenant.go b/pkg/controller/tenant/tenant.go index 7fbb4793..e0226009 100644 --- a/pkg/controller/tenant/tenant.go +++ b/pkg/controller/tenant/tenant.go @@ -18,12 +18,11 @@ package tenant import ( "context" - "github.com/caoyingjunz/pixiu/pkg/db/iface" - "k8s.io/klog/v2" "github.com/caoyingjunz/pixiu/api/server/errors" "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/types" ) @@ -42,7 +41,7 @@ type Interface interface { type tenant struct { cc config.Config - factory iface.ShareDaoFactory + factory db.ShareDaoFactory } func (t *tenant) Create(ctx context.Context, req *types.CreateTenantRequest) error { @@ -147,7 +146,7 @@ func (t *tenant) model2Type(o *model.Tenant) *types.Tenant { } } -func NewTenant(cfg config.Config, f iface.ShareDaoFactory) *tenant { +func NewTenant(cfg config.Config, f db.ShareDaoFactory) *tenant { return &tenant{ cc: cfg, factory: f, diff --git a/pkg/controller/user/user.go b/pkg/controller/user/user.go index d4495797..e288d9da 100644 --- a/pkg/controller/user/user.go +++ b/pkg/controller/user/user.go @@ -19,12 +19,12 @@ package user import ( "context" "fmt" - "github.com/caoyingjunz/pixiu/pkg/db/iface" "k8s.io/klog/v2" "github.com/caoyingjunz/pixiu/api/server/errors" "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/types" "github.com/caoyingjunz/pixiu/pkg/util" @@ -51,7 +51,7 @@ type Interface interface { type user struct { cc config.Config - factory iface.ShareDaoFactory + factory db.ShareDaoFactory } func (u *user) Create(ctx context.Context, req *types.CreateUserRequest) error { @@ -191,7 +191,7 @@ func model2Type(o *model.User) *types.User { } } -func NewUser(cfg config.Config, f iface.ShareDaoFactory) *user { +func NewUser(cfg config.Config, f db.ShareDaoFactory) *user { return &user{ cc: cfg, factory: f, diff --git a/pkg/db/factory.go b/pkg/db/factory.go index 42775a29..57a07fbe 100644 --- a/pkg/db/factory.go +++ b/pkg/db/factory.go @@ -17,16 +17,23 @@ limitations under the License. package db import ( + "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/cmd/app/config" "github.com/caoyingjunz/pixiu/pkg/db/dbconn" "github.com/caoyingjunz/pixiu/pkg/db/iface" - "github.com/caoyingjunz/pixiu/pkg/db/mysql" "github.com/caoyingjunz/pixiu/pkg/db/sqlite" - "gorm.io/gorm" + ) -func NewDaoFactory(dbConfig *config.DbConfig, mode string, migrate bool) (iface.ShareDaoFactory, error) { +type ShareDaoFactory interface { + Cluster() iface.ClusterInterface + Tenant() iface.TenantInterface + User() iface.UserInterface +} + +func NewDaoFactory(dbConfig *config.DbConfig, mode string, migrate bool) (ShareDaoFactory, error) { var db *dbconn.DbConn var err error switch dbConfig.Type { diff --git a/pkg/db/iface/factory.go b/pkg/db/iface/factory.go deleted file mode 100644 index 957cf9bf..00000000 --- a/pkg/db/iface/factory.go +++ /dev/null @@ -1,7 +0,0 @@ -package iface - -type ShareDaoFactory interface { - Cluster() ClusterInterface - Tenant() TenantInterface - User() UserInterface -} diff --git a/pkg/db/mysql/cluster.go b/pkg/db/mysql/cluster.go index a8ff158c..6776aaf8 100644 --- a/pkg/db/mysql/cluster.go +++ b/pkg/db/mysql/cluster.go @@ -3,11 +3,13 @@ package mysql import ( "context" "fmt" + "time" + + "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "github.com/caoyingjunz/pixiu/pkg/util/errors" - "gorm.io/gorm" - "time" ) type cluster struct { diff --git a/pkg/db/mysql/conn.go b/pkg/db/mysql/conn.go index c429c6eb..20a3abf0 100644 --- a/pkg/db/mysql/conn.go +++ b/pkg/db/mysql/conn.go @@ -2,12 +2,14 @@ package mysql import ( "fmt" - "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db/dbconn" - "github.com/caoyingjunz/pixiu/pkg/types" + mysqlDriver "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" + + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db/dbconn" + "github.com/caoyingjunz/pixiu/pkg/types" ) func NewDb(sqlConfig config.MysqlOptions, mode string, migrate bool) (*dbconn.DbConn, error) { diff --git a/pkg/db/mysql/dao.go b/pkg/db/mysql/dao.go index 42de3a9e..1b9719d9 100644 --- a/pkg/db/mysql/dao.go +++ b/pkg/db/mysql/dao.go @@ -1,8 +1,8 @@ package mysql import ( - "github.com/caoyingjunz/pixiu/pkg/db/iface" "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/iface" ) type mysql struct { @@ -21,7 +21,7 @@ func (m *mysql) User() iface.UserInterface { return newUser(m.db) } -func New(db *gorm.DB) (iface.ShareDaoFactory, error) { +func New(db *gorm.DB) (*mysql, error) { return &mysql{db: db}, nil } diff --git a/pkg/db/mysql/migrator.go b/pkg/db/mysql/migrator.go index c1c755fa..d2c6971d 100644 --- a/pkg/db/mysql/migrator.go +++ b/pkg/db/mysql/migrator.go @@ -17,8 +17,8 @@ limitations under the License. package mysql import ( - "github.com/caoyingjunz/pixiu/pkg/db/model" "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" ) type migrator struct { diff --git a/pkg/db/mysql/tenant.go b/pkg/db/mysql/tenant.go index 9ec9d9d5..af55fa82 100644 --- a/pkg/db/mysql/tenant.go +++ b/pkg/db/mysql/tenant.go @@ -2,10 +2,12 @@ package mysql import ( "context" + "time" + + "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/util/errors" - "gorm.io/gorm" - "time" ) type tenant struct { diff --git a/pkg/db/mysql/user.go b/pkg/db/mysql/user.go index 07ff58d5..c197fe8a 100644 --- a/pkg/db/mysql/user.go +++ b/pkg/db/mysql/user.go @@ -2,10 +2,12 @@ package mysql import ( "context" + "time" + + "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/util/errors" - "gorm.io/gorm" - "time" ) type user struct { diff --git a/pkg/db/sqlite/cluster.go b/pkg/db/sqlite/cluster.go index 780dbc84..b2f7b5a0 100644 --- a/pkg/db/sqlite/cluster.go +++ b/pkg/db/sqlite/cluster.go @@ -3,11 +3,13 @@ package sqlite import ( "context" "fmt" + "time" + + "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "github.com/caoyingjunz/pixiu/pkg/util/errors" - "gorm.io/gorm" - "time" ) type cluster struct { diff --git a/pkg/db/sqlite/conn.go b/pkg/db/sqlite/conn.go index 7a8f7959..72401d0c 100644 --- a/pkg/db/sqlite/conn.go +++ b/pkg/db/sqlite/conn.go @@ -2,12 +2,14 @@ package sqlite import ( "fmt" - "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db/dbconn" - "github.com/caoyingjunz/pixiu/pkg/types" + sqliteDriver "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" + + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db/dbconn" + "github.com/caoyingjunz/pixiu/pkg/types" ) func NewDb(sqlConfig config.SqliteOptions, mode string, migrate bool) (*dbconn.DbConn, error) { diff --git a/pkg/db/sqlite/dao.go b/pkg/db/sqlite/dao.go index 4c24399d..05f2b82e 100644 --- a/pkg/db/sqlite/dao.go +++ b/pkg/db/sqlite/dao.go @@ -1,8 +1,8 @@ package sqlite import ( - "github.com/caoyingjunz/pixiu/pkg/db/iface" "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/iface" ) type sqlite struct { @@ -21,6 +21,6 @@ func (s *sqlite) User() iface.UserInterface { return newUser(s.db) } -func New(db *gorm.DB) (iface.ShareDaoFactory, error) { +func New(db *gorm.DB) (*sqlite, error) { return &sqlite{db: db}, nil } diff --git a/pkg/db/sqlite/migrator.go b/pkg/db/sqlite/migrator.go index 8348d9fd..7a52e66b 100644 --- a/pkg/db/sqlite/migrator.go +++ b/pkg/db/sqlite/migrator.go @@ -17,8 +17,8 @@ limitations under the License. package sqlite import ( - "github.com/caoyingjunz/pixiu/pkg/db/model" "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" ) type migrator struct { diff --git a/pkg/db/sqlite/tenant.go b/pkg/db/sqlite/tenant.go index 50b52aec..dc0acfc9 100644 --- a/pkg/db/sqlite/tenant.go +++ b/pkg/db/sqlite/tenant.go @@ -2,10 +2,12 @@ package sqlite import ( "context" + "time" + + "gorm.io/gorm" + "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/util/errors" - "gorm.io/gorm" - "time" ) type tenant struct { From bef19c83b50649e92dbc9de111a4dd1db8473e96 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Tue, 23 Apr 2024 11:49:59 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=8C=85?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/options/options.go | 2 +- pkg/controller/tenant/tenant.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/app/options/options.go b/cmd/app/options/options.go index 3c7da5b1..934366e4 100644 --- a/cmd/app/options/options.go +++ b/cmd/app/options/options.go @@ -19,13 +19,13 @@ package options import ( "os" - pixiuConfig "github.com/caoyingjunz/pixiulib/config" "github.com/gin-gonic/gin" "github.com/spf13/cobra" "github.com/caoyingjunz/pixiu/cmd/app/config" "github.com/caoyingjunz/pixiu/pkg/controller" "github.com/caoyingjunz/pixiu/pkg/db" + pixiuConfig "github.com/caoyingjunz/pixiulib/config" ) const ( diff --git a/pkg/controller/tenant/tenant.go b/pkg/controller/tenant/tenant.go index e0226009..5c77d91d 100644 --- a/pkg/controller/tenant/tenant.go +++ b/pkg/controller/tenant/tenant.go @@ -18,6 +18,7 @@ package tenant import ( "context" + "k8s.io/klog/v2" "github.com/caoyingjunz/pixiu/api/server/errors" From e9fad82ece5eef5e18539dbe70749c4fb34864e0 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Tue, 23 Apr 2024 11:51:41 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/config.yaml b/config.yaml index 802c0cdc..eeaeb31c 100644 --- a/config.yaml +++ b/config.yaml @@ -10,7 +10,6 @@ default: db: type: sqlite - # 数据库地址信息 # mysql: # host: peng From 7db8c3c341968b90b5e59d02d1b931c6c83fb5bb Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 10:19:54 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dconfig=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index eeaeb31c..bcfaa63d 100644 --- a/config.yaml +++ b/config.yaml @@ -8,6 +8,8 @@ default: # 自动创建指定模型的数据库表结构,不会更新已存在的数据库表 auto_migrate: true + log_format: json + db: type: sqlite # 数据库地址信息 @@ -18,4 +20,4 @@ db: # port: 3306 # name: pixiu sqlite: - db: pixiu.db \ No newline at end of file + db: pixiu.db From 20b6c09f0c0afa015eea63d9abc9a1245177967b Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 11:29:55 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E5=A4=84=E7=90=86gofmt=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/options/options.go | 4 ++-- pkg/controller/cluster/cluster.go | 4 ++-- pkg/db/factory.go | 1 - pkg/db/mysql/dao.go | 2 +- pkg/db/mysql/migrator.go | 2 +- pkg/db/sqlite/dao.go | 2 +- pkg/db/sqlite/migrator.go | 2 +- 7 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cmd/app/options/options.go b/cmd/app/options/options.go index 5a580b06..7515ed2c 100644 --- a/cmd/app/options/options.go +++ b/cmd/app/options/options.go @@ -29,8 +29,8 @@ import ( ) const ( - defaultListen = 8080 - defaultTokenKey = "pixiu" + defaultListen = 8080 + defaultTokenKey = "pixiu" defaultConfigFile = "/etc/pixiu/config.yaml" defaultLogFormat = config.LogFormatJson ) diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index 828744bd..1bc564a3 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -41,11 +41,11 @@ import ( "github.com/caoyingjunz/pixiu/api/server/errors" "github.com/caoyingjunz/pixiu/cmd/app/config" "github.com/caoyingjunz/pixiu/pkg/client" + "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "github.com/caoyingjunz/pixiu/pkg/types" "github.com/caoyingjunz/pixiu/pkg/util/uuid" - "github.com/caoyingjunz/pixiu/pkg/db" - "github.com/caoyingjunz/pixiu/pkg/db/tx_func" ) type ClusterGetter interface { diff --git a/pkg/db/factory.go b/pkg/db/factory.go index 57a07fbe..8204c2f8 100644 --- a/pkg/db/factory.go +++ b/pkg/db/factory.go @@ -24,7 +24,6 @@ import ( "github.com/caoyingjunz/pixiu/pkg/db/iface" "github.com/caoyingjunz/pixiu/pkg/db/mysql" "github.com/caoyingjunz/pixiu/pkg/db/sqlite" - ) type ShareDaoFactory interface { diff --git a/pkg/db/mysql/dao.go b/pkg/db/mysql/dao.go index 1b9719d9..cf608725 100644 --- a/pkg/db/mysql/dao.go +++ b/pkg/db/mysql/dao.go @@ -1,8 +1,8 @@ package mysql import ( - "gorm.io/gorm" "github.com/caoyingjunz/pixiu/pkg/db/iface" + "gorm.io/gorm" ) type mysql struct { diff --git a/pkg/db/mysql/migrator.go b/pkg/db/mysql/migrator.go index d2c6971d..c1c755fa 100644 --- a/pkg/db/mysql/migrator.go +++ b/pkg/db/mysql/migrator.go @@ -17,8 +17,8 @@ limitations under the License. package mysql import ( - "gorm.io/gorm" "github.com/caoyingjunz/pixiu/pkg/db/model" + "gorm.io/gorm" ) type migrator struct { diff --git a/pkg/db/sqlite/dao.go b/pkg/db/sqlite/dao.go index 05f2b82e..2f81a186 100644 --- a/pkg/db/sqlite/dao.go +++ b/pkg/db/sqlite/dao.go @@ -1,8 +1,8 @@ package sqlite import ( - "gorm.io/gorm" "github.com/caoyingjunz/pixiu/pkg/db/iface" + "gorm.io/gorm" ) type sqlite struct { diff --git a/pkg/db/sqlite/migrator.go b/pkg/db/sqlite/migrator.go index 7a52e66b..8348d9fd 100644 --- a/pkg/db/sqlite/migrator.go +++ b/pkg/db/sqlite/migrator.go @@ -17,8 +17,8 @@ limitations under the License. package sqlite import ( - "gorm.io/gorm" "github.com/caoyingjunz/pixiu/pkg/db/model" + "gorm.io/gorm" ) type migrator struct { From d4112b66f5880dd3c0d542d286fb893e6cf6d76d Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 11:37:02 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9config.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/config/config.go | 52 +++++++--------------------------------- 1 file changed, 9 insertions(+), 43 deletions(-) diff --git a/cmd/app/config/config.go b/cmd/app/config/config.go index ae89acf4..d07c89ab 100644 --- a/cmd/app/config/config.go +++ b/cmd/app/config/config.go @@ -16,20 +16,9 @@ limitations under the License. package config -import "errors" - -type LogFormat string - -const ( - LogFormatJson LogFormat = "json" - LogFormatText LogFormat = "text" -) - -var ErrInvalidLogFormat = errors.New("invalid log format") - type Config struct { Default DefaultOptions `yaml:"default"` - Mysql MysqlOptions `yaml:"mysql"` + Db DbConfig `yaml:"db"` } type DefaultOptions struct { @@ -39,15 +28,16 @@ type DefaultOptions struct { // 自动创建指定模型的数据库表结构,不会更新已存在的数据库表 AutoMigrate bool `yaml:"auto_migrate"` +} - LogOptions `yaml:",inline"` +type DbConfig struct { + Type string `yaml:"type"` + Sqlite SqliteOptions `yaml:"sqlite"` + Mysql MysqlOptions `yaml:"mysql"` } -func (o DefaultOptions) Valid() error { - if err := o.LogOptions.Valid(); err != nil { - return err - } - return nil +type SqliteOptions struct { + Db string `yaml:"db"` } // MysqlOptions 数据库具体配置 @@ -59,30 +49,6 @@ type MysqlOptions struct { Name string `yaml:"name"` } -func (o MysqlOptions) Valid() error { - // TODO +func (c *Config) Valid() error { return nil } - -type LogOptions struct { - LogFormat `yaml:"log_format"` -} - -func (o LogOptions) Valid() error { - switch o.LogFormat { - case LogFormatJson, LogFormatText: - return nil - default: - return ErrInvalidLogFormat - } -} - -func (c *Config) Valid() (err error) { - if err = c.Default.Valid(); err != nil { - return - } - if err = c.Mysql.Valid(); err != nil { - return - } - return -} From e88f10a85fb41c7dac8881bb48e2edc8ff7a1fee Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 12:16:34 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/config/config.go | 38 +++++++++++++++++++++++++++++++++++++- pkg/db/factory.go | 6 ++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/cmd/app/config/config.go b/cmd/app/config/config.go index d07c89ab..78f4ad98 100644 --- a/cmd/app/config/config.go +++ b/cmd/app/config/config.go @@ -16,9 +16,20 @@ limitations under the License. package config +import "errors" + +type LogFormat string + +const ( + LogFormatJson LogFormat = "json" + LogFormatText LogFormat = "text" +) + +var ErrInvalidLogFormat = errors.New("invalid log format") + type Config struct { Default DefaultOptions `yaml:"default"` - Db DbConfig `yaml:"db"` + Db DbConfig `yaml:"db"` } type DefaultOptions struct { @@ -28,6 +39,8 @@ type DefaultOptions struct { // 自动创建指定模型的数据库表结构,不会更新已存在的数据库表 AutoMigrate bool `yaml:"auto_migrate"` + + LogOptions `yaml:",inline"` } type DbConfig struct { @@ -40,6 +53,11 @@ type SqliteOptions struct { Db string `yaml:"db"` } +func (o SqliteOptions) Valid() error { + // TODO + return nil +} + // MysqlOptions 数据库具体配置 type MysqlOptions struct { Host string `yaml:"host"` @@ -49,6 +67,24 @@ type MysqlOptions struct { Name string `yaml:"name"` } +func (o MysqlOptions) Valid() error { + // TODO + return nil +} + +type LogOptions struct { + LogFormat `yaml:"log_format"` +} + +func (o LogOptions) Valid() error { + switch o.LogFormat { + case LogFormatJson, LogFormatText: + return nil + default: + return ErrInvalidLogFormat + } +} + func (c *Config) Valid() error { return nil } diff --git a/pkg/db/factory.go b/pkg/db/factory.go index 8204c2f8..b4847273 100644 --- a/pkg/db/factory.go +++ b/pkg/db/factory.go @@ -38,9 +38,15 @@ func NewDaoFactory(dbConfig *config.DbConfig, mode string, migrate bool) (ShareD switch dbConfig.Type { case "mysql": db, err = mysql.NewDb(dbConfig.Mysql, mode, migrate) + if err != nil { + return nil, err + } return mysql.New(db.Conn.(*gorm.DB)) case "sqlite": db, err = sqlite.NewDb(dbConfig.Sqlite, mode, migrate) + if err != nil { + return nil, err + } return sqlite.New(db.Conn.(*gorm.DB)) } From 8b2dc04da8b6d4fc87611bb460ef79ac08ba9b42 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 13:18:31 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E6=8F=90=E4=BA=A4go.sum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.sum | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 6c2fcf0d..e55b7bfe 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,8 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m github.com/mattn/go-shellwords v1.0.11/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mattn/go-sqlite3 v1.12.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= -github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= +github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -1524,8 +1524,11 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM= gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= -gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= +gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E= +gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg= +gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= From 314f6eeb8b787325c1af4c19961c52a31482371d Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 13:42:38 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dci=E6=9C=AA=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 ++--- pkg/db/model/tenant.go | 2 +- pkg/db/model/user.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 06cf79c4..e49c5678 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,6 @@ require ( github.com/lib/pq v1.10.2 // indirect github.com/mattn/go-colorable v0.1.6 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-sqlite3 v1.14.12 // indirect github.com/mittwald/go-helm-client v0.8.1 github.com/pelletier/go-toml/v2 v2.2.0 // indirect github.com/rogpeppe/go-internal v1.8.1 // indirect @@ -43,8 +42,8 @@ require ( golang.org/x/time v0.1.0 google.golang.org/protobuf v1.33.0 // indirect gorm.io/driver/mysql v1.3.6 - gorm.io/driver/sqlite v1.5.5 - gorm.io/gorm v1.23.8 + gorm.io/driver/sqlite v1.5.5 + gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde helm.sh/helm/v3 v3.6.3 k8s.io/api v0.22.1 k8s.io/apimachinery v0.22.1 diff --git a/pkg/db/model/tenant.go b/pkg/db/model/tenant.go index 37895c95..c63c9e95 100644 --- a/pkg/db/model/tenant.go +++ b/pkg/db/model/tenant.go @@ -25,7 +25,7 @@ func init() { type Tenant struct { pixiu.Model - Name string `gorm:"index:idx_name,unique" json:"name"` + Name string `gorm:"index:idx_tenant_name,unique" json:"name"` Description string `gorm:"type:text" json:"description"` Extension string `gorm:"type:text" json:"extension,omitempty"` } diff --git a/pkg/db/model/user.go b/pkg/db/model/user.go index 88da0d7c..3e0ab80a 100644 --- a/pkg/db/model/user.go +++ b/pkg/db/model/user.go @@ -35,7 +35,7 @@ type UserStatus uint8 // TODO type User struct { pixiu.Model - Name string `gorm:"index:idx_name,unique" json:"name"` + Name string `gorm:"index:idx_user_name,unique" json:"name"` Password string `gorm:"type:varchar(256)" json:"-"` Status UserStatus `gorm:"type:tinyint" json:"status"` Role UserRole `gorm:"type:tinyint" json:"role"` From f84cd8ebcfd7dc680e26364379ae4a544c128b41 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Wed, 24 Apr 2024 14:18:07 +0800 Subject: [PATCH 11/15] =?UTF-8?q?=E9=99=8D=E4=BD=8Esqlite=E5=8C=85?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index e49c5678..e1fcb374 100644 --- a/go.mod +++ b/go.mod @@ -42,8 +42,8 @@ require ( golang.org/x/time v0.1.0 google.golang.org/protobuf v1.33.0 // indirect gorm.io/driver/mysql v1.3.6 - gorm.io/driver/sqlite v1.5.5 - gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde + gorm.io/driver/sqlite v1.3.6 + gorm.io/gorm v1.23.8 helm.sh/helm/v3 v3.6.3 k8s.io/api v0.22.1 k8s.io/apimachinery v0.22.1 diff --git a/go.sum b/go.sum index e55b7bfe..bc757b6f 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,8 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m github.com/mattn/go-shellwords v1.0.11/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mattn/go-sqlite3 v1.12.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= -github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= +github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -1524,11 +1524,11 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM= gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= -gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E= -gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE= +gorm.io/driver/sqlite v1.3.6 h1:Fi8xNYCUplOqWiPa3/GuCeowRNBRGTf62DEmhMDHeQQ= +gorm.io/driver/sqlite v1.3.6/go.mod h1:Sg1/pvnKtbQ7jLXxfZa+jSHvoX8hoZA8cn4xllOMTgE= +gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= -gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg= -gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= From df7a21ff9a30b691b06c9877cca5278dadb31454 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Thu, 25 Apr 2024 09:39:51 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E5=8E=BB=E9=99=A4config=E5=86=97?= =?UTF-8?q?=E4=BD=99=E5=AD=97=E6=AE=B5=E5=8F=8A=E5=85=B6=E4=BB=96=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/app/config/config.go | 29 ++++++++++++++++++++++------- config.yaml | 1 - pkg/db/dbconn/conn.go | 16 ++++++++++++++++ pkg/db/factory.go | 6 +++--- pkg/db/iface/cluster.go | 1 + pkg/db/iface/tenant.go | 1 + pkg/db/iface/user.go | 1 + pkg/db/mysql/cluster.go | 16 ++++++++++++++++ pkg/db/mysql/conn.go | 18 +++++++++++++++++- pkg/db/mysql/dao.go | 16 ++++++++++++++++ pkg/db/mysql/tenant.go | 16 ++++++++++++++++ pkg/db/mysql/user.go | 16 ++++++++++++++++ pkg/db/sqlite/cluster.go | 16 ++++++++++++++++ pkg/db/sqlite/conn.go | 18 +++++++++++++++++- pkg/db/sqlite/dao.go | 16 ++++++++++++++++ pkg/db/sqlite/tenant.go | 16 ++++++++++++++++ pkg/db/sqlite/user.go | 16 ++++++++++++++++ 17 files changed, 206 insertions(+), 13 deletions(-) diff --git a/cmd/app/config/config.go b/cmd/app/config/config.go index 78f4ad98..4ffb6c83 100644 --- a/cmd/app/config/config.go +++ b/cmd/app/config/config.go @@ -44,16 +44,25 @@ type DefaultOptions struct { } type DbConfig struct { - Type string `yaml:"type"` - Sqlite SqliteOptions `yaml:"sqlite"` - Mysql MysqlOptions `yaml:"mysql"` + Sqlite *SqliteOptions `yaml:"sqlite"` + Mysql *MysqlOptions `yaml:"mysql"` +} + +func (d DbConfig) Valid() error{ + if d.Sqlite != nil{ + return d.Sqlite.Valid() + } + if d.Mysql != nil{ + return d.Mysql.Valid() + } + return nil } type SqliteOptions struct { Db string `yaml:"db"` } -func (o SqliteOptions) Valid() error { +func (o *SqliteOptions) Valid() error { // TODO return nil } @@ -67,7 +76,7 @@ type MysqlOptions struct { Name string `yaml:"name"` } -func (o MysqlOptions) Valid() error { +func (o *MysqlOptions) Valid() error { // TODO return nil } @@ -85,6 +94,12 @@ func (o LogOptions) Valid() error { } } -func (c *Config) Valid() error { - return nil +func (c *Config) Valid() (err error) { + if err = c.Default.Valid(); err != nil { + return + } + if err = c.Db.Valid(); err != nil { + return + } + return } diff --git a/config.yaml b/config.yaml index bcfaa63d..0a5eb1d6 100644 --- a/config.yaml +++ b/config.yaml @@ -11,7 +11,6 @@ default: log_format: json db: - type: sqlite # 数据库地址信息 # mysql: # host: peng diff --git a/pkg/db/dbconn/conn.go b/pkg/db/dbconn/conn.go index 051713b8..40bbc602 100644 --- a/pkg/db/dbconn/conn.go +++ b/pkg/db/dbconn/conn.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package dbconn type DbConn struct { diff --git a/pkg/db/factory.go b/pkg/db/factory.go index b4847273..4fb27f74 100644 --- a/pkg/db/factory.go +++ b/pkg/db/factory.go @@ -35,14 +35,14 @@ type ShareDaoFactory interface { func NewDaoFactory(dbConfig *config.DbConfig, mode string, migrate bool) (ShareDaoFactory, error) { var db *dbconn.DbConn var err error - switch dbConfig.Type { - case "mysql": + if dbConfig.Mysql != nil{ db, err = mysql.NewDb(dbConfig.Mysql, mode, migrate) if err != nil { return nil, err } return mysql.New(db.Conn.(*gorm.DB)) - case "sqlite": + } + if dbConfig.Sqlite != nil{ db, err = sqlite.NewDb(dbConfig.Sqlite, mode, migrate) if err != nil { return nil, err diff --git a/pkg/db/iface/cluster.go b/pkg/db/iface/cluster.go index ca9c8b67..7f22375e 100644 --- a/pkg/db/iface/cluster.go +++ b/pkg/db/iface/cluster.go @@ -18,6 +18,7 @@ package iface import ( "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" "github.com/caoyingjunz/pixiu/pkg/db/tx_func" ) diff --git a/pkg/db/iface/tenant.go b/pkg/db/iface/tenant.go index 4928d665..1b36fc3c 100644 --- a/pkg/db/iface/tenant.go +++ b/pkg/db/iface/tenant.go @@ -18,6 +18,7 @@ package iface import ( "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" ) diff --git a/pkg/db/iface/user.go b/pkg/db/iface/user.go index 00f575a3..d7fe3468 100644 --- a/pkg/db/iface/user.go +++ b/pkg/db/iface/user.go @@ -18,6 +18,7 @@ package iface import ( "context" + "github.com/caoyingjunz/pixiu/pkg/db/model" ) diff --git a/pkg/db/mysql/cluster.go b/pkg/db/mysql/cluster.go index 6776aaf8..469c797b 100644 --- a/pkg/db/mysql/cluster.go +++ b/pkg/db/mysql/cluster.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package mysql import ( diff --git a/pkg/db/mysql/conn.go b/pkg/db/mysql/conn.go index 20a3abf0..f215ba5f 100644 --- a/pkg/db/mysql/conn.go +++ b/pkg/db/mysql/conn.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package mysql import ( @@ -12,7 +28,7 @@ import ( "github.com/caoyingjunz/pixiu/pkg/types" ) -func NewDb(sqlConfig config.MysqlOptions, mode string, migrate bool) (*dbconn.DbConn, error) { +func NewDb(sqlConfig *config.MysqlOptions, mode string, migrate bool) (*dbconn.DbConn, error) { opt := &gorm.Config{} if mode == mode { diff --git a/pkg/db/mysql/dao.go b/pkg/db/mysql/dao.go index cf608725..d0f5ad6e 100644 --- a/pkg/db/mysql/dao.go +++ b/pkg/db/mysql/dao.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package mysql import ( diff --git a/pkg/db/mysql/tenant.go b/pkg/db/mysql/tenant.go index af55fa82..147a9b3b 100644 --- a/pkg/db/mysql/tenant.go +++ b/pkg/db/mysql/tenant.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package mysql import ( diff --git a/pkg/db/mysql/user.go b/pkg/db/mysql/user.go index c197fe8a..2b93519e 100644 --- a/pkg/db/mysql/user.go +++ b/pkg/db/mysql/user.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package mysql import ( diff --git a/pkg/db/sqlite/cluster.go b/pkg/db/sqlite/cluster.go index b2f7b5a0..8f0a2359 100644 --- a/pkg/db/sqlite/cluster.go +++ b/pkg/db/sqlite/cluster.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package sqlite import ( diff --git a/pkg/db/sqlite/conn.go b/pkg/db/sqlite/conn.go index 72401d0c..bfa09bfe 100644 --- a/pkg/db/sqlite/conn.go +++ b/pkg/db/sqlite/conn.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package sqlite import ( @@ -12,7 +28,7 @@ import ( "github.com/caoyingjunz/pixiu/pkg/types" ) -func NewDb(sqlConfig config.SqliteOptions, mode string, migrate bool) (*dbconn.DbConn, error) { +func NewDb(sqlConfig *config.SqliteOptions, mode string, migrate bool) (*dbconn.DbConn, error) { opt := &gorm.Config{} if mode == mode { diff --git a/pkg/db/sqlite/dao.go b/pkg/db/sqlite/dao.go index 2f81a186..a03c691f 100644 --- a/pkg/db/sqlite/dao.go +++ b/pkg/db/sqlite/dao.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package sqlite import ( diff --git a/pkg/db/sqlite/tenant.go b/pkg/db/sqlite/tenant.go index dc0acfc9..4dbab9d0 100644 --- a/pkg/db/sqlite/tenant.go +++ b/pkg/db/sqlite/tenant.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package sqlite import ( diff --git a/pkg/db/sqlite/user.go b/pkg/db/sqlite/user.go index fc527bab..8dd3274a 100644 --- a/pkg/db/sqlite/user.go +++ b/pkg/db/sqlite/user.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package sqlite import ( From c41d05a5ad3a4d43a2ad9156c440496216beaf71 Mon Sep 17 00:00:00 2001 From: crazytaxii Date: Mon, 29 Apr 2024 09:06:28 -0400 Subject: [PATCH 13/15] Optimized support of multi databases --- .gitignore | 3 + cmd/app/config/config.go | 16 +- cmd/app/options/options.go | 21 +-- cmd/app/server.go | 23 ++- pkg/controller/cluster/cluster.go | 3 +- pkg/db/cluster.go | 244 ++++++++++++++++++++++++++++++ pkg/db/db.go | 207 +++++++++++++++++++++++++ pkg/db/factory.go | 41 ++--- pkg/db/iface/cluster.go | 3 +- pkg/db/{mysql => }/migrator.go | 11 +- pkg/db/mysql/cluster.go | 131 ---------------- pkg/db/mysql/conn.go | 60 -------- pkg/db/mysql/dao.go | 43 ------ pkg/db/mysql/tenant.go | 111 -------------- pkg/db/mysql/user.go | 99 ------------ pkg/db/sqlite/cluster.go | 131 ---------------- pkg/db/sqlite/conn.go | 55 ------- pkg/db/sqlite/dao.go | 42 ----- pkg/db/sqlite/migrator.go | 48 ------ pkg/db/sqlite/tenant.go | 111 -------------- pkg/db/sqlite/user.go | 97 ------------ pkg/db/tenant.go | 207 +++++++++++++++++++++++++ pkg/db/tx_func/tx_func.go | 3 - pkg/db/user.go | 185 ++++++++++++++++++++++ pkg/types/constant.go | 6 - 25 files changed, 899 insertions(+), 1002 deletions(-) create mode 100644 pkg/db/cluster.go create mode 100644 pkg/db/db.go rename pkg/db/{mysql => }/migrator.go (75%) delete mode 100644 pkg/db/mysql/cluster.go delete mode 100644 pkg/db/mysql/conn.go delete mode 100644 pkg/db/mysql/dao.go delete mode 100644 pkg/db/mysql/tenant.go delete mode 100644 pkg/db/mysql/user.go delete mode 100644 pkg/db/sqlite/cluster.go delete mode 100644 pkg/db/sqlite/conn.go delete mode 100644 pkg/db/sqlite/dao.go delete mode 100644 pkg/db/sqlite/migrator.go delete mode 100644 pkg/db/sqlite/tenant.go delete mode 100644 pkg/db/sqlite/user.go create mode 100644 pkg/db/tenant.go delete mode 100644 pkg/db/tx_func/tx_func.go create mode 100644 pkg/db/user.go delete mode 100644 pkg/types/constant.go diff --git a/.gitignore b/.gitignore index 39a9e0a7..472b335b 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,7 @@ dist # licensefmt hack/tools/licfmt/licfmt +# SQLite +pixiu.db + vendor diff --git a/cmd/app/config/config.go b/cmd/app/config/config.go index 4ffb6c83..ba0d6130 100644 --- a/cmd/app/config/config.go +++ b/cmd/app/config/config.go @@ -16,7 +16,9 @@ limitations under the License. package config -import "errors" +import ( + "errors" +) type LogFormat string @@ -48,14 +50,14 @@ type DbConfig struct { Mysql *MysqlOptions `yaml:"mysql"` } -func (d DbConfig) Valid() error{ - if d.Sqlite != nil{ +func (d DbConfig) Valid() error { + if d.Sqlite != nil { return d.Sqlite.Valid() } - if d.Mysql != nil{ - return d.Mysql.Valid() - } - return nil + if d.Mysql != nil { + return d.Mysql.Valid() + } + return errors.New("invalid database config") } type SqliteOptions struct { diff --git a/cmd/app/options/options.go b/cmd/app/options/options.go index 7515ed2c..e6ca3b49 100644 --- a/cmd/app/options/options.go +++ b/cmd/app/options/options.go @@ -41,6 +41,8 @@ type Options struct { ComponentConfig config.Config HttpEngine *gin.Engine + // 数据库 + db.DB // 数据库接口 Factory db.ShareDaoFactory // 貔貅主控制接口 @@ -105,24 +107,17 @@ func (o *Options) BindFlags(cmd *cobra.Command) { cmd.Flags().StringVar(&o.ConfigFile, "configfile", defaultConfigFile, "The location of the pixiu configuration file") } -func (o *Options) register() error { +func (o *Options) register() (err error) { // 注册数据库 - if err := o.registerDatabase(); err != nil { - return err - } + o.registerDatabase() // TODO: 注册其他依赖 - return nil + return } -func (o *Options) registerDatabase() error { - - var err error - o.Factory, err = db.NewDaoFactory(&o.ComponentConfig.Db, o.ComponentConfig.Default.Mode, o.ComponentConfig.Default.AutoMigrate) - if err != nil { - return err - } - return nil +func (o *Options) registerDatabase() { + o.DB = db.NewDB(o.ComponentConfig.Db, o.ComponentConfig.Default.Mode == "debug") + o.Factory = db.NewDaoFactory(o.DB) } // Validate validates all the required options. diff --git a/cmd/app/server.go b/cmd/app/server.go index cd5bf7ca..d7ee8f6a 100644 --- a/cmd/app/server.go +++ b/cmd/app/server.go @@ -82,6 +82,20 @@ func NewServerCommand(version string) *cobra.Command { // Run 优雅启动貔貅服务 func Run(opt *options.Options) error { + // Try to connect database. + klog.Info("connecting to database") + if err := opt.DB.Open(); err != nil { + return fmt.Errorf("failed to open database connection: %v", err) + } + + // Auto apply the latest data models. + if opt.ComponentConfig.Default.AutoMigrate { + klog.Info("migrating data models") + if err := opt.Factory.Migrate(); err != nil { + return fmt.Errorf("failed to migrate database: %v", err) + } + } + srv := &http.Server{ Addr: fmt.Sprintf(":%d", opt.ComponentConfig.Default.Listen), Handler: opt.HttpEngine, @@ -99,10 +113,10 @@ func Run(opt *options.Options) error { }() // Wait for interrupt signal to gracefully shut down the server with a timeout of 5 seconds. - quit := make(chan os.Signal) + quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) <-quit - klog.Infof("shutting pixiu server down ...") + klog.Infof("shutting pixiu server down...") // The context is used to inform the server it has 5 seconds to finish the request ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) @@ -112,5 +126,10 @@ func Run(opt *options.Options) error { klog.Fatalf("pixiu server forced to shutdown: %v", err) } + // Close the database connection. + if err := opt.DB.Close(); err != nil { + klog.Fatalf("failed to close database connection: %v", err) + } + return nil } diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index 1bc564a3..b0ea2ab4 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -43,7 +43,6 @@ import ( "github.com/caoyingjunz/pixiu/pkg/client" "github.com/caoyingjunz/pixiu/pkg/db" "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/db/tx_func" "github.com/caoyingjunz/pixiu/pkg/types" "github.com/caoyingjunz/pixiu/pkg/util/uuid" ) @@ -108,7 +107,7 @@ func (c *cluster) Create(ctx context.Context, req *types.CreateClusterRequest) e } var cs *client.ClusterSet - var txFunc tx_func.TxFunc = func() (err error) { + var txFunc = func() (err error) { cs, err = client.NewClusterSet(req.KubeConfig) return err } diff --git a/pkg/db/cluster.go b/pkg/db/cluster.go new file mode 100644 index 00000000..fac42ed1 --- /dev/null +++ b/pkg/db/cluster.go @@ -0,0 +1,244 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package db + +import ( + "context" + "fmt" + "time" + + "gorm.io/gorm" + + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/util/errors" +) + +type ClusterInterface interface { + Create(ctx context.Context, object *model.Cluster, fns ...func() error) (*model.Cluster, error) + Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error + Delete(ctx context.Context, cid int64) (*model.Cluster, error) + Get(ctx context.Context, cid int64) (*model.Cluster, error) + List(ctx context.Context) ([]model.Cluster, error) + + GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) +} + +// MySQL implementation +type clusterMySQL struct { + db *gorm.DB +} + +func newClusterMySQL(db *gorm.DB) ClusterInterface { + return &clusterMySQL{db} +} + +func (c *clusterMySQL) Create(ctx context.Context, object *model.Cluster, fns ...func() error) (*model.Cluster, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := c.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + if err := tx.Create(object).Error; err != nil { + return err + } + + for _, fn := range fns { + if err := fn(); err != nil { + return err + } + } + return nil + }); err != nil { + return nil, err + } + return object, nil +} + +func (c *clusterMySQL) Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error { + // 系统维护字段 + updates["gmt_modified"] = time.Now() + updates["resource_version"] = resourceVersion + 1 + + f := c.db.WithContext(ctx).Model(&model.Cluster{}).Where("id = ? and resource_version = ?", cid, resourceVersion).Updates(updates) + if f.Error != nil { + return f.Error + } + if f.RowsAffected == 0 { + return errors.ErrRecordNotUpdate + } + + return nil +} + +func (c *clusterMySQL) Delete(ctx context.Context, cid int64) (*model.Cluster, error) { + // 仅当数据库支持回写功能时才能正常 + //if err := c.db.Clauses(clause.Returning{}).Where("id = ?", cid).Delete(&object).Error; err != nil { + // return nil, err + //} + object, err := c.Get(ctx, cid) + if err != nil { + return nil, err + } + if object == nil { + return nil, nil + } + + if object.Protected { + return nil, fmt.Errorf("集群开启删除保护,不允许被删除") + } + if err = c.db.WithContext(ctx).Where("id = ?", cid).Delete(&model.Cluster{}).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (c *clusterMySQL) Get(ctx context.Context, cid int64) (*model.Cluster, error) { + var object model.Cluster + if err := c.db.WithContext(ctx).Where("id = ?", cid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +func (c *clusterMySQL) List(ctx context.Context) ([]model.Cluster, error) { + var cs []model.Cluster + if err := c.db.WithContext(ctx).Find(&cs).Error; err != nil { + return nil, err + } + + return cs, nil +} + +func (c *clusterMySQL) GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) { + var object model.Cluster + if err := c.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +// SQLite implementation +type clusterSQLite struct { + db *gorm.DB +} + +func newClusterSQLite(db *gorm.DB) ClusterInterface { + return &clusterSQLite{db} +} + +func (c *clusterSQLite) Create(ctx context.Context, object *model.Cluster, fns ...func() error) (*model.Cluster, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := c.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + if err := tx.Create(object).Error; err != nil { + return err + } + + for _, fn := range fns { + if err := fn(); err != nil { + return err + } + } + return nil + }); err != nil { + return nil, err + } + return object, nil +} + +func (c *clusterSQLite) Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error { + // 系统维护字段 + updates["gmt_modified"] = time.Now() + updates["resource_version"] = resourceVersion + 1 + + f := c.db.WithContext(ctx).Model(&model.Cluster{}).Where("id = ? and resource_version = ?", cid, resourceVersion).Updates(updates) + if f.Error != nil { + return f.Error + } + if f.RowsAffected == 0 { + return errors.ErrRecordNotUpdate + } + + return nil +} + +func (c *clusterSQLite) Delete(ctx context.Context, cid int64) (*model.Cluster, error) { + // 仅当数据库支持回写功能时才能正常 + //if err := c.db.Clauses(clause.Returning{}).Where("id = ?", cid).Delete(&object).Error; err != nil { + // return nil, err + //} + object, err := c.Get(ctx, cid) + if err != nil { + return nil, err + } + if object == nil { + return nil, nil + } + + if object.Protected { + return nil, fmt.Errorf("集群开启删除保护,不允许被删除") + } + if err = c.db.WithContext(ctx).Where("id = ?", cid).Delete(&model.Cluster{}).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (c *clusterSQLite) Get(ctx context.Context, cid int64) (*model.Cluster, error) { + var object model.Cluster + if err := c.db.WithContext(ctx).Where("id = ?", cid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +func (c *clusterSQLite) List(ctx context.Context) ([]model.Cluster, error) { + var cs []model.Cluster + if err := c.db.WithContext(ctx).Find(&cs).Error; err != nil { + return nil, err + } + + return cs, nil +} + +func (c *clusterSQLite) GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) { + var object model.Cluster + if err := c.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} diff --git a/pkg/db/db.go b/pkg/db/db.go new file mode 100644 index 00000000..fd643df2 --- /dev/null +++ b/pkg/db/db.go @@ -0,0 +1,207 @@ +/* +Copyright 2024 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package db + +import ( + "fmt" + + "gorm.io/driver/mysql" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "github.com/caoyingjunz/pixiu/cmd/app/config" + "github.com/caoyingjunz/pixiu/pkg/db/model" +) + +const ( + MaxIdleConns = 10 + MaxOpenConns = 100 +) + +// DB is the interface for database operations. +type DB interface { + // Open connects to database. + Open() error + // Close closes the database connection. + Close() error + + ShareDaoFactory +} + +func NewDB(cfg config.DbConfig, debug bool) DB { + if cfg.Mysql != nil { + return NewMySQLStore(cfg.Mysql, debug) + } + if cfg.Sqlite != nil { + return NewSQLiteStore(cfg.Sqlite, debug) + } + + return nil +} + +var _ DB = (*mysqlStore)(nil) +var _ ShareDaoFactory = (*mysqlStore)(nil) + +// MySQL implementation +type mysqlStore struct { + cfg *config.MysqlOptions + db *gorm.DB + debugMode bool +} + +func NewMySQLStore(cfg *config.MysqlOptions, debug bool) *mysqlStore { + return &mysqlStore{ + cfg: cfg, + debugMode: debug, + } +} + +// getDSN returns the DSN string for MySQL. +func (s *mysqlStore) getDSN() string { + return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", + s.cfg.User, + s.cfg.Password, + s.cfg.Host, + s.cfg.Port, + s.cfg.Name) +} + +// Open connects to MySQL. +func (s *mysqlStore) Open() (err error) { + opt := &gorm.Config{} + if s.debugMode { + opt.Logger = logger.Default.LogMode(logger.Info) + } + if s.db, err = gorm.Open(mysql.Open(s.getDSN()), opt); err != nil { + return + } + + sqlDB, err := s.db.DB() + if err != nil { + return + } + sqlDB.SetMaxIdleConns(MaxIdleConns) + sqlDB.SetMaxOpenConns(MaxOpenConns) + + return sqlDB.Ping() +} + +// Close closes the MySQL connection. +func (s *mysqlStore) Close() error { + sqlDB, err := s.db.DB() + if err != nil { + return err + } + return sqlDB.Close() +} + +// Migrate applies the latest data models to MySQL. +func (s *mysqlStore) Migrate() error { + return newMigrator(s.db).createTables(model.GetMigrationModels()...) +} + +// Cluster implements the ClusterInterface with MySQL. +func (s *mysqlStore) Cluster() ClusterInterface { + return newClusterMySQL(s.db) +} + +// User implements the UserInterface with MySQL. +func (s *mysqlStore) User() UserInterface { + return newUserMySQL(s.db) +} + +// Tenant implements the TenantInterface with MySQL. +func (s *mysqlStore) Tenant() TenantInterface { + return newTenantMySQL(s.db) +} + +var _ DB = (*sqliteStore)(nil) +var _ ShareDaoFactory = (*sqliteStore)(nil) + +// SQLite implementation +type sqliteStore struct { + cfg *config.SqliteOptions + db *gorm.DB + debugMode bool +} + +func NewSQLiteStore(cfg *config.SqliteOptions, debug bool) *sqliteStore { + return &sqliteStore{ + cfg: cfg, + debugMode: debug, + } +} + +// getDSN returns the DSN string for SQLite. +func (s *sqliteStore) getDSN() string { + return fmt.Sprintf("%s?charset=utf8&parseTime=True&loc=Local", s.cfg.Db) +} + +// Open connects to SQLite. +func (s *sqliteStore) Open() (err error) { + opt := &gorm.Config{} + if s.debugMode { + opt.Logger = logger.Default.LogMode(logger.Info) + } + if s.db, err = gorm.Open(sqlite.Open(s.getDSN()), opt); err != nil { + return + } + + sqlDB, err := s.db.DB() + if err != nil { + return + } + sqlDB.SetMaxIdleConns(MaxIdleConns) + sqlDB.SetMaxOpenConns(MaxOpenConns) + + return sqlDB.Ping() +} + +// Close closes the SQLite connection. +func (s *sqliteStore) Close() error { + sqlDB, err := s.db.DB() + if err != nil { + return err + } + return sqlDB.Close() +} + +// Migrate applies the latest data models to SQLite. +func (s *sqliteStore) Migrate() error { + return newMigrator(s.db).createTables(model.GetMigrationModels()...) +} + +// Cluster implements the ClusterInterface with SQLite. +func (s *sqliteStore) Cluster() ClusterInterface { + return newClusterSQLite(s.db) +} + +// User implements the UserInterface with SQLite. +func (s *sqliteStore) User() UserInterface { + return newUserSQLite(s.db) +} + +// Tenant implements the TenantInterface with SQLite. +func (s *sqliteStore) Tenant() TenantInterface { + return newTenantSQLite(s.db) +} + +// TODO: PostgreSQL implementation +// type pgStore struct { +// db *gorm.DB +// } diff --git a/pkg/db/factory.go b/pkg/db/factory.go index 4fb27f74..0b0b10c2 100644 --- a/pkg/db/factory.go +++ b/pkg/db/factory.go @@ -16,39 +16,18 @@ limitations under the License. package db -import ( - "gorm.io/gorm" +type ShareDaoFactory interface { + Cluster() ClusterInterface + Tenant() TenantInterface + User() UserInterface - "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db/dbconn" - "github.com/caoyingjunz/pixiu/pkg/db/iface" - "github.com/caoyingjunz/pixiu/pkg/db/mysql" - "github.com/caoyingjunz/pixiu/pkg/db/sqlite" -) + Migrate() error +} -type ShareDaoFactory interface { - Cluster() iface.ClusterInterface - Tenant() iface.TenantInterface - User() iface.UserInterface +type shareDaoFactory struct { + DB } -func NewDaoFactory(dbConfig *config.DbConfig, mode string, migrate bool) (ShareDaoFactory, error) { - var db *dbconn.DbConn - var err error - if dbConfig.Mysql != nil{ - db, err = mysql.NewDb(dbConfig.Mysql, mode, migrate) - if err != nil { - return nil, err - } - return mysql.New(db.Conn.(*gorm.DB)) - } - if dbConfig.Sqlite != nil{ - db, err = sqlite.NewDb(dbConfig.Sqlite, mode, migrate) - if err != nil { - return nil, err - } - return sqlite.New(db.Conn.(*gorm.DB)) - } - - return nil, err +func NewDaoFactory(db DB) ShareDaoFactory { + return &shareDaoFactory{DB: db} } diff --git a/pkg/db/iface/cluster.go b/pkg/db/iface/cluster.go index 7f22375e..5a329ec3 100644 --- a/pkg/db/iface/cluster.go +++ b/pkg/db/iface/cluster.go @@ -20,11 +20,10 @@ import ( "context" "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/db/tx_func" ) type ClusterInterface interface { - Create(ctx context.Context, object *model.Cluster, fns ...tx_func.TxFunc) (*model.Cluster, error) + Create(ctx context.Context, object *model.Cluster, fns ...func() error) (*model.Cluster, error) Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error Delete(ctx context.Context, cid int64) (*model.Cluster, error) Get(ctx context.Context, cid int64) (*model.Cluster, error) diff --git a/pkg/db/mysql/migrator.go b/pkg/db/migrator.go similarity index 75% rename from pkg/db/mysql/migrator.go rename to pkg/db/migrator.go index c1c755fa..51fd79bb 100644 --- a/pkg/db/mysql/migrator.go +++ b/pkg/db/migrator.go @@ -14,23 +14,18 @@ See the License for the specific language governing permissions and limitations under the License. */ -package mysql +package db import ( - "github.com/caoyingjunz/pixiu/pkg/db/model" "gorm.io/gorm" ) +// a migrator implemented by GORM type migrator struct { db *gorm.DB } -// AutoMigrate 自动创建指定模型的数据库表结构 -func (m *migrator) AutoMigrate() error { - return m.CreateTables(model.GetMigrationModels()...) -} - -func (m *migrator) CreateTables(dst ...interface{}) error { +func (m *migrator) createTables(dst ...interface{}) error { for _, d := range dst { if m.db.Migrator().HasTable(d) { continue diff --git a/pkg/db/mysql/cluster.go b/pkg/db/mysql/cluster.go deleted file mode 100644 index 469c797b..00000000 --- a/pkg/db/mysql/cluster.go +++ /dev/null @@ -1,131 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package mysql - -import ( - "context" - "fmt" - "time" - - "gorm.io/gorm" - - "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/db/tx_func" - "github.com/caoyingjunz/pixiu/pkg/util/errors" -) - -type cluster struct { - db *gorm.DB -} - -func newCluster(db *gorm.DB) *cluster { - return &cluster{db: db} -} - -func (c *cluster) Create(ctx context.Context, object *model.Cluster, fns ...tx_func.TxFunc) (*model.Cluster, error) { - now := time.Now() - object.GmtCreate = now - object.GmtModified = now - - if err := c.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { - if err := tx.Create(object).Error; err != nil { - return err - } - - for _, fn := range fns { - if err := fn(); err != nil { - return err - } - } - return nil - }); err != nil { - return nil, err - } - return object, nil -} - -func (c *cluster) Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error { - // 系统维护字段 - updates["gmt_modified"] = time.Now() - updates["resource_version"] = resourceVersion + 1 - - f := c.db.WithContext(ctx).Model(&model.Cluster{}).Where("id = ? and resource_version = ?", cid, resourceVersion).Updates(updates) - if f.Error != nil { - return f.Error - } - if f.RowsAffected == 0 { - return errors.ErrRecordNotUpdate - } - - return nil -} - -func (c *cluster) Delete(ctx context.Context, cid int64) (*model.Cluster, error) { - // 仅当数据库支持回写功能时才能正常 - //if err := c.db.Clauses(clause.Returning{}).Where("id = ?", cid).Delete(&object).Error; err != nil { - // return nil, err - //} - object, err := c.Get(ctx, cid) - if err != nil { - return nil, err - } - if object == nil { - return nil, nil - } - - if object.Protected { - return nil, fmt.Errorf("集群开启删除保护,不允许被删除") - } - if err = c.db.WithContext(ctx).Where("id = ?", cid).Delete(&model.Cluster{}).Error; err != nil { - return nil, err - } - - return object, nil -} - -func (c *cluster) Get(ctx context.Context, cid int64) (*model.Cluster, error) { - var object model.Cluster - if err := c.db.WithContext(ctx).Where("id = ?", cid).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} - -func (c *cluster) List(ctx context.Context) ([]model.Cluster, error) { - var cs []model.Cluster - if err := c.db.WithContext(ctx).Find(&cs).Error; err != nil { - return nil, err - } - - return cs, nil -} - -func (c *cluster) GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) { - var object model.Cluster - if err := c.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} diff --git a/pkg/db/mysql/conn.go b/pkg/db/mysql/conn.go deleted file mode 100644 index f215ba5f..00000000 --- a/pkg/db/mysql/conn.go +++ /dev/null @@ -1,60 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package mysql - -import ( - "fmt" - - mysqlDriver "gorm.io/driver/mysql" - "gorm.io/gorm" - "gorm.io/gorm/logger" - - "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db/dbconn" - "github.com/caoyingjunz/pixiu/pkg/types" -) - -func NewDb(sqlConfig *config.MysqlOptions, mode string, migrate bool) (*dbconn.DbConn, error) { - - opt := &gorm.Config{} - if mode == mode { - opt.Logger = logger.Default.LogMode(logger.Info) - } - - dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", - sqlConfig.User, - sqlConfig.Password, - sqlConfig.Host, - sqlConfig.Port, - sqlConfig.Name) - DB, err := gorm.Open(mysqlDriver.Open(dsn), opt) - sqlDB, err := DB.DB() - if err != nil { - return nil, err - } - sqlDB.SetMaxIdleConns(types.MaxIdleConns) - sqlDB.SetMaxOpenConns(types.MaxOpenConns) - if migrate { - if err := newMigrator(DB).AutoMigrate(); err != nil { - return nil, err - } - } - - return &dbconn.DbConn{ - Conn: DB, - }, nil -} diff --git a/pkg/db/mysql/dao.go b/pkg/db/mysql/dao.go deleted file mode 100644 index d0f5ad6e..00000000 --- a/pkg/db/mysql/dao.go +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package mysql - -import ( - "github.com/caoyingjunz/pixiu/pkg/db/iface" - "gorm.io/gorm" -) - -type mysql struct { - db *gorm.DB -} - -func (m *mysql) Cluster() iface.ClusterInterface { - return newCluster(m.db) -} - -func (m *mysql) Tenant() iface.TenantInterface { - return newTenant(m.db) -} - -func (m *mysql) User() iface.UserInterface { - return newUser(m.db) -} - -func New(db *gorm.DB) (*mysql, error) { - - return &mysql{db: db}, nil -} diff --git a/pkg/db/mysql/tenant.go b/pkg/db/mysql/tenant.go deleted file mode 100644 index 147a9b3b..00000000 --- a/pkg/db/mysql/tenant.go +++ /dev/null @@ -1,111 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package mysql - -import ( - "context" - "time" - - "gorm.io/gorm" - - "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/util/errors" -) - -type tenant struct { - db *gorm.DB -} - -func newTenant(db *gorm.DB) *tenant { - return &tenant{db: db} -} - -func (t *tenant) Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) { - now := time.Now() - object.GmtCreate = now - object.GmtModified = now - - if err := t.db.WithContext(ctx).Create(object).Error; err != nil { - return nil, err - } - return object, nil -} - -func (t *tenant) Update(ctx context.Context, tid int64, resourceVersion int64, updates map[string]interface{}) error { - // 系统维护字段 - updates["gmt_modified"] = time.Now() - updates["resource_version"] = resourceVersion + 1 - - f := t.db.WithContext(ctx).Model(&model.Tenant{}).Where("id = ? and resource_version = ?", tid, resourceVersion).Updates(updates) - if f.Error != nil { - return f.Error - } - - if f.RowsAffected == 0 { - return errors.ErrRecordNotFound - } - - return nil -} - -func (t *tenant) Delete(ctx context.Context, tid int64) (*model.Tenant, error) { - object, err := t.Get(ctx, tid) - if err != nil { - return nil, err - } - if object == nil { - return nil, nil - } - if err = t.db.WithContext(ctx).Where("id = ?", tid).Delete(&model.Tenant{}).Error; err != nil { - return nil, err - } - - return object, nil -} - -func (t *tenant) Get(ctx context.Context, tid int64) (*model.Tenant, error) { - var object model.Tenant - if err := t.db.WithContext(ctx).Where("id = ?", tid).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} - -func (t *tenant) List(ctx context.Context) ([]model.Tenant, error) { - var objects []model.Tenant - if err := t.db.WithContext(ctx).Find(&objects).Error; err != nil { - return nil, err - } - - return objects, nil -} - -func (t *tenant) GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) { - var object model.Tenant - if err := t.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} diff --git a/pkg/db/mysql/user.go b/pkg/db/mysql/user.go deleted file mode 100644 index 2b93519e..00000000 --- a/pkg/db/mysql/user.go +++ /dev/null @@ -1,99 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package mysql - -import ( - "context" - "time" - - "gorm.io/gorm" - - "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/util/errors" -) - -type user struct { - db *gorm.DB -} - -func newUser(db *gorm.DB) *user { - return &user{db: db} -} - -func (u *user) Create(ctx context.Context, object *model.User) (*model.User, error) { - now := time.Now() - object.GmtCreate = now - object.GmtModified = now - - if err := u.db.WithContext(ctx).Create(object).Error; err != nil { - return nil, err - } - - return object, nil -} - -func (u *user) Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error { - return nil -} - -func (u *user) Delete(ctx context.Context, uid int64) error { - return u.db.WithContext(ctx).Where("id = ?", uid).Delete(&model.User{}).Error -} - -func (u *user) Get(ctx context.Context, uid int64) (*model.User, error) { - var object model.User - if err := u.db.WithContext(ctx).Where("id = ?", uid).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} - -// List 获取用户列表 -// TODO: 暂时不做分页考虑 -func (u *user) List(ctx context.Context) ([]model.User, error) { - var objects []model.User - if err := u.db.WithContext(ctx).Find(&objects).Error; err != nil { - return nil, err - } - - return objects, nil -} - -func (u *user) Count(ctx context.Context) (int64, error) { - var total int64 - if err := u.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil { - return 0, err - } - - return total, nil -} - -func (u *user) GetUserByName(ctx context.Context, userName string) (*model.User, error) { - var object model.User - if err := u.db.WithContext(ctx).Where("name = ?", userName).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} diff --git a/pkg/db/sqlite/cluster.go b/pkg/db/sqlite/cluster.go deleted file mode 100644 index 8f0a2359..00000000 --- a/pkg/db/sqlite/cluster.go +++ /dev/null @@ -1,131 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sqlite - -import ( - "context" - "fmt" - "time" - - "gorm.io/gorm" - - "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/db/tx_func" - "github.com/caoyingjunz/pixiu/pkg/util/errors" -) - -type cluster struct { - db *gorm.DB -} - -func newCluster(db *gorm.DB) *cluster { - return &cluster{db: db} -} - -func (c *cluster) Create(ctx context.Context, object *model.Cluster, fns ...tx_func.TxFunc) (*model.Cluster, error) { - now := time.Now() - object.GmtCreate = now - object.GmtModified = now - - if err := c.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { - if err := tx.Create(object).Error; err != nil { - return err - } - - for _, fn := range fns { - if err := fn(); err != nil { - return err - } - } - return nil - }); err != nil { - return nil, err - } - return object, nil -} - -func (c *cluster) Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error { - // 系统维护字段 - updates["gmt_modified"] = time.Now() - updates["resource_version"] = resourceVersion + 1 - - f := c.db.WithContext(ctx).Model(&model.Cluster{}).Where("id = ? and resource_version = ?", cid, resourceVersion).Updates(updates) - if f.Error != nil { - return f.Error - } - if f.RowsAffected == 0 { - return errors.ErrRecordNotUpdate - } - - return nil -} - -func (c *cluster) Delete(ctx context.Context, cid int64) (*model.Cluster, error) { - // 仅当数据库支持回写功能时才能正常 - //if err := c.db.Clauses(clause.Returning{}).Where("id = ?", cid).Delete(&object).Error; err != nil { - // return nil, err - //} - object, err := c.Get(ctx, cid) - if err != nil { - return nil, err - } - if object == nil { - return nil, nil - } - - if object.Protected { - return nil, fmt.Errorf("集群开启删除保护,不允许被删除") - } - if err = c.db.WithContext(ctx).Where("id = ?", cid).Delete(&model.Cluster{}).Error; err != nil { - return nil, err - } - - return object, nil -} - -func (c *cluster) Get(ctx context.Context, cid int64) (*model.Cluster, error) { - var object model.Cluster - if err := c.db.WithContext(ctx).Where("id = ?", cid).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} - -func (c *cluster) List(ctx context.Context) ([]model.Cluster, error) { - var cs []model.Cluster - if err := c.db.WithContext(ctx).Find(&cs).Error; err != nil { - return nil, err - } - - return cs, nil -} - -func (c *cluster) GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) { - var object model.Cluster - if err := c.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} diff --git a/pkg/db/sqlite/conn.go b/pkg/db/sqlite/conn.go deleted file mode 100644 index bfa09bfe..00000000 --- a/pkg/db/sqlite/conn.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sqlite - -import ( - "fmt" - - sqliteDriver "gorm.io/driver/sqlite" - "gorm.io/gorm" - "gorm.io/gorm/logger" - - "github.com/caoyingjunz/pixiu/cmd/app/config" - "github.com/caoyingjunz/pixiu/pkg/db/dbconn" - "github.com/caoyingjunz/pixiu/pkg/types" -) - -func NewDb(sqlConfig *config.SqliteOptions, mode string, migrate bool) (*dbconn.DbConn, error) { - - opt := &gorm.Config{} - if mode == mode { - opt.Logger = logger.Default.LogMode(logger.Info) - } - - dsn := fmt.Sprintf("%s?charset=utf8&parseTime=True&loc=Local", sqlConfig.Db) - DB, err := gorm.Open(sqliteDriver.Open(dsn), opt) - sqlDB, err := DB.DB() - if err != nil { - return nil, err - } - sqlDB.SetMaxIdleConns(types.MaxIdleConns) - sqlDB.SetMaxOpenConns(types.MaxOpenConns) - if migrate { - if err := newMigrator(DB).AutoMigrate(); err != nil { - return nil, err - } - } - - return &dbconn.DbConn{ - Conn: DB, - }, nil -} diff --git a/pkg/db/sqlite/dao.go b/pkg/db/sqlite/dao.go deleted file mode 100644 index a03c691f..00000000 --- a/pkg/db/sqlite/dao.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sqlite - -import ( - "github.com/caoyingjunz/pixiu/pkg/db/iface" - "gorm.io/gorm" -) - -type sqlite struct { - db *gorm.DB -} - -func (s *sqlite) Cluster() iface.ClusterInterface { - return newCluster(s.db) -} - -func (s *sqlite) Tenant() iface.TenantInterface { - return newTenant(s.db) -} - -func (s *sqlite) User() iface.UserInterface { - return newUser(s.db) -} - -func New(db *gorm.DB) (*sqlite, error) { - return &sqlite{db: db}, nil -} diff --git a/pkg/db/sqlite/migrator.go b/pkg/db/sqlite/migrator.go deleted file mode 100644 index 8348d9fd..00000000 --- a/pkg/db/sqlite/migrator.go +++ /dev/null @@ -1,48 +0,0 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sqlite - -import ( - "github.com/caoyingjunz/pixiu/pkg/db/model" - "gorm.io/gorm" -) - -type migrator struct { - db *gorm.DB -} - -// AutoMigrate 自动创建指定模型的数据库表结构 -func (m *migrator) AutoMigrate() error { - return m.CreateTables(model.GetMigrationModels()...) -} - -func (m *migrator) CreateTables(dst ...interface{}) error { - for _, d := range dst { - if m.db.Migrator().HasTable(d) { - continue - } - if err := m.db.Migrator().CreateTable(d); err != nil { - return err - } - } - - return nil -} - -func newMigrator(db *gorm.DB) *migrator { - return &migrator{db} -} diff --git a/pkg/db/sqlite/tenant.go b/pkg/db/sqlite/tenant.go deleted file mode 100644 index 4dbab9d0..00000000 --- a/pkg/db/sqlite/tenant.go +++ /dev/null @@ -1,111 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sqlite - -import ( - "context" - "time" - - "gorm.io/gorm" - - "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/util/errors" -) - -type tenant struct { - db *gorm.DB -} - -func newTenant(db *gorm.DB) *tenant { - return &tenant{db: db} -} - -func (t *tenant) Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) { - now := time.Now() - object.GmtCreate = now - object.GmtModified = now - - if err := t.db.WithContext(ctx).Create(object).Error; err != nil { - return nil, err - } - return object, nil -} - -func (t *tenant) Update(ctx context.Context, tid int64, resourceVersion int64, updates map[string]interface{}) error { - // 系统维护字段 - updates["gmt_modified"] = time.Now() - updates["resource_version"] = resourceVersion + 1 - - f := t.db.WithContext(ctx).Model(&model.Tenant{}).Where("id = ? and resource_version = ?", tid, resourceVersion).Updates(updates) - if f.Error != nil { - return f.Error - } - - if f.RowsAffected == 0 { - return errors.ErrRecordNotFound - } - - return nil -} - -func (t *tenant) Delete(ctx context.Context, tid int64) (*model.Tenant, error) { - object, err := t.Get(ctx, tid) - if err != nil { - return nil, err - } - if object == nil { - return nil, nil - } - if err = t.db.WithContext(ctx).Where("id = ?", tid).Delete(&model.Tenant{}).Error; err != nil { - return nil, err - } - - return object, nil -} - -func (t *tenant) Get(ctx context.Context, tid int64) (*model.Tenant, error) { - var object model.Tenant - if err := t.db.WithContext(ctx).Where("id = ?", tid).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} - -func (t *tenant) List(ctx context.Context) ([]model.Tenant, error) { - var objects []model.Tenant - if err := t.db.WithContext(ctx).Find(&objects).Error; err != nil { - return nil, err - } - - return objects, nil -} - -func (t *tenant) GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) { - var object model.Tenant - if err := t.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} diff --git a/pkg/db/sqlite/user.go b/pkg/db/sqlite/user.go deleted file mode 100644 index 8dd3274a..00000000 --- a/pkg/db/sqlite/user.go +++ /dev/null @@ -1,97 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package sqlite - -import ( - "context" - "github.com/caoyingjunz/pixiu/pkg/db/model" - "github.com/caoyingjunz/pixiu/pkg/util/errors" - "gorm.io/gorm" - "time" -) - -type user struct { - db *gorm.DB -} - -func newUser(db *gorm.DB) *user { - return &user{db: db} -} - -func (u *user) Create(ctx context.Context, object *model.User) (*model.User, error) { - now := time.Now() - object.GmtCreate = now - object.GmtModified = now - - if err := u.db.WithContext(ctx).Create(object).Error; err != nil { - return nil, err - } - - return object, nil -} - -func (u *user) Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error { - return nil -} - -func (u *user) Delete(ctx context.Context, uid int64) error { - return u.db.WithContext(ctx).Where("id = ?", uid).Delete(&model.User{}).Error -} - -func (u *user) Get(ctx context.Context, uid int64) (*model.User, error) { - var object model.User - if err := u.db.WithContext(ctx).Where("id = ?", uid).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} - -// List 获取用户列表 -// TODO: 暂时不做分页考虑 -func (u *user) List(ctx context.Context) ([]model.User, error) { - var objects []model.User - if err := u.db.WithContext(ctx).Find(&objects).Error; err != nil { - return nil, err - } - - return objects, nil -} - -func (u *user) Count(ctx context.Context) (int64, error) { - var total int64 - if err := u.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil { - return 0, err - } - - return total, nil -} - -func (u *user) GetUserByName(ctx context.Context, userName string) (*model.User, error) { - var object model.User - if err := u.db.WithContext(ctx).Where("name = ?", userName).First(&object).Error; err != nil { - if errors.IsRecordNotFound(err) { - return nil, nil - } - return nil, err - } - - return &object, nil -} diff --git a/pkg/db/tenant.go b/pkg/db/tenant.go new file mode 100644 index 00000000..c285d0d5 --- /dev/null +++ b/pkg/db/tenant.go @@ -0,0 +1,207 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package db + +import ( + "context" + "time" + + "gorm.io/gorm" + + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/util/errors" +) + +type TenantInterface interface { + Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) + Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error + Delete(ctx context.Context, cid int64) (*model.Tenant, error) + Get(ctx context.Context, cid int64) (*model.Tenant, error) + List(ctx context.Context) ([]model.Tenant, error) + + GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) +} + +// MySQL implementation +type tenantMySQL struct { + db *gorm.DB +} + +func newTenantMySQL(db *gorm.DB) *tenantMySQL { + return &tenantMySQL{db} +} + +func (t *tenantMySQL) Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := t.db.WithContext(ctx).Create(object).Error; err != nil { + return nil, err + } + return object, nil +} + +func (t *tenantMySQL) Update(ctx context.Context, tid int64, resourceVersion int64, updates map[string]interface{}) error { + // 系统维护字段 + updates["gmt_modified"] = time.Now() + updates["resource_version"] = resourceVersion + 1 + + f := t.db.WithContext(ctx).Model(&model.Tenant{}).Where("id = ? and resource_version = ?", tid, resourceVersion).Updates(updates) + if f.Error != nil { + return f.Error + } + + if f.RowsAffected == 0 { + return errors.ErrRecordNotFound + } + + return nil +} + +func (t *tenantMySQL) Delete(ctx context.Context, tid int64) (*model.Tenant, error) { + object, err := t.Get(ctx, tid) + if err != nil { + return nil, err + } + if object == nil { + return nil, nil + } + if err = t.db.WithContext(ctx).Where("id = ?", tid).Delete(&model.Tenant{}).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (t *tenantMySQL) Get(ctx context.Context, tid int64) (*model.Tenant, error) { + var object model.Tenant + if err := t.db.WithContext(ctx).Where("id = ?", tid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +func (t *tenantMySQL) List(ctx context.Context) ([]model.Tenant, error) { + var objects []model.Tenant + if err := t.db.WithContext(ctx).Find(&objects).Error; err != nil { + return nil, err + } + + return objects, nil +} + +func (t *tenantMySQL) GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) { + var object model.Tenant + if err := t.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +// SQLite implementation +type tenantSQLite struct { + db *gorm.DB +} + +func newTenantSQLite(db *gorm.DB) *tenantSQLite { + return &tenantSQLite{db} +} + +func (t *tenantSQLite) Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := t.db.WithContext(ctx).Create(object).Error; err != nil { + return nil, err + } + return object, nil +} + +func (t *tenantSQLite) Update(ctx context.Context, tid int64, resourceVersion int64, updates map[string]interface{}) error { + // 系统维护字段 + updates["gmt_modified"] = time.Now() + updates["resource_version"] = resourceVersion + 1 + + f := t.db.WithContext(ctx).Model(&model.Tenant{}).Where("id = ? and resource_version = ?", tid, resourceVersion).Updates(updates) + if f.Error != nil { + return f.Error + } + + if f.RowsAffected == 0 { + return errors.ErrRecordNotFound + } + + return nil +} + +func (t *tenantSQLite) Delete(ctx context.Context, tid int64) (*model.Tenant, error) { + object, err := t.Get(ctx, tid) + if err != nil { + return nil, err + } + if object == nil { + return nil, nil + } + if err = t.db.WithContext(ctx).Where("id = ?", tid).Delete(&model.Tenant{}).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (t *tenantSQLite) Get(ctx context.Context, tid int64) (*model.Tenant, error) { + var object model.Tenant + if err := t.db.WithContext(ctx).Where("id = ?", tid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +func (t *tenantSQLite) List(ctx context.Context) ([]model.Tenant, error) { + var objects []model.Tenant + if err := t.db.WithContext(ctx).Find(&objects).Error; err != nil { + return nil, err + } + + return objects, nil +} + +func (t *tenantSQLite) GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) { + var object model.Tenant + if err := t.db.WithContext(ctx).Where("name = ?", name).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} diff --git a/pkg/db/tx_func/tx_func.go b/pkg/db/tx_func/tx_func.go deleted file mode 100644 index b010623c..00000000 --- a/pkg/db/tx_func/tx_func.go +++ /dev/null @@ -1,3 +0,0 @@ -package tx_func - -type TxFunc func() error diff --git a/pkg/db/user.go b/pkg/db/user.go new file mode 100644 index 00000000..27a79a00 --- /dev/null +++ b/pkg/db/user.go @@ -0,0 +1,185 @@ +/* +Copyright 2021 The Pixiu Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package db + +import ( + "context" + "time" + + "gorm.io/gorm" + + "github.com/caoyingjunz/pixiu/pkg/db/model" + "github.com/caoyingjunz/pixiu/pkg/util/errors" +) + +type UserInterface interface { + Create(ctx context.Context, object *model.User) (*model.User, error) + Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error + Delete(ctx context.Context, uid int64) error + Get(ctx context.Context, uid int64) (*model.User, error) + List(ctx context.Context) ([]model.User, error) + + Count(ctx context.Context) (int64, error) + + GetUserByName(ctx context.Context, userName string) (*model.User, error) +} + +// MySQL implementation +type userMySQL struct { + db *gorm.DB +} + +func newUserMySQL(db *gorm.DB) *userMySQL { + return &userMySQL{db} +} + +func (u *userMySQL) Create(ctx context.Context, object *model.User) (*model.User, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := u.db.WithContext(ctx).Create(object).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (u *userMySQL) Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error { + return nil +} + +func (u *userMySQL) Delete(ctx context.Context, uid int64) error { + return u.db.WithContext(ctx).Where("id = ?", uid).Delete(&model.User{}).Error +} + +func (u *userMySQL) Get(ctx context.Context, uid int64) (*model.User, error) { + var object model.User + if err := u.db.WithContext(ctx).Where("id = ?", uid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +// List 获取用户列表 +// TODO: 暂时不做分页考虑 +func (u *userMySQL) List(ctx context.Context) ([]model.User, error) { + var objects []model.User + if err := u.db.WithContext(ctx).Find(&objects).Error; err != nil { + return nil, err + } + + return objects, nil +} + +func (u *userMySQL) Count(ctx context.Context) (int64, error) { + var total int64 + if err := u.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil { + return 0, err + } + + return total, nil +} + +func (u *userMySQL) GetUserByName(ctx context.Context, userName string) (*model.User, error) { + var object model.User + if err := u.db.WithContext(ctx).Where("name = ?", userName).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +// SQLite implementation +type userSQLite struct { + db *gorm.DB +} + +func newUserSQLite(db *gorm.DB) *userSQLite { + return &userSQLite{db} +} + +func (u *userSQLite) Create(ctx context.Context, object *model.User) (*model.User, error) { + now := time.Now() + object.GmtCreate = now + object.GmtModified = now + + if err := u.db.WithContext(ctx).Create(object).Error; err != nil { + return nil, err + } + + return object, nil +} + +func (u *userSQLite) Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error { + return nil +} + +func (u *userSQLite) Delete(ctx context.Context, uid int64) error { + return u.db.WithContext(ctx).Where("id = ?", uid).Delete(&model.User{}).Error +} + +func (u *userSQLite) Get(ctx context.Context, uid int64) (*model.User, error) { + var object model.User + if err := u.db.WithContext(ctx).Where("id = ?", uid).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} + +// List 获取用户列表 +// TODO: 暂时不做分页考虑 +func (u *userSQLite) List(ctx context.Context) ([]model.User, error) { + var objects []model.User + if err := u.db.WithContext(ctx).Find(&objects).Error; err != nil { + return nil, err + } + + return objects, nil +} + +func (u *userSQLite) Count(ctx context.Context) (int64, error) { + var total int64 + if err := u.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil { + return 0, err + } + + return total, nil +} + +func (u *userSQLite) GetUserByName(ctx context.Context, userName string) (*model.User, error) { + var object model.User + if err := u.db.WithContext(ctx).Where("name = ?", userName).First(&object).Error; err != nil { + if errors.IsRecordNotFound(err) { + return nil, nil + } + return nil, err + } + + return &object, nil +} diff --git a/pkg/types/constant.go b/pkg/types/constant.go deleted file mode 100644 index ecf74223..00000000 --- a/pkg/types/constant.go +++ /dev/null @@ -1,6 +0,0 @@ -package types - -const ( - MaxIdleConns = 10 - MaxOpenConns = 100 -) From ef4e613f965e167ec3fd9964a052fdb1778e2d9f Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Tue, 30 Apr 2024 10:52:34 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=E5=88=A0=E9=99=A4iface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/db/iface/cluster.go | 33 --------------------------------- pkg/db/iface/tenant.go | 33 --------------------------------- pkg/db/iface/user.go | 35 ----------------------------------- 3 files changed, 101 deletions(-) delete mode 100644 pkg/db/iface/cluster.go delete mode 100644 pkg/db/iface/tenant.go delete mode 100644 pkg/db/iface/user.go diff --git a/pkg/db/iface/cluster.go b/pkg/db/iface/cluster.go deleted file mode 100644 index 5a329ec3..00000000 --- a/pkg/db/iface/cluster.go +++ /dev/null @@ -1,33 +0,0 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package iface - -import ( - "context" - - "github.com/caoyingjunz/pixiu/pkg/db/model" -) - -type ClusterInterface interface { - Create(ctx context.Context, object *model.Cluster, fns ...func() error) (*model.Cluster, error) - Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error - Delete(ctx context.Context, cid int64) (*model.Cluster, error) - Get(ctx context.Context, cid int64) (*model.Cluster, error) - List(ctx context.Context) ([]model.Cluster, error) - - GetClusterByName(ctx context.Context, name string) (*model.Cluster, error) -} diff --git a/pkg/db/iface/tenant.go b/pkg/db/iface/tenant.go deleted file mode 100644 index 1b36fc3c..00000000 --- a/pkg/db/iface/tenant.go +++ /dev/null @@ -1,33 +0,0 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package iface - -import ( - "context" - - "github.com/caoyingjunz/pixiu/pkg/db/model" -) - -type TenantInterface interface { - Create(ctx context.Context, object *model.Tenant) (*model.Tenant, error) - Update(ctx context.Context, cid int64, resourceVersion int64, updates map[string]interface{}) error - Delete(ctx context.Context, cid int64) (*model.Tenant, error) - Get(ctx context.Context, cid int64) (*model.Tenant, error) - List(ctx context.Context) ([]model.Tenant, error) - - GetTenantByName(ctx context.Context, name string) (*model.Tenant, error) -} diff --git a/pkg/db/iface/user.go b/pkg/db/iface/user.go deleted file mode 100644 index d7fe3468..00000000 --- a/pkg/db/iface/user.go +++ /dev/null @@ -1,35 +0,0 @@ -/* -Copyright 2021 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package iface - -import ( - "context" - - "github.com/caoyingjunz/pixiu/pkg/db/model" -) - -type UserInterface interface { - Create(ctx context.Context, object *model.User) (*model.User, error) - Update(ctx context.Context, uid int64, resourceVersion int64, updates map[string]interface{}) error - Delete(ctx context.Context, uid int64) error - Get(ctx context.Context, uid int64) (*model.User, error) - List(ctx context.Context) ([]model.User, error) - - Count(ctx context.Context) (int64, error) - - GetUserByName(ctx context.Context, userName string) (*model.User, error) -} From 671770b626d3c3b121e9926fe8fb31be47b2a709 Mon Sep 17 00:00:00 2001 From: yvan <1124645485@qq.com> Date: Tue, 30 Apr 2024 11:13:24 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E5=88=A0=E9=99=A4dbconn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/db/dbconn/conn.go | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 pkg/db/dbconn/conn.go diff --git a/pkg/db/dbconn/conn.go b/pkg/db/dbconn/conn.go deleted file mode 100644 index 40bbc602..00000000 --- a/pkg/db/dbconn/conn.go +++ /dev/null @@ -1,21 +0,0 @@ -/* -Copyright 2024 The Pixiu Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package dbconn - -type DbConn struct { - Conn interface{} -}