Ensure that the macro query result should be download properly. Fixes #5965

This commit is contained in:
Rahul Shirsat
2020-12-14 11:58:53 +05:30
committed by Akshay Joshi
parent 296d22ad83
commit 6475a70514
18 changed files with 147 additions and 76 deletions

View File

@@ -1336,7 +1336,7 @@ def start_query_download_tool(trans_id):
)
data = request.values if request.values else None
if data is None or (data and 'query' not in data):
if data is None:
return make_json_response(
status=410,
success=0,
@@ -1346,12 +1346,9 @@ def start_query_download_tool(trans_id):
)
try:
sql = data['query']
# This returns generator of records.
status, gen = sync_conn.execute_on_server_as_csv(
sql, records=2000
)
status, gen = sync_conn.execute_on_server_as_csv(records=2000)
if not status:
return make_json_response(
@@ -1362,6 +1359,7 @@ def start_query_download_tool(trans_id):
r = Response(
gen(
trans_obj,
quote=blueprint.csv_quoting.get(),
quote_char=blueprint.csv_quote_char.get(),
field_separator=blueprint.csv_field_separator.get(),

View File

@@ -136,7 +136,7 @@ define('tools.querytool', [
'click #btn-flash': 'on_flash',
'click #btn-flash-menu': 'on_flash',
'click #btn-cancel-query': 'on_cancel_query',
'click #btn-download': 'on_download',
'click #btn-save-results-to-file': 'on_download',
'click #btn-clear': 'on_clear',
'click #btn-auto-commit': 'on_auto_commit',
'click #btn-auto-rollback': 'on_auto_rollback',
@@ -1358,7 +1358,7 @@ define('tools.querytool', [
self.handler.fetching_rows = true;
$('#btn-flash').prop('disabled', true);
$('#btn-download').prop('disabled', true);
$('#btn-save-results-to-file').prop('disabled', true);
if (fetch_all) {
self.handler.trigger(
@@ -1382,7 +1382,7 @@ define('tools.querytool', [
.done(function(res) {
self.handler.has_more_rows = res.data.has_more_rows;
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
$('#btn-save-results-to-file').prop('disabled', false);
self.handler.trigger('pgadmin-sqleditor:loading-icon:hide');
self.update_grid_data(res.data.result);
self.handler.fetching_rows = false;
@@ -1392,7 +1392,7 @@ define('tools.querytool', [
})
.fail(function(e) {
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
$('#btn-save-results-to-file').prop('disabled', false);
self.handler.trigger('pgadmin-sqleditor:loading-icon:hide');
self.handler.has_more_rows = false;
self.handler.fetching_rows = false;
@@ -2534,6 +2534,7 @@ define('tools.querytool', [
self.server_type = url_params.server_type;
self.url_params = url_params;
self.is_transaction_buttons_disabled = true;
self.is_save_results_to_file_disabled = true;
// We do not allow to call the start multiple times.
if (self.gridView)
@@ -2783,7 +2784,7 @@ define('tools.querytool', [
);
$('#btn-flash').prop('disabled', true);
$('#btn-download').prop('disabled', true);
self.enable_disable_download_btn(true);
self.trigger(
'pgadmin-sqleditor:loading-icon:message',
@@ -3058,7 +3059,12 @@ define('tools.querytool', [
// Hide the loading icon
self_col.trigger('pgadmin-sqleditor:loading-icon:hide');
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
if (!_.isUndefined(data) && Array.isArray(data.result) && data.result.length > 0) {
self.enable_disable_download_btn(false);
}
else {
self.enable_disable_download_btn(true);
}
}.bind(self)
);
},
@@ -3239,7 +3245,6 @@ define('tools.querytool', [
if (status != 'Busy') {
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
self.trigger('pgadmin-sqleditor:loading-icon:hide');
if(!self.total_time) {
@@ -3301,6 +3306,12 @@ define('tools.querytool', [
return (self.get('can_edit'));
},
enable_disable_download_btn: function(is_save_results_to_file_disabled) {
var self = this;
self.is_save_results_to_file_disabled = is_save_results_to_file_disabled;
$('#btn-save-results-to-file').prop('disabled', is_save_results_to_file_disabled);
},
rows_to_delete: function(data) {
let self = this;
let tmp_keys = self.primary_keys;
@@ -4373,10 +4384,9 @@ define('tools.querytool', [
}
self.disable_tool_buttons(false);
is_query_running = false;
if(!_.isUndefined(self.download_csv_obj)) {
self.download_csv_obj.abort();
if(!_.isUndefined(self.download_results_obj)) {
self.download_results_obj.abort();
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
self.trigger(
'pgadmin-sqleditor:loading-icon:hide');
}
@@ -4394,25 +4404,25 @@ define('tools.querytool', [
},
// Trigger query result download to csv.
trigger_csv_download: function(query, filename) {
trigger_csv_download: function(filename) {
var self = this,
url = url_for('sqleditor.query_tool_download', {
'trans_id': self.transId,
}),
data = { query: query, filename: filename };
data = { filename: filename };
// Disable the Execute button
$('#btn-flash').prop('disabled', true);
$('#btn-download').prop('disabled', true);
self.enable_disable_download_btn(true);
self.disable_tool_buttons(true);
self.set_sql_message('');
self.trigger(
'pgadmin-sqleditor:loading-icon:show',
gettext('Downloading CSV...')
gettext('Downloading Results...')
);
// Get the CSV file
self.download_csv_obj = $.ajax({
self.download_results_obj = $.ajax({
type: 'POST',
url: url,
data: data,
@@ -4443,19 +4453,19 @@ define('tools.querytool', [
}
document.body.removeChild(link);
self.download_csv_obj = undefined;
self.download_results_obj = undefined;
}
// Enable the execute button
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
self.enable_disable_download_btn(false);
self.disable_tool_buttons(false);
self.trigger('pgadmin-sqleditor:loading-icon:hide');
}).fail(function(err) {
let msg = '';
// Enable the execute button
$('#btn-flash').prop('disabled', false);
$('#btn-download').prop('disabled', false);
self.enable_disable_download_btn(false);
self.disable_tool_buttons(false);
self.trigger('pgadmin-sqleditor:loading-icon:hide');

View File

@@ -96,7 +96,7 @@ class TestDownloadCSV(BaseTestGenerator):
]
def setUp(self):
self._db_name = 'download_csv_' + str(random.randint(10000, 65535))
self._db_name = 'download_results_' + str(random.randint(10000, 65535))
self._sid = self.server_information['server_id']
server_con = server_utils.connect_server(self, self._sid)
@@ -105,6 +105,24 @@ class TestDownloadCSV(BaseTestGenerator):
self.server, self._db_name
)
# This method is responsible for initiating query hit at least once,
# so that download csv works
def initiate_sql_query_tool(self, trans_id, sql_query):
# This code is to ensure to create a async cursor so that downloading
# csv can work.
# Start query tool transaction
url = '/sqleditor/query_tool/start/{0}'.format(trans_id)
response = self.tester.post(url, data=json.dumps({"sql": sql_query}),
content_type='html/json')
self.assertEqual(response.status_code, 200)
# Query tool polling
url = '/sqleditor/poll/{0}'.format(trans_id)
response = self.tester.get(url)
return response
def runTest(self):
db_con = database_utils.connect_database(self,
@@ -121,6 +139,8 @@ class TestDownloadCSV(BaseTestGenerator):
response = self.tester.post(url)
self.assertEqual(response.status_code, 200)
res = self.initiate_sql_query_tool(self.trans_id, self.sql)
# If invalid tx test then make the Tx id invalid so that tests fails
if not self.is_valid_tx:
self.trans_id = self.trans_id + '007'
@@ -129,7 +149,11 @@ class TestDownloadCSV(BaseTestGenerator):
url = self.donwload_url.format(self.trans_id)
# Disable the console logging from Flask logger
self.app.logger.disabled = True
if self.filename is None:
if not self.is_valid and self.is_valid_tx:
# When user enters wrong query, poll will throw 500, so expecting
# 500, as poll is never called for a wrong query.
self.assertEqual(res.status_code, 500)
elif self.filename is None:
if self.download_as_txt:
with patch('pgadmin.tools.sqleditor.blueprint.'
'csv_field_separator.get', return_value=';'), patch(

View File

@@ -377,8 +377,8 @@ def register_query_tool_preferences(self):
self.preference.register(
'keyboard_shortcuts',
'download_csv',
gettext('Download CSV'),
'download_results',
gettext('Download Results'),
'keyboardshortcut',
{
'alt': False,