Skip to content

A platform for community discussion. Free, open, simple.

FIX: Reconnect to the database after restoring a backup (#44323)

David Taylor6162fab1264f
diff --git a/lib/backup_restore/database_restorer.rb b/lib/backup_restore/database_restorer.rb
index bdea8aba2cd..4415fd17b10 100644
--- a/lib/backup_restore/database_restorer.rb
+++ b/lib/backup_restore/database_restorer.rb
@@ -190,6 +190,7 @@ module BackupRestore
log "Reconnecting to the database..."
RailsMultisite::ConnectionManagement.reload if RailsMultisite::ConnectionManagement.instance
RailsMultisite::ConnectionManagement.establish_connection(db: @current_db)
+ ActiveRecord::Base.connection.reconnect!
end
def create_missing_discourse_functions
diff --git a/spec/lib/backup_restore/database_restorer_multisite_spec.rb b/spec/lib/backup_restore/database_restorer_multisite_spec.rb
index e63ea0377f4..3321c1fe7e9 100644
--- a/spec/lib/backup_restore/database_restorer_multisite_spec.rb
+++ b/spec/lib/backup_restore/database_restorer_multisite_spec.rb
@@ -13,7 +13,7 @@ RSpec.describe BackupRestore::DatabaseRestorer, type: :multisite do
context "with database connection" do
it "reconnects to the correct database" do
RailsMultisite::ConnectionManagement.establish_connection(db: "second")
- execute_stubbed_restore
+ execute_stubbed_restore(stub_reconnect: false)
expect(RailsMultisite::ConnectionManagement.current_db).to eq("second")
end
end
diff --git a/spec/lib/backup_restore/database_restorer_spec.rb b/spec/lib/backup_restore/database_restorer_spec.rb
index f4f36114ab8..3d7459c9bab 100644
--- a/spec/lib/backup_restore/database_restorer_spec.rb
+++ b/spec/lib/backup_restore/database_restorer_spec.rb
@@ -17,6 +17,7 @@ RSpec.describe BackupRestore::DatabaseRestorer do
expect_psql(stub_thread: true).in_sequence(restore)
expect_db_migrate.in_sequence(restore)
expect_db_reconnect.in_sequence(restore)
+ ActiveRecord::Base.connection.expects(:reconnect!).in_sequence(restore)
restorer.restore("foo.sql")
end
@@ -29,6 +30,28 @@ RSpec.describe BackupRestore::DatabaseRestorer do
expect(BackupMetadata.value_for(BackupMetadata::LAST_RESTORE_DATE)).to eq(date_string)
end
+ context "without transactional tests" do
+ self.use_transactional_tests = false
+
+ after { BackupMetadata.delete_all }
+
+ it "reconnects when the database connection drops while the dump is restored" do
+ pid = DB.query_single("SELECT pg_backend_pid()").first
+ psql = BackupRestore::DatabaseRestorer.psql_command
+ BackupRestore::DatabaseRestorer.stubs(:psql_command).returns(
+ "#{psql} --command='SELECT pg_terminate_backend(#{pid})'",
+ )
+
+ execute_stubbed_restore(
+ stub_psql: false,
+ stub_reconnect: false,
+ dump_file_path: "/dev/null",
+ )
+
+ expect(BackupMetadata.last_restore_date).to be_present
+ end
+ end
+
context "with real psql" do
after do
psql = BackupRestore::DatabaseRestorer.psql_command
diff --git a/spec/lib/backup_restore/shared_context_for_backup_restore.rb b/spec/lib/backup_restore/shared_context_for_backup_restore.rb
index edff7de3202..08a73467da9 100644
--- a/spec/lib/backup_restore/shared_context_for_backup_restore.rb
+++ b/spec/lib/backup_restore/shared_context_for_backup_restore.rb
@@ -50,16 +50,23 @@ RSpec.shared_context "with shared backup restore context" do
RailsMultisite::ConnectionManagement.expects(:establish_connection).once
end
+ # A real reconnect drops the test transaction.
+ def stub_db_reconnect
+ ActiveRecord::Base.connection.stubs(:reconnect!)
+ end
+
def execute_stubbed_restore(
stub_readonly_functions: true,
stub_psql: true,
stub_migrate: true,
+ stub_reconnect: true,
dump_file_path: "foo.sql"
)
expect_table_move
expect_create_readonly_functions if stub_readonly_functions
expect_psql if stub_psql
expect_db_migrate if stub_migrate
+ stub_db_reconnect if stub_reconnect
subject.restore(dump_file_path)
end