Skip to content

Commit fa2bb05

Browse files
committed
fix: MS SQL throws "Cannot drop database xyz, because it is currently in use" when user is about to drop the current database
Sets the next or previous sibling database, whichever is available Refs #1436
1 parent 59d4f1f commit fa2bb05

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

source/main.pas

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,7 +3760,7 @@ procedure TMainForm.actInsertFilesExecute(Sender: TObject);
37603760
procedure TMainForm.actDropObjectsExecute(Sender: TObject);
37613761
var
37623762
msg, db: String;
3763-
Node: PVirtualNode;
3763+
Node, SiblingDB: PVirtualNode;
37643764
Obj: PDBObject;
37653765
DBObject: TDBObject;
37663766
ObjectList: TDBObjectList;
@@ -3781,7 +3781,15 @@ procedure TMainForm.actDropObjectsExecute(Sender: TObject);
37813781
try
37823782
db := DBObject.Database;
37833783
Node := FindDBNode(DBtree, Conn, db);
3784-
SetActiveDatabase('', Conn);
3784+
// Set focus on previous or next database, to prevent "Cannot drop database xyz, because it is currently in use"
3785+
// MS SQL on top cannot "un-use" the current database
3786+
SiblingDB := DBtree.GetNextSibling(Node);
3787+
if not Assigned(SiblingDB) then
3788+
SiblingDB := DBtree.GetPreviousSibling(Node);
3789+
if Assigned(SiblingDB) then
3790+
SetActiveDatabase(DBtree.Text[SiblingDB, 0], Conn)
3791+
else
3792+
SetActiveDatabase('', Conn); // Fallback if there is no sibling. Works on MySQL only.
37853793
Conn.Query(qDatabaseDrop, [Conn.QuoteIdent(db)]);
37863794
DBtree.DeleteNode(Node);
37873795
Conn.ClearDbObjects(db);

0 commit comments

Comments
 (0)