Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/sequent/internal/partition_key_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ class PartitionKeyChange < Sequent::ApplicationRecord
belongs_to :partitioned_aggregate, primary_key: :aggregate_id, foreign_key: :aggregate_id

def self.update_aggregate_partition_keys(limit:)
count = 0
limit.times do
ActiveRecord::Base.transaction do
if Sequent::Migrations::Versions.running.present?
fail Sequent::Migrations::ConcurrentMigration,
'cannot update partition keys while view schema migration is running'
end

change = PartitionKeyChange.first
return unless change
PartitionKeyChange.connection.exec_update("SET LOCAL statement_timeout TO '30s'", 'statement_timeout')

change = PartitionKeyChange.lock('FOR UPDATE SKIP LOCKED').first
return count unless change

count += 1

PartitionedAggregate
.where(aggregate_id: change.aggregate_id)
Expand All @@ -30,6 +35,7 @@ def self.update_aggregate_partition_keys(limit:)
change.destroy!
end
end
count
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/sequent/rake/migration_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ def register_tasks!
'usage rake sequent:aggregates:update_partition_keys[limit]'
end

count = Sequent::Internal::PartitionKeyChange.count
Sequent.logger.info "Applying #{count} partition key changes (limited to #{limit})"
total_count = Sequent::Internal::PartitionKeyChange.count
Sequent.logger.info "Applying #{total_count} partition key changes (limited to #{limit})"

begin
Sequent::Internal::PartitionKeyChange.update_aggregate_partition_keys(limit:)
applied_count = Sequent::Internal::PartitionKeyChange.update_aggregate_partition_keys(limit:)
Sequent.logger.info "Applied #{applied_count} out of #{total_count} partition key changes"
rescue Sequent::Migrations::ConcurrentMigration
Sequent.logger.error 'View schema migration is active so not updating partition keys'
end
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/sequent/internal/partitioned_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ module Internal
end

it 'updates the aggregate and events using a maintenance task' do
PartitionKeyChange.update_aggregate_partition_keys(limit: 10)
applied_count = PartitionKeyChange.update_aggregate_partition_keys(limit: 10)
expect(applied_count).to eq(1)

logged_change = PartitionKeyChange.find_by(aggregate_id:)
expect(logged_change).to be_nil
Expand Down
Loading