Use convention to create boilerplate record sets

This commit is contained in:
Stanko K.R.
2026-01-19 12:58:30 +01:00
parent f457273937
commit 122a228ed4
34 changed files with 88 additions and 1382 deletions
@@ -1,48 +0,0 @@
class Account::DataTransfer::AccessRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
accessed_at
account_id
board_id
created_at
id
involvement
updated_at
user_id
].freeze
private
def records
Access.where(account: account)
end
def export_record(access)
zip.add_file "data/accesses/#{access.id}.json", access.as_json.to_json
end
def files
zip.glob("data/accesses/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Access.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Access record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -10,6 +10,10 @@ class Account::DataTransfer::AccountRecordSet < Account::DataTransfer::RecordSet
usage_limit
]
def initialize(account)
super(account: account, model: Account)
end
private
def records
[ account ]
@@ -10,6 +10,10 @@ class Account::DataTransfer::ActionTextRichTextRecordSet < Account::DataTransfer
updated_at
].freeze
def initialize(account)
super(account: account, model: ActionText::RichText)
end
private
def records
ActionText::RichText.where(account: account)
@@ -1,47 +0,0 @@
class Account::DataTransfer::ActiveStorageAttachmentRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
blob_id
created_at
id
name
record_id
record_type
].freeze
private
def records
ActiveStorage::Attachment.where(account: account)
end
def export_record(attachment)
zip.add_file "data/active_storage_attachments/#{attachment.id}.json", attachment.as_json.to_json
end
def files
zip.glob("data/active_storage_attachments/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
ActiveStorage::Attachment.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "ActiveStorageAttachment record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,50 +0,0 @@
class Account::DataTransfer::ActiveStorageBlobRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
byte_size
checksum
content_type
created_at
filename
id
key
metadata
service_name
].freeze
private
def records
ActiveStorage::Blob.where(account: account)
end
def export_record(blob)
zip.add_file "data/active_storage_blobs/#{blob.id}.json", blob.as_json.to_json
end
def files
zip.glob("data/active_storage_blobs/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
ActiveStorage::Blob.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "ActiveStorageBlob record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,47 +0,0 @@
class Account::DataTransfer::AssignmentRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
assignee_id
assigner_id
card_id
created_at
id
updated_at
].freeze
private
def records
Assignment.where(account: account)
end
def export_record(assignment)
zip.add_file "data/assignments/#{assignment.id}.json", assignment.as_json.to_json
end
def files
zip.glob("data/assignments/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Assignment.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Assignment record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,4 +1,8 @@
class Account::DataTransfer::BlobFileRecordSet < Account::DataTransfer::RecordSet
def initialize(account)
super(account: account, model: ActiveStorage::Blob)
end
private
def records
ActiveStorage::Blob.where(account: account)
@@ -1,46 +0,0 @@
class Account::DataTransfer::BoardPublicationRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
board_id
created_at
id
key
updated_at
].freeze
private
def records
Board::Publication.where(account: account)
end
def export_record(publication)
zip.add_file "data/board_publications/#{publication.id}.json", publication.as_json.to_json
end
def files
zip.glob("data/board_publications/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Board::Publication.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "BoardPublication record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,47 +0,0 @@
class Account::DataTransfer::BoardRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
all_access
created_at
creator_id
id
name
updated_at
].freeze
private
def records
Board.where(account: account)
end
def export_record(board)
zip.add_file "data/boards/#{board.id}.json", board.as_json.to_json
end
def files
zip.glob("data/boards/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Board.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Board record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,45 +0,0 @@
class Account::DataTransfer::CardActivitySpikeRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
updated_at
].freeze
private
def records
Card::ActivitySpike.where(account: account)
end
def export_record(activity_spike)
zip.add_file "data/card_activity_spikes/#{activity_spike.id}.json", activity_spike.as_json.to_json
end
def files
zip.glob("data/card_activity_spikes/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Card::ActivitySpike.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "CardActivitySpike record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,45 +0,0 @@
class Account::DataTransfer::CardGoldnessRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
updated_at
].freeze
private
def records
Card::Goldness.where(account: account)
end
def export_record(goldness)
zip.add_file "data/card_goldnesses/#{goldness.id}.json", goldness.as_json.to_json
end
def files
zip.glob("data/card_goldnesses/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Card::Goldness.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "CardGoldness record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,46 +0,0 @@
class Account::DataTransfer::CardNotNowRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
updated_at
user_id
].freeze
private
def records
Card::NotNow.where(account: account)
end
def export_record(not_now)
zip.add_file "data/card_not_nows/#{not_now.id}.json", not_now.as_json.to_json
end
def files
zip.glob("data/card_not_nows/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Card::NotNow.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "CardNotNow record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,52 +0,0 @@
class Account::DataTransfer::CardRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
board_id
column_id
created_at
creator_id
due_on
id
last_active_at
number
status
title
updated_at
].freeze
private
def records
Card.where(account: account)
end
def export_record(card)
zip.add_file "data/cards/#{card.id}.json", card.as_json.to_json
end
def files
zip.glob("data/cards/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Card.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Card record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,46 +0,0 @@
class Account::DataTransfer::ClosureRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
updated_at
user_id
].freeze
private
def records
Closure.where(account: account)
end
def export_record(closure)
zip.add_file "data/closures/#{closure.id}.json", closure.as_json.to_json
end
def files
zip.glob("data/closures/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Closure.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Closure record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,48 +0,0 @@
class Account::DataTransfer::ColumnRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
board_id
color
created_at
id
name
position
updated_at
].freeze
private
def records
Column.where(account: account)
end
def export_record(column)
zip.add_file "data/columns/#{column.id}.json", column.as_json.to_json
end
def files
zip.glob("data/columns/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Column.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Column record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,46 +0,0 @@
class Account::DataTransfer::CommentRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
creator_id
id
updated_at
].freeze
private
def records
Comment.where(account: account)
end
def export_record(comment)
zip.add_file "data/comments/#{comment.id}.json", comment.as_json.to_json
end
def files
zip.glob("data/comments/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Comment.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Comment record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,51 +0,0 @@
class Account::DataTransfer::EntropyRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
auto_postpone_period
container_id
container_type
created_at
id
updated_at
].freeze
private
def records
Entropy.where(account: account)
end
def export_record(entropy)
zip.add_file "data/entropies/#{entropy.id}.json", entropy.as_json.to_json
end
def files
zip.glob("data/entropies/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Entropy.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Entropy record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
unless %w[Account Board].include?(data["container_type"])
raise IntegrityError, "#{file_path} has invalid container_type: #{data['container_type']}"
end
end
end
@@ -1,50 +0,0 @@
class Account::DataTransfer::EventRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
action
board_id
created_at
creator_id
eventable_id
eventable_type
id
particulars
updated_at
].freeze
private
def records
Event.where(account: account)
end
def export_record(event)
zip.add_file "data/events/#{event.id}.json", event.as_json.to_json
end
def files
zip.glob("data/events/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Event.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Event record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,47 +0,0 @@
class Account::DataTransfer::FilterRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
created_at
creator_id
fields
id
params_digest
updated_at
].freeze
private
def records
Filter.where(account: account)
end
def export_record(filter)
zip.add_file "data/filters/#{filter.id}.json", filter.as_json.to_json
end
def files
zip.glob("data/filters/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Filter.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Filter record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
+40 -39
View File
@@ -1,43 +1,6 @@
class Account::DataTransfer::Manifest
Cursor = Struct.new(:record_class, :last_id)
include Enumerable
RECORD_SETS = [
Account::DataTransfer::AccountRecordSet,
Account::DataTransfer::UserRecordSet,
Account::DataTransfer::TagRecordSet,
Account::DataTransfer::BoardRecordSet,
Account::DataTransfer::ColumnRecordSet,
Account::DataTransfer::EntropyRecordSet,
Account::DataTransfer::BoardPublicationRecordSet,
Account::DataTransfer::WebhookRecordSet,
Account::DataTransfer::AccessRecordSet,
Account::DataTransfer::CardRecordSet,
Account::DataTransfer::CommentRecordSet,
Account::DataTransfer::StepRecordSet,
Account::DataTransfer::AssignmentRecordSet,
Account::DataTransfer::TaggingRecordSet,
Account::DataTransfer::ClosureRecordSet,
Account::DataTransfer::CardGoldnessRecordSet,
Account::DataTransfer::CardNotNowRecordSet,
Account::DataTransfer::CardActivitySpikeRecordSet,
Account::DataTransfer::WatchRecordSet,
Account::DataTransfer::PinRecordSet,
Account::DataTransfer::ReactionRecordSet,
Account::DataTransfer::MentionRecordSet,
Account::DataTransfer::FilterRecordSet,
Account::DataTransfer::WebhookDelinquencyTrackerRecordSet,
Account::DataTransfer::EventRecordSet,
Account::DataTransfer::NotificationRecordSet,
Account::DataTransfer::NotificationBundleRecordSet,
Account::DataTransfer::WebhookDeliveryRecordSet,
Account::DataTransfer::ActiveStorageBlobRecordSet,
Account::DataTransfer::ActiveStorageAttachmentRecordSet,
Account::DataTransfer::ActionTextRichTextRecordSet,
Account::DataTransfer::BlobFileRecordSet
]
attr_reader :account
def initialize(account)
@@ -47,8 +10,46 @@ class Account::DataTransfer::Manifest
def each_record_set(start: nil)
raise ArgumentError, "No block given" unless block_given?
RECORD_SETS.each do |record_set_class|
yield record_set_class.new(account)
record_sets.each do |record_set|
yield record_set
end
end
private
def record_sets
[
Account::DataTransfer::AccountRecordSet.new(account),
Account::DataTransfer::UserRecordSet.new(account),
Account::DataTransfer::RecordSet.new(account: account, model: ::Tag),
Account::DataTransfer::RecordSet.new(account: account, model: ::Board),
Account::DataTransfer::RecordSet.new(account: account, model: ::Column),
Account::DataTransfer::RecordSet.new(account: account, model: ::Entropy),
Account::DataTransfer::RecordSet.new(account: account, model: ::Board::Publication),
Account::DataTransfer::RecordSet.new(account: account, model: ::Webhook),
Account::DataTransfer::RecordSet.new(account: account, model: ::Access),
Account::DataTransfer::RecordSet.new(account: account, model: ::Card),
Account::DataTransfer::RecordSet.new(account: account, model: ::Comment),
Account::DataTransfer::RecordSet.new(account: account, model: ::Step),
Account::DataTransfer::RecordSet.new(account: account, model: ::Assignment),
Account::DataTransfer::RecordSet.new(account: account, model: ::Tagging),
Account::DataTransfer::RecordSet.new(account: account, model: ::Closure),
Account::DataTransfer::RecordSet.new(account: account, model: ::Card::Goldness),
Account::DataTransfer::RecordSet.new(account: account, model: ::Card::NotNow),
Account::DataTransfer::RecordSet.new(account: account, model: ::Card::ActivitySpike),
Account::DataTransfer::RecordSet.new(account: account, model: ::Watch),
Account::DataTransfer::RecordSet.new(account: account, model: ::Pin),
Account::DataTransfer::RecordSet.new(account: account, model: ::Reaction),
Account::DataTransfer::RecordSet.new(account: account, model: ::Mention),
Account::DataTransfer::RecordSet.new(account: account, model: ::Filter),
Account::DataTransfer::RecordSet.new(account: account, model: ::Webhook::DelinquencyTracker),
Account::DataTransfer::RecordSet.new(account: account, model: ::Event),
Account::DataTransfer::RecordSet.new(account: account, model: ::Notification),
Account::DataTransfer::RecordSet.new(account: account, model: ::Notification::Bundle),
Account::DataTransfer::RecordSet.new(account: account, model: ::Webhook::Delivery),
Account::DataTransfer::RecordSet.new(account: account, model: ::ActiveStorage::Blob),
Account::DataTransfer::RecordSet.new(account: account, model: ::ActiveStorage::Attachment),
Account::DataTransfer::ActionTextRichTextRecordSet.new(account),
Account::DataTransfer::BlobFileRecordSet.new(account)
]
end
end
@@ -1,54 +0,0 @@
class Account::DataTransfer::MentionRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
created_at
id
mentionee_id
mentioner_id
source_id
source_type
updated_at
].freeze
VALID_SOURCE_TYPES = %w[Card Comment].freeze
private
def records
Mention.where(account: account)
end
def export_record(mention)
zip.add_file "data/mentions/#{mention.id}.json", mention.as_json.to_json
end
def files
zip.glob("data/mentions/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Mention.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Mention record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
unless VALID_SOURCE_TYPES.include?(data["source_type"])
raise IntegrityError, "#{file_path} has invalid source_type: #{data['source_type']}"
end
end
end
@@ -1,48 +0,0 @@
class Account::DataTransfer::NotificationBundleRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
created_at
ends_at
id
starts_at
status
updated_at
user_id
].freeze
private
def records
Notification::Bundle.where(account: account)
end
def export_record(bundle)
zip.add_file "data/notification_bundles/#{bundle.id}.json", bundle.as_json.to_json
end
def files
zip.glob("data/notification_bundles/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Notification::Bundle.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "NotificationBundle record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,49 +0,0 @@
class Account::DataTransfer::NotificationRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
created_at
creator_id
id
read_at
source_id
source_type
updated_at
user_id
].freeze
private
def records
Notification.where(account: account)
end
def export_record(notification)
zip.add_file "data/notifications/#{notification.id}.json", notification.as_json.to_json
end
def files
zip.glob("data/notifications/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Notification.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Notification record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,46 +0,0 @@
class Account::DataTransfer::PinRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
updated_at
user_id
].freeze
private
def records
Pin.where(account: account)
end
def export_record(pin)
zip.add_file "data/pins/#{pin.id}.json", pin.as_json.to_json
end
def files
zip.glob("data/pins/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Pin.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Pin record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,47 +0,0 @@
class Account::DataTransfer::ReactionRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
comment_id
content
created_at
id
reacter_id
updated_at
].freeze
private
def records
Reaction.where(account: account)
end
def export_record(reaction)
zip.add_file "data/reactions/#{reaction.id}.json", reaction.as_json.to_json
end
def files
zip.glob("data/reactions/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Reaction.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Reaction record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
+32 -7
View File
@@ -3,10 +3,12 @@ class Account::DataTransfer::RecordSet
IMPORT_BATCH_SIZE = 100
attr_reader :account
attr_reader :account, :model, :attributes
def initialize(account)
def initialize(account:, model:, attributes: nil)
@account = account
@model = model
@attributes = (attributes || model.column_names).map(&:to_s)
end
def export(to:, start: nil)
@@ -49,23 +51,42 @@ class Account::DataTransfer::RecordSet
end
def records
[]
model.where(account_id: account.id)
end
def export_record(record)
raise NotImplementedError
zip.add_file "data/#{model_dir}/#{record.id}.json", record.to_json
end
def files
[]
zip.glob("data/#{model_dir}/*.json")
end
def import_batch(files)
raise NotImplementedError
batch_data = files.map do |file|
data = load(file)
data.slice(*attributes).merge("account_id" => account.id)
end
model.insert_all!(batch_data)
end
def validate_record(file_path)
raise NotImplementedError
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "#{model} record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = attributes - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
if model.exists?(id: data["id"])
raise IntegrityError, "#{model} record with ID #{data['id']} already exists"
end
end
def load(file_path)
@@ -73,4 +94,8 @@ class Account::DataTransfer::RecordSet
rescue ArgumentError => e
raise IntegrityError, e.message
end
def model_dir
model.table_name
end
end
@@ -1,47 +0,0 @@
class Account::DataTransfer::StepRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
completed
content
created_at
id
updated_at
].freeze
private
def records
Step.where(account: account)
end
def export_record(step)
zip.add_file "data/steps/#{step.id}.json", step.as_json.to_json
end
def files
zip.glob("data/steps/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Step.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Step record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,45 +0,0 @@
class Account::DataTransfer::TagRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
created_at
id
title
updated_at
].freeze
private
def records
Tag.where(account: account)
end
def export_record(tag)
zip.add_file "data/tags/#{tag.id}.json", tag.as_json.to_json
end
def files
zip.glob("data/tags/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Tag.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Tag record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,46 +0,0 @@
class Account::DataTransfer::TaggingRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
tag_id
updated_at
].freeze
private
def records
Tagging.where(account: account)
end
def export_record(tagging)
zip.add_file "data/taggings/#{tagging.id}.json", tagging.as_json.to_json
end
def files
zip.glob("data/taggings/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Tagging.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Tagging record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -10,6 +10,10 @@ class Account::DataTransfer::UserRecordSet < Account::DataTransfer::RecordSet
updated_at
]
def initialize(account)
super(account: account, model: User)
end
private
def records
User.where(account: account)
@@ -1,47 +0,0 @@
class Account::DataTransfer::WatchRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
card_id
created_at
id
updated_at
user_id
watching
].freeze
private
def records
Watch.where(account: account)
end
def export_record(watch)
zip.add_file "data/watches/#{watch.id}.json", watch.as_json.to_json
end
def files
zip.glob("data/watches/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Watch.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Watch record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,47 +0,0 @@
class Account::DataTransfer::WebhookDelinquencyTrackerRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
consecutive_failures_count
created_at
first_failure_at
id
updated_at
webhook_id
].freeze
private
def records
Webhook::DelinquencyTracker.where(account: account)
end
def export_record(tracker)
zip.add_file "data/webhook_delinquency_trackers/#{tracker.id}.json", tracker.as_json.to_json
end
def files
zip.glob("data/webhook_delinquency_trackers/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Webhook::DelinquencyTracker.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "WebhookDelinquencyTracker record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,49 +0,0 @@
class Account::DataTransfer::WebhookDeliveryRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
created_at
event_id
id
request
response
state
updated_at
webhook_id
].freeze
private
def records
Webhook::Delivery.where(account: account)
end
def export_record(delivery)
zip.add_file "data/webhook_deliveries/#{delivery.id}.json", delivery.as_json.to_json
end
def files
zip.glob("data/webhook_deliveries/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Webhook::Delivery.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "WebhookDelivery record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end
@@ -1,50 +0,0 @@
class Account::DataTransfer::WebhookRecordSet < Account::DataTransfer::RecordSet
ATTRIBUTES = %w[
account_id
active
board_id
created_at
id
name
signing_secret
subscribed_actions
updated_at
url
].freeze
private
def records
Webhook.where(account: account)
end
def export_record(webhook)
zip.add_file "data/webhooks/#{webhook.id}.json", webhook.as_json.to_json
end
def files
zip.glob("data/webhooks/*.json")
end
def import_batch(files)
batch_data = files.map do |file|
data = load(file)
data.slice(*ATTRIBUTES).merge("account_id" => account.id)
end
Webhook.insert_all!(batch_data)
end
def validate_record(file_path)
data = load(file_path)
expected_id = File.basename(file_path, ".json")
unless data["id"].to_s == expected_id
raise IntegrityError, "Webhook record ID mismatch: expected #{expected_id}, got #{data['id']}"
end
missing = ATTRIBUTES - data.keys
if missing.any?
raise IntegrityError, "#{file_path} is missing required fields: #{missing.join(', ')}"
end
end
end