Coder Social home page Coder Social logo

jaynetics / delete_recursively Goto Github PK

View Code? Open in Web Editor NEW
20.0 4.0 2.0 161 KB

A Ruby gem that adds a better option to recursively delete dependent ActiveRecords.

License: MIT License

Ruby 85.18% JavaScript 2.40% CSS 1.55% HTML 10.87%
rails activerecord deletion performance

delete_recursively's People

Contributors

jaynetics avatar knu avatar

Stargazers

 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

Forkers

iamonkara knu

delete_recursively's Issues

ArgumentError: Polymorphic associations do not support computing the class.

I'm having an issue with a polymorphic association. Here is a minimal test case that reproduces the error:

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'activerecord', '~> 7.0', '>= 7.0.4.3'
  gem 'delete_recursively', '~> 1.1'
  gem 'sqlite3', '~> 1.6', '>= 1.6.2'
end

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')

class Task < ActiveRecord::Base
  has_one(:object_statuses, as: :statusable, class_name: :ObjectStatus, dependent: :destroy)
end

class ObjectStatus < ActiveRecord::Base
  belongs_to :statusable, polymorphic: true, inverse_of: :object_statuses
  belongs_to :status, inverse_of: :object_statuses
end

class Status < ActiveRecord::Base
  has_many :object_statuses, inverse_of: :status, dependent: :destroy
end

ActiveRecord::Schema.define do
  create_table :tasks do |t|
    t.string :description
  end

  create_table :object_statuses do |t|
    t.references :statusable, polymorphic: true, index: true
    t.references :status, null: false, foreign_key: true
  end

  create_table :statuses do |t|
    t.string :name, null: false
  end
end

ActiveRecord::Base.transaction do
  t = Task.create(description: "Buy fish and chips")
  s = Status.create(name: 'done')
  ObjectStatus.create(statusable: t, status: s)
end

puts "Task count: #{Task.count}"

DeleteRecursively.all(Task)

puts "Task count: #{Task.count}"

When executed, I get the following output:

-- create_table(:tasks)
   -> 0.0076s
-- create_table(:object_statuses)
   -> 0.0006s
-- create_table(:statuses)
   -> 0.0002s

Task count: 1

activerecord-7.0.4.3/lib/active_record/reflection.rb:423:in `compute_class': Polymorphic associations do not support computing the class. (ArgumentError)
from activerecord-7.0.4.3/lib/active_record/reflection.rb:382:in `klass'
from activerecord-7.0.4.3/lib/active_record/reflection.rb:230:in `inverse_of'
from delete_recursively-1.1.0/lib/delete_recursively.rb:77:in `block (2 levels) in associated_classes'
from delete_recursively-1.1.0/lib/delete_recursively.rb:77:in `any?'
from delete_recursively-1.1.0/lib/delete_recursively.rb:77:in `block in associated_classes'
from delete_recursively-1.1.0/lib/delete_recursively.rb:75:in `select'
from delete_recursively-1.1.0/lib/delete_recursively.rb:75:in `associated_classes'
from delete_recursively-1.1.0/lib/delete_recursively.rb:161:in `block in all'
from delete_recursively-1.1.0/lib/delete_recursively.rb:160:in `each'
from delete_recursively-1.1.0/lib/delete_recursively.rb:160:in `all'
from delete_recursively-1.1.0/lib/delete_recursively.rb:163:in `block (2 levels) in all'
from delete_recursively-1.1.0/lib/delete_recursively.rb:161:in `each'
from delete_recursively-1.1.0/lib/delete_recursively.rb:161:in `block in all'
from delete_recursively-1.1.0/lib/delete_recursively.rb:160:in `each'
from delete_recursively-1.1.0/lib/delete_recursively.rb:160:in `all'
from polymorphic_associations.rb:48:in `<main>'

I find that if I edit this line to ignore the ArgumentError it does what I want I need but I'm not sure that's correct.

`require_relative': cannot load such file

Hi, i have obtain the next error after install delete_recursively

C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/delete_recursively-1.0.0/lib/delete_recursively.rb:7:in `require_relative': cannot load such file -- C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/delete_recursively-1.0.0/lib/delete_recursively/version (LoadError)
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/delete_recursively-1.0.0/lib/delete_recursively.rb:7:in `<module:DeleteRecursively>'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/delete_recursively-1.0.0/lib/delete_recursively.rb:6:in `<top (required)>'
        from C:/Bitnami/rubystack-2.3.4-0/test-app/config/application.rb:5:in `require'
        from C:/Bitnami/rubystack-2.3.4-0/test-app/config/application.rb:5:in `<top (required)>'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:78:in `require'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:78:in `block in server'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:75:in `tap'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:75:in `server'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from C:/Bitnami/rubystack-2.3.4-0/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

What happened?

empty array of record_ids issues a DELETE SQL statement

Here's a contrived example of what I mean:

 require 'bundler/inline'
 
 gemfile do
   source 'https://rubygems.org'
   gem 'activerecord', '~> 7.0', '>= 7.0.4.3'
   gem 'delete_recursively'
   gem 'sqlite3', '~> 1.6', '>= 1.6.2'
 end
 
 ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
 
 class Task < ActiveRecord::Base
   has_one :status, dependent: :destroy
 end
 
 class Status < ActiveRecord::Base
   belongs_to :status
 end
 
 ActiveRecord::Schema.define do
   create_table(:tasks) { |t| t.string :description }
   create_table(:statuses) { |t| t.references :task }
 end
 
 task = Task.create(description: "Buy fish and chips")
 
 ActiveRecord::Base.logger = ActiveSupport::Logger.new($stdout)
 
 DeleteRecursively.delete_records_recursively(Task, task.id, force: true)

This generates the following output:

 -- create_table(:tasks)
    -> 0.0093s
 -- create_table(:statuses)
    -> 0.0002s
   Status Pluck (0.0ms)  SELECT "statuses"."id" FROM "statuses" WHERE "statuses"."task_id" = ?  [["task_id", 1]]
   Status Delete All (0.0ms)  DELETE FROM "statuses" WHERE 1=0
   Task Delete All (0.0ms)  DELETE FROM "tasks" WHERE "tasks"."id" = ?  [["id", 1]]

The Status Delete All statement above seems unnecessary and is being issued because ActiveRecord is being asked to delete rows with an empty array of ids.

Is this intended? Perhaps it makes sense to add an if record_ids.size > 0 when:

Does it work with Foreign Key database constraints?

image

I get these errors on a has_one and belongs_to association

/home/onkara/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.7.2/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_params': ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_5676777bf1" on table "settings" (PG::ForeignKeyViolation)

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.