From 55c0dd8d47772f148e29db3dd22c098f0e33bf31 Mon Sep 17 00:00:00 2001 From: pratap0007 Date: Tue, 1 Jun 2021 13:57:57 +0530 Subject: [PATCH] Removes the association between Category and Tag table 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 --- api/pkg/app/data.go | 1 - api/pkg/db/initializer/initializer.go | 7 ---- ...and_tag_association_from_category_table.go | 36 +++++++++++++++++++ ...remove_categoryid_column_from_tag_table.go | 36 +++++++++++++++++++ api/pkg/db/migration/migration.go | 2 ++ api/pkg/db/model/model.go | 7 ++-- api/pkg/service/catalog/syncer.go | 4 +-- api/test/fixtures/tags.yaml | 4 --- 8 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 api/pkg/db/migration/202105311600_remove_categories_and_tag_association_from_category_table.go create mode 100644 api/pkg/db/migration/202105311800_remove_categoryid_column_from_tag_table.go diff --git a/api/pkg/app/data.go b/api/pkg/app/data.go index 2290f51f9d..e8b0951bc7 100644 --- a/api/pkg/app/data.go +++ b/api/pkg/app/data.go @@ -31,7 +31,6 @@ type Data struct { type Category struct { Name string - Tags []string } type Catalog struct { diff --git a/api/pkg/db/initializer/initializer.go b/api/pkg/db/initializer/initializer.go index 49d6adc9c1..d4a7066841 100644 --- a/api/pkg/db/initializer/initializer.go +++ b/api/pkg/db/initializer/initializer.go @@ -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 } diff --git a/api/pkg/db/migration/202105311600_remove_categories_and_tag_association_from_category_table.go b/api/pkg/db/migration/202105311600_remove_categories_and_tag_association_from_category_table.go new file mode 100644 index 0000000000..bc0223dbf8 --- /dev/null +++ b/api/pkg/db/migration/202105311600_remove_categories_and_tag_association_from_category_table.go @@ -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 + }, + } +} diff --git a/api/pkg/db/migration/202105311800_remove_categoryid_column_from_tag_table.go b/api/pkg/db/migration/202105311800_remove_categoryid_column_from_tag_table.go new file mode 100644 index 0000000000..524db94a4b --- /dev/null +++ b/api/pkg/db/migration/202105311800_remove_categoryid_column_from_tag_table.go @@ -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 + }, + } +} diff --git a/api/pkg/db/migration/migration.go b/api/pkg/db/migration/migration.go index 267ebf8804..0ed144563c 100644 --- a/api/pkg/db/migration/migration.go +++ b/api/pkg/db/migration/migration.go @@ -36,6 +36,8 @@ func Migrate(api *app.APIBase) error { addRefreshTokenChecksumColumnInUserTable(log), updateCatalogBranchToMain(log), addAvatarURLColumnInUsersTable(log), + removeCatgoryAndTagAssociation(log), + removeCatgoryIdFromTagtable(log), }, ) diff --git a/api/pkg/db/model/model.go b/api/pkg/db/model/model.go index 2d4d5192f8..e0de0f8b15 100644 --- a/api/pkg/db/model/model.go +++ b/api/pkg/db/model/model.go @@ -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 { diff --git a/api/pkg/service/catalog/syncer.go b/api/pkg/service/catalog/syncer.go index 05ea6ad0dd..72e683350e 100644 --- a/api/pkg/service/catalog/syncer.go +++ b/api/pkg/service/catalog/syncer.go @@ -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) diff --git a/api/test/fixtures/tags.yaml b/api/test/fixtures/tags.yaml index 16af90e31d..7249652f0e 100644 --- a/api/test/fixtures/tags.yaml +++ b/api/test/fixtures/tags.yaml @@ -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