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 Migrations for category and tag table
- Modifies category parsing logic from config file

Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 committed Jun 1, 2021
1 parent 9106afc commit 55c0dd8
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 20 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,36 @@
// 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 removeCatgoryAndTagAssociation(log *log.Logger) *gormigrate.Migration {

return &gormigrate.Migration{
ID: "202105311600_remove_categories_and_tag_association_from_category_table",
Migrate: func(db *gorm.DB) error {
if err := db.AutoMigrate(&model.Category{}); err != nil {
log.Error(err)
return err
}
return nil
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 removeCatgoryIdFromTagtable(log *log.Logger) *gormigrate.Migration {

return &gormigrate.Migration{
ID: "202105311800_remove_categoryid_column_from_tag_table.go",
Migrate: func(db *gorm.DB) error {
if err := db.AutoMigrate(&model.Tag{}); err != nil {
log.Error(err)
return err
}
return nil
},
}
}
2 changes: 2 additions & 0 deletions api/pkg/db/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func Migrate(api *app.APIBase) error {
addRefreshTokenChecksumColumnInUserTable(log),
updateCatalogBranchToMain(log),
addAvatarURLColumnInUsersTable(log),
removeCatgoryAndTagAssociation(log),
removeCatgoryIdFromTagtable(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
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 55c0dd8

Please sign in to comment.