diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2017-04-21 08:55:24 +0300 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2017-04-24 09:46:10 +0300 |
commit | 133f00bedd990e4220cbd89cbfbe7f69e98c0c76 (patch) | |
tree | 9ecf1575341c19c1b690edac5bb504c7f917f2dd /spec/services/users | |
parent | 6c65b63ca5fb60ae26c900b4615054d2ff66eeb9 (diff) |
Move records to the ghost user in a transaction.
- While deleting a user, some of the user's associated records are moved to the
ghost user so they aren't deleted. The user is blocked before these records
are moved, to prevent the user from creating new records while the migration
is happening, and so preventing a data race.
- Previously, if the migration failed, the user would _remain_ blocked, which is
not the expected behavior. On the other hand, we can't just stick the block +
migration into a transaction, because we want the block to be committed before
the migration starts (for the data race reason mentioned above).
- One solution (implemented in this commit) is to block the user in a parent
transaction, migrate the associated records in a nested sub-transaction, and
then unblock the user in the parent transaction if the sub-transaction fails.
Diffstat (limited to 'spec/services/users')
-rw-r--r-- | spec/services/users/migrate_to_ghost_user_service_spec.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/spec/services/users/migrate_to_ghost_user_service_spec.rb b/spec/services/users/migrate_to_ghost_user_service_spec.rb index 8c5b7e41c15..9e1edf1ac30 100644 --- a/spec/services/users/migrate_to_ghost_user_service_spec.rb +++ b/spec/services/users/migrate_to_ghost_user_service_spec.rb @@ -60,5 +60,23 @@ describe Users::MigrateToGhostUserService, services: true do end end end + + context "when record migration fails with a rollback exception" do + before do + expect_any_instance_of(MergeRequest::ActiveRecord_Associations_CollectionProxy) + .to receive(:update_all).and_raise(ActiveRecord::Rollback) + end + + context "for records that were already migrated" do + let!(:issue) { create(:issue, project: project, author: user) } + let!(:merge_request) { create(:merge_request, source_project: project, author: user, target_branch: "first") } + + it "reverses the migration" do + service.execute + + expect(issue.reload.author).to eq(user) + end + end + end end end |