bulk_import.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: bulk_imports
  5. #
  6. # id :bigint(8) not null, primary key
  7. # type :integer not null
  8. # state :integer not null
  9. # total_items :integer default(0), not null
  10. # imported_items :integer default(0), not null
  11. # processed_items :integer default(0), not null
  12. # finished_at :datetime
  13. # overwrite :boolean default(FALSE), not null
  14. # likely_mismatched :boolean default(FALSE), not null
  15. # original_filename :string default(""), not null
  16. # account_id :bigint(8) not null
  17. # created_at :datetime not null
  18. # updated_at :datetime not null
  19. #
  20. class BulkImport < ApplicationRecord
  21. self.inheritance_column = false
  22. belongs_to :account
  23. has_many :rows, class_name: 'BulkImportRow', inverse_of: :bulk_import, dependent: :delete_all
  24. enum :type, {
  25. following: 0,
  26. blocking: 1,
  27. muting: 2,
  28. domain_blocking: 3,
  29. bookmarks: 4,
  30. lists: 5,
  31. }
  32. enum :state, {
  33. unconfirmed: 0,
  34. scheduled: 1,
  35. in_progress: 2,
  36. finished: 3,
  37. }, prefix: true
  38. validates :type, presence: true
  39. def self.progress!(bulk_import_id, imported: false)
  40. # Use `increment_counter` so that the incrementation is done atomically in the database
  41. BulkImport.increment_counter(:processed_items, bulk_import_id)
  42. BulkImport.increment_counter(:imported_items, bulk_import_id) if imported
  43. # Since the incrementation has been done atomically, concurrent access to `bulk_import` is now bening
  44. bulk_import = BulkImport.find(bulk_import_id)
  45. bulk_import.update!(state: :finished, finished_at: Time.now.utc) if bulk_import.processed_items == bulk_import.total_items
  46. end
  47. end