Skip to content

Commit

Permalink
Removes the association between Category and Tag table
Browse files Browse the repository at this point in the history
This commit includes following changes

- Modifies category and tag table
- Add Migration for tag table to drop category_id column
  and delete the category-tag constraints
- Modifies category parsing logic

Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 committed Jun 8, 2021
1 parent 29d53ef commit 611bf10
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
1 change: 0 additions & 1 deletion api/pkg/app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Data struct {

type Category struct {
Name string
Tags []string
}

type Catalog struct {
Expand Down
7 changes: 0 additions & 7 deletions api/pkg/db/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ func addCategories(db *gorm.DB, log *log.Logger, data *app.Data) error {
log.Error(err)
return err
}
for _, t := range c.Tags {
tag := model.Tag{Name: t, CategoryID: cat.ID}
if err := db.Where(tag).FirstOrCreate(&tag).Error; err != nil {
log.Error(err)
return err
}
}
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © 2021 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package migration

import (
"github.com/go-gormigrate/gormigrate/v2"
"github.com/tektoncd/hub/api/gen/log"
"github.com/tektoncd/hub/api/pkg/db/model"
"gorm.io/gorm"
)

func removeCatgoryIdColumnAndConstraintsFromTagtable(log *log.Logger) *gormigrate.Migration {

return &gormigrate.Migration{
ID: "202105311800_remove_categoryid_column_and_category_tag_constraints_from_tag_table",
Migrate: func(db *gorm.DB) error {

if err := db.Migrator().DropConstraint(&model.Tag{}, "fk_categories_tags"); err != nil {
log.Error(err)
return err
}

if err := db.Migrator().DropConstraint(&model.Tag{}, "fk_tags_category"); err != nil {
log.Error(err)
return err
}

if err := db.Migrator().DropColumn(&model.Tag{}, "category_id"); err != nil {
log.Error(err)
return err
}

return nil
},
}
}
1 change: 1 addition & 0 deletions api/pkg/db/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Migrate(api *app.APIBase) error {
addRefreshTokenChecksumColumnInUserTable(log),
updateCatalogBranchToMain(log),
addAvatarURLColumnInUsersTable(log),
removeCatgoryIdColumnAndConstraintsFromTagtable(log),
},
)

Expand Down
7 changes: 2 additions & 5 deletions api/pkg/db/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ type (
Category struct {
gorm.Model
Name string `gorm:"not null;unique"`
Tags []Tag
}

Tag struct {
gorm.Model
Name string `gorm:"not null;unique"`
Category Category
CategoryID uint
Resources []*Resource `gorm:"many2many:resource_tags;"`
Name string `gorm:"not null;unique"`
Resources []*Resource `gorm:"many2many:resource_tags;"`
}

Catalog struct {
Expand Down
4 changes: 1 addition & 3 deletions api/pkg/service/catalog/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,10 @@ func (s *syncer) updateResourceTags(
if len(tags) == 0 {
return
}
others := model.Category{}
txn.Model(&model.Category{}).Where(&model.Category{Name: "Others"}).First(&others)

for _, t := range tags {

tag := model.Tag{Name: t, CategoryID: others.ID}
tag := model.Tag{Name: t}

txn.Model(&model.Tag{}).Where(&model.Tag{Name: t}).FirstOrCreate(&tag)

Expand Down
1 change: 0 additions & 1 deletion api/pkg/service/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (s *service) List(ctx context.Context) (*category.ListResult, error) {
if err := db.Model(&model.Category{}).Order("name").Find(&all).Error; err != nil {
log.Error(err)
return nil, fetchError

}

res := []*category.Category{}
Expand Down
4 changes: 0 additions & 4 deletions api/test/fixtures/tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@

- id: 1
name: ztag
category_id: 1
created_at: 2016-01-01 12:30:12 UTC
updated_at: 2016-01-01 12:30:12 UTC
- id: 2
name: rtag
category_id: 2
created_at: 2016-01-01 12:30:12 UTC
updated_at: 2016-01-01 12:30:12 UTC
- id: 3
name: ptag
category_id: 3
created_at: 2016-01-01 12:30:12 UTC
updated_at: 2016-01-01 12:30:12 UTC
- id: 4
name: atag
category_id: 2
created_at: 2016-01-01 12:30:12 UTC
updated_at: 2016-01-01 12:30:12 UTC

0 comments on commit 611bf10

Please sign in to comment.