Skip to content

Commit

Permalink
fix: table name case sensitivity in join and pattern statement (#1314)
Browse files Browse the repository at this point in the history
* fix: table name case sensitivity in join and pattern statement

* fix: a unit test error
  • Loading branch information
liu-hm19 authored Nov 13, 2024
1 parent ec0231b commit 210ef29
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions pkg/infra/persistence/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (r *resourceRepository) Get(ctx context.Context, id uint) (*entity.Resource
var dataModel ResourceModel
err := r.db.WithContext(ctx).
Preload("Stack").Preload("Stack.Project").Preload("Stack.Project.Organization").Preload("Stack.Project.Source").
Joins("JOIN Stack ON Stack.id = Resource.stack_id").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN stack ON stack.id = resource.stack_id").
Joins("JOIN project ON project.id = stack.project_id").
First(&dataModel, id).Error
if err != nil {
return nil, err
Expand All @@ -125,8 +125,8 @@ func (r *resourceRepository) GetByKusionResourceID(ctx context.Context, id strin
var dataModel ResourceModel
err := r.db.WithContext(ctx).
Preload("Stack").Preload("Stack.Project").Preload("Stack.Project.Organization").Preload("Stack.Project.Source").
Joins("JOIN Stack ON Stack.id = Resource.stack_id").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN stack ON stack.id = resource.stack_id").
Joins("JOIN project ON project.id = stack.project_id").
Where("kusion_resource_id = ?", id).
First(&dataModel).Error
if err != nil {
Expand All @@ -142,8 +142,8 @@ func (r *resourceRepository) List(ctx context.Context, filter *entity.ResourceFi
pattern, args := GetResourceQuery(filter)
result := r.db.WithContext(ctx).
Preload("Stack").Preload("Stack.Project").Preload("Stack.Project.Organization").Preload("Stack.Project.Source").
Joins("JOIN Stack ON Stack.id = Resource.stack_id").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN stack ON stack.id = resource.stack_id").
Joins("JOIN project ON project.id = stack.project_id").
Where(pattern, args...).
Find(&dataModel)
if result.Error != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/infra/persistence/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (r *runRepository) Get(ctx context.Context, id uint) (*entity.Run, error) {
var dataModel RunModel
err := r.db.WithContext(ctx).
Preload("Stack").Preload("Stack.Project").
Joins("JOIN Stack ON Stack.id = Run.stack_id").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN stack ON stack.id = run.stack_id").
Joins("JOIN project ON project.id = stack.project_id").
First(&dataModel, id).Error
if err != nil {
return nil, err
Expand All @@ -102,9 +102,9 @@ func (r *runRepository) List(ctx context.Context, filter *entity.RunFilter) ([]*
pattern, args := GetRunQuery(filter)
result := r.db.WithContext(ctx).
Preload("Stack").Preload("Stack.Project").
Joins("JOIN Stack ON Stack.id = Run.stack_id").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN Workspace ON Workspace.name = Run.workspace").
Joins("JOIN stack ON stack.id = run.stack_id").
Joins("JOIN project ON project.id = stack.project_id").
Joins("JOIN workspace ON workspace.name = run.workspace").
Where(pattern, args...).
Find(&dataModel)
if result.Error != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/persistence/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (r *stackRepository) Get(ctx context.Context, id uint) (*entity.Stack, erro
var dataModel StackModel
err := r.db.WithContext(ctx).
Preload("Project").Preload("Project.Organization").Preload("Project.Source").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN project ON project.id = stack.project_id").
First(&dataModel, id).Error
if err != nil {
return nil, err
Expand All @@ -104,7 +104,7 @@ func (r *stackRepository) List(ctx context.Context, filter *entity.StackFilter)
pattern, args := GetStackQuery(filter)
result := r.db.WithContext(ctx).
Preload("Project").Preload("Project.Organization").Preload("Project.Source").
Joins("JOIN Project ON Project.id = Stack.project_id").
Joins("JOIN project ON project.id = stack.project_id").
Where(pattern, args...).
Find(&dataModel)
if result.Error != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/infra/persistence/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ func GetStackQuery(filter *entity.StackFilter) (string, []interface{}) {
pattern := make([]string, 0)
args := make([]interface{}, 0)
if filter.OrgID != 0 {
pattern = append(pattern, "Project.organization_id = ?")
pattern = append(pattern, "project.organization_id = ?")
args = append(args, fmt.Sprint(filter.OrgID))
}
if filter.ProjectID != 0 {
pattern = append(pattern, "project_id = ?")
args = append(args, fmt.Sprint(filter.ProjectID))
}
if filter.Path != "" {
pattern = append(pattern, "Stack.path = ?")
pattern = append(pattern, "stack.path = ?")
args = append(args, filter.Path)
}
return CombineQueryParts(pattern), args
Expand Down Expand Up @@ -165,15 +165,15 @@ func GetRunQuery(filter *entity.RunFilter) (string, []interface{}) {
pattern := make([]string, 0)
args := make([]interface{}, 0)
if filter.ProjectID != 0 {
pattern = append(pattern, "Project.ID = ?")
pattern = append(pattern, "project.ID = ?")
args = append(args, fmt.Sprint(filter.ProjectID))
}
if filter.StackID != 0 {
pattern = append(pattern, "stack_id = ?")
args = append(args, filter.StackID)
}
if filter.Workspace != "" {
pattern = append(pattern, "Workspace.name = ?")
pattern = append(pattern, "workspace.name = ?")
args = append(args, filter.Workspace)
}
return CombineQueryParts(pattern), args
Expand Down
6 changes: 3 additions & 3 deletions pkg/infra/persistence/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestGetStackQuery(t *testing.T) {
filter: &entity.StackFilter{
OrgID: 42,
},
expectedQuery: "Project.organization_id = ?",
expectedQuery: "project.organization_id = ?",
expectedArgs: []interface{}{"42"},
},
{
Expand All @@ -126,7 +126,7 @@ func TestGetStackQuery(t *testing.T) {
filter: &entity.StackFilter{
Path: "example",
},
expectedQuery: "Stack.path = ?",
expectedQuery: "stack.path = ?",
expectedArgs: []interface{}{"example"},
},
{
Expand All @@ -136,7 +136,7 @@ func TestGetStackQuery(t *testing.T) {
ProjectID: 1,
Path: "example",
},
expectedQuery: "Project.organization_id = ? AND project_id = ? AND Stack.path = ?",
expectedQuery: "project.organization_id = ? AND project_id = ? AND stack.path = ?",
expectedArgs: []interface{}{"42", "1", "example"},
},
}
Expand Down

0 comments on commit 210ef29

Please sign in to comment.