diff --git a/database/migrations/000030_subscription_profile_fk.down.sql b/database/migrations/000030_subscription_profile_fk.down.sql new file mode 100644 index 0000000000..bcd7626b8f --- /dev/null +++ b/database/migrations/000030_subscription_profile_fk.down.sql @@ -0,0 +1,18 @@ +-- Copyright 2024 Stacklok, Inc +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +ALTER TABLE profiles DROP COLUMN subscription_id; +ALTER TABLE projects + ADD COLUMN subscription_id UUID DEFAULT NULL + REFERENCES subscriptions(id); \ No newline at end of file diff --git a/database/migrations/000030_subscription_profile_fk.up.sql b/database/migrations/000030_subscription_profile_fk.up.sql new file mode 100644 index 0000000000..50a68e3051 --- /dev/null +++ b/database/migrations/000030_subscription_profile_fk.up.sql @@ -0,0 +1,19 @@ +-- Copyright 2024 Stacklok, Inc +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +-- FK was created on wrong table (I mixed up profiles and projects again...) +ALTER TABLE projects DROP COLUMN subscription_id; +ALTER TABLE profiles + ADD COLUMN subscription_id UUID DEFAULT NULL + REFERENCES subscriptions(id); \ No newline at end of file diff --git a/internal/db/models.go b/internal/db/models.go index 6c6362569e..d95477edeb 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -441,15 +441,16 @@ type MigrationProfileBackfillLog struct { } type Profile struct { - ID uuid.UUID `json:"id"` - Name string `json:"name"` - Provider string `json:"provider"` - ProjectID uuid.UUID `json:"project_id"` - Remediate NullActionType `json:"remediate"` - Alert NullActionType `json:"alert"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - ProviderID uuid.UUID `json:"provider_id"` + ID uuid.UUID `json:"id"` + Name string `json:"name"` + Provider string `json:"provider"` + ProjectID uuid.UUID `json:"project_id"` + Remediate NullActionType `json:"remediate"` + Alert NullActionType `json:"alert"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + ProviderID uuid.UUID `json:"provider_id"` + SubscriptionID uuid.NullUUID `json:"subscription_id"` } type ProfileStatus struct { @@ -467,7 +468,6 @@ type Project struct { ParentID uuid.NullUUID `json:"parent_id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` - SubscriptionID uuid.NullUUID `json:"subscription_id"` } type Provider struct { diff --git a/internal/db/profiles.sql.go b/internal/db/profiles.sql.go index 6335c6949b..079bebdf80 100644 --- a/internal/db/profiles.sql.go +++ b/internal/db/profiles.sql.go @@ -67,7 +67,7 @@ INSERT INTO profiles ( alert, name, provider_id - ) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id + ) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id, subscription_id ` type CreateProfileParams struct { @@ -99,6 +99,7 @@ func (q *Queries) CreateProfile(ctx context.Context, arg CreateProfileParams) (P &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, ) return i, err } @@ -174,7 +175,7 @@ func (q *Queries) DeleteRuleInstantiation(ctx context.Context, arg DeleteRuleIns } const getEntityProfileByProjectAndName = `-- name: GetEntityProfileByProjectAndName :many -SELECT profiles.id, name, provider, project_id, remediate, alert, profiles.created_at, profiles.updated_at, provider_id, entity_profiles.id, entity, profile_id, contextual_rules, entity_profiles.created_at, entity_profiles.updated_at FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id +SELECT profiles.id, name, provider, project_id, remediate, alert, profiles.created_at, profiles.updated_at, provider_id, subscription_id, entity_profiles.id, entity, profile_id, contextual_rules, entity_profiles.created_at, entity_profiles.updated_at FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id WHERE profiles.project_id = $1 AND profiles.name = $2 ` @@ -193,6 +194,7 @@ type GetEntityProfileByProjectAndNameRow struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` ProviderID uuid.UUID `json:"provider_id"` + SubscriptionID uuid.NullUUID `json:"subscription_id"` ID_2 uuid.UUID `json:"id_2"` Entity Entities `json:"entity"` ProfileID uuid.UUID `json:"profile_id"` @@ -220,6 +222,7 @@ func (q *Queries) GetEntityProfileByProjectAndName(ctx context.Context, arg GetE &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, &i.ID_2, &i.Entity, &i.ProfileID, @@ -241,7 +244,7 @@ func (q *Queries) GetEntityProfileByProjectAndName(ctx context.Context, arg GetE } const getProfileByID = `-- name: GetProfileByID :one -SELECT id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id FROM profiles WHERE id = $1 AND project_id = $2 +SELECT id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id, subscription_id FROM profiles WHERE id = $1 AND project_id = $2 ` type GetProfileByIDParams struct { @@ -262,12 +265,13 @@ func (q *Queries) GetProfileByID(ctx context.Context, arg GetProfileByIDParams) &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, ) return i, err } const getProfileByIDAndLock = `-- name: GetProfileByIDAndLock :one -SELECT id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id FROM profiles WHERE id = $1 AND project_id = $2 FOR UPDATE +SELECT id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id, subscription_id FROM profiles WHERE id = $1 AND project_id = $2 FOR UPDATE ` type GetProfileByIDAndLockParams struct { @@ -288,12 +292,13 @@ func (q *Queries) GetProfileByIDAndLock(ctx context.Context, arg GetProfileByIDA &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, ) return i, err } const getProfileByNameAndLock = `-- name: GetProfileByNameAndLock :one -SELECT id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id FROM profiles WHERE name = $1 AND project_id = $2 FOR UPDATE +SELECT id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id, subscription_id FROM profiles WHERE name = $1 AND project_id = $2 FOR UPDATE ` type GetProfileByNameAndLockParams struct { @@ -314,12 +319,13 @@ func (q *Queries) GetProfileByNameAndLock(ctx context.Context, arg GetProfileByN &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, ) return i, err } const getProfileByProjectAndID = `-- name: GetProfileByProjectAndID :many -SELECT profiles.id, name, provider, project_id, remediate, alert, profiles.created_at, profiles.updated_at, provider_id, entity_profiles.id, entity, profile_id, contextual_rules, entity_profiles.created_at, entity_profiles.updated_at FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id +SELECT profiles.id, name, provider, project_id, remediate, alert, profiles.created_at, profiles.updated_at, provider_id, subscription_id, entity_profiles.id, entity, profile_id, contextual_rules, entity_profiles.created_at, entity_profiles.updated_at FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id WHERE profiles.project_id = $1 AND profiles.id = $2 ` @@ -338,6 +344,7 @@ type GetProfileByProjectAndIDRow struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` ProviderID uuid.UUID `json:"provider_id"` + SubscriptionID uuid.NullUUID `json:"subscription_id"` ID_2 uuid.UUID `json:"id_2"` Entity Entities `json:"entity"` ProfileID uuid.UUID `json:"profile_id"` @@ -365,6 +372,7 @@ func (q *Queries) GetProfileByProjectAndID(ctx context.Context, arg GetProfileBy &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, &i.ID_2, &i.Entity, &i.ProfileID, @@ -409,7 +417,7 @@ func (q *Queries) GetProfileForEntity(ctx context.Context, arg GetProfileForEnti } const listProfilesByProjectID = `-- name: ListProfilesByProjectID :many -SELECT profiles.id, name, provider, project_id, remediate, alert, profiles.created_at, profiles.updated_at, provider_id, entity_profiles.id, entity, profile_id, contextual_rules, entity_profiles.created_at, entity_profiles.updated_at FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id +SELECT profiles.id, name, provider, project_id, remediate, alert, profiles.created_at, profiles.updated_at, provider_id, subscription_id, entity_profiles.id, entity, profile_id, contextual_rules, entity_profiles.created_at, entity_profiles.updated_at FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id WHERE profiles.project_id = $1 ` @@ -423,6 +431,7 @@ type ListProfilesByProjectIDRow struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` ProviderID uuid.UUID `json:"provider_id"` + SubscriptionID uuid.NullUUID `json:"subscription_id"` ID_2 uuid.UUID `json:"id_2"` Entity Entities `json:"entity"` ProfileID uuid.UUID `json:"profile_id"` @@ -450,6 +459,7 @@ func (q *Queries) ListProfilesByProjectID(ctx context.Context, projectID uuid.UU &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, &i.ID_2, &i.Entity, &i.ProfileID, @@ -515,7 +525,7 @@ UPDATE profiles SET remediate = $3, alert = $4, updated_at = NOW() -WHERE id = $1 AND project_id = $2 RETURNING id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id +WHERE id = $1 AND project_id = $2 RETURNING id, name, provider, project_id, remediate, alert, created_at, updated_at, provider_id, subscription_id ` type UpdateProfileParams struct { @@ -543,6 +553,7 @@ func (q *Queries) UpdateProfile(ctx context.Context, arg UpdateProfileParams) (P &i.CreatedAt, &i.UpdatedAt, &i.ProviderID, + &i.SubscriptionID, ) return i, err } diff --git a/internal/db/projects.sql.go b/internal/db/projects.sql.go index 9c182e5fed..5cb9ec45a9 100644 --- a/internal/db/projects.sql.go +++ b/internal/db/projects.sql.go @@ -20,7 +20,7 @@ INSERT INTO projects ( metadata ) VALUES ( $1, $2, $3::jsonb -) RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id +) RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at ` type CreateProjectParams struct { @@ -40,7 +40,6 @@ func (q *Queries) CreateProject(ctx context.Context, arg CreateProjectParams) (P &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ) return i, err } @@ -52,7 +51,7 @@ INSERT INTO projects ( metadata ) VALUES ( $1, $2, $3::jsonb -) RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id +) RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at ` type CreateProjectWithIDParams struct { @@ -72,7 +71,6 @@ func (q *Queries) CreateProjectWithID(ctx context.Context, arg CreateProjectWith &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ) return i, err } @@ -271,7 +269,7 @@ func (q *Queries) GetParentProjectsUntil(ctx context.Context, arg GetParentProje } const getProjectByID = `-- name: GetProjectByID :one -SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id FROM projects +SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at FROM projects WHERE id = $1 AND is_organization = FALSE LIMIT 1 ` @@ -286,13 +284,12 @@ func (q *Queries) GetProjectByID(ctx context.Context, id uuid.UUID) (Project, er &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ) return i, err } const getProjectByName = `-- name: GetProjectByName :one -SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id FROM projects +SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at FROM projects WHERE name = $1 AND is_organization = FALSE LIMIT 1 ` @@ -307,14 +304,13 @@ func (q *Queries) GetProjectByName(ctx context.Context, name string) (Project, e &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ) return i, err } const listNonOrgProjects = `-- name: ListNonOrgProjects :many -SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id FROM projects +SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at FROM projects WHERE is_organization = FALSE ` @@ -338,7 +334,6 @@ func (q *Queries) ListNonOrgProjects(ctx context.Context) ([]Project, error) { &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ); err != nil { return nil, err } @@ -355,7 +350,7 @@ func (q *Queries) ListNonOrgProjects(ctx context.Context) ([]Project, error) { const listOldOrgProjects = `-- name: ListOldOrgProjects :many -SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id FROM projects +SELECT id, name, is_organization, metadata, parent_id, created_at, updated_at FROM projects WHERE is_organization = TRUE ` @@ -379,7 +374,6 @@ func (q *Queries) ListOldOrgProjects(ctx context.Context) ([]Project, error) { &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ); err != nil { return nil, err } @@ -398,7 +392,7 @@ const orphanProject = `-- name: OrphanProject :one UPDATE projects SET metadata = $2, parent_id = NULL -WHERE id = $1 RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id +WHERE id = $1 RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at ` type OrphanProjectParams struct { @@ -418,7 +412,6 @@ func (q *Queries) OrphanProject(ctx context.Context, arg OrphanProjectParams) (P &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ) return i, err } @@ -426,7 +419,7 @@ func (q *Queries) OrphanProject(ctx context.Context, arg OrphanProjectParams) (P const updateProjectMeta = `-- name: UpdateProjectMeta :one UPDATE projects SET metadata = $2 -WHERE id = $1 RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at, subscription_id +WHERE id = $1 RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at ` type UpdateProjectMetaParams struct { @@ -445,7 +438,6 @@ func (q *Queries) UpdateProjectMeta(ctx context.Context, arg UpdateProjectMetaPa &i.ParentID, &i.CreatedAt, &i.UpdatedAt, - &i.SubscriptionID, ) return i, err }