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"
|
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/db/config"
|
|
|
|
"github.com/goodrain/rainbond/db/model"
|
2017-11-07 11:40:44 +08:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Manager db manager
|
|
|
|
type Manager struct {
|
|
|
|
db *gorm.DB
|
|
|
|
config config.Config
|
|
|
|
initOne sync.Once
|
|
|
|
models []model.Interface
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:14:32 +08:00
|
|
|
//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=utf8&parseTime=True&loc=Local")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.DBType == "cockroachdb" {
|
|
|
|
var err error
|
2018-04-26 11:12:34 +08:00
|
|
|
addr := config.MysqlConnectionInfo
|
2017-11-07 11:40:44 +08:00
|
|
|
db, err = gorm.Open("postgres", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
//Print Print
|
|
|
|
func (m *Manager) Print(v ...interface{}) {
|
|
|
|
logrus.Info(v)
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:14:32 +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{})
|
2018-11-22 14:33:29 +08:00
|
|
|
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{})
|
|
|
|
//vs map port
|
|
|
|
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{})
|
2017-11-14 18:54:27 +08:00
|
|
|
m.models = append(m.models, &model.CodeCheckResult{})
|
2017-11-16 16:30:22 +08:00
|
|
|
m.models = append(m.models, &model.ServiceEvent{})
|
2017-11-20 17:29:39 +08:00
|
|
|
m.models = append(m.models, &model.VersionInfo{})
|
2017-12-03 19:11:16 +08:00
|
|
|
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{})
|
2018-04-14 15:20:22 +08:00
|
|
|
m.models = append(m.models, &model.LocalScheduler{})
|
2018-05-11 13:44:15 +08:00
|
|
|
m.models = append(m.models, &model.NotificationEvent{})
|
2018-05-10 18:54:32 +08:00
|
|
|
m.models = append(m.models, &model.AppStatus{})
|
2018-05-23 11:08:44 +08:00
|
|
|
m.models = append(m.models, &model.AppBackup{})
|
2018-11-14 23:08:30 +08:00
|
|
|
m.models = append(m.models, &model.ServiceSourceConfig{})
|
2018-11-18 11:53:11 +08:00
|
|
|
// 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{})
|
|
|
|
m.models = append(m.models, &model.TCPRule{})
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:14:32 +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" {
|
|
|
|
err := m.db.Set("gorm:table_options", "ENGINE=InnoDB charset=utf8").CreateTable(md).Error
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 11:14:32 +08:00
|
|
|
} 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() {
|
2018-11-22 14:33:29 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|