Skip to content

Commit eca9d42

Browse files
committed
Switch away from the current database when trying to recreate it
1 parent d8e4bbb commit eca9d42

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,20 @@ def add_lock!(sql, options)
584584
sql
585585
end
586586

587-
def recreate_database(name)
587+
def recreate_database(name)
588+
# Switch to another database or we'll receive a "Database in use" error message.
589+
existing_database = current_database.to_s
590+
if name.to_s == existing_database
591+
# The master database should be available on all SQL Server instances, use that
592+
execute 'USE master'
593+
end
594+
595+
# Recreate the database
588596
drop_database(name)
589597
create_database(name)
598+
599+
# Switch back to the database if we switched away from it above
600+
execute "USE #{existing_database}" if name.to_s == existing_database
590601
end
591602

592603
def drop_database(name)

0 commit comments

Comments
 (0)