2
0

api_contract_revision.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package models
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "time"
  6. "github.com/google/uuid"
  7. "gorm.io/gorm"
  8. )
  9. // APIContractRevision represents a revision of an API contract
  10. type APIContractRevision struct {
  11. gorm.Model
  12. // ID is a UUID for the APIContract
  13. ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
  14. // Base64Contract is the APIContract as json encoded in base64
  15. Base64Contract string `json:"base64_contract"`
  16. // ClusterID is the ID of the cluster that the config created.
  17. // This should be a foreign key, but GORM doesnt play well with FKs.
  18. ClusterID int `json:"cluster_id"`
  19. // ProjectID is the ID of the project that the config belongs to.
  20. // This should be a foreign key, but GORM doesnt play well with FKs.
  21. ProjectID int `json:"project_id"`
  22. // Condition is the status of the apply that happened for this revision.
  23. // Condition will contain any failure reasons for a revision, or "SUCCESS" if the revision was applied successfully.
  24. // Further details to expand upon this condition are available in ConditionMetadata
  25. Condition string `json:"condition"`
  26. // ConditionMetadata contains all information about the condition of a given revision.
  27. // If condition is "SUCCESS", this will likely be empty.
  28. // This will follow the error response contract, with the following keys:
  29. // {
  30. // "errors": [
  31. // {
  32. // "code": "string",
  33. // "message": "string",
  34. // "metadata": {}
  35. // }
  36. // ]
  37. // }
  38. ConditionMetadata JSONB `json:"condition_metadata" sql:"type:jsonb" gorm:"type:jsonb"`
  39. CreatedAt time.Time `json:"created_at"`
  40. UpdatedAt time.Time `json:"updated_at"`
  41. }
  42. // TableName overrides the table name
  43. func (APIContractRevision) TableName() string {
  44. return "api_contract_revisions"
  45. }
  46. // JSONB implements the jsonb type in postgres for gorm
  47. type JSONB map[string]any
  48. // Value implements the driver.Valuer interface
  49. func (j JSONB) Value() (driver.Value, error) {
  50. valueString, err := json.Marshal(j)
  51. return string(valueString), err
  52. }
  53. // Scan implements the sql.Scanner interface
  54. func (j *JSONB) Scan(value interface{}) error {
  55. if err := json.Unmarshal(value.([]byte), &j); err != nil {
  56. return err
  57. }
  58. return nil
  59. }