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
Fix CI #8
Fix CI #8
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
/gemfiles/*.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'activerecord', '~> 4.2.0' | ||
gem 'bundler', '~> 1.3' | ||
gem 'rake' | ||
gem 'rspec' | ||
gem 'sqlite3' | ||
gem 'pg', '~> 0.11' | ||
gem 'mysql2', '~> 0.3.20' | ||
gem 'simplecov', require: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,50 +24,56 @@ def _rescue_from_duplicate_handlers | |
|
||
def create_or_update(*params, &block) | ||
super | ||
rescue ActiveRecord::RecordNotUnique => exception | ||
handler = exception_handler(exception) | ||
rescue ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid => 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. Look like AR 3.2 raises a 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. when the logger is involved yes, https://github.com/github/rails/blob/3-2-github/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb#L289-L292 =( 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. how long should we support 3.2? 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. As long as we want. But 3.2 will go EOL when Rails 5 is released around this winter or early 2016 worst case. 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. We can also just bump rescue_form_duplicate and change the dependency constraints. 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. let's call it "until the next time it breaks" |
||
raise unless handle_unicity_error(exception) | ||
false | ||
end | ||
|
||
private | ||
|
||
raise exception unless handler | ||
def handle_unicity_error(exception) | ||
handler = exception_handler(exception) | ||
return false unless handler | ||
|
||
attribute = handler.attributes.first | ||
options = handler.options.except(:case_sensitive, :scope).merge(value: self.send(:read_attribute_for_validation, attribute)) | ||
|
||
self.errors.add(attribute, :taken, options) | ||
false | ||
true | ||
end | ||
|
||
private | ||
|
||
def exception_handler(exception) | ||
columns = exception_columns(exception) | ||
return unless columns | ||
columns.sort! | ||
|
||
self.class._rescue_from_duplicate_handlers.detect do |handler| | ||
handler.rescue? && columns == handler.columns | ||
end | ||
end | ||
|
||
def exception_columns(exception) | ||
case | ||
when exception.message =~ /SQLite3::ConstraintException/ | ||
if exception.message =~ /SQLite3::ConstraintException/ | ||
sqlite3_exception_columns(exception) | ||
when exception.message =~ /PG::UniqueViolation/ | ||
elsif exception.message =~ /PG::UniqueViolation/ | ||
postgresql_exception_columns(exception) | ||
else | ||
other_exception_columns(exception) | ||
end.sort | ||
end | ||
end | ||
|
||
def postgresql_exception_columns(exception) | ||
extract_columns(exception.message[/Key \((.*?)\)=\(.*?\) already exists./, 1]) | ||
end | ||
|
||
def sqlite3_exception_columns(exception) | ||
extract_columns(exception.message[/columns? (.*) (?:is|are) not unique/, 1]) | ||
extract_columns(exception.message[/columns? (.*) (?:is|are) not unique/, 1]) || | ||
extract_columns(exception.message[/UNIQUE constraint failed: ([^:]*)\:/, 1]) | ||
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. Look like sqlite changed it's format. I don't know if there was a major update of sqlite or something, nor if we have a way to test both version :/ 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. lol :/ |
||
end | ||
|
||
def extract_columns(columns_string) | ||
return unless columns_string | ||
columns_string.split(",").map(&:strip) | ||
columns_string.split(",").map { |column| column.split('.').last.strip } | ||
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. ? 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. Right. This is because in that new format, columns are in their "fully qualified names", e.g. |
||
end | ||
|
||
def other_exception_columns(exception) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,6 @@ | |
rescue LoadError | ||
end | ||
|
||
|
||
|
||
module RescueFromDuplicate | ||
class Base | ||
cattr_accessor :exception | ||
|
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 moved the Gemfile outside of
spec
because otherwise gems were installed inspec/gemfiles/bundler
and rspec was trying to runt he dependencies specs.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.
yep... thats for sure a better idea.