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

Remove controller soft delete #1229

Merged
merged 4 commits into from
Jun 11, 2024

Conversation

kian99
Copy link
Contributor

@kian99 kian99 commented Jun 10, 2024

Description

This PR allows a controller to be removed from JIMM and then later re-added. This is not currently possible because removing a controller results in a soft-delete which is not handled properly when adding the controller again. To resolve this I've simply removed soft-deletes.

Another issue that cropped up because of this is that the function to remove a controller was also deleting all models associated with that controller first. This functions was broken because the Models property on the controller was never populated when fetching a controller. This could be fixed by adding a .Preload("Models") on the GetController function but would be very unnecessary as the only place we use this reverse-relationship from a Controller->Models is when we remove a controller.

Instead I've opted to introduce a new GetModelByController function to do this action.

Fixes CSS-8891

Engineering checklist

Check only items that apply

  • Documentation updated
  • Covered by unit tests
  • Covered by integration tests

Copy link
Contributor

@ale8k ale8k left a comment

Choose a reason for hiding this comment

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

lgtm nice work, two minor things

internal/db/model.go Outdated Show resolved Hide resolved
internal/db/model.go Outdated Show resolved Hide resolved
internal/db/model_test.go Show resolved Hide resolved
}
var models []dbmodel.Model
db := d.DB.WithContext(ctx)
err := db.Where("controller_id = ?", controllerID).Find(&models).Error
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer this approach, makes more sense to me. Really wish our domain objects had logic like this inside though. for example controller.LoadModels() - I guess another argument to refactor our whole "Database" vs "Models" thing going on here

Copy link
Collaborator

Choose a reason for hiding this comment

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

if we are going this way.. then what's the point in using gorm? should we simply abandon gorm in favor of sqlair?

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 mean the with gorm I don't need to write

fmt.Sprintf("SELECT * FROM models WHERE controller_id = %s",...)

Which is very helpful. And I'm sure we get other benefits that I can't think up right now. All this change means is that we don't get all the models every time we get a controller. We could just as easily remove our entire DB package and do the gorm queries in internal/jimm when we need them. So db.Controllers.Preload("Models").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed it to more gorm appropriate syntax

Copy link
Contributor

Choose a reason for hiding this comment

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

No you just use the gorm model and put its domain methods on the model itself

Copy link
Collaborator

@alesstimec alesstimec left a comment

Choose a reason for hiding this comment

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

LGTM once alex's comments are addressed

}
var models []dbmodel.Model
db := d.DB.WithContext(ctx)
err := db.Where("controller_id = ?", controllerID).Find(&models).Error
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we are going this way.. then what's the point in using gorm? should we simply abandon gorm in favor of sqlair?

internal/db/model_test.go Show resolved Hide resolved
@kian99 kian99 force-pushed the CSS-8891-remove-controller-soft-delete branch from 4f1e611 to a80cee3 Compare June 10, 2024 16:02
@kian99 kian99 merged commit 4618aec into canonical:v3 Jun 11, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants