You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2022/07/22 09:40:11 UTC

[incubator-devlake] 06/13: fix: change table desgn in components commit_file_components commit_files

This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit d9b99a69b221c5204e1681cc1e6071fa5fa71e19
Author: xgdyp <zx...@163.com>
AuthorDate: Fri Jul 15 17:22:50 2022 +0800

    fix: change table desgn in components commit_file_components commit_files
---
 models/domainlayer/code/commit.go                | 21 ++++++-------
 models/migrationscripts/updateSchemas20220711.go | 39 +++++++++---------------
 plugins/gitextractor/models/interface.go         |  2 +-
 plugins/gitextractor/parser/repo.go              | 25 ++++++++-------
 plugins/gitextractor/store/csv.go                |  2 +-
 plugins/gitextractor/store/database.go           |  2 +-
 6 files changed, 39 insertions(+), 52 deletions(-)

diff --git a/models/domainlayer/code/commit.go b/models/domainlayer/code/commit.go
index 77065b1e..1fc76bc0 100644
--- a/models/domainlayer/code/commit.go
+++ b/models/domainlayer/code/commit.go
@@ -46,11 +46,11 @@ func (Commit) TableName() string {
 
 type CommitFile struct {
 	common.NoPKModel
-	CommitFileID string `gorm:"primaryKey;type:varchar(255)"`
-	CommitSha    string `gorm:"type:varchar(40)"`
-	FilePath     string `gorm:"type:varchar(255)"`
-	Additions    int
-	Deletions    int
+	ID        string `gorm:"primaryKey;type:varchar(255)"`
+	CommitSha string `gorm:"type:varchar(40)"`
+	FilePath  string `gorm:"type:varchar(255)"`
+	Additions int
+	Deletions int
 }
 
 func (CommitFile) TableName() string {
@@ -59,21 +59,20 @@ func (CommitFile) TableName() string {
 
 type Component struct {
 	RepoId    string `gorm:"primaryKey;type:varchar(255)"`
-	Component string `gorm:"primaryKey;type:varchar(255)"`
+	Name      string `gorm:"type:varchar(255)"`
 	PathRegex string `gorm:"type:varchar(255)"`
 }
 
 func (Component) TableName() string {
-	return "component"
+	return "components"
 }
 
-type FileComponent struct {
+type CommitFileComponent struct {
 	common.NoPKModel
 	CommitFileID string `gorm:"primaryKey;type:varchar(255)"`
-	RepoId       string `gorm:"primaryKey;type:varchar(255)"`
 	Component    string `gorm:"type:varchar(255)"`
 }
 
-func (FileComponent) TableName() string {
-	return "file_component"
+func (CommitFileComponent) TableName() string {
+	return "commit_file_components"
 }
diff --git a/models/migrationscripts/updateSchemas20220711.go b/models/migrationscripts/updateSchemas20220711.go
index 4aa8c64b..15ad50f7 100644
--- a/models/migrationscripts/updateSchemas20220711.go
+++ b/models/migrationscripts/updateSchemas20220711.go
@@ -23,55 +23,44 @@ import (
 	"gorm.io/gorm"
 )
 
-//type CodeComponent20220711 struct {
-//	ComponentId string `gorm:"primaryKey;type:varchar(255)"`
-//	PathRegex   string `gorm:"type:varchar(255)"`
-//}
-//
-//func (CodeComponent20220711) TableName() string {
-//	return "code_component_20220711"
-//}
-
 type Component struct {
-	RepoId    string `gorm:"primaryKey;type:varchar(255)"`
-	Component string `gorm:"primaryKey;type:varchar(255)"`
+	RepoId    string `gorm:"type:varchar(255)"`
+	Name      string `gorm:"primaryKey;type:varchar(255)"`
 	PathRegex string `gorm:"type:varchar(255)"`
 }
 
 func (Component) TableName() string {
-	return "component"
+	return "components"
 }
 
 type CommitFile struct {
 	common.NoPKModel
-	CommitFileID string `gorm:"primaryKey;type:varchar(255)"`
-	CommitSha    string `gorm:"type:varchar(40)"`
-	FilePath     string `gorm:"type:varchar(255)"`
-	Additions    int
-	Deletions    int
-	Component    string `gorm:"type:varchar(255)"`
+	ID        string `gorm:"primaryKey;type:varchar(255)"`
+	CommitSha string `gorm:"type:varchar(40)"`
+	FilePath  string `gorm:"type:varchar(255)"`
+	Additions int
+	Deletions int
 }
 
 func (CommitFile) TableName() string {
 	return "commit_files"
 }
 
-type FileComponent struct {
+type CommitFileComponent struct {
 	common.NoPKModel
 	CommitFileID string `gorm:"primaryKey;type:varchar(255)"`
-	RepoId       string `gorm:"primaryKey;type:varchar(255)"`
 	Component    string `gorm:"type:varchar(255)"`
 }
 
-func (FileComponent) TableName() string {
-	return "file_component"
+func (CommitFileComponent) TableName() string {
+	return "commit_file_components"
 }
 
 type updateSchemas20220711 struct{}
 
 func (*updateSchemas20220711) Up(ctx context.Context, db *gorm.DB) error {
 
-	err := db.Migrator().AutoMigrate(Component{}, CommitFile{}, FileComponent{})
+	err := db.Migrator().AutoMigrate(Component{}, CommitFile{}, CommitFileComponent{})
 	if err != nil {
 		return err
 	}
@@ -80,9 +69,9 @@ func (*updateSchemas20220711) Up(ctx context.Context, db *gorm.DB) error {
 }
 
 func (*updateSchemas20220711) Version() uint64 {
-	return 202207151420
+	return 202207151644
 }
 
 func (*updateSchemas20220711) Name() string {
-	return "file_component table"
+	return "add commit_file_components components table,update commit_files table"
 }
diff --git a/plugins/gitextractor/models/interface.go b/plugins/gitextractor/models/interface.go
index a5ae3a86..b3e5ca6c 100644
--- a/plugins/gitextractor/models/interface.go
+++ b/plugins/gitextractor/models/interface.go
@@ -27,6 +27,6 @@ type Store interface {
 	Refs(ref *code.Ref) error
 	CommitFiles(file *code.CommitFile) error
 	CommitParents(pp []*code.CommitParent) error
-	FileComponent(component *code.FileComponent) error
+	FileComponent(component *code.CommitFileComponent) error
 	Close() error
 }
diff --git a/plugins/gitextractor/parser/repo.go b/plugins/gitextractor/parser/repo.go
index a796ca23..8807ded0 100644
--- a/plugins/gitextractor/parser/repo.go
+++ b/plugins/gitextractor/parser/repo.go
@@ -198,7 +198,7 @@ func (r *GitRepo) CollectCommits(subtaskCtx core.SubTaskContext) error {
 	}
 	componentMap := make(map[string]*regexp.Regexp)
 	for _, component := range components {
-		componentMap[component.Component] = regexp.MustCompile(component.PathRegex)
+		componentMap[component.Name] = regexp.MustCompile(component.PathRegex)
 	}
 	odb, err := r.repo.Odb()
 	if err != nil {
@@ -312,7 +312,7 @@ func (r *GitRepo) getDiffComparedToParent(commitSha string, commit *git.Commit,
 
 func (r *GitRepo) storeCommitFilesFromDiff(commitSha string, diff *git.Diff, componentMap map[string]*regexp.Regexp) error {
 	var commitFile *code.CommitFile
-	var commitfileComponent *code.FileComponent
+	var commitFileComponent *code.CommitFileComponent
 	var err error
 	err = diff.ForEach(func(file git.DiffDelta, progress float64) (
 		git.DiffForEachHunkCallback, error) {
@@ -327,20 +327,19 @@ func (r *GitRepo) storeCommitFilesFromDiff(commitSha string, diff *git.Diff, com
 		commitFile = new(code.CommitFile)
 		commitFile.CommitSha = commitSha
 		commitFile.FilePath = file.NewFile.Path
-		commitFile.CommitFileID = commitSha + ":" + file.NewFile.Path
-		commitfileComponent = new(code.FileComponent)
+		commitFile.ID = commitSha + ":" + file.NewFile.Path
+		commitFileComponent = new(code.CommitFileComponent)
 		for component, reg := range componentMap {
 			if reg.MatchString(commitFile.FilePath) {
-				commitfileComponent.Component = component
+				commitFileComponent.Component = component
 				break
 			}
 		}
-		commitfileComponent.RepoId = r.id
-		commitfileComponent.CommitFileID = commitSha + ":" + file.NewFile.Path
-		//commitfileComponent.FilePath = file.NewFile.Path
-		//commitfileComponent.CommitSha = commitSha
-		if commitfileComponent.Component == "" {
-			commitfileComponent.Component = "Default"
+		commitFileComponent.CommitFileID = commitSha + ":" + file.NewFile.Path
+		//commitFileComponent.FilePath = file.NewFile.Path
+		//commitFileComponent.CommitSha = commitSha
+		if commitFileComponent.Component == "" {
+			commitFileComponent.Component = "Default"
 		}
 		return func(hunk git.DiffHunk) (git.DiffForEachLineCallback, error) {
 			return func(line git.DiffLine) error {
@@ -354,8 +353,8 @@ func (r *GitRepo) storeCommitFilesFromDiff(commitSha string, diff *git.Diff, com
 			}, nil
 		}, nil
 	}, git.DiffDetailLines)
-	if commitfileComponent != nil {
-		err = r.store.FileComponent(commitfileComponent)
+	if commitFileComponent != nil {
+		err = r.store.FileComponent(commitFileComponent)
 		if err != nil {
 			r.logger.Error("FileComponent error:", err)
 		}
diff --git a/plugins/gitextractor/store/csv.go b/plugins/gitextractor/store/csv.go
index bdf4205c..fa2b8845 100644
--- a/plugins/gitextractor/store/csv.go
+++ b/plugins/gitextractor/store/csv.go
@@ -132,7 +132,7 @@ func (c *CsvStore) Refs(ref *code.Ref) error {
 func (c *CsvStore) CommitFiles(file *code.CommitFile) error {
 	return c.commitFileWriter.Write(file)
 }
-func (c *CsvStore) FileComponent(component *code.FileComponent) error {
+func (c *CsvStore) FileComponent(component *code.CommitFileComponent) error {
 	return c.commitFileWriter.Write(component)
 }
 
diff --git a/plugins/gitextractor/store/database.go b/plugins/gitextractor/store/database.go
index 564433d5..8bca2147 100644
--- a/plugins/gitextractor/store/database.go
+++ b/plugins/gitextractor/store/database.go
@@ -91,7 +91,7 @@ func (d *Database) CommitFiles(file *code.CommitFile) error {
 	return batch.Add(file)
 }
 
-func (d *Database) FileComponent(commitfile *code.FileComponent) error {
+func (d *Database) FileComponent(commitfile *code.CommitFileComponent) error {
 	batch, err := d.driver.ForType(reflect.TypeOf(commitfile))
 	if err != nil {
 		return err