2023-05-02 19:08:48 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: bulk_imports
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# type :integer not null
|
|
|
|
# state :integer not null
|
|
|
|
# total_items :integer default(0), not null
|
|
|
|
# imported_items :integer default(0), not null
|
|
|
|
# processed_items :integer default(0), not null
|
|
|
|
# finished_at :datetime
|
|
|
|
# overwrite :boolean default(FALSE), not null
|
|
|
|
# likely_mismatched :boolean default(FALSE), not null
|
|
|
|
# original_filename :string default(""), not null
|
|
|
|
# account_id :bigint(8) not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
class BulkImport < ApplicationRecord
|
|
|
|
self.inheritance_column = false
|
|
|
|
|
2024-11-12 17:57:06 +09:00
|
|
|
ARCHIVE_PERIOD = 1.week
|
|
|
|
CONFIRM_PERIOD = 10.minutes
|
|
|
|
|
2023-05-02 19:08:48 +09:00
|
|
|
belongs_to :account
|
|
|
|
has_many :rows, class_name: 'BulkImportRow', inverse_of: :bulk_import, dependent: :delete_all
|
|
|
|
|
2024-02-16 23:54:23 +09:00
|
|
|
enum :type, {
|
2023-05-02 19:08:48 +09:00
|
|
|
following: 0,
|
|
|
|
blocking: 1,
|
|
|
|
muting: 2,
|
|
|
|
domain_blocking: 3,
|
|
|
|
bookmarks: 4,
|
2023-06-01 21:47:31 +09:00
|
|
|
lists: 5,
|
2023-05-02 19:08:48 +09:00
|
|
|
}
|
|
|
|
|
2024-02-16 23:54:23 +09:00
|
|
|
enum :state, {
|
2023-05-02 19:08:48 +09:00
|
|
|
unconfirmed: 0,
|
|
|
|
scheduled: 1,
|
|
|
|
in_progress: 2,
|
|
|
|
finished: 3,
|
2024-04-18 19:13:35 +09:00
|
|
|
}, prefix: true
|
2023-05-02 19:08:48 +09:00
|
|
|
|
|
|
|
validates :type, presence: true
|
|
|
|
|
2024-11-12 17:57:06 +09:00
|
|
|
scope :archival_completed, -> { where(created_at: ..ARCHIVE_PERIOD.ago) }
|
|
|
|
scope :confirmation_missed, -> { state_unconfirmed.where(created_at: ..CONFIRM_PERIOD.ago) }
|
|
|
|
|
2023-05-02 19:08:48 +09:00
|
|
|
def self.progress!(bulk_import_id, imported: false)
|
|
|
|
# Use `increment_counter` so that the incrementation is done atomically in the database
|
2024-01-15 22:46:47 +09:00
|
|
|
BulkImport.increment_counter(:processed_items, bulk_import_id)
|
|
|
|
BulkImport.increment_counter(:imported_items, bulk_import_id) if imported
|
2023-05-02 19:08:48 +09:00
|
|
|
|
|
|
|
# Since the incrementation has been done atomically, concurrent access to `bulk_import` is now bening
|
|
|
|
bulk_import = BulkImport.find(bulk_import_id)
|
|
|
|
bulk_import.update!(state: :finished, finished_at: Time.now.utc) if bulk_import.processed_items == bulk_import.total_items
|
|
|
|
end
|
|
|
|
end
|