Skip to content

Commit fc41c79

Browse files
Add menu items for truncating foreign tables. pgadmin-org#6396
1 parent 7caaf2d commit fc41c79

3 files changed

Lines changed: 112 additions & 4 deletions

File tree

docs/en_US/menu_bar.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ following options (in alphabetical order):
8181
+-----------------------------+--------------------------------------------------------------------------------------------------------------------------+
8282
| *Trigger(s)* | Click to *Disable* or *Enable* trigger(s) for the currently selected table. Options are displayed on the flyout menu. |
8383
+-----------------------------+--------------------------------------------------------------------------------------------------------------------------+
84-
| *Truncate* | Click to remove all rows from a table (*Truncate*), to remove all rows from a table and its child tables |
85-
| | (*Truncate Cascade*) or to remove all rows from a table and automatically restart sequences owned by columns |
86-
| | (*Truncate Restart Identity*). Options are displayed on the flyout menu. |
84+
| *Truncate* | Click to remove all rows from a table/foreign tables (*Truncate*), to remove all rows from a table/foreign tables and |
85+
| | its child tables (*Truncate Cascade*) or to remove all rows from a table/foreign tables and automatically restart |
86+
| | sequences owned by columns (*Truncate Restart Identity*). Options are displayed on the flyout menu. |
8787
+-----------------------------+--------------------------------------------------------------------------------------------------------------------------+
8888
| *View Data* | Click to access a context menu that provides several options for viewing data (see below). |
8989
+-----------------------------+--------------------------------------------------------------------------------------------------------------------------+

web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
columns import utils as column_utils
3838
from pgadmin.browser.server_groups.servers.databases.schemas.tables.\
3939
triggers import utils as trigger_utils
40+
from pgadmin.browser.server_groups.servers.databases.schemas.tables.\
41+
utils import BaseTableView
4042

4143

4244
class ForeignTableModule(SchemaChildModule):
@@ -102,7 +104,7 @@ def register(self, app, options):
102104
blueprint = ForeignTableModule(__name__)
103105

104106

105-
class ForeignTableView(PGChildNodeView, DataTypeReader,
107+
class ForeignTableView(BaseTableView, DataTypeReader,
106108
SchemaDiffObjectCompare):
107109
"""
108110
class ForeignTableView(PGChildNodeView)
@@ -187,6 +189,9 @@ class ForeignTableView(PGChildNodeView)
187189
* compare(**kwargs):
188190
- This function will compare the foreign table nodes from two different
189191
schemas.
192+
193+
* truncate(gid, sid, scid, tid):
194+
- This function will truncate foreign table object
190195
"""
191196

192197
node_type = blueprint.node_type
@@ -211,6 +216,7 @@ class ForeignTableView(PGChildNodeView)
211216
{'get': 'list', 'post': 'create', 'delete': 'delete'}
212217
],
213218
'delete': [{'delete': 'delete'}, {'delete': 'delete'}],
219+
'truncate': [{'put': 'truncate'}],
214220
'children': [{'get': 'children'}],
215221
'nodes': [{'get': 'node'}, {'get': 'nodes'}],
216222
'sql': [{'get': 'sql'}],
@@ -409,6 +415,9 @@ def wrap(*args, **kwargs):
409415
self.template_path = \
410416
self.BASE_TEMPLATE_PATH.format(self.manager.version)
411417

418+
self.table_template_path = compile_template_path(
419+
'tables/sql', self.manager.version)
420+
412421
self.foreign_table_column_template_path = compile_template_path(
413422
'foreign_table_columns/sql', self.manager.version)
414423

@@ -884,6 +893,39 @@ def update(self, gid, sid, did, scid, foid):
884893
except Exception as e:
885894
return internal_server_error(errormsg=str(e))
886895

896+
@check_precondition
897+
def truncate(self, gid, sid, did, scid, foid):
898+
"""
899+
This function will truncate the foreign table.
900+
901+
Args:
902+
gid: Server Group ID
903+
sid: Server ID
904+
did: Database ID
905+
scid: Schema ID
906+
foid: Foreign Table ID
907+
"""
908+
909+
try:
910+
SQL = render_template(
911+
"/".join([self.template_path, self._PROPERTIES_SQL]),
912+
did=did, scid=scid, foid=foid,
913+
datlastsysoid=self._DATABASE_LAST_SYSTEM_OID
914+
)
915+
status, res = self.conn.execute_dict(SQL)
916+
if not status:
917+
return internal_server_error(errormsg=res)
918+
919+
if len(res['rows']) == 0:
920+
return gone(gettext(self.not_found_error_msg()))
921+
922+
return super().truncate(
923+
gid, sid, did, scid, foid, res
924+
)
925+
926+
except Exception as e:
927+
return internal_server_error(errormsg=str(e))
928+
887929
@check_precondition
888930
def sql(self, gid, sid, did, scid, foid=None, **kwargs):
889931
"""

web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getNodePrivilegeRoleSchema } from '../../../../../static/js/privilege.u
1212
import ForeignTableSchema from './foreign_table.ui';
1313
import _ from 'lodash';
1414
import Notify from '../../../../../../../../static/js/helpers/Notifier';
15+
import getApiInstance from '../../../../../../../../static/js/api_instance';
1516

1617
/* Create and Register Foreign Table Collection and Node. */
1718
define('pgadmin.node.foreign_table', ['pgadmin.tables.js/enable_disable_triggers',
@@ -72,6 +73,21 @@ define('pgadmin.node.foreign_table', ['pgadmin.tables.js/enable_disable_triggers
7273
applies: ['object', 'context'], callback: 'show_obj_properties',
7374
category: 'create', priority: 4, label: gettext('Foreign Table...'),
7475
data: {action: 'create', check: false}, enable: 'canCreate',
76+
},{
77+
name: 'truncate_foreign_table', node: 'foreign_table', module: this,
78+
applies: ['object', 'context'], callback: 'truncate_foreign_table',
79+
category: gettext('Truncate'), priority: 3, label: gettext('Truncate'),
80+
enable : 'canCreate',
81+
},{
82+
name: 'truncate_foreign_table_cascade', node: 'foreign_table', module: this,
83+
applies: ['object', 'context'], callback: 'truncate_foreign_table_cascade',
84+
category: gettext('Truncate'), priority: 3, label: gettext('Truncate Cascade'),
85+
enable : 'canCreate',
86+
},{
87+
name: 'truncate_foreign_table_identity', node: 'foreign_table', module: this,
88+
applies: ['object', 'context'], callback: 'truncate_foreign_table_identity',
89+
category: gettext('Truncate'), priority: 3, label: gettext('Truncate Restart Identity'),
90+
enable : 'canCreate',
7591
},{
7692
// To enable/disable all triggers for the table
7793
name: 'enable_all_triggers', node: 'foreign_table', module: this,
@@ -112,6 +128,56 @@ define('pgadmin.node.foreign_table', ['pgadmin.tables.js/enable_disable_triggers
112128
args
113129
);
114130
},
131+
/* Truncate foreign table */
132+
truncate_foreign_table: function(args) {
133+
let params = {'cascade': false };
134+
this.callbacks.truncate.apply(this, [args, params]);
135+
},
136+
/* Truncate foreign table with cascade */
137+
truncate_foreign_table_cascade: function(args) {
138+
let params = {'cascade': true };
139+
this.callbacks.truncate.apply(this, [args, params]);
140+
},
141+
/* Truncate foreign table with identity */
142+
truncate_foreign_table_identity: function(args) {
143+
let params = {'identity': true };
144+
this.callbacks.truncate.apply(this, [args, params]);
145+
},
146+
truncate: function(args, params) {
147+
let input = args || {},
148+
obj = this,
149+
t = pgBrowser.tree,
150+
i = input.item || t.selected(),
151+
d = i ? t.itemData(i) : undefined;
152+
153+
if (!d)
154+
return false;
155+
156+
pgBrowser.notifier.confirm(
157+
gettext('Truncate Foreign Table'),
158+
gettext('Are you sure you want to truncate foreign table <b>%s</b>?', d.label),
159+
function () {
160+
let data = d;
161+
getApiInstance().put(obj.generate_url(i, 'truncate' , d, true), params)
162+
.then(({data: res})=>{
163+
if (res.success == 1) {
164+
pgBrowser.notifier.success(res.info);
165+
t.removeIcon(i);
166+
data.icon = data.is_partitioned ? 'icon-partition': 'icon-table';
167+
t.addIcon(i, {icon: data.icon});
168+
t.updateAndReselectNode(i, data);
169+
}
170+
if (res.success == 2) {
171+
pgBrowser.notifier.error(res.info);
172+
}
173+
})
174+
.catch((error)=>{
175+
pgBrowser.notifier.pgRespErrorNotify(error);
176+
t.refresh(i);
177+
});
178+
}, function() {/*This is intentional (SonarQube)*/}
179+
);
180+
},
115181
},
116182
// Check to whether table has disable trigger(s)
117183
canCreate_with_trigger_enable: function(itemData) {

0 commit comments

Comments
 (0)