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

refactor(obligations): Make separate database tables for classification and type #86

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

deo002
Copy link
Collaborator

@deo002 deo002 commented Oct 15, 2024

Changes

  • Make separate database tables for classification and type. They are not a fixed set of values. We can add/remove the possible classification and type for obligation. Support for this functionality will be added in future PRs.
  • Use GORM notation for many-to-many relationship of obligation maps.
  • While converting from the new Obligation struct to json, the classification, type and shortnames were not being marshalled in the desired format. So wrote custom marshaller and unmarshaller for it. We could have used Converter functions too, same effort would be required.
  • Added BeforeCreate and BeforeUpdate hook for data validation(non empty strings for topic, text, unique topic and text, valid classification and type values etc).
  • Deleted optional data type wrappers as they are no longer needed.

Submitter Checklist

  • Includes tests (if there is a feature changed/added)
  • Includes docs ( if changes are user facing)
  • I have tested my changes locally.

References

Copy link
Member

@GMishx GMishx left a comment

Choose a reason for hiding this comment

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

Changes looks good. Needs test

Copy link
Member

@avinal avinal left a comment

Choose a reason for hiding this comment

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

Few nitpicks, looks good to me.

Comment on lines +917 to +919
if licenses[i].Shortname != nil {
erroredLicense = *licenses[i].Shortname
}
Copy link
Member

Choose a reason for hiding this comment

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

Good one

Classification *ObligationClassification `gorm:"foreignKey:ObligationClassificationId"`
}

func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) {
Copy link
Member

Choose a reason for hiding this comment

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

I would highly recommend moving all functions out of this file. Probably in the same package but a different file. It somewhat undermines the structure of the project.

Not a blocker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. What do you think about moving a GORM struct and all it's methods into separate files? Like one for licenses, one for obligations etc.
I'll do it in another PR, this one's already big.

Copy link
Member

Choose a reason for hiding this comment

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

This is a good idea, under models package, you can separate them into differnt files.

@GMishx
Copy link
Member

GMishx commented Oct 18, 2024

For existing database, these new columns should hold some default values:

LicenseDb/laas -dbname laas -datafile fossology/install/db/licenseRef.json -populatedb true

2024/10/18 10:33:40 LicenseDb/cmd/laas/main.go:53 ERROR: column "obligation_classification_id" of relation "obligations" contains null values (SQLSTATE 23502)
[0.252ms] [rows:0] ALTER TABLE "obligations" ADD "obligation_classification_id" bigint NOT NULL
2024/10/18 10:33:40 Failed to automigrate database: ERROR: column "obligation_classification_id" of relation "obligations" contains null values (SQLSTATE 23502)

Process finished with the exit code 1

@GMishx
Copy link
Member

GMishx commented Oct 22, 2024

While trying to pass wrong data, the unmarshling failed which printed some text making the overall response invalid json.

can't parse JSON.  Raw result:

{"status":500,"message":"Failed to update license","error":"obligation classification must be one of the following values: GREEN WHITE YELLOW RED","path":"/api/v1/obligations/provide license upstream","timestamp":"2024-10-22T10:56:59+05:30"}{"status":200,"data":[{"topic":null,"type":"RISK","text":null,"classification":"WHITTTE","modifications":null,"comment":null,"active":null,"text_updatable":null,"shortnames":[]}],"paginationmeta":{"resource_count":1}}

@deo002 deo002 force-pushed the refactor/obligations branch from 7d24fdd to a2daf91 Compare October 22, 2024 06:03
Copy link
Member

@GMishx GMishx left a comment

Choose a reason for hiding this comment

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

Tested, working as expected.

@GMishx GMishx removed the needs test label Oct 22, 2024
@GMishx GMishx merged commit a90a43d into fossology:main Oct 22, 2024
7 checks passed
@GMishx GMishx deleted the refactor/obligations branch October 22, 2024 06:11
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