Giter VIP home page Giter VIP logo

rescue-unique-constraint's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rescue-unique-constraint's Issues

SystemStackError when rescuing two unique constraints.

Sadly no fix for you, but if you try and rescue two unique constraints your app will crash with a SystemStackError.

spec/rescue_two_unique_constraints_spec.rb

require 'active_record'
require 'rescue_unique_constraint'

describe RescueUniqueConstraint do
  before do
    ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
    ActiveRecord::Schema.verbose = false
    ActiveRecord::Schema.define(:version => 1) do
      create_table :things do |t|
        t.string :foo
        t.string :bar
      end

      add_index :things, :foo, unique: true, name: "idx_things_on_foo_unique"
      add_index :things, :bar, unique: true, name: "idx_things_on_bar_unique"
    end
  end

  class Thing < ActiveRecord::Base
    rescue_unique_constraint index: "idx_things_on_foo_unique", field: "foo"
    rescue_unique_constraint index: "idx_things_on_bar_unique", field: "bar"
  end

  it "rescues unique constraint violations as activerecord errors" do
    thing = Thing.create(foo: "1", bar: "2")
    dupe = Thing.new(foo: "1", bar: "2")
    expect(dupe.save).to eql false
    expect(dupe.errors[:foo].first).to match /taken/
    expect(dupe.errors[:bar].first).to match /taken/
  end
end

Spec run output

F

Failures:

  1) RescueUniqueConstraint rescues unique constraint violations as activerecord errors
     Failure/Error:
       define_method(:create_or_update_with_rescue) do
         begin
           create_or_update_without_rescue
         rescue ActiveRecord::RecordNotUnique => e
           case e.message
           when /#{index}/  # Postgres
             errors.add(field, :taken)
           when /UNIQUE.*#{field}/ # SQLite
             errors.add(field, :taken)
           else

     SystemStackError:
       stack level too deep
     # ./lib/rescue_unique_constraint.rb:11:in `block in rescue_unique_constraint'

    .... the above line repeats many many many times....

  # ./lib/rescue_unique_constraint.rb:13:in `block in rescue_unique_constraint'
     # .../gems/activerecord-4.2.5/lib/active_record/persistence.rb:120:in `save'
     # .../gems/activerecord-4.2.5/lib/active_record/validations.rb:37:in `save'
     # .../gems/activerecord-4.2.5/lib/active_record/attribute_methods/dirty.rb:21:in `save'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:286:in `block (2 levels) in save'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
     # .../gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
     # .../gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
     # .../gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:286:in `block in save'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
     # .../gems/activerecord-4.2.5/lib/active_record/transactions.rb:285:in `save'
     # .../gems/activerecord-4.2.5/lib/active_record/persistence.rb:34:in `create'
     # ./spec/rescue_two_unique_constraints_spec.rb:25:in `block (2 levels) in <top (required)>'
Finished in 0.03207 seconds (files took 0.44959 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/rescue_two_unique_constraints_spec.rb:24 # RescueUniqueConstraint rescues unique constraint violations as activerecord errors

ActiveRecord::StatementInvalid when saving with association

I'm getting an error while using this gem when saving a model with built associations.

#<ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block

An example would be:

user = User.new(some: attrs)
user.build_profile(some: attrs)
user.save

(not real but communicating the scenario of a has_one being built)

rescue_unique_constraint is on both the association (profile) and primary model (user) but the failure in this scenario is stemmed from the uniqueness failure on the association (profile).

Has anybody ran into this before? Any suggestions?

Wondering if the rescue in the gem for the unique constraint should also re-raise with ActiveRecord::Rollback if ActiveRecord::Base.connection.open_transactions.positive? is true?

This might be related?
jeremyevans/sequel#908

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.