From faa66f1636ef3a8e6e8f82c011126f11a08b7ad0 Mon Sep 17 00:00:00 2001 From: Alessandro De Maria Date: Mon, 1 Mar 2021 17:59:47 +0530 Subject: [PATCH] =?UTF-8?q?Added=20'--replace'=20option=20in=20Import=20se?= =?UTF-8?q?rver=20to=C2=A0replace=20the=20list=20of=20servers=20with=20the?= =?UTF-8?q?=20newly=20imported=20one.=20Fixes=20#6270?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en_US/import_export_servers.rst | 7 ++++ docs/en_US/release_notes_5_1.rst | 1 + web/setup.py | 58 ++++++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/docs/en_US/import_export_servers.rst b/docs/en_US/import_export_servers.rst index 1e05f9617..25e842ef8 100644 --- a/docs/en_US/import_export_servers.rst +++ b/docs/en_US/import_export_servers.rst @@ -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 diff --git a/docs/en_US/release_notes_5_1.rst b/docs/en_US/release_notes_5_1.rst index 009ed9297..6db8f570d 100644 --- a/docs/en_US/release_notes_5_1.rst +++ b/docs/en_US/release_notes_5_1.rst @@ -9,6 +9,7 @@ This release contains a number of bug fixes and new features since the release o New features ************ +| `Issue #6270 `_ - Added '--replace' option in Import server to replace the list of servers with the newly imported one. Housekeeping ************ diff --git a/web/setup.py b/web/setup.py index 20ce6563b..d7bfe1a9d 100644 --- a/web/setup.py +++ b/web/setup.py @@ -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))