Hi, PFA patch for RM1720
Issue: In pgAdmin4 we use server id (sid, which is id of server table in sqlite db) as key to keep track of server connection (server manger). But sqlite reuses these ids and therefore pgadmin4 connection manager assigns connection details of one database server to another in some cases. To avoid this issue we now deleting server connection details (server manger) when user drops server. -- *Harshal Dhumal* *Software Engineer* EnterpriseDB India: http://www.enterprisedb.com The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/browser/server_groups/servers/__init__.py b/web/pgadmin/browser/server_groups/servers/__init__.py index 0b6c6ca..627cc5f 100644 --- a/web/pgadmin/browser/server_groups/servers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/__init__.py @@ -24,6 +24,7 @@ from pgadmin.utils.menu import MenuItem import config from config import PG_DEFAULT_DRIVER from pgadmin.model import db, Server, ServerGroup, User +from pgadmin.utils.driver import get_driver def has_any(data, keys): @@ -64,7 +65,6 @@ class ServerModule(sg.ServerGroupPluginModule): servers = Server.query.filter_by(user_id=current_user.id, servergroup_id=gid) - from pgadmin.utils.driver import get_driver driver = get_driver(PG_DEFAULT_DRIVER) for server in servers: @@ -156,7 +156,6 @@ class ServerModule(sg.ServerGroupPluginModule): sub-modules at once. """ if first_registration: - from pgadmin.utils.driver import get_driver driver = get_driver(PG_DEFAULT_DRIVER, app) app.jinja_env.filters['qtLiteral'] = driver.qtLiteral app.jinja_env.filters['qtIdent'] = driver.qtIdent @@ -224,7 +223,6 @@ class ServerNode(PGChildNodeView): servers = Server.query.filter_by(user_id=current_user.id, servergroup_id=gid) - from pgadmin.utils.driver import get_driver driver = get_driver(PG_DEFAULT_DRIVER) for server in servers: @@ -293,7 +291,6 @@ class ServerNode(PGChildNodeView): ) ) - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(server.id) conn = manager.connection() connected = conn.connected() @@ -355,6 +352,8 @@ class ServerNode(PGChildNodeView): for s in servers: db.session.delete(s) db.session.commit() + get_driver(PG_DEFAULT_DRIVER).delete_manager(sid) + except Exception as e: current_app.logger.exception(e) return make_json_response( @@ -405,7 +404,6 @@ class ServerNode(PGChildNodeView): request.data, encoding='utf-8' ) - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() connected = conn.connected() @@ -473,7 +471,6 @@ class ServerNode(PGChildNodeView): ).first() res = [] - from pgadmin.utils.driver import get_driver driver = get_driver(PG_DEFAULT_DRIVER) for server in servers: @@ -519,7 +516,6 @@ class ServerNode(PGChildNodeView): id=server.servergroup_id ).first() - from pgadmin.utils.driver import get_driver driver = get_driver(PG_DEFAULT_DRIVER) manager = driver.connection_manager(sid) @@ -594,7 +590,6 @@ class ServerNode(PGChildNodeView): user = None if 'connect_now' in data and data['connect_now']: - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(server.id) manager.update(server) conn = manager.connection() @@ -663,7 +658,6 @@ class ServerNode(PGChildNodeView): return make_json_response(data='') def statistics(self, gid, sid): - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() @@ -717,7 +711,6 @@ class ServerNode(PGChildNodeView): def connect_status(self, gid, sid): """Check and return the connection status.""" - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() res = conn.connected() @@ -769,7 +762,6 @@ class ServerNode(PGChildNodeView): save_password = False # Connect the Server - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() @@ -902,7 +894,6 @@ class ServerNode(PGChildNodeView): return bad_request(gettext("Server not found.")) # Release Connection - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) status = manager.release() @@ -923,7 +914,6 @@ class ServerNode(PGChildNodeView): """Reload the server configuration""" # Reload the server configurations - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() @@ -958,7 +948,6 @@ class ServerNode(PGChildNodeView): try: data = request.form restore_point_name = data['value'] if data else None - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() @@ -1033,7 +1022,6 @@ class ServerNode(PGChildNodeView): if user is None: return unauthorized(gettext("Unauthorized request.")) - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() @@ -1100,7 +1088,6 @@ class ServerNode(PGChildNodeView): ) try: - from pgadmin.utils.driver import get_driver manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection() diff --git a/web/pgadmin/utils/driver/psycopg2/__init__.py b/web/pgadmin/utils/driver/psycopg2/__init__.py index 2d6239f..5c2c53c 100644 --- a/web/pgadmin/utils/driver/psycopg2/__init__.py +++ b/web/pgadmin/utils/driver/psycopg2/__init__.py @@ -1721,6 +1721,16 @@ class Driver(BaseDriver): """ return self.connection_manager(sid).release(database, conn_id) + def delete_manager(self, sid): + """ + Delete manager for given server id. + """ + manager = self.connection_manager(sid) + manager.release() + if session['_id'] in self.managers and \ + str(sid) in self.managers[session['_id']]: + del self.managers[session['_id']][str(sid)] + def gc(self): """ Release the connections for the sessions, which have not pinged the
-- Sent via pgadmin-hackers mailing list (pgadmin-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgadmin-hackers