Fix an issue where the default server-group is being deleted if the load-server json file contains no servers. #6602

This commit is contained in:
Benjamin Blattberg
2023-10-23 00:46:57 -05:00
committed by GitHub
parent fe753bd272
commit e587ef404c
2 changed files with 12 additions and 7 deletions

View File

@@ -781,14 +781,15 @@ def clear_database_servers(load_user=current_user, from_setup=False):
for server in servers:
db.session.delete(server)
# Remove all groups
groups = ServerGroup.query.filter_by(user_id=user_id)
# Remove all servergroups except for the first
# This matches the UI behavior in
# web/pgadmin/browser/server_groups/__init__.py#delete
# TODO: Investigate if we can skip the first with an `offset(1)`
groups = ServerGroup.query.filter_by(user_id=user_id).order_by("id")
default_sg = groups.first()
for group in groups:
db.session.delete(group)
servers = Server.query.filter_by(user_id=user_id)
for server in servers:
db.session.delete(server)
if group.id != default_sg.id:
db.session.delete(group)
try:
db.session.commit()