Skip to content

Commit 2e3d4e6

Browse files
Ensure proper error message shown if any error occurs while restoring psql tool.
1 parent 7c2b773 commit 2e3d4e6

6 files changed

Lines changed: 46 additions & 39 deletions

File tree

docs/en_US/preferences.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Use the fields on the *User Interface* panel to set the user interface related p
329329
format. If the application is closed unexpectedly, the tab is accidentally closed,
330330
or the page is refreshed, the saved state will be automatically restored for
331331
each respective tool. **Note:** Saving the application state will not preserve data for tool tabs opened in
332-
separate browser tabs when running in server mode.
332+
separate browser tabs when running in server mode.Any tool referring ad-hoc server connection will not be restored.
333333

334334
* Use the *Themes* drop-down listbox to select the theme for pgAdmin. You'll also get a preview just below the
335335
drop down. You can also submit your own themes,

web/pgadmin/static/js/ToolErrorView.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LAYOUT_EVENTS } from './helpers/Layout';
55
import { styled } from '@mui/material/styles';
66
import { FormHelperText, Box } from '@mui/material';
77
import HTMLReactParse from 'html-react-parser';
8+
import { deleteToolData } from '../../settings/static/ApplicationStateProvider';
89

910
const StyledBox = styled(Box)(({theme}) => ({
1011
color: theme.palette.text.primary,
@@ -15,11 +16,14 @@ const StyledBox = styled(Box)(({theme}) => ({
1516
height: '100%',
1617
}));
1718

18-
export default function ToolErrorView({error, panelId, panelDocker}){
19-
19+
export default function ToolErrorView({error, panelId, panelDocker, toolDataId}){
2020
panelDocker.eventBus.registerListener(LAYOUT_EVENTS.CLOSING, (id)=>{
2121
if(panelId == id) {
2222
panelDocker.close(panelId, true);
23+
if(toolDataId){
24+
let transId = toolDataId.replace('-','_');
25+
deleteToolData(transId, transId);
26+
}
2327
}
2428
});
2529

@@ -33,4 +37,5 @@ ToolErrorView.propTypes = {
3337
error: PropTypes.string,
3438
panelId: PropTypes.string,
3539
panelDocker: PropTypes.object,
40+
toolDataId: PropTypes.string
3641
};

web/pgadmin/tools/erd/static/js/erd_tool/components/ERDTool.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ export default class ERDTool extends React.Component {
348348
});
349349

350350
let done = await this.initConnection();
351-
if(!done) return;
351+
if(!done && !this.props.params.sql_id) return;
352352

353353
done = await this.loadPrequisiteData();
354-
if(!done) return;
354+
if(!done && !this.props.params.sql_id) return;
355355

356356

357357
if(this.props.params.sql_id){
@@ -361,7 +361,7 @@ export default class ERDTool extends React.Component {
361361
this.diagram.clearSelection();
362362
this.registerModelEvents();
363363
this.setState({dirty: true});
364-
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, this.serializeFile());
364+
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, sqlValue);
365365
}
366366
}
367367
else if(this.props.params.gen) {
@@ -597,6 +597,7 @@ export default class ERDTool extends React.Component {
597597
current_file: fileName,
598598
dirty: false,
599599
});
600+
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, res.data);
600601
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, false);
601602
this.setTitle(fileName);
602603
this.diagram.deserialize(res.data);

web/pgadmin/tools/psql/__init__.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ def panel(trans_id):
101101
s = Server.query.filter_by(id=int(params['sid'])).first()
102102
if s:
103103
data = _get_database_role(params['sid'], params['did'])
104-
params['db'] = underscore_escape(data['db_name']) \
105-
if 'db_name' in data else 'postgres'
106-
params['role'] = underscore_escape(data['role'])
104+
if data:
105+
params['db'] = underscore_escape(data['db_name']) \
106+
if 'db_name' in data else 'postgres'
107+
params['role'] = underscore_escape(data['role'])
107108
set_env_variables(is_win=_platform == 'win32')
108109
return render_template("psql/index.html",
109110
params=json.dumps(params))
@@ -303,7 +304,9 @@ def read_and_forward_pty_output(sid, data):
303304
# Check user is authenticated and PSQL is enabled in config.
304305
if current_user.is_authenticated and config.ENABLE_PSQL:
305306
connection_data = []
307+
connection_successful = False
306308
try:
309+
print('In first try')
307310
db = ''
308311
if data['db']:
309312
db = underscore_unescape(data['db'])
@@ -326,33 +329,36 @@ def read_and_forward_pty_output(sid, data):
326329
return
327330
connection_data = get_connection_str(psql_utility, db,
328331
manager)
332+
connection_successful = True
329333
except Exception as e:
330334
# If any error raised during the start the PSQL emit error to UI.
331335
# request.sid: This sid is socket id.
332-
sio.emit(
333-
'conn_error',
334-
{
335-
'error': 'Error while running psql command: {0}'.format(e),
336-
}, namespace='/pty', room=request.sid)
336+
error_msg = 'Error while running psql command: {0}'.format(e)
337+
if str(e) == 'Server is not connected.':
338+
error_msg = 'Error while opening psql tool: {0}'.format(e)
337339

338-
try:
339-
if str(data['sid']) not in app.config['sid_soid_mapping']:
340-
# request.sid: refer request.sid as socket id.
341-
app.config['sid_soid_mapping'][str(data['sid'])] = list()
342-
app.config['sid_soid_mapping'][str(data['sid'])].append(
343-
request.sid)
344-
else:
345-
app.config['sid_soid_mapping'][str(data['sid'])].append(
346-
request.sid)
340+
sio.emit( 'conn_error', {'error': error_msg},
341+
namespace='/pty', room=request.sid)
347342

348-
sio.start_background_task(read_and_forward_pty_output,
349-
request.sid, data)
350-
except Exception as e:
351-
sio.emit(
352-
'conn_error',
353-
{
354-
'error': 'Error while running psql command: {0}'.format(e),
355-
}, namespace='/pty', room=request.sid)
343+
if connection_successful:
344+
try:
345+
if str(data['sid']) not in app.config['sid_soid_mapping']:
346+
# request.sid: refer request.sid as socket id.
347+
app.config['sid_soid_mapping'][str(data['sid'])] = list()
348+
app.config['sid_soid_mapping'][str(data['sid'])].append(
349+
request.sid)
350+
else:
351+
app.config['sid_soid_mapping'][str(data['sid'])].append(
352+
request.sid)
353+
354+
sio.start_background_task(read_and_forward_pty_output,
355+
request.sid, data)
356+
except Exception as e:
357+
sio.emit(
358+
'conn_error',
359+
{
360+
'error': 'Error while running psql command: {0}'.format(e),
361+
}, namespace='/pty', room=request.sid)
356362
else:
357363
# Show error if user is not authenticated.
358364
sio.emit('conn_not_allow', {'sid': request.sid}, namespace='/pty',
@@ -378,12 +384,6 @@ def _get_connection(sid, data):
378384
status, msg = conn.connect()
379385
if not status:
380386
app.logger.error(msg)
381-
sio.emit(sio.emit(
382-
'conn_error',
383-
{
384-
'error': 'Error while running psql command: {0}'
385-
''.format('Server connection not present.'),
386-
}, namespace='/pty', room=request.sid))
387387
raise RuntimeError('Server is not connected.')
388388

389389
return conn, manager

web/pgadmin/tools/psql/static/js/PsqlModule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export default class Psql {
122122
},
123123
database: {
124124
_id: connectionInfo.did,
125-
label: connectionInfo.db
125+
_label: connectionInfo.db
126126
},
127127
schema: {
128128
_id: connectionInfo.scid || null,
@@ -161,7 +161,7 @@ export default class Psql {
161161
const transId = getRandomInt(1, 9999999);
162162
// Set psql tab title as per prefrences setting.
163163
let title_data = {
164-
'database': parentData.database ? _.unescape(parentData.database.label) : 'postgres' ,
164+
'database': parentData.database ? _.unescape(parentData.database._label) : 'postgres' ,
165165
'username': parentData.server.user.name,
166166
'server': parentData.server.label,
167167
'type': 'psql_tool',

web/pgadmin/tools/sqleditor/static/js/SQLEditorModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export default class SQLEditor {
254254
error={params.error}
255255
panelId={`${BROWSER_PANELS.QUERY_TOOL}_${params.trans_id}`}
256256
panelDocker={panelDocker}
257+
toolDataId={params.toolDataId}
257258
/> :
258259
<QueryToolComponent params={params}
259260
pgWindow={pgWindow}

0 commit comments

Comments
 (0)