From 50c0a1d286f18c44a7041eb53168f8519bba7317 Mon Sep 17 00:00:00 2001 From: Michael Mraka Date: Fri, 31 May 2024 15:36:25 +0200 Subject: [PATCH] RHINENG-10439: store environment id for template --- base/models/models.go | 9 +++++---- base/mqueue/template_event.go | 15 ++++++++------- .../migrations/126_template_environment.down.sql | 1 + .../migrations/126_template_environment.up.sql | 5 +++++ database_admin/schema/create_schema.sql | 3 ++- listener/templates.go | 9 +++++---- 6 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 database_admin/migrations/126_template_environment.down.sql create mode 100644 database_admin/migrations/126_template_environment.up.sql diff --git a/base/models/models.go b/base/models/models.go index 429577659..88f53d75d 100644 --- a/base/models/models.go +++ b/base/models/models.go @@ -41,10 +41,11 @@ func (Baseline) TableName() string { } type Template struct { - ID int64 `gorm:"primaryKey"` - RhAccountID int `gorm:"primaryKey"` - UUID string - Name string + ID int64 `gorm:"primaryKey"` + RhAccountID int `gorm:"primaryKey"` + UUID string + EnvironmentID string + Name string // Config pgtype.JSONB // currently unused Description *string Creator *string // pointer for compatibility with previous API versions diff --git a/base/mqueue/template_event.go b/base/mqueue/template_event.go index 634b5209c..e7cf5b3ae 100644 --- a/base/mqueue/template_event.go +++ b/base/mqueue/template_event.go @@ -10,13 +10,14 @@ import ( // adds too many dependencies and some are incompatible type TemplateResponse struct { UUID string `json:"uuid" readonly:"true"` - Name string `json:"name"` // Name of the template - OrgID string `json:"org_id"` // Organization ID of the owner - Description *string `json:"description"` // Description of the template - Arch string `json:"arch"` // Architecture of the template - Version string `json:"version"` // Version of the template - Date time.Time `json:"date"` // Latest date to include snapshots for - RepositoryUUIDS []string `json:"repository_uuids"` // Repositories added to the template + Name string `json:"name"` // Name of the template + OrgID string `json:"org_id"` // Organization ID of the owner + Description *string `json:"description"` // Description of the template + Arch string `json:"arch"` // Architecture of the template + Version string `json:"version"` // Version of the template + Date time.Time `json:"date"` // Latest date to include snapshots for + RepositoryUUIDS []string `json:"repository_uuids"` // Repositories added to the template + EnvironmentID string `json:"client_environment_id"` // Environment ID used by subscription-manager & candlepin } type TemplateEvent struct { diff --git a/database_admin/migrations/126_template_environment.down.sql b/database_admin/migrations/126_template_environment.down.sql new file mode 100644 index 000000000..e8446bba8 --- /dev/null +++ b/database_admin/migrations/126_template_environment.down.sql @@ -0,0 +1 @@ +ALTER TABLE template DROP COLUMN IF EXISTS environment_id; diff --git a/database_admin/migrations/126_template_environment.up.sql b/database_admin/migrations/126_template_environment.up.sql new file mode 100644 index 000000000..873a524b0 --- /dev/null +++ b/database_admin/migrations/126_template_environment.up.sql @@ -0,0 +1,5 @@ +ALTER TABLE template ADD COLUMN IF NOT EXISTS environment_id TEXT CHECK (NOT empty(environment_id)); + +UPDATE template set environment_id = REPLACE(uuid::text, '-', ''); + +ALTER TABLE template ALTER COLUMN environment_id SET NOT NULL; diff --git a/database_admin/schema/create_schema.sql b/database_admin/schema/create_schema.sql index 6e86f9259..5616553ee 100644 --- a/database_admin/schema/create_schema.sql +++ b/database_admin/schema/create_schema.sql @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations INSERT INTO schema_migrations -VALUES (125, false); +VALUES (126, false); -- --------------------------------------------------------------------------- -- Functions @@ -692,6 +692,7 @@ CREATE TABLE IF NOT EXISTS template creator TEXT CHECK (NOT empty(creator)), published TIMESTAMP WITH TIME ZONE, last_edited TIMESTAMP WITH TIME ZONE, + environment_id TEXT NOT NULL CHECK (not empty(environment_id)), PRIMARY KEY (rh_account_id, id), UNIQUE(rh_account_id, uuid) ) PARTITION BY HASH (rh_account_id); diff --git a/listener/templates.go b/listener/templates.go index 966ca6a72..6c30f61df 100644 --- a/listener/templates.go +++ b/listener/templates.go @@ -89,9 +89,10 @@ func TemplateUpdate(template mqueue.TemplateResponse) error { } row := models.Template{ - RhAccountID: accountID, - UUID: template.UUID, - Name: template.Name, + RhAccountID: accountID, + UUID: template.UUID, + EnvironmentID: template.EnvironmentID, + Name: template.Name, //Config: nil, Description: template.Description, Creator: nil, @@ -99,7 +100,7 @@ func TemplateUpdate(template mqueue.TemplateResponse) error { } err = database.OnConflictUpdateMulti(database.DB, []string{"rh_account_id", "uuid"}, - "name", "description", "creator", "published").Save(&row).Error + "name", "environment_id", "description", "creator", "published").Save(&row).Error if err != nil { return errors.Wrap(err, "creating template from message") }