-
Notifications
You must be signed in to change notification settings - Fork 981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #10479 - prevent validation error on host creation when domain … #2374
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class AddIndexToDomainName < ActiveRecord::Migration | ||
def up | ||
case ActiveRecord::Base.connection.instance_values["config"][:adapter] | ||
when "postgresql" | ||
execute 'CREATE UNIQUE INDEX domain_name_index ON domains (lower(name))' | ||
when "sqlite3" | ||
execute 'CREATE UNIQUE INDEX domain_name_index ON domains (name collate nocase)' | ||
else | ||
add_index :domains, :name, :name => 'domain_name_index', :unique => true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this relies on MySQL unique indexes being case sensitive by default, please leave a comment |
||
end | ||
end | ||
|
||
def down | ||
remove_index! :domains, 'domain_name_index' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,14 @@ def setup | |
should validate_uniqueness_of(:fullname).allow_nil | ||
should validate_uniqueness_of(:fullname).allow_blank | ||
|
||
test "should have a unique case-insensitive name" do | ||
other_domain = Domain.new(:name => @domain.name.upcase) | ||
refute other_domain.save | ||
assert_raises ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the index is only enforcing this on MySQL, Postgres and sqlite won't raise this exception. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The weirdest part is that if I remove the migration and add the index manually running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The failure is actually because
I'd be happy to get your input on what would be the best approach, as I will need to do the same for the next commit addressing the host names as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dLobatog ping, trying to revive this old PR. Could I get you input about the previous comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the case-insensitive enforcement from the database isn't the way to go as you can encounter this situation when 2 people submit a domain to be created or updated.
Removing the test isn't good either IMO. We want to verify this behavior. I'm not sure right now how to move forward, I'm checking why exactly this happens, even when not loading the test via rake ( |
||
other_domain.save(:validate => false) #make sure the DB enforces this | ||
end | ||
end | ||
|
||
test "when cast to string should return the name" do | ||
s = @domain.to_s | ||
assert_equal @domain.name, s | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be enforced with a DB index of some sort too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a case-insensitive unique index for this.
I will also attach to this PR another commit making host names case insensitive, feel free to return this PR to waiting on contributor.