Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Catalog sync to delete the removed resources info from existing db #292

Merged
merged 1 commit into from
Oct 21, 2021

Conversation

pratap0007
Copy link
Contributor

@pratap0007 pratap0007 commented Jul 15, 2021

This patch includes following changes

  • Adds a migration to add OnDelete:CASECADE constraint on existing
    foreign key constraints
  • Update the catalog sync which compares hosted catalog resources to
    existing db info of resources for same catalog while syncing catalogs and delete
    resources info from the db if any resources from the hosted catalog are removed

Signed-off-by: Shiv Verma shverma@redhat.com

Changes

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes tests (if functionality changed/added)
  • Run API Unit Tests, Lint Checks, API Design, Golden Files with make api-check
  • Run UI Unit Tests, Lint Checks with make ui-check
  • Commit messages follow commit message best practices

See the contribution guide for more details.

@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 15, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 6c74cac to b29c628 Compare July 19, 2021 06:02
@tekton-robot tekton-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 19, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from b29c628 to 47be2b9 Compare July 22, 2021 10:18
@tekton-robot tekton-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 22, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 47be2b9 to b90c97e Compare July 22, 2021 10:21
@tekton-robot tekton-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 22, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch 3 times, most recently from 8ef9a39 to 5cca636 Compare July 28, 2021 05:08
@pratap0007
Copy link
Contributor Author

/test pull-tekton-hub-unit-tests

@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 5cca636 to d112a15 Compare July 29, 2021 13:39
@pratap0007
Copy link
Contributor Author

/retest

@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from d112a15 to a98edc8 Compare July 30, 2021 12:21
@tekton-robot tekton-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 10, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from a98edc8 to 1674c51 Compare August 16, 2021 04:58
@tekton-robot tekton-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 16, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 1674c51 to 63a4fa8 Compare August 20, 2021 13:59
@@ -55,9 +56,6 @@ func Migrate(api *app.APIBase) error {
&model.SyncJob{},
&model.Scope{},
&model.Config{},
&model.Platform{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing these from here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were added in the migration as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are removing platform? the other 2 will be autocreated how platform will get created on a new db installation if we remove from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here platforms is optional and we can keep or remove it,if we remove platforms from here it will get created by the automigration on a new db installation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay but I didn't understood why this is optional? 🤔 on new db creation initschema

migration.InitSchema(func(db *gorm.DB) error {
is run right?
and if platforms is not added there still it will get created

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So platform table is getting created because of this migration

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

@tekton-robot tekton-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 1, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 63a4fa8 to 56a2b98 Compare September 1, 2021 09:20
@tekton-robot tekton-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 1, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 56a2b98 to 3c1ec7d Compare September 1, 2021 09:22
}

// Delete the tags which doesn't belongs to any other existing resources
func (s *syncer) deleteTag(txn *gorm.DB, log *zap.SugaredLogger, tags []model.ResourceTag) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to do this as part of this api ? 🤔 what if we have a clean up api which would remove things like this which are no longer required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes , if we want to have a clean up api then how we would identify which things are no longer required ?

ID: "202107221500_add_ondelete_constraints",
Migrate: func(db *gorm.DB) error {
txn := db.Begin()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a function like this https://github.com/tektoncd/hub/blob/main/api/pkg/db/migration/202107291608_refresh_all_tables.go#L36
and handle the error in a different function instead of writing txn.Rollback multiple times

@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from 3c1ec7d to f8f3086 Compare September 9, 2021 09:04
return err
}

if err := txn.Exec("ALTER TABLE resource_categories ADD CONSTRAINT fk_resource_categories_resource FOREIGN KEY (resource_id) REFERENCES resources(id) ON DELETE CASCADE;").Error; err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not mandatory just a thought can't we use update query from gorm instead of writing the whole query, else the patch looks fine to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example
Drop Constraint
update column from table by adding foreign key constraints with on Delete cascade
For ref : https://gorm.io/docs/update.html#Update-single-column

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we can't do with gorm and with gorm CreateConstraint does't create

@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from f8f3086 to b2cfc4f Compare September 15, 2021 13:46
@pratap0007
Copy link
Contributor Author

/test pull-tekton-hub-unit-tests

Copy link
Member

@PuneetPunamiya PuneetPunamiya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Platform linux/amd64 is getting deleted which is still there in the other resources. Could you please take a look at it once

/hold

@tekton-robot tekton-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 12, 2021
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from b2cfc4f to a0a060e Compare October 12, 2021 12:28
@PuneetPunamiya
Copy link
Member

Thanks 🙂

/approve
/meow

/hold cancel

@tekton-robot
Copy link

@PuneetPunamiya: cat image

In response to this:

Thanks 🙂

/approve
/meow

/hold cancel

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tekton-robot tekton-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 13, 2021
@tekton-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: PuneetPunamiya

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 13, 2021
Copy link
Member

@vinamra28 vinamra28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also at the time of migration from 1.4 to latest migration fails. Can you please look into this?
/hold

err := addOnDelete(txn, log)
if err != nil {
txn.Rollback()
log.Error(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I guess we can avoid logging this error since we are already logging the errors in addOnDelete function

@tekton-robot tekton-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 14, 2021
…sting db

This patch includes following changes

- Adds a migration to add `OnDelete:CASECADE` constraint on existing
  foreign key constraints
- Update the catalog sync which compares hosted catalog resources to
  existing db info of resources for same catalog while syncing catalogs and delete
  resources info from the db if any resources from the hosted catalog are removed

Signed-off-by: Shiv Verma <shverma@redhat.com>
@pratap0007 pratap0007 force-pushed the delete-removed-resource branch from a0a060e to 81e16b4 Compare October 14, 2021 11:16
@pratap0007
Copy link
Contributor Author

@vinamra28 done, could you please take a look once more

@vinamra28
Copy link
Member

/hold cancel
/lgtm

@tekton-robot tekton-robot added lgtm Indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Oct 21, 2021
@tekton-robot tekton-robot merged commit efdc199 into tektoncd:main Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants