Rainbond/db/mysql/mysql.go

217 lines
7.7 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2017-11-07 11:40:44 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-03-14 14:33:31 +08:00
2017-11-07 11:40:44 +08:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mysql
import (
2017-11-09 14:50:28 +08:00
"sync"
"github.com/goodrain/rainbond/db/config"
"github.com/goodrain/rainbond/db/model"
2017-11-07 11:40:44 +08:00
"github.com/jinzhu/gorm"
2020-09-19 17:17:28 +08:00
"github.com/sirupsen/logrus"
2020-04-10 16:52:06 +08:00
// import sql driver manually
_ "github.com/go-sql-driver/mysql"
2017-11-07 11:40:44 +08:00
_ "github.com/jinzhu/gorm/dialects/postgres"
)
//Manager db manager
type Manager struct {
db *gorm.DB
config config.Config
initOne sync.Once
models []model.Interface
}
//CreateManager create manager
2017-11-07 11:40:44 +08:00
func CreateManager(config config.Config) (*Manager, error) {
var db *gorm.DB
if config.DBType == "mysql" {
var err error
db, err = gorm.Open("mysql", config.MysqlConnectionInfo+"?charset=utf8mb4&parseTime=True&loc=Local")
2017-11-07 11:40:44 +08:00
if err != nil {
return nil, err
}
}
if config.DBType == "cockroachdb" {
var err error
addr := config.MysqlConnectionInfo
2017-11-07 11:40:44 +08:00
db, err = gorm.Open("postgres", addr)
if err != nil {
return nil, err
}
}
2021-05-20 00:01:37 +08:00
if config.ShowSQL {
db = db.Debug()
}
2017-11-07 11:40:44 +08:00
manager := &Manager{
db: db,
config: config,
initOne: sync.Once{},
}
db.SetLogger(manager)
manager.RegisterTableModel()
manager.CheckTable()
logrus.Debug("mysql db driver create")
return manager, nil
}
//CloseManager 关闭管理器
func (m *Manager) CloseManager() error {
return m.db.Close()
}
//Begin begin a transaction
func (m *Manager) Begin() *gorm.DB {
return m.db.Begin()
}
2021-05-20 00:01:37 +08:00
// DB returns the db.
func (m *Manager) DB() *gorm.DB {
return m.db
}
2019-11-05 11:34:23 +08:00
// EnsureEndTransactionFunc -
func (m *Manager) EnsureEndTransactionFunc() func(tx *gorm.DB) {
return func(tx *gorm.DB) {
if r := recover(); r != nil {
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
tx.Rollback()
}
}
}
2017-11-07 11:40:44 +08:00
//Print Print
func (m *Manager) Print(v ...interface{}) {
logrus.Info(v...)
2017-11-07 11:40:44 +08:00
}
//RegisterTableModel register table model
2017-11-07 11:40:44 +08:00
func (m *Manager) RegisterTableModel() {
m.models = append(m.models, &model.Tenants{})
m.models = append(m.models, &model.TenantServices{})
m.models = append(m.models, &model.TenantServicesPort{})
m.models = append(m.models, &model.TenantServiceRelation{})
m.models = append(m.models, &model.TenantServiceEnvVar{})
m.models = append(m.models, &model.TenantServiceMountRelation{})
m.models = append(m.models, &model.TenantServiceVolume{})
m.models = append(m.models, &model.TenantServiceLable{})
m.models = append(m.models, &model.TenantServiceProbe{})
2017-11-07 11:40:44 +08:00
m.models = append(m.models, &model.LicenseInfo{})
m.models = append(m.models, &model.TenantServicesDelete{})
m.models = append(m.models, &model.TenantServiceLBMappingPort{})
m.models = append(m.models, &model.TenantPlugin{})
m.models = append(m.models, &model.TenantPluginBuildVersion{})
2017-11-09 14:50:28 +08:00
m.models = append(m.models, &model.TenantServicePluginRelation{})
m.models = append(m.models, &model.TenantPluginVersionEnv{})
m.models = append(m.models, &model.TenantPluginVersionDiscoverConfig{})
m.models = append(m.models, &model.CodeCheckResult{})
m.models = append(m.models, &model.ServiceEvent{})
m.models = append(m.models, &model.VersionInfo{})
m.models = append(m.models, &model.RegionUserInfo{})
2017-12-15 00:28:25 +08:00
m.models = append(m.models, &model.TenantServicesStreamPluginPort{})
2017-12-07 09:56:08 +08:00
m.models = append(m.models, &model.RegionAPIClass{})
2018-01-02 18:02:08 +08:00
m.models = append(m.models, &model.RegionProcotols{})
m.models = append(m.models, &model.LocalScheduler{})
m.models = append(m.models, &model.NotificationEvent{})
m.models = append(m.models, &model.AppStatus{})
m.models = append(m.models, &model.AppBackup{})
m.models = append(m.models, &model.ServiceSourceConfig{})
2020-09-17 15:45:46 +08:00
m.models = append(m.models, &model.Application{})
2020-09-21 18:33:10 +08:00
m.models = append(m.models, &model.ApplicationConfigGroup{})
m.models = append(m.models, &model.ConfigGroupService{})
2020-10-08 16:10:03 +08:00
m.models = append(m.models, &model.ConfigGroupItem{})
// gateway
m.models = append(m.models, &model.Certificate{})
m.models = append(m.models, &model.RuleExtension{})
2018-11-23 20:39:34 +08:00
m.models = append(m.models, &model.HTTPRule{})
2022-01-05 17:13:08 +08:00
m.models = append(m.models, &model.HTTPRuleRewrite{})
2018-11-23 20:39:34 +08:00
m.models = append(m.models, &model.TCPRule{})
m.models = append(m.models, &model.TenantServiceConfigFile{})
m.models = append(m.models, &model.Endpoint{})
m.models = append(m.models, &model.ThirdPartySvcDiscoveryCfg{})
m.models = append(m.models, &model.GwRuleConfig{})
// volumeType
m.models = append(m.models, &model.TenantServiceVolumeType{})
2019-11-13 11:05:25 +08:00
// pod autoscaler
m.models = append(m.models, &model.TenantServiceAutoscalerRules{})
m.models = append(m.models, &model.TenantServiceAutoscalerRuleMetrics{})
2019-11-14 15:30:40 +08:00
m.models = append(m.models, &model.TenantServiceScalingRecords{})
2020-09-19 17:17:28 +08:00
m.models = append(m.models, &model.TenantServiceMonitor{})
m.models = append(m.models, &model.ComponentK8sAttributes{})
m.models = append(m.models, &model.K8sResource{})
2017-11-07 11:40:44 +08:00
}
//CheckTable check and create tables
2017-11-07 11:40:44 +08:00
func (m *Manager) CheckTable() {
m.initOne.Do(func() {
for _, md := range m.models {
if !m.db.HasTable(md) {
if m.config.DBType == "mysql" {
2021-12-09 10:36:55 +08:00
err := m.db.Set("gorm:table_options", "ENGINE=InnoDB charset=utf8mb4").CreateTable(md).Error
2017-11-07 11:40:44 +08:00
if err != nil {
logrus.Errorf("auto create table %s to db error."+err.Error(), md.TableName())
} else {
logrus.Infof("auto create table %s to db success", md.TableName())
}
} else { //cockroachdb
err := m.db.CreateTable(md).Error
if err != nil {
logrus.Errorf("auto create cockroachdb table %s to db error."+err.Error(), md.TableName())
} else {
logrus.Infof("auto create cockroachdb table %s to db success", md.TableName())
}
}
} else {
if err := m.db.AutoMigrate(md).Error; err != nil {
logrus.Errorf("auto Migrate table %s to db error."+err.Error(), md.TableName())
}
2017-11-07 11:40:44 +08:00
}
}
m.patchTable()
})
}
func (m *Manager) patchTable() {
2021-06-02 19:48:47 +08:00
if err := m.db.Exec("alter table tenant_services_envs modify column attr_value text;").Error; err != nil {
logrus.Errorf("alter table tenant_services_envs error %s", err.Error())
}
2019-09-10 12:53:50 +08:00
if err := m.db.Exec("alter table tenant_services_event modify column request_body varchar(1024);").Error; err != nil {
logrus.Errorf("alter table tenant_services_envent error %s", err.Error())
}
2019-09-18 17:56:52 +08:00
if err := m.db.Exec("update gateway_tcp_rule set ip=? where ip=?", "0.0.0.0", "").Error; err != nil {
logrus.Errorf("update gateway_tcp_rule data error %s", err.Error())
}
2019-12-12 17:40:40 +08:00
if err := m.db.Exec("alter table tenant_services_volume modify column volume_type varchar(64);").Error; err != nil {
logrus.Errorf("alter table tenant_services_volume error: %s", err.Error())
}
if err := m.db.Exec("update tenants set namespace=uuid where namespace is NULL;").Error; err != nil {
logrus.Errorf("update tenants namespace error: %s", err.Error())
}
2021-12-09 20:45:10 +08:00
if err := m.db.Exec("update applications set k8s_app=concat('app-',LEFT(app_id,8)) where k8s_app is NULL;").Error; err != nil {
logrus.Errorf("update tenants namespace error: %s", err.Error())
}
if err := m.db.Exec("update tenant_services set k8s_component_name=service_alias where k8s_component_name is NULL;").Error; err != nil {
logrus.Errorf("update tenants namespace error: %s", err.Error())
}
}