Skip to content

Commit

Permalink
Add explicit order for user related models
Browse files Browse the repository at this point in the history
In some cases it might happen that the returned results have the wrong
order, e.g. when running cc with postgres 10 and special foreign key
indexes (commit: 5bb3fe9 /  #3509)

This commit adds a '.order' for all user related models to ensure the
correct order.
  • Loading branch information
johha committed Nov 20, 2023
1 parent 6d913ff commit 6dd67b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
10 changes: 5 additions & 5 deletions app/models/runtime/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class DBNameUniqueRaceError < Sequel::ValidationFailed; end
primary_key: :guid,
key: :isolation_segment_guid

define_user_group :developers, reciprocal: :spaces, before_add: :validate_developer
define_user_group :managers, reciprocal: :managed_spaces, before_add: :validate_manager
define_user_group :auditors, reciprocal: :audited_spaces, before_add: :validate_auditor
define_user_group :supporters, reciprocal: :supporter_spaces, before_add: :validate_supporter
define_user_group :developers, reciprocal: :spaces, before_add: :validate_developer, order: :space_id
define_user_group :managers, reciprocal: :managed_spaces, before_add: :validate_manager, order: :space_id
define_user_group :auditors, reciprocal: :audited_spaces, before_add: :validate_auditor, order: :space_id
define_user_group :supporters, reciprocal: :supporter_spaces, before_add: :validate_supporter, order: :space_id

many_to_one :organization, before_set: :validate_change_organization

Expand Down Expand Up @@ -117,7 +117,7 @@ class DBNameUniqueRaceError < Sequel::ValidationFailed; end
one_to_many :app_events,
dataset: -> { AppEvent.filter(app: apps) }

one_to_many :default_users, class: 'VCAP::CloudController::User', key: :default_space_id
one_to_many :default_users, class: 'VCAP::CloudController::User', key: :default_space_id, order: :id

one_to_many :domains,
dataset: -> { organization.domains_dataset },
Expand Down
40 changes: 24 additions & 16 deletions app/models/runtime/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,56 @@ class InvalidOrganizationRelation < CloudController::Errors::InvalidRelation
no_auto_guid

many_to_many :organizations,
before_remove: :validate_organization_roles
before_remove: :validate_organization_roles,
order: :id

many_to_one :default_space, key: :default_space_id, class: 'VCAP::CloudController::Space'

many_to_many :managed_organizations,
class: 'VCAP::CloudController::Organization',
join_table: 'organizations_managers',
right_key: :organization_id, reciprocal: :managers,
before_add: :validate_organization
before_add: :validate_organization,
order: :id

many_to_many :billing_managed_organizations,
class: 'VCAP::CloudController::Organization',
join_table: 'organizations_billing_managers',
right_key: :organization_id,
reciprocal: :billing_managers,
before_add: :validate_organization
before_add: :validate_organization,
order: :id

many_to_many :audited_organizations,
class: 'VCAP::CloudController::Organization',
join_table: 'organizations_auditors',
right_key: :organization_id, reciprocal: :auditors,
before_add: :validate_organization
before_add: :validate_organization,
order: :id

many_to_many :spaces,
class: 'VCAP::CloudController::Space',
join_table: 'spaces_developers',
right_key: :space_id, reciprocal: :developers
right_key: :space_id, reciprocal: :developers,
order: :id

many_to_many :managed_spaces,
class: 'VCAP::CloudController::Space',
join_table: 'spaces_managers',
right_key: :space_id, reciprocal: :managers
right_key: :space_id, reciprocal: :managers,
order: :id

many_to_many :audited_spaces,
class: 'VCAP::CloudController::Space',
join_table: 'spaces_auditors',
right_key: :space_id, reciprocal: :auditors
right_key: :space_id, reciprocal: :auditors,
order: :id

many_to_many :supported_spaces,
class: 'VCAP::CloudController::Space',
join_table: 'spaces_supporters',
right_key: :space_id, reciprocal: :supporters
right_key: :space_id, reciprocal: :supporters,
order: :id

one_to_many :labels, class: 'VCAP::CloudController::UserLabelModel', key: :resource_guid, primary_key: :guid
one_to_many :annotations, class: 'VCAP::CloudController::UserAnnotationModel', key: :resource_guid, primary_key: :guid
Expand Down Expand Up @@ -191,35 +199,35 @@ def membership_org_ids
end

def org_user_org_ids
OrganizationUser.where(user_id: id).select(:organization_id)
OrganizationUser.where(user_id: id).select(:organization_id).order(:user_id)
end

def org_manager_org_ids
OrganizationManager.where(user_id: id).select(:organization_id)
OrganizationManager.where(user_id: id).select(:organization_id).order(:user_id)
end

def org_billing_manager_org_ids
OrganizationBillingManager.where(user_id: id).select(:organization_id)
OrganizationBillingManager.where(user_id: id).select(:organization_id).order(:user_id)
end

def org_auditor_org_ids
OrganizationAuditor.where(user_id: id).select(:organization_id)
OrganizationAuditor.where(user_id: id).select(:organization_id).order(:user_id)
end

def space_developer_space_ids
SpaceDeveloper.where(user_id: id).select(:space_id)
SpaceDeveloper.where(user_id: id).select(:space_id).order(:user_id)
end

def space_auditor_space_ids
SpaceAuditor.where(user_id: id).select(:space_id)
SpaceAuditor.where(user_id: id).select(:space_id).order(:user_id)
end

def space_supporter_space_ids
SpaceSupporter.where(user_id: id).select(:space_id)
SpaceSupporter.where(user_id: id).select(:space_id).order(:user_id)
end

def space_manager_space_ids
SpaceManager.where(user_id: id).select(:space_id)
SpaceManager.where(user_id: id).select(:space_id).order(:user_id)
end

def visible_users_in_my_orgs
Expand Down

0 comments on commit 6dd67b7

Please sign in to comment.