Added '--replace' option in Import server to replace the list of servers with the newly imported one. Fixes #6270

This commit is contained in:
Alessandro De Maria
2021-03-01 17:59:47 +05:30
committed by Akshay Joshi
parent cb5d7190c4
commit faa66f1636
3 changed files with 62 additions and 4 deletions

View File

@@ -10,6 +10,9 @@
"""Perform the initial setup of the application, by creating the auth
and settings database."""
from pgadmin.model import db, User, Version, ServerGroup, Server, \
SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
from pgadmin import create_app
import argparse
import json
import os
@@ -28,10 +31,6 @@ root = os.path.dirname(os.path.realpath(__file__))
if sys.path[0] != root:
sys.path.insert(0, root)
from pgadmin import create_app
from pgadmin.model import db, User, Version, ServerGroup, Server, \
SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
def add_value(attr_dict, key, value):
"""Add a value to the attribute dict if non-empty.
@@ -398,6 +397,51 @@ def setup_db():
os.chmod(config.SQLITE_PATH, 0o600)
def clear_servers():
"""Clear groups and servers configurations.
Args:
args (ArgParser): The parsed command line options
"""
# What user?
load_user = args.user if args.user is not None else config.DESKTOP_USER
# And the sqlite path
if args.sqlite_path is not None:
config.SQLITE_PATH = args.sqlite_path
app = create_app(config.APP_NAME + '-cli')
with app.app_context():
user = User.query.filter_by(email=load_user).first()
if user is None:
print("The specified user ID (%s) could not be found." %
load_user)
sys.exit(1)
user_id = user.id
# Remove all servers
servers = Server.query.filter_by(user_id=user_id)
for server in servers:
db.session.delete(server)
# Remove all groups
groups = ServerGroup.query.filter_by(user_id=user_id)
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)
try:
db.session.commit()
except Exception as e:
print("Error clearing server configuration")
if __name__ == '__main__':
# Configuration settings
import config
@@ -415,7 +459,11 @@ if __name__ == '__main__':
imp_group = parser.add_argument_group('Load server config')
imp_group.add_argument('--load-servers', metavar="INPUT_FILE",
help='Load servers into the DB', required=False)
imp_group.add_argument('--replace', dest='replace', action='store_true',
help='replace server configurations',
required=False)
imp_group.set_defaults(replace=False)
# Common args
parser.add_argument('--sqlite-path', metavar="PATH",
help='Dump/load with the specified pgAdmin config DB'
@@ -442,6 +490,8 @@ if __name__ == '__main__':
print(str(e))
elif args.load_servers is not None:
try:
if args.replace:
clear_servers()
load_servers(args)
except Exception as e:
print(str(e))