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

@ -57,6 +57,9 @@ path) of the JSON file containing the server definitions. Servers will be owned
by the desktop mode user (pgadmin4@pgadmin.org by default - see the DESKTOP_USER
setting in ``config.py``). This can be overridden with the ``--user`` command
line option. There can be multiple configuations of pgAdmin on the same system.
The default behaviour is for the imported servers to be added to the existent list,
which might lead to duplicates. This can be overridden with the ``--replace`` command
line option, which will replace the list of servers with the newly imported one.
To load the servers into a specific pgAdmin config DB file, ``--sqlite-path`` option
can be used. It is also recommended to use this option when running pgAdmin in
desktop mode. By default SQLITE_PATH setting in ``config.py`` is taken. For example:
@ -65,6 +68,10 @@ desktop mode. By default SQLITE_PATH setting in ``config.py`` is taken. For exam
/path/to/python /path/to/setup.py --load-servers input_file.json
# or, to replace the list of servers with the newly imported one:
/path/to/python /path/to/setup.py --load-servers input_file.json --replace
# or, to specify a non-default user name to own the new servers:
/path/to/python /path/to/setup.py --load-servers input_file.json --user user@example.com

View File

@ -9,6 +9,7 @@ This release contains a number of bug fixes and new features since the release o
New features
************
| `Issue #6270 <https://redmine.postgresql.org/issues/6270>`_ - Added '--replace' option in Import server to replace the list of servers with the newly imported one.
Housekeeping
************

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