Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Require region codes are globally unique #621

Merged
merged 1 commit into from
Sep 22, 2020

Conversation

sethvargo
Copy link
Member

@sethvargo sethvargo commented Sep 21, 2020

This is required for the ENX redirector, but we've also always made the
assumption that these need to be globally unique.

Note that this requires changing the column to citext (for
case-insensitive matching) and to NULL out the default of '', since ''
is a duplicate of ''.

It also handles any existing duplicates by appending -N to them before
applying the database-level unique constraint.

Release Note

**Potentially breaking** Require region codes be globally unique, add database constraint for realm name uniqueness

/cc @icco @mikehelmick

@googlebot googlebot added the cla: yes Auto: added by CLA bot when all committers have signed a CLA. label Sep 21, 2020
// I call this the "Microsoft method". For each duplicate realm,
// append -N, starting with 1. If there are 3 realms with the region
// code "PA", their new values will be "PA", "PA-1", and "PA-2"
// respectively.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there is a 'good' way do this this automatically.

I also don't have another suggestion.

pkg/database/migrations.go Outdated Show resolved Hide resolved
pkg/database/migrations.go Show resolved Hide resolved
// handle NULL and uniqueness, the field is converted from it's ptr type to a
// concrete type in callbacks. Do not modify RegionCodePtr directly.
RegionCode string `gorm:"-"`
RegionCodePtr *string `gorm:"column:region_code; type:varchar(10);"`
Copy link
Contributor

Choose a reason for hiding this comment

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

does gorm work w/ sql.NullString?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, but our code doesn't (really). Changing the type of RegionCode requires every caller to update their type. So all cases of:

realm.RegionCode = "foo"
printf(realm.RegionCode)

become

if v != "" {
  realm.RegionCode = sql.NullString{Value:v, Valid: true}
} else {
  ream.RegionCode = sql.NullString{}
}

if realm.RegionCode.Valid() {
  printf(realm.RegionCode.Value)
} else {
  printf("")
}

and I don't want to put that burden on callers tbh.

This is required for the ENX redirector, but we've also always made the
assumption that these need to be globally unique.

Note that this requires changing the column to citext (for
case-insensitive matching) and to NULL out the default of '', since ''
is a duplicate of ''.

It also handles any existing duplicates by appending -N to them before
applying the database-level unique constraint.
@sethvargo
Copy link
Member Author

@mikehelmick PTAnotherL

@google-oss-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jeremyfaller, mikehelmick, sethvargo

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [jeremyfaller,mikehelmick,sethvargo]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-robot google-oss-robot merged commit 4c1f0d2 into main Sep 22, 2020
@google-oss-robot google-oss-robot deleted the sethvargo/region_unique branch September 22, 2020 03:07
@google google locked and limited conversation to collaborators Oct 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes Auto: added by CLA bot when all committers have signed a CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants