Multisite restores uploads to 'default' instead of actual db name

I spent some time on this bug, and I wanted to update you with my findings. Basically, I confirmed everything you already said, the restore process is like that:

migrate_database
reconnect_database
...
extract_uploads

Problem is that after the migration task, reconnect_database is not switching to the desired one, but starts working on default.

Therefore this code is wrong

def extract_uploads
  ...
  current_db_name = 
  RailsMultisite::ConnectionManagement.current_db # default
  optimized_images_exist = File.exist?(File.join(tmp_uploads_path, 'optimized'))

A hacky solution would be to use here @current_db which still contains correct name instead of RailsMultisite::ConnectionManagement.current_db but this is not solving real issue why reconnect database is not working properly.

I need to dig deeper to find the problem.