2019-01-02 04:24:12 -06:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2024-01-01 02:43:48 -06:00
|
|
|
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2019-01-02 04:24:12 -06:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-28 02:15:59 -06:00
|
|
|
import { getNodeListByName } from '../../../../browser/static/js/node_ajax';
|
|
|
|
import getApiInstance from 'sources/api_instance';
|
|
|
|
import {retrieveAncestorOfTypeServer} from 'sources/tree/tree_utils';
|
2023-07-13 02:31:48 -05:00
|
|
|
import RestoreSchema, {getRestoreSaveOptSchema, getRestoreDisableOptionSchema, getRestoreMiscellaneousSchema, getRestoreTypeObjSchema, getRestoreSectionSchema} from './restore.ui';
|
2023-10-23 07:13:17 -05:00
|
|
|
import pgAdmin from 'sources/pgadmin';
|
2021-12-28 02:15:59 -06:00
|
|
|
|
2017-07-18 09:13:16 -05:00
|
|
|
define('tools.restore', [
|
2021-12-28 02:15:59 -06:00
|
|
|
'sources/gettext', 'sources/url_for', 'pgadmin.browser',
|
|
|
|
'tools/restore/static/js/menu_utils', 'sources/nodes/supported_database_node',
|
2017-06-07 05:23:02 -05:00
|
|
|
], function(
|
2021-12-28 02:15:59 -06:00
|
|
|
gettext, url_for, pgBrowser, menuUtils, supportedNodes
|
2017-06-07 05:23:02 -05:00
|
|
|
) {
|
2016-05-15 09:29:57 -05:00
|
|
|
|
2018-01-12 01:29:51 -06:00
|
|
|
// if module is already initialized, refer to that.
|
|
|
|
if (pgBrowser.Restore) {
|
|
|
|
return pgBrowser.Restore;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an Object Restore of pgBrowser class
|
|
|
|
pgBrowser.Restore = {
|
|
|
|
init: function() {
|
|
|
|
if (this.initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
|
|
|
// Define the nodes on which the menus to be appear
|
2022-09-08 07:38:58 -05:00
|
|
|
let menus = [{
|
2018-01-12 01:29:51 -06:00
|
|
|
name: 'restore_object',
|
|
|
|
module: this,
|
|
|
|
applies: ['tools'],
|
2021-12-28 02:15:59 -06:00
|
|
|
callback: 'restoreObjects',
|
2021-12-16 06:59:44 -06:00
|
|
|
priority: 2,
|
2018-01-12 01:29:51 -06:00
|
|
|
label: gettext('Restore...'),
|
|
|
|
icon: 'fa fa-upload',
|
2021-12-16 06:59:44 -06:00
|
|
|
below: true,
|
2018-06-05 05:36:19 -05:00
|
|
|
enable: supportedNodes.enabled.bind(
|
2021-09-27 06:14:26 -05:00
|
|
|
null, pgBrowser.tree, menuUtils.restoreSupportedNodes
|
2018-06-05 05:36:19 -05:00
|
|
|
),
|
2021-02-02 03:17:58 -06:00
|
|
|
data: {
|
2023-03-28 11:50:14 -05:00
|
|
|
data_disabled: gettext('Please select any schema or table from the object explorer to Restore data.'),
|
2021-02-02 03:17:58 -06:00
|
|
|
},
|
2018-01-12 01:29:51 -06:00
|
|
|
}];
|
|
|
|
|
2022-01-18 03:19:54 -06:00
|
|
|
for (let sup_node_val of menuUtils.restoreSupportedNodes) {
|
2018-01-12 01:29:51 -06:00
|
|
|
menus.push({
|
2022-01-18 03:19:54 -06:00
|
|
|
name: 'restore_' + sup_node_val,
|
|
|
|
node: sup_node_val,
|
2018-01-12 01:29:51 -06:00
|
|
|
module: this,
|
|
|
|
applies: ['context'],
|
2021-12-28 02:15:59 -06:00
|
|
|
callback: 'restoreObjects',
|
|
|
|
priority: 2,
|
2018-01-12 01:29:51 -06:00
|
|
|
label: gettext('Restore...'),
|
2018-06-05 05:36:19 -05:00
|
|
|
enable: supportedNodes.enabled.bind(
|
2021-09-27 06:14:26 -05:00
|
|
|
null, pgBrowser.tree, menuUtils.restoreSupportedNodes
|
2018-06-05 05:36:19 -05:00
|
|
|
),
|
2018-01-12 01:29:51 -06:00
|
|
|
});
|
|
|
|
}
|
2016-08-08 05:59:37 -05:00
|
|
|
|
2018-01-12 01:29:51 -06:00
|
|
|
pgBrowser.add_menus(menus);
|
|
|
|
return this;
|
|
|
|
},
|
2021-12-28 02:15:59 -06:00
|
|
|
getUISchema: function(treeItem) {
|
|
|
|
let treeNodeInfo = pgBrowser.tree.getTreeNodeHierarchy(treeItem);
|
|
|
|
const selectedNode = pgBrowser.tree.selected();
|
|
|
|
let itemNodeData = pgBrowser.tree.findNodeByDomElement(selectedNode).getData();
|
|
|
|
|
|
|
|
return new RestoreSchema(
|
|
|
|
()=>getRestoreSectionSchema({selectedNodeType: itemNodeData._type}),
|
|
|
|
()=>getRestoreTypeObjSchema({selectedNodeType: itemNodeData._type}),
|
|
|
|
()=>getRestoreSaveOptSchema({nodeInfo: treeNodeInfo}),
|
|
|
|
()=>getRestoreDisableOptionSchema({nodeInfo: treeNodeInfo}),
|
|
|
|
()=>getRestoreMiscellaneousSchema({nodeInfo: treeNodeInfo}),
|
|
|
|
{
|
|
|
|
role: ()=>getNodeListByName('role', treeNodeInfo, itemNodeData)
|
|
|
|
},
|
|
|
|
treeNodeInfo,
|
|
|
|
pgBrowser
|
2018-06-05 05:36:19 -05:00
|
|
|
);
|
2021-12-28 02:15:59 -06:00
|
|
|
},
|
2022-07-27 23:44:04 -05:00
|
|
|
saveCallBack: function(data) {
|
2021-12-28 02:15:59 -06:00
|
|
|
if(data.errormsg) {
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.notifier.alert(
|
2022-02-07 00:21:27 -06:00
|
|
|
gettext('Error'),
|
2021-12-28 02:15:59 -06:00
|
|
|
gettext(data.errormsg)
|
|
|
|
);
|
|
|
|
} else {
|
2022-08-11 00:19:45 -05:00
|
|
|
pgBrowser.BgProcessManager.startProcess(data.data.job_id, data.data.desc);
|
2021-12-28 02:15:59 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setExtraParameters: function(treeInfo, nodeData) {
|
2022-09-08 07:38:58 -05:00
|
|
|
let extraData = {};
|
2021-12-28 02:15:59 -06:00
|
|
|
extraData['database'] = treeInfo.database._label;
|
|
|
|
|
|
|
|
if('schema' in treeInfo) {
|
2022-01-25 08:44:10 -06:00
|
|
|
extraData['schemas'] = [treeInfo.schema._label];
|
2021-12-28 02:15:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if('table' in treeInfo) {
|
|
|
|
extraData['tables'] = [nodeData._label];
|
|
|
|
}
|
|
|
|
|
|
|
|
if('function' in treeInfo) {
|
|
|
|
extraData['functions'] = [nodeData._label];
|
|
|
|
}
|
2022-01-06 05:16:44 -06:00
|
|
|
extraData['save_btn_icon'] = 'upload';
|
2021-12-28 02:15:59 -06:00
|
|
|
return extraData;
|
|
|
|
},
|
|
|
|
url_for_utility_exists: function(id){
|
|
|
|
return url_for('restore.utility_exists', {
|
|
|
|
'sid': id,
|
|
|
|
});
|
|
|
|
},
|
2023-10-23 07:13:17 -05:00
|
|
|
restoreObjects: function(_action, treeItem) {
|
2022-09-08 07:38:58 -05:00
|
|
|
let that = this,
|
2021-12-28 02:15:59 -06:00
|
|
|
tree = pgBrowser.tree,
|
|
|
|
i = treeItem || tree.selected(),
|
|
|
|
data = i ? tree.itemData(i) : undefined,
|
|
|
|
treeNodeInfo = pgBrowser.tree.getTreeNodeHierarchy(treeItem);
|
|
|
|
|
|
|
|
const serverInformation = retrieveAncestorOfTypeServer(pgBrowser, treeItem, gettext('Restore Error')),
|
|
|
|
sid = serverInformation._type == 'database' ? serverInformation._pid : serverInformation._id,
|
|
|
|
api = getApiInstance(),
|
|
|
|
utility_exists_url = that.url_for_utility_exists(sid);
|
|
|
|
|
|
|
|
return api({
|
|
|
|
url: utility_exists_url,
|
|
|
|
method: 'GET'
|
|
|
|
}).then((res)=>{
|
|
|
|
if (!res.data.success) {
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.notifier.alert(
|
2021-12-28 02:15:59 -06:00
|
|
|
gettext('Utility not found'),
|
|
|
|
gettext(res.data.errormsg)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-08 07:38:58 -05:00
|
|
|
let schema = that.getUISchema(treeItem);
|
2021-12-28 02:15:59 -06:00
|
|
|
let urlShortcut = 'restore.create_job',
|
2023-10-23 07:13:17 -05:00
|
|
|
urlBase = url_for(urlShortcut, {
|
2021-12-28 02:15:59 -06:00
|
|
|
'sid': sid,
|
|
|
|
}),
|
|
|
|
extraData = that.setExtraParameters(treeNodeInfo, data);
|
|
|
|
|
2022-09-08 07:38:58 -05:00
|
|
|
let sqlHelpUrl = 'backup.html',
|
2021-12-28 02:15:59 -06:00
|
|
|
helpUrl = url_for('help.static', {
|
|
|
|
'filename': 'restore_dialog.html',
|
|
|
|
});
|
|
|
|
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.Events.trigger('pgadmin:utility:show', treeItem,
|
|
|
|
gettext(`Restore (${pgBrowser.Nodes[data._type].label}: ${data.label})`),{
|
|
|
|
schema, extraData, urlBase, sqlHelpUrl, helpUrl, saveBtnName: gettext('Restore')
|
|
|
|
}, pgAdmin.Browser.stdW.md
|
|
|
|
);
|
2021-12-28 02:15:59 -06:00
|
|
|
}).catch(()=>{
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.notifier.alert(
|
2021-12-28 02:15:59 -06:00
|
|
|
gettext('Utility not found'),
|
|
|
|
gettext('Failed to fetch Utility information')
|
|
|
|
);
|
|
|
|
});
|
2018-01-12 01:29:51 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
return pgBrowser.Restore;
|
|
|
|
});
|