diff --git a/lib/backup_restore/restorer.rb b/lib/backup_restore/restorer.rb index 42fd2db39f9..1efceafd128 100644 --- a/lib/backup_restore/restorer.rb +++ b/lib/backup_restore/restorer.rb @@ -404,6 +404,7 @@ module BackupRestore def reconnect_database log "Reconnecting to the database..." + RailsMultisite::ConnectionManagement::reload RailsMultisite::ConnectionManagement::establish_connection(db: @current_db) end diff --git a/spec/lib/backup_restore/restorer_spec.rb b/spec/lib/backup_restore/restorer_spec.rb index 2968156d94a..3ae24d76c7d 100644 --- a/spec/lib/backup_restore/restorer_spec.rb +++ b/spec/lib/backup_restore/restorer_spec.rb @@ -100,4 +100,26 @@ describe BackupRestore::Restorer do @restorer.copy_archive_to_tmp_directory end end + + context 'Database connection' do + fab!(:admin) { Fabricate(:admin) } + before do + SiteSetting.allow_restore = true + @restore_path = File.join(Rails.root, 'public', 'backups', RailsMultisite::ConnectionManagement.current_db) + described_class.any_instance.stubs(ensure_we_have_a_filename: true) + described_class.any_instance.stubs(initialize_state: true) + end + let(:conn) { RailsMultisite::ConnectionManagement } + let(:restorer) { described_class.new(admin.id) } + + it 'correctly reconnects to database' do + restorer.instance_variable_set(:@current_db, 'second') + conn.config_filename = "spec/fixtures/multisite/two_dbs.yml" + conn.establish_connection(db: 'second') + expect(RailsMultisite::ConnectionManagement.current_db).to eq('second') + ActiveRecord::Base.connection_pool.spec.config[:db_key] = "incorrect_db" + restorer.send(:reconnect_database) + expect(RailsMultisite::ConnectionManagement.current_db).to eq('second') + end + end end