Prevent auto-commit/rollback being changed mid-transaction, and make the Preferences the defaults, not the current values.

This commit is contained in:
Akshay Joshi 2019-03-04 10:44:13 +00:00 committed by Dave Page
parent efeb9d6d20
commit 0aeb20ee64
6 changed files with 46 additions and 96 deletions

View File

@ -105,20 +105,6 @@ function updateUIPreferences(sqlEditor) {
.attr('title',
shortcut_title('Rollback',preferences.rollback_transaction));
/* Set Auto-commit and auto-rollback on query editor */
if (preferences.auto_commit) {
$el.find('.auto-commit').removeClass('visibility-hidden');
}
else {
$el.find('.auto-commit').addClass('visibility-hidden');
}
if (preferences.auto_rollback) {
$el.find('.auto-rollback').removeClass('visibility-hidden');
}
else {
$el.find('.auto-rollback').addClass('visibility-hidden');
}
/* Set explain options on query editor */
if (preferences.explain_verbose){
$el.find('.explain-verbose').removeClass('visibility-hidden');

View File

@ -28,6 +28,7 @@ from config import PG_DEFAULT_DRIVER
from pgadmin.model import Server
from pgadmin.utils.driver import get_driver
from pgadmin.utils.exception import ConnectionLost, SSHTunnelConnectionLost
from pgadmin.utils.preferences import Preferences
query_tool_close_session_lock = Lock()
@ -384,6 +385,11 @@ def initialize_query_tool(sgid, sid, did=None):
else:
sql_grid_data = session['gridData']
# Set the value of auto commit and auto rollback specified in Preferences
pref = Preferences.module('sqleditor')
command_obj.set_auto_commit(pref.preference('auto_commit').get())
command_obj.set_auto_rollback(pref.preference('auto_rollback').get())
# Use pickle to store the command object which will be used
# later by the sql grid module.
sql_grid_data[trans_id] = {

View File

@ -308,15 +308,6 @@ def start_query_tool(trans_id):
connect = 'connect' in request.args and request.args['connect'] == '1'
# Preferences can be changed from outside.
# Get the latest preferences for auto commit, rollback,etc. before
# executing the query
error_msg = set_trans_preferences(trans_id)
if error_msg == gettext('Transaction ID not found in the session.'):
return make_json_response(success=0, errormsg=error_msg,
info='DATAGRID_TRANSACTION_REQUIRED',
status=404)
return StartRunningQuery(blueprint, current_app.logger).execute(
sql, trans_id, session, connect
)
@ -335,30 +326,9 @@ def extract_sql_from_network_parameters(request_data, request_arguments,
return request_arguments or request_form_data
def set_trans_preferences(trans_id):
# Check the transaction and connection status
status, error_msg, conn, trans_obj, session_obj = \
check_transaction_status(trans_id)
if error_msg == gettext('Transaction ID not found in the session.'):
return error_msg
if status and conn is not None and \
trans_obj is not None and session_obj is not None:
# Call the set_auto_commit and set_auto_rollback method of
# transaction object
trans_obj.set_auto_commit(blueprint.auto_commit.get())
trans_obj.set_auto_rollback(blueprint.auto_rollback.get())
# As we changed the transaction object we need to
# restore it and update the session variable.
session_obj['command_obj'] = pickle.dumps(trans_obj, -1)
update_session_grid_transaction(trans_id, session_obj)
@blueprint.route(
'/query_tool/preferences/<int:trans_id>',
methods=["GET", "PUT"], endpoint='query_tool_preferences'
methods=["PUT"], endpoint='query_tool_preferences'
)
@login_required
def preferences(trans_id):
@ -368,42 +338,24 @@ def preferences(trans_id):
Args:
trans_id: unique transaction id
"""
if request.method == 'GET':
error_msg = set_trans_preferences(trans_id)
if error_msg == gettext('Transaction ID not found in the session.'):
return make_json_response(success=0, errormsg=error_msg,
info='DATAGRID_TRANSACTION_REQUIRED',
status=404)
return make_json_response(
data={
'explain_verbose': blueprint.explain_verbose.get(),
'explain_costs': blueprint.explain_costs.get(),
'explain_buffers': blueprint.explain_buffers.get(),
'explain_timing': blueprint.explain_timing.get(),
'auto_commit': blueprint.auto_commit.get(),
'auto_rollback': blueprint.auto_rollback.get()
}
)
data = None
if request.data:
data = json.loads(request.data, encoding='utf-8')
else:
data = None
if request.data:
data = json.loads(request.data, encoding='utf-8')
else:
data = request.args or request.form
for k, v in data.items():
v = bool(v)
if k == 'explain_verbose':
blueprint.explain_verbose.set(v)
elif k == 'explain_costs':
blueprint.explain_costs.set(v)
elif k == 'explain_buffers':
blueprint.explain_buffers.set(v)
elif k == 'explain_timing':
blueprint.explain_timing.set(v)
data = request.args or request.form
for k, v in data.items():
v = bool(v)
if k == 'explain_verbose':
blueprint.explain_verbose.set(v)
elif k == 'explain_costs':
blueprint.explain_costs.set(v)
elif k == 'explain_buffers':
blueprint.explain_buffers.set(v)
elif k == 'explain_timing':
blueprint.explain_timing.set(v)
return success_return()
return success_return()
@blueprint.route('/poll/<int:trans_id>', methods=["GET"], endpoint='poll')
@ -1157,9 +1109,6 @@ def set_auto_commit(trans_id):
# Call the set_auto_commit method of transaction object
trans_obj.set_auto_commit(auto_commit)
# Set Auto commit in preferences
blueprint.auto_commit.set(bool(auto_commit))
# As we changed the transaction object we need to
# restore it and update the session variable.
session_obj['command_obj'] = pickle.dumps(trans_obj, -1)
@ -1205,9 +1154,6 @@ def set_auto_rollback(trans_id):
# Call the set_auto_rollback method of transaction object
trans_obj.set_auto_rollback(auto_rollback)
# Set Auto Rollback in preferences
blueprint.auto_rollback.set(bool(auto_rollback))
# As we changed the transaction object we need to
# restore it and update the session variable.
session_obj['command_obj'] = pickle.dumps(trans_obj, -1)

View File

@ -554,6 +554,20 @@ define('tools.querytool', [
self.reflectPreferences();
/* Set Auto-commit and auto-rollback on query editor */
if (self.preferences.auto_commit) {
$('.auto-commit').removeClass('visibility-hidden');
}
else {
$('.auto-commit').addClass('visibility-hidden');
}
if (self.preferences.auto_rollback) {
$('.auto-rollback').removeClass('visibility-hidden');
}
else {
$('.auto-rollback').addClass('visibility-hidden');
}
/* Register for preference changed event broadcasted in parent
* to reload the shorcuts. As sqleditor is in iFrame of wcDocker
* window parent is referred
@ -3414,7 +3428,6 @@ define('tools.querytool', [
// This function is used to enable/disable buttons
disable_tool_buttons: function(disabled) {
$('#btn-clear-dropdown').prop('disabled', disabled);
$('#btn-query-dropdown').prop('disabled', disabled);
$('#btn-explain').prop('disabled', disabled);
$('#btn-explain-analyze').prop('disabled', disabled);
$('#btn-explain-options-dropdown').prop('disabled', disabled);
@ -3427,6 +3440,9 @@ define('tools.querytool', [
if (this.is_query_tool) {
// Cancel query tool needs opposite behaviour
$('#btn-cancel-query').prop('disabled', !disabled);
$('#btn-query-dropdown').prop('disabled', !this.is_transaction_buttons_disabled);
} else {
$('#btn-query-dropdown').prop('disabled', disabled);
}
},
@ -3665,8 +3681,6 @@ define('tools.querytool', [
.done(function(res) {
if (!res.data.status)
alertify.alert(gettext('Auto Rollback Error'), res.data.result);
else
self.call_cache_preferences();
})
.fail(function(e) {
@ -3700,8 +3714,6 @@ define('tools.querytool', [
.done(function(res) {
if (!res.data.status)
alertify.alert(gettext('Auto Commit Error'), res.data.result);
else
self.call_cache_preferences();
})
.fail(function(e) {
let msg = httpErrorHandler.handleQueryToolAjaxError(

View File

@ -23,17 +23,13 @@ class StartQueryTool(BaseTestGenerator):
calls the needed functions
"""
@patch('pgadmin.tools.sqleditor.set_trans_preferences')
@patch('pgadmin.tools.sqleditor.extract_sql_from_network_parameters')
def runTest(self, extract_sql_from_network_parameters_mock,
set_trans_preferences):
def runTest(self, extract_sql_from_network_parameters_mock):
"""Check correct function is called to handle to run query."""
extract_sql_from_network_parameters_mock.return_value = \
'transformed sql'
set_trans_preferences.return_value = ''
with patch.object(StartRunningQuery,
'execute',
return_value='some result'

View File

@ -65,13 +65,17 @@ def RegisterQueryToolPreferences(self):
self.auto_commit = self.preference.register(
'Options', 'auto_commit',
gettext("Auto commit?"), 'boolean', True,
category_label=gettext('Options')
category_label=gettext('Options'),
help_str=gettext('Set auto commit on or off by default in new Query '
'Tool tabs.')
)
self.auto_rollback = self.preference.register(
'Options', 'auto_rollback',
gettext("Auto rollback?"), 'boolean', False,
category_label=gettext('Options')
category_label=gettext('Options'),
help_str=gettext('Set auto rollback on or off by default in new Query '
'Tool tabs.')
)
self.sql_font_size = self.preference.register(