Skip to content

fix: git material saved in transaction #5040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func InitializeApp() (*App, error) {
wire.Bind(new(router.PProfRouter), new(*router.PProfRouterImpl)),
// ---- pprof end ----

sql.NewTransactionUtilImpl,
wire.Bind(new(sql.TransactionWrapper), new(*sql.TransactionUtilImpl)),

trigger.NewPipelineRestHandler,
wire.Bind(new(trigger.PipelineTriggerRestHandler), new(*trigger.PipelineTriggerRestHandlerImpl)),
app.GetAppServiceConfig,
Expand Down
2 changes: 2 additions & 0 deletions cmd/external-app/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func InitializeApp() (*App, error) {
telemetry.NewPosthogClient,
delete2.NewDeleteServiceImpl,

sql.NewTransactionUtilImpl,

pipelineConfig.NewMaterialRepositoryImpl,
wire.Bind(new(pipelineConfig.MaterialRepository), new(*pipelineConfig.MaterialRepositoryImpl)),
// appStatus
Expand Down
11 changes: 6 additions & 5 deletions cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ type ImageTaggingRepositoryImpl struct {
*sql.TransactionUtilImpl
}

func NewImageTaggingRepositoryImpl(db *pg.DB) *ImageTaggingRepositoryImpl {
func NewImageTaggingRepositoryImpl(db *pg.DB, TransactionUtilImpl *sql.TransactionUtilImpl) *ImageTaggingRepositoryImpl {
return &ImageTaggingRepositoryImpl{
dbConnection: db,
TransactionUtilImpl: sql.NewTransactionUtilImpl(db),
TransactionUtilImpl: TransactionUtilImpl,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ type CiPipelineRepositoryImpl struct {
*sql.TransactionUtilImpl
}

func NewCiPipelineRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *CiPipelineRepositoryImpl {
func NewCiPipelineRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger, TransactionUtilImpl *sql.TransactionUtilImpl) *CiPipelineRepositoryImpl {
return &CiPipelineRepositoryImpl{
dbConnection: dbConnection,
logger: logger,
TransactionUtilImpl: sql.NewTransactionUtilImpl(dbConnection),
TransactionUtilImpl: TransactionUtilImpl,
}
}

Expand Down
18 changes: 9 additions & 9 deletions internal/sql/repository/pipelineConfig/MaterialRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ type GitMaterial struct {

type MaterialRepository interface {
MaterialExists(url string) (bool, error)
SaveMaterial(material *GitMaterial) error
UpdateMaterial(material *GitMaterial) error
SaveMaterial(tx *pg.Tx, material *GitMaterial) error
UpdateMaterial(tx *pg.Tx, material *GitMaterial) error
Update(materials []*GitMaterial) error
FindByAppId(appId int) ([]*GitMaterial, error)
FindById(Id int) (*GitMaterial, error)
FindByAppIdAndGitMaterialId(appId, id int) (*GitMaterial, error)
UpdateMaterialScmId(material *GitMaterial) error
FindByAppIdAndCheckoutPath(appId int, checkoutPath string) (*GitMaterial, error)
FindByGitProviderId(gitProviderId int) (materials []*GitMaterial, err error)
MarkMaterialDeleted(material *GitMaterial) error
MarkMaterialDeleted(tx *pg.Tx, material *GitMaterial) error
FindNumberOfAppsWithGitRepo(appIds []int) (int, error)
FindByAppIds(appIds []int) ([]*GitMaterial, error)
}
Expand Down Expand Up @@ -113,12 +113,12 @@ func (repo MaterialRepositoryImpl) MaterialExists(url string) (bool, error) {
return exists, err
}

func (repo MaterialRepositoryImpl) SaveMaterial(material *GitMaterial) error {
return repo.dbConnection.Insert(material)
func (repo MaterialRepositoryImpl) SaveMaterial(tx *pg.Tx, material *GitMaterial) error {
return tx.Insert(material)
}

func (repo MaterialRepositoryImpl) UpdateMaterial(material *GitMaterial) error {
return repo.dbConnection.Update(material)
func (repo MaterialRepositoryImpl) UpdateMaterial(tx *pg.Tx, material *GitMaterial) error {
return tx.Update(material)
}

func (repo MaterialRepositoryImpl) UpdateMaterialScmId(material *GitMaterial) error {
Expand Down Expand Up @@ -164,9 +164,9 @@ func (repo MaterialRepositoryImpl) FindByGitProviderId(gitProviderId int) (mater
return materials, err
}

func (repo MaterialRepositoryImpl) MarkMaterialDeleted(material *GitMaterial) error {
func (repo MaterialRepositoryImpl) MarkMaterialDeleted(tx *pg.Tx, material *GitMaterial) error {
material.Active = false
return repo.dbConnection.Update(material)
return tx.Update(material)
}

func (repo MaterialRepositoryImpl) FindNumberOfAppsWithGitRepo(appIds []int) (int, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/chartRepo/repository/ChartsRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ type ChartRepository interface {
sql.TransactionWrapper
}

func NewChartRepository(dbConnection *pg.DB) *ChartRepositoryImpl {
func NewChartRepository(dbConnection *pg.DB, TransactionUtilImpl *sql.TransactionUtilImpl) *ChartRepositoryImpl {
return &ChartRepositoryImpl{
dbConnection: dbConnection,
TransactionUtilImpl: sql.NewTransactionUtilImpl(dbConnection),
TransactionUtilImpl: TransactionUtilImpl,
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/repository/EphemeralContainersRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ type EphemeralContainersRepository interface {
FindContainerByName(clusterID int, namespace, podName, name string) (*EphemeralContainerBean, error)
}

func NewEphemeralContainersRepositoryImpl(db *pg.DB) *EphemeralContainersRepositoryImpl {
func NewEphemeralContainersRepositoryImpl(db *pg.DB, transactionUtilImpl *sql.TransactionUtilImpl) *EphemeralContainersRepositoryImpl {
return &EphemeralContainersRepositoryImpl{
dbConnection: db,
TransactionUtilImpl: sql.NewTransactionUtilImpl(db),
TransactionUtilImpl: transactionUtilImpl,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ type GenericNoteHistoryRepository interface {
FindHistoryByNoteId(id []int) ([]GenericNoteHistory, error)
}

func NewGenericNoteHistoryRepositoryImpl(dbConnection *pg.DB) *GenericNoteHistoryRepositoryImpl {
TransactionUtilImpl := sql.NewTransactionUtilImpl(dbConnection)
func NewGenericNoteHistoryRepositoryImpl(dbConnection *pg.DB, TransactionUtilImpl *sql.TransactionUtilImpl) *GenericNoteHistoryRepositoryImpl {
return &GenericNoteHistoryRepositoryImpl{
dbConnection: dbConnection,
TransactionUtilImpl: TransactionUtilImpl,
Expand Down
3 changes: 1 addition & 2 deletions pkg/genericNotes/repository/GenericNoteRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ type GenericNoteRepository interface {
GetDescriptionFromAppIds(appIds []int) ([]*GenericNote, error)
}

func NewGenericNoteRepositoryImpl(dbConnection *pg.DB) *GenericNoteRepositoryImpl {
TransactionUtilImpl := sql.NewTransactionUtilImpl(dbConnection)
func NewGenericNoteRepositoryImpl(dbConnection *pg.DB, TransactionUtilImpl *sql.TransactionUtilImpl) *GenericNoteRepositoryImpl {
return &GenericNoteRepositoryImpl{
dbConnection: dbConnection,
TransactionUtilImpl: TransactionUtilImpl,
Expand Down
4 changes: 2 additions & 2 deletions pkg/infraConfig/infraConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type InfraConfigRepositoryImpl struct {
*sql.TransactionUtilImpl
}

func NewInfraProfileRepositoryImpl(dbConnection *pg.DB) *InfraConfigRepositoryImpl {
func NewInfraProfileRepositoryImpl(dbConnection *pg.DB, TransactionUtilImpl *sql.TransactionUtilImpl) *InfraConfigRepositoryImpl {
return &InfraConfigRepositoryImpl{
dbConnection: dbConnection,
TransactionUtilImpl: sql.NewTransactionUtilImpl(dbConnection),
TransactionUtilImpl: TransactionUtilImpl,
}
}

Expand Down
40 changes: 31 additions & 9 deletions pkg/pipeline/CiCdPipelineOrchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type CiCdPipelineOrchestratorImpl struct {
genericNoteService genericNotes.GenericNoteService
customTagService CustomTagService
chartService chart.ChartService
transactionManager sql.TransactionWrapper
}

func NewCiCdPipelineOrchestrator(
Expand All @@ -156,7 +157,7 @@ func NewCiCdPipelineOrchestrator(
configMapService ConfigMapService,
customTagService CustomTagService,
genericNoteService genericNotes.GenericNoteService,
chartService chart.ChartService) *CiCdPipelineOrchestratorImpl {
chartService chart.ChartService, transactionManager sql.TransactionWrapper) *CiCdPipelineOrchestratorImpl {
return &CiCdPipelineOrchestratorImpl{
appRepository: pipelineGroupRepository,
logger: logger,
Expand All @@ -183,6 +184,7 @@ func NewCiCdPipelineOrchestrator(
genericNoteService: genericNoteService,
customTagService: customTagService,
chartService: chartService,
transactionManager: transactionManager,
}
}

Expand Down Expand Up @@ -1271,6 +1273,11 @@ func (impl CiCdPipelineOrchestratorImpl) DeleteApp(appId int, userId int32) erro
}

func (impl CiCdPipelineOrchestratorImpl) CreateMaterials(createMaterialRequest *bean.CreateMaterialDTO) (*bean.CreateMaterialDTO, error) {
tx, err := impl.transactionManager.StartTx()
if err != nil {
return nil, err
}
defer tx.Rollback()
existingMaterials, err := impl.materialRepository.FindByAppId(createMaterialRequest.AppId)
if err != nil {
impl.logger.Errorw("err", "err", err)
Expand All @@ -1295,7 +1302,7 @@ func (impl CiCdPipelineOrchestratorImpl) CreateMaterials(createMaterialRequest *
var materials []*bean.GitMaterial
for _, inputMaterial := range createMaterialRequest.Material {
inputMaterial.UpdateSanitisedGitRepoUrl()
m, err := impl.createMaterial(inputMaterial, createMaterialRequest.AppId, createMaterialRequest.UserId)
m, err := impl.createMaterial(tx, inputMaterial, createMaterialRequest.AppId, createMaterialRequest.UserId)
inputMaterial.Id = m.Id
if err != nil {
return nil, err
Expand All @@ -1307,12 +1314,22 @@ func (impl CiCdPipelineOrchestratorImpl) CreateMaterials(createMaterialRequest *
impl.logger.Errorw("error in updating to sensor", "err", err)
return nil, err
}
err = impl.transactionManager.CommitTx(tx)
if err != nil {
impl.logger.Errorw("error in committing tx Create material", "err", err, "materials", materials)
return nil, err
}
impl.logger.Debugw("all materials are ", "materials", materials)
return createMaterialRequest, nil
}

func (impl CiCdPipelineOrchestratorImpl) UpdateMaterial(updateMaterialDTO *bean.UpdateMaterialDTO) (*bean.UpdateMaterialDTO, error) {
updatedMaterial, err := impl.updateMaterial(updateMaterialDTO)
tx, err := impl.transactionManager.StartTx()
if err != nil {
return nil, err
}
defer tx.Rollback()
updatedMaterial, err := impl.updateMaterial(tx, updateMaterialDTO)
if err != nil {
impl.logger.Errorw("err", "err", err)
return nil, err
Expand All @@ -1323,6 +1340,11 @@ func (impl CiCdPipelineOrchestratorImpl) UpdateMaterial(updateMaterialDTO *bean.
impl.logger.Errorw("error in updating to git-sensor", "err", err)
return nil, err
}
err = impl.transactionManager.CommitTx(tx)
if err != nil {
impl.logger.Errorw("error in committing tx Update material", "err", err)
return nil, err
}
return updateMaterialDTO, nil
}

Expand Down Expand Up @@ -1456,7 +1478,7 @@ func (impl CiCdPipelineOrchestratorImpl) validateCheckoutPathsForMultiGit(allPat
return nil
}

func (impl CiCdPipelineOrchestratorImpl) updateMaterial(updateMaterialDTO *bean.UpdateMaterialDTO) (*pipelineConfig.GitMaterial, error) {
func (impl CiCdPipelineOrchestratorImpl) updateMaterial(tx *pg.Tx, updateMaterialDTO *bean.UpdateMaterialDTO) (*pipelineConfig.GitMaterial, error) {
existingMaterials, err := impl.materialRepository.FindByAppId(updateMaterialDTO.AppId)
if err != nil {
impl.logger.Errorw("err", "err", err)
Expand Down Expand Up @@ -1497,19 +1519,19 @@ func (impl CiCdPipelineOrchestratorImpl) updateMaterial(updateMaterialDTO *bean.
currentMaterial.FilterPattern = updateMaterialDTO.Material.FilterPattern
currentMaterial.AuditLog = sql.AuditLog{UpdatedBy: updateMaterialDTO.UserId, CreatedBy: currentMaterial.CreatedBy, UpdatedOn: time.Now(), CreatedOn: currentMaterial.CreatedOn}

err = impl.materialRepository.UpdateMaterial(currentMaterial)
err = impl.materialRepository.UpdateMaterial(tx, currentMaterial)

if err != nil {
impl.logger.Errorw("error in updating material", "material", currentMaterial, "err", err)
return nil, err
}

err = impl.gitMaterialHistoryService.CreateMaterialHistory(currentMaterial)
err = impl.gitMaterialHistoryService.CreateMaterialHistory(tx, currentMaterial)

return currentMaterial, nil
}

func (impl CiCdPipelineOrchestratorImpl) createMaterial(inputMaterial *bean.GitMaterial, appId int, userId int32) (*pipelineConfig.GitMaterial, error) {
func (impl CiCdPipelineOrchestratorImpl) createMaterial(tx *pg.Tx, inputMaterial *bean.GitMaterial, appId int, userId int32) (*pipelineConfig.GitMaterial, error) {
basePath := path.Base(inputMaterial.Url)
basePath = strings.TrimSuffix(basePath, ".git")
material := &pipelineConfig.GitMaterial{
Expand All @@ -1523,12 +1545,12 @@ func (impl CiCdPipelineOrchestratorImpl) createMaterial(inputMaterial *bean.GitM
FilterPattern: inputMaterial.FilterPattern,
AuditLog: sql.AuditLog{UpdatedBy: userId, CreatedBy: userId, UpdatedOn: time.Now(), CreatedOn: time.Now()},
}
err := impl.materialRepository.SaveMaterial(material)
err := impl.materialRepository.SaveMaterial(tx, material)
if err != nil {
impl.logger.Errorw("error in saving material", "material", material, "err", err)
return nil, err
}
err = impl.gitMaterialHistoryService.CreateMaterialHistory(material)
err = impl.gitMaterialHistoryService.CreateMaterialHistory(tx, material)
return material, err
}

Expand Down
19 changes: 11 additions & 8 deletions pkg/pipeline/CiMaterialConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
"github.com/devtron-labs/devtron/pkg/bean"
"github.com/devtron-labs/devtron/pkg/pipeline/history"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"github.com/juju/errors"
"go.uber.org/zap"
Expand Down Expand Up @@ -52,6 +53,7 @@ type CiMaterialConfigServiceImpl struct {
gitMaterialHistoryService history.GitMaterialHistoryService
pipelineRepository pipelineConfig.PipelineRepository
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository
transactionManager sql.TransactionWrapper
}

func NewCiMaterialConfigServiceImpl(
Expand All @@ -62,7 +64,7 @@ func NewCiMaterialConfigServiceImpl(
ciPipelineRepository pipelineConfig.CiPipelineRepository,
gitMaterialHistoryService history.GitMaterialHistoryService,
pipelineRepository pipelineConfig.PipelineRepository,
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository) *CiMaterialConfigServiceImpl {
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository, transactionManager sql.TransactionWrapper) *CiMaterialConfigServiceImpl {

return &CiMaterialConfigServiceImpl{
logger: logger,
Expand All @@ -73,6 +75,7 @@ func NewCiMaterialConfigServiceImpl(
gitMaterialHistoryService: gitMaterialHistoryService,
pipelineRepository: pipelineRepository,
ciPipelineMaterialRepository: ciPipelineMaterialRepository,
transactionManager: transactionManager,
}
}

Expand Down Expand Up @@ -122,21 +125,21 @@ func (impl *CiMaterialConfigServiceImpl) DeleteMaterial(request *bean.UpdateMate
existingMaterial.UpdatedOn = time.Now()
existingMaterial.UpdatedBy = request.UserId

err = impl.materialRepo.MarkMaterialDeleted(existingMaterial)
tx, err := impl.transactionManager.StartTx()
if err != nil {
impl.logger.Errorw("error in deleting git material", "gitMaterial", existingMaterial)
return err
}

err = impl.gitMaterialHistoryService.MarkMaterialDeletedAndCreateHistory(existingMaterial)
defer tx.Rollback()

dbConnection := impl.pipelineRepository.GetConnection()
tx, err := dbConnection.Begin()
err = impl.materialRepo.MarkMaterialDeleted(tx, existingMaterial)
if err != nil {
impl.logger.Errorw("error in deleting git material", "gitMaterial", existingMaterial)
return err
}
// Rollback tx on error.
defer tx.Rollback()

err = impl.gitMaterialHistoryService.MarkMaterialDeletedAndCreateHistory(tx, existingMaterial)

var materials []*pipelineConfig.CiPipelineMaterial
for _, pipeline := range pipelines {
materialDbObject, err := impl.ciPipelineMaterialRepository.GetByPipelineIdAndGitMaterialId(pipeline.Id, request.Material.Id)
Expand Down
Loading