added transcation

This commit is contained in:
yangk 2020-09-23 18:58:54 +08:00
parent 244b2ff925
commit 1eada17fb1
6 changed files with 81 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/goodrain/rainbond/api/util/bcode"
"github.com/goodrain/rainbond/db"
httputil "github.com/goodrain/rainbond/util/http"
"github.com/sirupsen/logrus"
)
// AddConfigGroup -
@ -29,6 +30,7 @@ func (a *ApplicationStruct) AddConfigGroup(w http.ResponseWriter, r *http.Reques
for _, sid := range configReq.ServiceIDs {
_, ok := set[sid]
if !ok {
logrus.Infof("The serviceID [%s] is not under the application or does not exist", sid)
httputil.ReturnBcodeError(r, w, bcode.ErrServiceNotFound)
return
}

View File

@ -15,6 +15,8 @@ func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationC
if err != nil {
return nil, err
}
tx := db.GetManager().Begin()
// Create application configGroup-services
for _, s := range services {
serviceConfigGroup := dbmodel.ServiceConfigGroup{
@ -24,11 +26,12 @@ func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationC
ServiceAlias: s.ServiceAlias,
}
serviceResp = append(serviceResp, serviceConfigGroup)
if err := db.GetManager().AppConfigGroupServiceDao().AddModel(&serviceConfigGroup); err != nil {
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).AddModel(&serviceConfigGroup); err != nil {
if err == bcode.ErrServiceConfigGroupExist {
logrus.Warningf("config group \"%s\" under this service \"%s\" already exists.", serviceConfigGroup.ConfigGroupName, serviceConfigGroup.ServiceID)
continue
}
tx.Rollback()
return nil, err
}
}
@ -41,11 +44,12 @@ func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationC
ItemKey: it.ItemKey,
ItemValue: it.ItemValue,
}
if err := db.GetManager().AppConfigGroupItemDao().AddModel(configItem); err != nil {
if err := db.GetManager().AppConfigGroupItemDaoTransactions(tx).AddModel(configItem); err != nil {
if err == bcode.ErrConfigItemExist {
logrus.Warningf("config item \"%s\" under this config group \"%s\" already exists.", configItem.ItemKey, configItem.ConfigGroupName)
continue
}
tx.Rollback()
return nil, err
}
}
@ -56,7 +60,12 @@ func (a *ApplicationAction) AddConfigGroup(appID string, req *model.ApplicationC
ConfigGroupName: req.ConfigGroupName,
DeployType: req.DeployType,
}
if err := db.GetManager().AppConfigGroupDao().AddModel(config); err != nil {
if err := db.GetManager().AppConfigGroupDaoTransactions(tx).AddModel(config); err != nil {
tx.Rollback()
return nil, err
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, err
}

View File

@ -40,8 +40,11 @@ type Manager interface {
AppDao() dao.AppDao
ApplicationDao() dao.ApplicationDao
AppConfigGroupDao() dao.AppConfigGroupDao
AppConfigGroupDaoTransactions(db *gorm.DB) dao.AppConfigGroupDao
AppConfigGroupServiceDao() dao.AppConfigGroupServiceDao
AppConfigGroupServiceDaoTransactions(db *gorm.DB) dao.AppConfigGroupServiceDao
AppConfigGroupItemDao() dao.AppConfigGroupItemDao
AppConfigGroupItemDaoTransactions(db *gorm.DB) dao.AppConfigGroupItemDao
EnterpriseDao() dao.EnterpriseDao
TenantDao() dao.TenantDao
TenantDaoTransactions(db *gorm.DB) dao.TenantDao

View File

@ -147,6 +147,20 @@ func (mr *MockManagerMockRecorder) AppConfigGroupDao() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppConfigGroupDao", reflect.TypeOf((*MockManager)(nil).AppConfigGroupDao))
}
// AppConfigGroupDaoTransactions mocks base method.
func (m *MockManager) AppConfigGroupDaoTransactions(db *gorm.DB) dao.AppConfigGroupDao {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "AppConfigGroupDaoTransactions", db)
ret0, _ := ret[0].(dao.AppConfigGroupDao)
return ret0
}
// AppConfigGroupDaoTransactions indicates an expected call of AppConfigGroupDaoTransactions.
func (mr *MockManagerMockRecorder) AppConfigGroupDaoTransactions(db interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppConfigGroupDaoTransactions", reflect.TypeOf((*MockManager)(nil).AppConfigGroupDaoTransactions), db)
}
// AppConfigGroupServiceDao mocks base method.
func (m *MockManager) AppConfigGroupServiceDao() dao.AppConfigGroupServiceDao {
m.ctrl.T.Helper()
@ -161,6 +175,20 @@ func (mr *MockManagerMockRecorder) AppConfigGroupServiceDao() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppConfigGroupServiceDao", reflect.TypeOf((*MockManager)(nil).AppConfigGroupServiceDao))
}
// AppConfigGroupServiceDaoTransactions mocks base method.
func (m *MockManager) AppConfigGroupServiceDaoTransactions(db *gorm.DB) dao.AppConfigGroupServiceDao {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "AppConfigGroupServiceDaoTransactions", db)
ret0, _ := ret[0].(dao.AppConfigGroupServiceDao)
return ret0
}
// AppConfigGroupServiceDaoTransactions indicates an expected call of AppConfigGroupServiceDaoTransactions.
func (mr *MockManagerMockRecorder) AppConfigGroupServiceDaoTransactions(db interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppConfigGroupServiceDaoTransactions", reflect.TypeOf((*MockManager)(nil).AppConfigGroupServiceDaoTransactions), db)
}
// AppConfigGroupItemDao mocks base method.
func (m *MockManager) AppConfigGroupItemDao() dao.AppConfigGroupItemDao {
m.ctrl.T.Helper()
@ -175,6 +203,20 @@ func (mr *MockManagerMockRecorder) AppConfigGroupItemDao() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppConfigGroupItemDao", reflect.TypeOf((*MockManager)(nil).AppConfigGroupItemDao))
}
// AppConfigGroupItemDaoTransactions mocks base method.
func (m *MockManager) AppConfigGroupItemDaoTransactions(db *gorm.DB) dao.AppConfigGroupItemDao {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "AppConfigGroupItemDaoTransactions", db)
ret0, _ := ret[0].(dao.AppConfigGroupItemDao)
return ret0
}
// AppConfigGroupItemDaoTransactions indicates an expected call of AppConfigGroupItemDaoTransactions.
func (mr *MockManagerMockRecorder) AppConfigGroupItemDaoTransactions(db interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AppConfigGroupItemDaoTransactions", reflect.TypeOf((*MockManager)(nil).AppConfigGroupItemDaoTransactions), db)
}
// EnterpriseDao mocks base method.
func (m *MockManager) EnterpriseDao() dao.EnterpriseDao {
m.ctrl.T.Helper()

View File

@ -431,6 +431,13 @@ func (m *Manager) AppConfigGroupDao() dao.AppConfigGroupDao {
}
}
//AppConfigGroupDaoTransactions -
func (m *Manager) AppConfigGroupDaoTransactions(db *gorm.DB) dao.AppConfigGroupDao {
return &mysqldao.AppConfigGroupDaoImpl{
DB: db,
}
}
// AppConfigGroupServiceDao -
func (m *Manager) AppConfigGroupServiceDao() dao.AppConfigGroupServiceDao {
return &mysqldao.AppConfigGroupServiceDaoImpl{
@ -438,6 +445,13 @@ func (m *Manager) AppConfigGroupServiceDao() dao.AppConfigGroupServiceDao {
}
}
//AppConfigGroupServiceDaoTransactions -
func (m *Manager) AppConfigGroupServiceDaoTransactions(db *gorm.DB) dao.AppConfigGroupServiceDao {
return &mysqldao.AppConfigGroupServiceDaoImpl{
DB: db,
}
}
// AppConfigGroupItemDao -
func (m *Manager) AppConfigGroupItemDao() dao.AppConfigGroupItemDao {
return &mysqldao.AppConfigGroupItemDaoImpl{
@ -445,6 +459,13 @@ func (m *Manager) AppConfigGroupItemDao() dao.AppConfigGroupItemDao {
}
}
//AppConfigGroupItemDaoTransactions -
func (m *Manager) AppConfigGroupItemDaoTransactions(db *gorm.DB) dao.AppConfigGroupItemDao {
return &mysqldao.AppConfigGroupItemDaoImpl{
DB: db,
}
}
//AppBackupDao group app backup info
func (m *Manager) AppBackupDao() dao.AppBackupDao {
return &mysqldao.AppBackupDaoImpl{

1
go.mod
View File

@ -73,6 +73,7 @@ require (
github.com/sirupsen/logrus v1.6.0
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
github.com/testcontainers/testcontainers-go v0.7.0
github.com/thejerf/suture v3.0.3+incompatible
github.com/tidwall/gjson v1.6.1