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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View File

@ -182,6 +182,10 @@ class ServerGroupView(NodeView):
).order_by("id")
# if server group id is 1 we won't delete it.
# This matches the behavior of
# web/pgadmin/utils/__init.py__#clear_database_servers
# called by the setup script when importing and replacing servers:
# `python setup.py --load-servers input_file.json --replace`
sg = groups.first()
shared_servers = Server.query.filter_by(servergroup_id=gid,

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()