diff --git a/web/pgadmin/browser/server_groups/servers/databases/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/__init__.py index 53f5b41..2c0cf48 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/__init__.py @@ -134,6 +134,14 @@ class DatabaseView(PGChildNodeView): self.conn = self.manager.connection() elif 'did' in kwargs: self.conn = self.manager.connection(did=kwargs['did']) + self.db_allow_connection = True + # If connection to database is not allowed then + # provide generic connection + if kwargs['did'] in self.conn.manager.db_info: + self._db = self.conn.manager.db_info[kwargs['did']] + if self._db['datallowconn'] is False: + self.conn = self.manager.connection() + self.db_allow_connection = False else: self.conn = self.manager.connection() @@ -538,13 +546,15 @@ class DatabaseView(PGChildNodeView): request.data, encoding='utf-8' ) + # Generic connection for offline updates conn = self.manager.connection() + if did is not None: # Fetch the name of database for comparison - status, rset = conn.execute_dict( + status, rset = self.conn.execute_dict( render_template( "/".join([self.template_path, 'nodes.sql']), - did=did, conn=conn, last_system_oid=0 + did=did, conn=self.conn, last_system_oid=0 ) ) if not status: @@ -559,8 +569,8 @@ class DatabaseView(PGChildNodeView): if 'name' not in data: data['name'] = data['old_name'] - status = self.manager.release(did=did) for action in ["rename_database", "tablespace"]: + status = self.manager.release(did=did) SQL = self.get_offline_sql(gid, sid, data, did, action) SQL = SQL.strip('\n').strip(' ') if SQL and SQL != "": @@ -568,20 +578,18 @@ class DatabaseView(PGChildNodeView): if not status: return internal_server_error(errormsg=msg) - if not self.manager.db_info[did]['datallowconn']: - return jsonify( - node=self.blueprint.generate_browser_node( - did, - sid, - data['name'], - "icon-database-not-connected", - connected=False, - allowConn=False - ) - ) + # Make connection for database again + if self._db['datallowconn']: + self.conn = self.manager.connection(did=did, auto_reconnect=True) + status, errmsg = self.conn.connect() - self.conn = self.manager.connection(database=data['name'], auto_reconnect=True) - status, errmsg = self.conn.connect() + if not status: + current_app.logger.error( + "Could not connected to database(#{0}).\nError: {1}".format( + did, errmsg + ) + ) + return internal_server_error(errmsg) SQL = self.get_online_sql(gid, sid, data, did) SQL = SQL.strip('\n').strip(' ') @@ -595,7 +603,12 @@ class DatabaseView(PGChildNodeView): did, sid, data['name'], - "pg-icon-{0}".format(self.node_type) + "pg-icon-{0}".format(self.node_type) if + self._db['datallowconn'] else + "icon-database-not-connected", + connected=self.conn.connected() if + self._db['datallowconn'] else False, + allowConn=self._db['datallowconn'] ) ) @@ -639,7 +652,6 @@ class DatabaseView(PGChildNodeView): return internal_server_error(errormsg=msg) - return make_json_response(success=1) @check_precondition(action="msql") @@ -693,8 +705,7 @@ class DatabaseView(PGChildNodeView): for action in ["rename_database", "tablespace"]: SQL += self.get_offline_sql(gid, sid, data, did, action) - if rset['rows'][0]['datallowconn']: - SQL += self.get_online_sql(gid, sid, data, did) + SQL += self.get_online_sql(gid, sid, data, did) else: SQL += self.get_new_sql(gid, sid, data, did) @@ -715,33 +726,30 @@ class DatabaseView(PGChildNodeView): acls = [] SQL_acl = '' - if ('datallowconn' in data and data['datallowconn']) or \ - 'datallowconn' not in data: - try: - acls = render_template( - "/".join([self.template_path, 'allowed_privs.json']) + try: + acls = render_template( + "/".join([self.template_path, 'allowed_privs.json']) + ) + acls = json.loads(acls, encoding='utf-8') + except Exception as e: + current_app.logger.exception(e) + + # Privileges + for aclcol in acls: + if aclcol in data: + allowedacl = acls[aclcol] + data[aclcol] = parse_priv_to_db( + data[aclcol], allowedacl['acl'] ) - acls = json.loads(acls, encoding='utf-8') - except Exception as e: - current_app.logger.exception(e) - - # Privileges - for aclcol in acls: - if aclcol in data: - allowedacl = acls[aclcol] - data[aclcol] = parse_priv_to_db( - data[aclcol], allowedacl['acl'] - ) - SQL_acl = render_template( - "/".join([self.template_path, 'grant.sql']), - data=data, conn=self.conn - ) + SQL_acl = render_template( + "/".join([self.template_path, 'grant.sql']), + data=data, conn=self.conn + ) - conn = self.manager.connection() SQL = render_template( "/".join([self.template_path, 'create.sql']), - data=data, conn=conn + data=data, conn=self.conn ) SQL += "\n" SQL += SQL_acl @@ -847,46 +855,44 @@ class DatabaseView(PGChildNodeView): if not status: return internal_server_error(errormsg=res) - if res['rows'][0]['datallowconn']: - SQL = render_template( - "/".join([self.template_path, 'acl.sql']), - did=did, conn=self.conn - ) - status, dataclres = self.conn.execute_dict(SQL) - if not status: - return internal_server_error(errormsg=dataclres) - res = self.formatdbacl(res, dataclres['rows']) + SQL = render_template( + "/".join([self.template_path, 'acl.sql']), + did=did, conn=self.conn + ) + status, dataclres = self.conn.execute_dict(SQL) + if not status: + return internal_server_error(errormsg=dataclres) + res = self.formatdbacl(res, dataclres['rows']) - SQL = render_template( - "/".join([self.template_path, 'defacl.sql']), - did=did, conn=self.conn - ) - status, defaclres = self.conn.execute_dict(SQL) - if not status: - return internal_server_error(errormsg=defaclres) + SQL = render_template( + "/".join([self.template_path, 'defacl.sql']), + did=did, conn=self.conn + ) + status, defaclres = self.conn.execute_dict(SQL) + if not status: + return internal_server_error(errormsg=defaclres) - res = self.formatdbacl(res, defaclres['rows']) + res = self.formatdbacl(res, defaclres['rows']) result = res['rows'][0] - if result['datallowconn']: - SQL = render_template( - "/".join([self.template_path, 'get_variables.sql']), - did=did, conn=self.conn - ) - status, res1 = self.conn.execute_dict(SQL) - if not status: - return internal_server_error(errormsg=res1) + SQL = render_template( + "/".join([self.template_path, 'get_variables.sql']), + did=did, conn=self.conn + ) + status, res1 = self.conn.execute_dict(SQL) + if not status: + return internal_server_error(errormsg=res1) - # Get Formatted Security Labels - if 'seclabels' in result: - # Security Labels is not available for PostgreSQL <= 9.1 - frmtd_sec_labels = parse_sec_labels_from_db(result['seclabels']) - result.update(frmtd_sec_labels) + # Get Formatted Security Labels + if 'seclabels' in result: + # Security Labels is not available for PostgreSQL <= 9.1 + frmtd_sec_labels = parse_sec_labels_from_db(result['seclabels']) + result.update(frmtd_sec_labels) - # Get Formatted Variables - frmtd_variables = parse_variables_from_db(res1['rows']) - result.update(frmtd_variables) + # Get Formatted Variables + frmtd_variables = parse_variables_from_db(res1['rows']) + result.update(frmtd_variables) sql_header = "-- Database: {0}\n\n-- ".format(result['name']) if hasattr(str, 'decode'): diff --git a/web/pgadmin/utils/driver/psycopg2/__init__.py b/web/pgadmin/utils/driver/psycopg2/__init__.py index 036a4cb..f7ba1b4 100644 --- a/web/pgadmin/utils/driver/psycopg2/__init__.py +++ b/web/pgadmin/utils/driver/psycopg2/__init__.py @@ -1525,6 +1525,7 @@ WHERE db.oid = {0}""".format(did)) if my_id in self.connections: self.connections[my_id]._release() del self.connections[my_id] + del self.db_info[did] if len(self.connections) == 0: self.ver = None diff --git a/web/regression/test_config.json.in b/web/regression/test_config.json.in index a373dee..3c41266 100644 --- a/web/regression/test_config.json.in +++ b/web/regression/test_config.json.in @@ -16,7 +16,7 @@ "comment": "PostgreSQL 9.4 Server (EDB Installer)", "db_username": "postgres", "host": "localhost", - "db_password": "PASSWORD", + "db_password": "edb", "db_port": 5432, "maintenance_db": "postgres", "sslmode": "prefer", diff --git a/web/regression/test_utils.py b/web/regression/test_utils.py index a2d2f5c..199fac2 100644 --- a/web/regression/test_utils.py +++ b/web/regression/test_utils.py @@ -69,7 +69,7 @@ def get_config_data(): """This function reads the server data from config_data""" server_data = [] for srv in test_setup.config_data['server_credentials']: - if (not srv.has_key('enabled')) or srv['enabled'] == True: + if 'enabled' not in srv or srv['enabled']: data = {"name": srv['name'], "comment": srv['comment'], "host": srv['host'],