Properly handle setting comments and other options on databases with allowconn = False. Fixes #2024

This commit is contained in:
Murtuza Zabuawala 2017-02-01 10:23:45 +00:00 committed by Dave Page
parent 6d05302ca1
commit 2d442da9ff
2 changed files with 100 additions and 75 deletions

View File

@ -134,6 +134,14 @@ class DatabaseView(PGChildNodeView):
self.conn = self.manager.connection() self.conn = self.manager.connection()
elif 'did' in kwargs: elif 'did' in kwargs:
self.conn = self.manager.connection(did=kwargs['did']) 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: else:
self.conn = self.manager.connection() self.conn = self.manager.connection()
@ -532,13 +540,23 @@ class DatabaseView(PGChildNodeView):
request.data, encoding='utf-8' request.data, encoding='utf-8'
) )
conn = self.manager.connection() # Generic connection for offline updates
conn = self.manager.connection(conn_id="db_offline_update")
status, errmsg = conn.connect()
if not status:
current_app.logger.error(
"Could not create database connection for offline updates\nErr: {0}".format(
errmsg
)
)
return internal_server_error(errmsg)
if did is not None: if did is not None:
# Fetch the name of database for comparison # Fetch the name of database for comparison
status, rset = conn.execute_dict( status, rset = self.conn.execute_dict(
render_template( render_template(
"/".join([self.template_path, 'nodes.sql']), "/".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: if not status:
@ -553,7 +571,10 @@ class DatabaseView(PGChildNodeView):
if 'name' not in data: if 'name' not in data:
data['name'] = data['old_name'] data['name'] = data['old_name']
status = self.manager.release(did=did) # Release any existing connection from connection manager
# to perform offline operation
self.manager.release(did=did)
for action in ["rename_database", "tablespace"]: for action in ["rename_database", "tablespace"]:
SQL = self.get_offline_sql(gid, sid, data, did, action) SQL = self.get_offline_sql(gid, sid, data, did, action)
SQL = SQL.strip('\n').strip(' ') SQL = SQL.strip('\n').strip(' ')
@ -562,21 +583,21 @@ class DatabaseView(PGChildNodeView):
if not status: if not status:
return internal_server_error(errormsg=msg) return internal_server_error(errormsg=msg)
if not self.manager.db_info[did]['datallowconn']: # Make connection for database again
return jsonify( if self._db['datallowconn']:
node=self.blueprint.generate_browser_node( self.conn = self.manager.connection(
did, database=data['name'], auto_reconnect=True
sid,
data['name'],
"icon-database-not-connected",
connected=False,
allowConn=False
) )
)
self.conn = self.manager.connection(database=data['name'], auto_reconnect=True)
status, errmsg = self.conn.connect() 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 = self.get_online_sql(gid, sid, data, did)
SQL = SQL.strip('\n').strip(' ') SQL = SQL.strip('\n').strip(' ')
if SQL and SQL != "": if SQL and SQL != "":
@ -584,12 +605,21 @@ class DatabaseView(PGChildNodeView):
if not status: if not status:
return internal_server_error(errormsg=msg) return internal_server_error(errormsg=msg)
# Release any existing connection from connection manager
# used for offline updates
self.manager.release(conn_id="db_offline_update")
return jsonify( return jsonify(
node=self.blueprint.generate_browser_node( node=self.blueprint.generate_browser_node(
did, did,
sid, sid,
data['name'], 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']
) )
) )
@ -633,7 +663,6 @@ class DatabaseView(PGChildNodeView):
return internal_server_error(errormsg=msg) return internal_server_error(errormsg=msg)
return make_json_response(success=1) return make_json_response(success=1)
@check_precondition(action="msql") @check_precondition(action="msql")
@ -687,7 +716,6 @@ class DatabaseView(PGChildNodeView):
for action in ["rename_database", "tablespace"]: for action in ["rename_database", "tablespace"]:
SQL += self.get_offline_sql(gid, sid, data, did, action) 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: else:
SQL += self.get_new_sql(gid, sid, data, did) SQL += self.get_new_sql(gid, sid, data, did)
@ -709,8 +737,6 @@ class DatabaseView(PGChildNodeView):
acls = [] acls = []
SQL_acl = '' SQL_acl = ''
if ('datallowconn' in data and data['datallowconn']) or \
'datallowconn' not in data:
try: try:
acls = render_template( acls = render_template(
"/".join([self.template_path, 'allowed_privs.json']) "/".join([self.template_path, 'allowed_privs.json'])
@ -732,10 +758,9 @@ class DatabaseView(PGChildNodeView):
data=data, conn=self.conn data=data, conn=self.conn
) )
conn = self.manager.connection()
SQL = render_template( SQL = render_template(
"/".join([self.template_path, 'create.sql']), "/".join([self.template_path, 'create.sql']),
data=data, conn=conn data=data, conn=self.conn
) )
SQL += "\n" SQL += "\n"
SQL += SQL_acl SQL += SQL_acl
@ -841,7 +866,6 @@ class DatabaseView(PGChildNodeView):
if not status: if not status:
return internal_server_error(errormsg=res) return internal_server_error(errormsg=res)
if res['rows'][0]['datallowconn']:
SQL = render_template( SQL = render_template(
"/".join([self.template_path, 'acl.sql']), "/".join([self.template_path, 'acl.sql']),
did=did, conn=self.conn did=did, conn=self.conn
@ -863,7 +887,6 @@ class DatabaseView(PGChildNodeView):
result = res['rows'][0] result = res['rows'][0]
if result['datallowconn']:
SQL = render_template( SQL = render_template(
"/".join([self.template_path, 'get_variables.sql']), "/".join([self.template_path, 'get_variables.sql']),
did=did, conn=self.conn did=did, conn=self.conn

View File

@ -1525,6 +1525,8 @@ WHERE db.oid = {0}""".format(did))
if my_id in self.connections: if my_id in self.connections:
self.connections[my_id]._release() self.connections[my_id]._release()
del self.connections[my_id] del self.connections[my_id]
if did is not None:
del self.db_info[did]
if len(self.connections) == 0: if len(self.connections) == 0:
self.ver = None self.ver = None