Skip to content

Commit

Permalink
Add an object menu option to disconnect all server and database conne…
Browse files Browse the repository at this point in the history
…ctions. #1984
  • Loading branch information
pravesh-sharma authored Nov 6, 2024
1 parent 6cb71c9 commit b51eef1
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ define('pgadmin.node.database', [
enable : 'is_connected',data: {
data_disabled: gettext('Selected database is already disconnected.'),
},
},{
name: 'disconnect_all_databases', node: 'coll-database', module: this,
applies: ['object', 'context'], callback: 'disconnect_all_databases',
category: 'disconnect', priority: 5, label: gettext('Disconnect from all databases'),
enable: 'can_disconnect',data: {
data_disabled: gettext('All the databases are already disconnected.'),
},
},{
name: 'generate_erd', node: 'database', module: this,
applies: ['object', 'context'], callback: 'generate_erd',
Expand Down Expand Up @@ -207,6 +214,14 @@ define('pgadmin.node.database', [
}
}
},
can_disconnect: function(node, item) {
return _.some(item.children, (child) => {
let data = pgAdmin.Browser.tree.getData(child),
{connected, canDisconn} = data;
return connected && canDisconn;
}
);
},
callbacks: {
/* Connect the database */
connect_database: function(args){
Expand All @@ -230,45 +245,31 @@ define('pgadmin.node.database', [
d = i ? t.itemData(i) : undefined;

if (d) {
disconnect_from_database(obj, d, t, i, true);
}

return false;
},

disconnect_all_databases: function(args, item) {
let children = item.children ?? [],
obj = this,
t = pgBrowser.tree;

if (children) {
pgAdmin.Browser.notifier.confirm(
gettext('Disconnect from database'),
gettext('Are you sure you want to disconnect from database - %s?', d.label),
gettext('Disconnect from all databases'),
gettext('Are you sure you want to disconnect from all databases?'),
function() {
let data = d;
getApiInstance().delete(
obj.generate_url(i, 'connect', d, true),
).then(({data: res})=> {
if (res.success == 1) {
let prv_i = t.parent(i);
if(res.data.info_prefix) {
res.info = `${_.escape(res.data.info_prefix)} - ${res.info}`;
}
pgAdmin.Browser.notifier.success(res.info);
t.removeIcon(i);
data.connected = false;
data.icon = data.isTemplate ? 'icon-database-template-not-connected':'icon-database-not-connected';

t.addIcon(i, {icon: data.icon});
t.unload(i);
pgBrowser.Events.trigger('pgadmin:browser:tree:update-tree-state', i);
setTimeout(function() {
t.select(prv_i);
}, 10);

} else {
try {
pgAdmin.Browser.notifier.error(res.errormsg);
} catch (e) {
console.warn(e.stack || e);
}
t.unload(i);
_.forEach(children, function(child) {
let data = pgAdmin.Browser.tree.getData(child);
if (data.connected && data.canDisconn) {
disconnect_from_database(obj, data, t, child, false);
}
}).catch(function(error) {
pgAdmin.Browser.notifier.pgRespErrorNotify(error);
t.unload(i);
});
t.deselect(item);
},
function() { return true; }
function() { return true;},
);
}

Expand Down Expand Up @@ -502,6 +503,63 @@ define('pgadmin.node.database', [
error, obj, data, tree, item, wasConnected
);
});
},
disconnect_from_database = function(obj, data, tree, item, notify=false) {
let d = data,
t = tree,
i = item,
label = data.label;
let disconnect = function() {
d.label = `<span class='text-muted'>[Disconnecting...]</span> ${label}`;
t.setLabel(i,{label:d.label});
t.close(i);
let data = d;
getApiInstance().delete(
obj.generate_url(i, 'connect', d, true),
).then(({data: res})=> {
if (res.success == 1) {
let prv_i = t.parent(i);
if(res.data.info_prefix) {
res.info = `${_.escape(res.data.info_prefix)} - ${res.info}`;
}
pgAdmin.Browser.notifier.success(res.info);
t.removeIcon(i);
data.connected = false;
data.icon = data.isTemplate ? 'icon-database-template-not-connected':'icon-database-not-connected';
d.label = label;
t.setLabel(i, {label});
t.addIcon(i, {icon: data.icon});
t.unload(i);
pgBrowser.Events.trigger('pgadmin:browser:tree:update-tree-state', i);
setTimeout(function() {
t.select(prv_i);
}, 10);

} else {
try {
pgAdmin.Browser.notifier.error(res.errormsg);
} catch (e) {
console.warn(e.stack || e);
}
t.unload(i);
}
}).catch(function(error) {
pgAdmin.Browser.notifier.pgRespErrorNotify(error);
t.unload(i);
});
};
if (notify) {
pgAdmin.Browser.notifier.confirm(
gettext('Disconnect from database'),
gettext('Are you sure you want to disconnect from database - %s?', d.label),
function() {
disconnect();
},
function() { return true; }
);
} else {
disconnect();
}
};
}

Expand Down
165 changes: 106 additions & 59 deletions web/pgadmin/browser/server_groups/servers/static/js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ define('pgadmin.node.server', [
applies: ['object', 'context'], callback: 'show_obj_properties',
category: 'register', priority: 1, label: gettext('Server...'),
data: {action: 'create'}, enable: 'canCreate',
},{
name: 'disconnect_all_servers', node: 'server_group', module: this,
applies: ['object','context'], callback: 'disconnect_all_servers',
priority: 5, label: gettext('Disconnect from all servers'),
data:{action: 'disconnect_all'}, enable: 'can_disconnect_all'
},{
name: 'create_server', node: 'server', module: this,
applies: ['object', 'context'], callback: 'show_obj_properties',
Expand Down Expand Up @@ -200,6 +205,9 @@ define('pgadmin.node.server', [
node?.connected && node?.user?.is_superuser
&& node?.in_recovery && node?.wal_pause);
},
can_disconnect_all: function(node, item) {
return _.some(item.children, (child) => pgAdmin.Browser.tree.getData(child).connected);
},
callbacks: {
/* Connect the server */
connect_server: function(args){
Expand All @@ -215,73 +223,40 @@ define('pgadmin.node.server', [
return false;
},
/* Disconnect the server */
disconnect_server: function(args, notify) {
disconnect_server: function(args) {
let input = args || {},
obj = this,
t = pgBrowser.tree,
i = 'item' in input ? input.item : t.selected(),
d = i ? t.itemData(i) : undefined;

if (d) {
notify = notify || _.isUndefined(notify) || _.isNull(notify);

let disconnect = function() {
getApiInstance().delete(
obj.generate_url(i, 'connect', d, true),
).then(({data: res})=> {
if (res.success == 1) {
pgAdmin.Browser.notifier.success(res.info);
d = t.itemData(i);
t.removeIcon(i);
d.connected = false;
// Update server tree node data after server diconnected.
t.update(i,d);

// Generate the event that server is disconnected
pgBrowser.Events.trigger(
'pgadmin:server:disconnect',
{item: i, data: d}, false
);
if (d.shared && pgAdmin.server_mode == 'True'){
d.icon = 'icon-shared-server-not-connected';
}else{
d.icon = 'icon-server-not-connected';
}
t.addIcon(i, {icon: d.icon});
obj.callbacks.refresh.apply(obj, [null, i]);
setTimeout(() => {
t.close(i);
}, 10);
if (pgBrowser.serverInfo && d._id in pgBrowser.serverInfo) {
delete pgBrowser.serverInfo[d._id];
}
else {
try {
pgAdmin.Browser.notifier.error(res.errormsg);
} catch (e) {
console.warn(e.stack || e);
}
t.unload(i);
disconnect_from_server(obj, d, t, i, true);
}
return false;
},
disconnect_all_servers: function(args, item) {
let children = item.children ?? [],
obj = this,
t = pgBrowser.tree;
if (children) {
pgAdmin.Browser.notifier.confirm(
gettext('Disconnect from all servers'),
gettext('Are you sure you want to disconnect from all servers?'),
function() {
_.forEach(children, function(child) {
let data = pgAdmin.Browser.tree.getData(child);
if (data.connected) {
disconnect_from_server(obj, data, t, child, false);
}
}
}).catch(function(error) {
pgAdmin.Browser.notifier.pgRespErrorNotify(error);
t.unload(i);
});
};

if (notify) {
pgAdmin.Browser.notifier.confirm(
gettext('Disconnect from server'),
gettext('Are you sure you want to disconnect from the server %s?', d.label),
function() { disconnect(); },
function() { return true;},
);
} else {
disconnect();
}
});
t.deselect(item);
setTimeout(() => {
t.select(item);
}, 100);
},
function() { return true;},
);
}

return false;
},
/* Connect the server (if not connected), before opening this node */
Expand Down Expand Up @@ -819,6 +794,78 @@ define('pgadmin.node.server', [
pgAdmin.Browser.notifier.pgRespErrorNotify(error);
});
};
let disconnect_from_server = async function(obj, data, tree, item, notify=false) {
let d = data,
i = item,
t = tree,
label = data.label;

let disconnect = function() {
d.label = `<span class='text-muted'>[Disconnecting...]</span> ${label}`;
t.setLabel(i,{label:d.label});
t.close(i);
getApiInstance().delete(
obj.generate_url(i, 'connect', d, true),
).then(({data: res})=> {
if (res.success == 1) {
if (notify) {
pgAdmin.Browser.notifier.success(res.info);
} else {
pgAdmin.Browser.notifier.success(`${label} - ${res.info}`);
}
d = t.itemData(i);
t.removeIcon(i);
d.connected = false;
d.label = label;
t.setLabel(i, {label});
// Update server tree node data after server diconnected.
t.update(i,d);
// Generate the event that server is disconnected
pgBrowser.Events.trigger(
'pgadmin:server:disconnect',
{item: i, data: d}, false
);
if (d.shared && pgAdmin.server_mode == 'True'){
d.icon = 'icon-shared-server-not-connected';
}else{
d.icon = 'icon-server-not-connected';
}
t.addIcon(i, {icon: d.icon});
obj.callbacks.refresh.apply(obj, [null, i]);
setTimeout(() => {
t.close(i);
}, 10);
if (pgBrowser.serverInfo && d._id in pgBrowser.serverInfo) {
delete pgBrowser.serverInfo[d._id];
}
else {
try {
pgAdmin.Browser.notifier.error(res.errormsg);
} catch (e) {
console.warn(e.stack || e);
}
t.setLabel(i, {label});
t.unload(i);
}
}
}).catch(function(error) {
pgAdmin.Browser.notifier.pgRespErrorNotify(error);
t.setLabel(i, {label});
t.unload(i);
});
};

if (notify) {
pgAdmin.Browser.notifier.confirm(
gettext('Disconnect from server'),
gettext('Are you sure you want to disconnect from the server %s?', label),
function() { disconnect(); },
function() { return true;},
);
} else {
disconnect();
}
};
}

return pgBrowser.Nodes['server'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ export default function pgadminOverride(theme) {
'&.icon-terminal': {
fontSize: '1.3rem !important',
}
},
'.text-muted': {
color: theme.otherVars.textMuted
}
};}
2 changes: 1 addition & 1 deletion web/pgadmin/static/js/tree/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class Tree {
let idx = 0;
let node_cnt = 0;
let result = {};
if (identifier === undefined) return;
if (!identifier) return;
let item = TreeNode.prototype.isPrototypeOf(identifier) ? identifier : this.findNode(identifier.path);
if (!item) return;
do {
Expand Down

0 comments on commit b51eef1

Please sign in to comment.